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

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


def execute():

	xhiveframework.reload_doc("payroll", "doctype", "Salary Component Account")
	if xhiveframework.db.has_column("Salary Component Account", "default_account"):
		rename_field("Salary Component Account", "default_account", "account")

	doctype_list = [
		{"module": "HR", "doctype": "Employee Advance"},
		{"module": "HR", "doctype": "Leave Encashment"},
		{"module": "Payroll", "doctype": "Additional Salary"},
		{"module": "Payroll", "doctype": "Employee Benefit Application"},
		{"module": "Payroll", "doctype": "Employee Benefit Claim"},
		{"module": "Payroll", "doctype": "Employee Incentive"},
		{"module": "Payroll", "doctype": "Employee Tax Exemption Declaration"},
		{"module": "Payroll", "doctype": "Employee Tax Exemption Proof Submission"},
		{"module": "Payroll", "doctype": "Income Tax Slab"},
		{"module": "Payroll", "doctype": "Payroll Entry"},
		{"module": "Payroll", "doctype": "Retention Bonus"},
		{"module": "Payroll", "doctype": "Salary Structure"},
		{"module": "Payroll", "doctype": "Salary Structure Assignment"},
		{"module": "Payroll", "doctype": "Salary Slip"},
	]

	for item in doctype_list:
		xhiveframework.reload_doc(item["module"], "doctype", item["doctype"])

	# update company in employee advance based on employee company
	for dt in [
		"Employee Incentive",
		"Leave Encashment",
		"Employee Benefit Application",
		"Employee Benefit Claim",
	]:
		xhiveframework.db.sql(
			"""
			update `tab{doctype}`
			set company = (select company from tabEmployee where name=`tab{doctype}`.employee)
			where company IS NULL
		""".format(
				doctype=dt
			)
		)

	# update exchange rate for employee advance
	xhiveframework.db.sql("update `tabEmployee Advance` set exchange_rate=1 where exchange_rate is NULL")

	# get all companies and it's currency
	all_companies = xhiveframework.db.get_all(
		"Company", fields=["name", "default_currency", "default_payroll_payable_account"]
	)
	for d in all_companies:
		company = d.name
		company_currency = d.default_currency
		default_payroll_payable_account = d.default_payroll_payable_account

		if not default_payroll_payable_account:
			default_payroll_payable_account = xhiveframework.db.get_value(
				"Account",
				{
					"account_name": _("Payroll Payable"),
					"company": company,
					"account_currency": company_currency,
					"is_group": 0,
				},
			)

		# update currency in following doctypes based on company currency
		doctypes_for_currency = [
			"Employee Advance",
			"Leave Encashment",
			"Employee Benefit Application",
			"Employee Benefit Claim",
			"Employee Incentive",
			"Additional Salary",
			"Employee Tax Exemption Declaration",
			"Employee Tax Exemption Proof Submission",
			"Income Tax Slab",
			"Retention Bonus",
			"Salary Structure",
		]

		for dt in doctypes_for_currency:
			xhiveframework.db.sql(
				"""update `tab{doctype}` set currency = %s where company=%s and currency IS NULL""".format(
					doctype=dt
				),
				(company_currency, company),
			)

		# update fields in payroll entry
		xhiveframework.db.sql(
			"""
			update `tabPayroll Entry`
			set currency = %s,
				exchange_rate = 1,
				payroll_payable_account=%s
			where company=%s
			and currency IS NULL
		""",
			(company_currency, default_payroll_payable_account, company),
		)

		# update fields in Salary Structure Assignment
		xhiveframework.db.sql(
			"""
			update `tabSalary Structure Assignment`
			set currency = %s,
				payroll_payable_account=%s
			where company=%s
			and currency IS NULL
		""",
			(company_currency, default_payroll_payable_account, company),
		)

		# update fields in Salary Slip
		xhiveframework.db.sql(
			"""
			update `tabSalary Slip`
			set currency = %s,
				exchange_rate = 1,
				base_hour_rate = hour_rate,
				base_gross_pay = gross_pay,
				base_total_deduction = total_deduction,
				base_net_pay = net_pay,
				base_rounded_total = rounded_total,
				base_total_in_words = total_in_words
			where company=%s
			and currency IS NULL
		""",
			(company_currency, company),
		)
