import frappe
from frappe.utils import flt

from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions

from frappe.model.rename_doc import update_document_title

from hrms.payroll.doctype.payroll_entry.payroll_entry import PayrollEntry

BANK_ENTRY_TITLE = "Əmək həqqi və tutulmaların hesablanması"


class CustomPayrollEntry(PayrollEntry):
	@frappe.whitelist()
	def make_bank_entry(self, for_withheld_salaries=False):
		self._bank_entry_deductions = {}

		salary_details = self.get_salary_slip_details(for_withheld_salaries)
		for salary_detail in salary_details:
			if salary_detail.parentfield != "deductions":
				continue
			statistical_component = frappe.db.get_value(
				"Salary Component", salary_detail.salary_component, "statistical_component", cache=True
			)
			if not statistical_component:
				component = salary_detail.salary_component
				self._bank_entry_deductions[component] = (
					self._bank_entry_deductions.get(component, 0) + salary_detail.amount
				)

		bank_entry = super().make_bank_entry(for_withheld_salaries)

		if bank_entry:
			update_document_title(
				doctype="Journal Entry",
				docname=bank_entry.name,
				title=BANK_ENTRY_TITLE,
			)

		return bank_entry

	def set_accounting_entries_for_bank_entry(
		self, je_payment_amount, user_remark, employee_wise_accounting_enabled
	):
		bank_entry = super().set_accounting_entries_for_bank_entry(
			je_payment_amount, user_remark, employee_wise_accounting_enabled
		)

		deductions = getattr(self, "_bank_entry_deductions", {})
		if not deductions:
			return bank_entry

		precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency")
		company_currency = frappe.db.get_value("Company", self.company, "default_currency")
		accounting_dimensions = get_accounting_dimensions() or []
		currencies = [row.account_currency for row in bank_entry.accounts if row.get("account_currency")]

		for salary_component, total_amount in deductions.items():
			if not total_amount:
				continue

			deduction_account = self.get_salary_component_account(salary_component)

			exchange_rate, amount = self.get_amount_and_exchange_rate_for_journal_entry(
				deduction_account, total_amount, company_currency, currencies
			)
			debit_row = {
				"account": deduction_account,
				"debit_in_account_currency": flt(amount, precision),
				"exchange_rate": flt(exchange_rate),
				"cost_center": self.cost_center,
			}
			self.update_accounting_dimensions(debit_row, accounting_dimensions)
			bank_entry.append("accounts", debit_row)

			exchange_rate, amount = self.get_amount_and_exchange_rate_for_journal_entry(
				self.payment_account, total_amount, company_currency, currencies
			)
			credit_row = {
				"account": self.payment_account,
				"bank_account": self.bank_account,
				"credit_in_account_currency": flt(amount, precision),
				"exchange_rate": flt(exchange_rate),
				"cost_center": self.cost_center,
			}
			self.update_accounting_dimensions(credit_row, accounting_dimensions)
			bank_entry.append("accounts", credit_row)

		bank_entry.save(ignore_permissions=True)
		return bank_entry
