import xhiveframework


def execute():
	from xhiveerp.setup.setup_wizard.operations.install_fixtures import read_lines

	xhiveframework.reload_doc("selling", "doctype", "sales_partner_type")

	xhiveframework.local.lang = xhiveframework.db.get_default("lang") or "en"

	default_sales_partner_type = read_lines("sales_partner_type.txt")

	for s in default_sales_partner_type:
		insert_sales_partner_type(s)

	# get partner type in existing forms (customized)
	# and create a document if not created
	for d in ["Sales Partner"]:
		partner_type = xhiveframework.db.sql_list("select distinct partner_type from `tab{0}`".format(d))
		for s in partner_type:
			if s and s not in default_sales_partner_type:
				insert_sales_partner_type(s)

		# remove customization for partner type
		for p in xhiveframework.get_all(
			"Property Setter", {"doc_type": d, "field_name": "partner_type", "property": "options"}
		):
			xhiveframework.delete_doc("Property Setter", p.name)


def insert_sales_partner_type(s):
	if not xhiveframework.db.exists("Sales Partner Type", s):
		xhiveframework.get_doc(dict(doctype="Sales Partner Type", sales_partner_type=s)).insert()
