# Copyright (c) 2019, Xhiveframework and Contributors
# License: GNU General Public License v3. See license.txt


import xhiveframework


def execute():

	xhiveframework.reload_doc("accounts", "doctype", "subscription")
	xhiveframework.reload_doc("accounts", "doctype", "subscription_invoice")
	xhiveframework.reload_doc("accounts", "doctype", "subscription_plan")

	if xhiveframework.db.has_column("Subscription", "customer"):
		xhiveframework.db.sql(
			"""
			UPDATE `tabSubscription`
			SET
				start_date = start,
				party_type = 'Customer',
				party = customer,
				sales_tax_template = tax_template
			WHERE IFNULL(party,'') = ''
		"""
		)

	xhiveframework.db.sql(
		"""
		UPDATE `tabSubscription Invoice`
		SET document_type = 'Sales Invoice'
		WHERE IFNULL(document_type, '') = ''
	"""
	)

	price_determination_map = {
		"Fixed rate": "Fixed Rate",
		"Based on price list": "Based On Price List",
	}

	for key, value in price_determination_map.items():
		xhiveframework.db.sql(
			"""
			UPDATE `tabSubscription Plan`
			SET price_determination = %s
			WHERE price_determination = %s
		""",
			(value, key),
		)
