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


import influxframework
from influxframework.model.utils.rename_field import rename_field


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

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

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