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


import xhiveframework
from xhiveframework.model.utils.rename_field import rename_field


def execute():
	xhiveframework.reload_doc("setup", "doctype", "company")
	if xhiveframework.db.has_column("Company", "default_terms"):
		rename_field("Company", "default_terms", "default_selling_terms")

		for company in xhiveframework.get_all(
			"Company", ["name", "default_selling_terms", "default_buying_terms"]
		):
			if company.default_selling_terms and not company.default_buying_terms:
				xhiveframework.db.set_value(
					"Company", company.name, "default_buying_terms", company.default_selling_terms
				)

	xhiveframework.reload_doc("setup", "doctype", "terms_and_conditions")
	xhiveframework.db.sql("update `tabTerms and Conditions` set selling=1, buying=1, hr=1")
