From af2a622afb9d9d71f332e9e97b1f206a6baf5e2a Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 20 Nov 2024 00:15:22 +0100 Subject: [PATCH] fix: partial fix for #44291 (#44229) This is only a partial fix and improves the situaton. A proper fix will need to have items _always_ track their tax-related account head, namely for item-wise tax templates as well as default document taxes that are applied "On Net Total", in order to achive proper assignment of the net_amount. Currently there is no way to always identify the orignating account head per item. --- erpnext/controllers/taxes_and_totals.py | 22 +++++++++++++------ .../public/js/controllers/taxes_and_totals.js | 5 ----- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index d2dcda15ce6..f3212615161 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -21,6 +21,8 @@ from erpnext.controllers.accounts_controller import ( from erpnext.stock.get_item_details import _get_item_tax_template from erpnext.utilities.regional import temporary_flag +logger = frappe.logger(__name__) + ItemWiseTaxDetail = frappe._dict @@ -379,8 +381,10 @@ class calculate_taxes_and_totals: ] ) + logger.debug(f"{self.doc} ...") for n, item in enumerate(self._items): item_tax_map = self._load_item_tax_rate(item.item_tax_rate) + logger.debug(f" Item {n}: {item.item_code}" + (f" - {item_tax_map}" if item_tax_map else "")) for i, tax in enumerate(self.doc.get("taxes")): # tax_amount represents the amount of tax for the current step current_net_amount, current_tax_amount = self.get_current_tax_and_net_amount( @@ -447,6 +451,9 @@ class calculate_taxes_and_totals: self.doc.grand_total - flt(self.doc.discount_amount) - tax.total, self.doc.precision("rounding_adjustment"), ) + logger.debug( + f" net_amount: {current_net_amount:<20} tax_amount: {current_tax_amount:<20} - {tax.description}" + ) def get_tax_amount_if_for_valuation_or_deduction(self, tax_amount, tax): # if just for valuation, do not add the tax amount in total @@ -477,25 +484,26 @@ class calculate_taxes_and_totals: current_net_amount = 0.0 if tax.charge_type == "Actual": + current_net_amount = item.net_amount # distribute the tax amount proportionally to each item row actual = flt(tax.tax_amount, tax.precision("tax_amount")) if tax.get("is_tax_withholding_account") and item.meta.get_field("apply_tds"): if not item.get("apply_tds") or not self.doc.tax_withholding_net_total: current_tax_amount = 0.0 - current_net_amount = 0.0 else: - current_net_amount = item.net_amount - current_tax_amount = current_net_amount * actual / self.doc.tax_withholding_net_total + current_tax_amount = item.net_amount * actual / self.doc.tax_withholding_net_total else: - current_net_amount = item.net_amount current_tax_amount = ( - current_net_amount * actual / self.doc.net_total if self.doc.net_total else 0.0 + item.net_amount * actual / self.doc.net_total if self.doc.net_total else 0.0 ) elif tax.charge_type == "On Net Total": - current_net_amount = item.net_amount - current_tax_amount = (tax_rate / 100.0) * current_net_amount + if not item_tax_map: + current_net_amount = item.net_amount + elif tax.account_head in item_tax_map: + current_net_amount = item.net_amount + current_tax_amount = (tax_rate / 100.0) * item.net_amount elif tax.charge_type == "On Previous Row Amount": current_net_amount = self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item current_tax_amount = (tax_rate / 100.0) * current_net_amount diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js index 56fc36584c0..a7ef90522de 100644 --- a/erpnext/public/js/controllers/taxes_and_totals.js +++ b/erpnext/public/js/controllers/taxes_and_totals.js @@ -447,7 +447,6 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments { get_current_tax_amount(item, tax, item_tax_map) { var tax_rate = this._get_tax_rate(tax, item_tax_map); var current_tax_amount = 0.0; - var current_net_amount = 0.0; // To set row_id by default as previous row. if(["On Previous Row Amount", "On Previous Row Total"].includes(tax.charge_type)) { @@ -462,20 +461,16 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments { if(tax.charge_type == "Actual") { // distribute the tax amount proportionally to each item row var actual = flt(tax.tax_amount, precision("tax_amount", tax)); - current_net_amount = item.net_amount current_tax_amount = this.frm.doc.net_total ? ((item.net_amount / this.frm.doc.net_total) * actual) : 0.0; } else if(tax.charge_type == "On Net Total") { - current_net_amount = item.net_amount current_tax_amount = (tax_rate / 100.0) * item.net_amount; } else if(tax.charge_type == "On Previous Row Amount") { - current_net_amount = this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item current_tax_amount = (tax_rate / 100.0) * this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item; } else if(tax.charge_type == "On Previous Row Total") { - current_net_amount = this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item current_tax_amount = (tax_rate / 100.0) * this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item; } else if (tax.charge_type == "On Item Quantity") {