import xhiveframework
from xhiveframework import _
from xhiveframework.model.utils.rename_field import rename_field
from xhiveframework.utils.nestedset import rebuild_tree


def execute():
	if xhiveframework.db.table_exists("Supplier Group"):
		xhiveframework.reload_doc("setup", "doctype", "supplier_group")
	elif xhiveframework.db.table_exists("Supplier Type"):
		xhiveframework.rename_doc("DocType", "Supplier Type", "Supplier Group", force=True)
		xhiveframework.reload_doc("setup", "doctype", "supplier_group")
		xhiveframework.reload_doc("accounts", "doctype", "pricing_rule")
		xhiveframework.reload_doc("accounts", "doctype", "tax_rule")
		xhiveframework.reload_doc("buying", "doctype", "buying_settings")
		xhiveframework.reload_doc("buying", "doctype", "supplier")
		rename_field("Supplier Group", "supplier_type", "supplier_group_name")
		rename_field("Supplier", "supplier_type", "supplier_group")
		rename_field("Buying Settings", "supplier_type", "supplier_group")
		rename_field("Pricing Rule", "supplier_type", "supplier_group")
		rename_field("Tax Rule", "supplier_type", "supplier_group")

	build_tree()


def build_tree():
	xhiveframework.db.sql(
		"""update `tabSupplier Group` set parent_supplier_group = '{0}'
		where is_group = 0""".format(
			_("All Supplier Groups")
		)
	)

	if not xhiveframework.db.exists("Supplier Group", _("All Supplier Groups")):
		xhiveframework.get_doc(
			{
				"doctype": "Supplier Group",
				"supplier_group_name": _("All Supplier Groups"),
				"is_group": 1,
				"parent_supplier_group": "",
			}
		).insert(ignore_permissions=True)

	rebuild_tree("Supplier Group", "parent_supplier_group")
