From 91f3c82bdf4fb8f6a5cc678977830c34c4c263cf Mon Sep 17 00:00:00 2001 From: Lakshit Jain Date: Mon, 17 Nov 2025 19:02:31 +0530 Subject: [PATCH] feat!: Item Wise Tax Details Table (#48692) * fix: Add `Item Wise Tax Detail` Table and update related doctypes * fix: remove setting item_wise_tax_details in client side * fix: Remove redundant code for updating item_wise_tax_details after rename * fix: Add 'dont_recompute_tax' field to Item Wise Tax Detail * fix: update item_wise_tax_details after validations * chore: remove redundant code from payment_entry.js * fix: changes in POS for item_wise_tax_details * fix: handle merge taxes * fix: update test case and fix precision issue * chore: remove debugging statement * chore: remove redundant import * chore: linters * chore: remove redundant code and minor refactor * fix: correct function args * fix: fix test cases * fix: item wise sales register report * fix: remove dont recompute from item wise tax details and calculation for deduct * fix: do not retain old rows * fix: added validation for item wise tax details * fix: tax merging for pos * fix: vat audit report(regional report) * fix: query issue in item-wise sales register * fix: set other_charges using temp object * fix: precision issue in validation * fix: changes as per failing test cases * fix: tax merging * fix: set no_copy for item wise tax detail * fix: correct select field in query and other charged in item_wise_purchase_register * fix: do not include rows with missing item or tax in merge_taxes * fix: respect row wise rounding * chore: remove unused import * chore: incorrect tuple creation * fix: handle rounding adjustment * fix: currency option in item wise tax detail doctype * fix: patch to migrate item_wise tax_details to table * chore: remove item_wise_tax_detail from taxes table * fix: use base_tax_withholding_net_total instead of tax_withholding_net_total * fix: implemet item_wise_tax_detail for e-invoice (italy) * fix: fetch document by doctypes in migration patch * fix: fix multiple syntax errors and inconsistent variable usage * fix: remove deprecated settings and update item wise tax details flag * fix: enhance validation for item wise tax details and handle discrepancies * fix: increase chunk size for migration and improve item-wise tax detail calculations * fix: delete existing item-wise tax details to prevent duplicates during migration * fix: remove unnecessary docstatus filter from tax details query * fix: streamline validation checks in item wise tax details adjustment * fix: update additional fields to reference item and invoice attributes in tax detail queries * fix: Restrict tax query to the selected invoices in vat audit report * fix: use `base_tax_withholding_net_total` for calculation in patch * fix: set tax row_id and idx to None instead of empty strings * fix: remove unused precision parameter from rounding differences handler * fix: update docstatus in item_wise_tax_details as per doc * fix: remove empty on_update method from SalesOrder class * fix: remove empty on_update method from PurchaseOrder class * fix: incorporate zero cutoff in tax calculation logic * fix: increase threshold for rounding diff --- .../doctype/item_wise_tax_detail/__init__.py | 0 .../item_wise_tax_detail.json | 63 ++++ .../item_wise_tax_detail.py | 27 ++ .../doctype/payment_entry/payment_entry.js | 1 - .../doctype/pos_invoice/pos_invoice.json | 9 + .../doctype/pos_invoice/pos_invoice.py | 2 + .../pos_invoice_merge_log.py | 41 +-- .../test_pos_invoice_merge_log.py | 40 ++- .../purchase_invoice/purchase_invoice.json | 9 + .../purchase_invoice/purchase_invoice.py | 2 + .../purchase_taxes_and_charges.json | 15 +- .../purchase_taxes_and_charges.py | 1 - .../doctype/sales_invoice/sales_invoice.json | 9 + .../doctype/sales_invoice/sales_invoice.py | 2 + .../sales_invoice/test_sales_invoice.py | 18 +- .../sales_taxes_and_charges.json | 15 +- .../sales_taxes_and_charges.py | 1 - .../item_wise_purchase_register.py | 15 +- .../item_wise_sales_register.py | 206 +++++------- .../purchase_order/purchase_order.json | 9 + .../doctype/purchase_order/purchase_order.py | 5 +- .../supplier_quotation.json | 13 +- .../supplier_quotation/supplier_quotation.py | 2 + erpnext/controllers/accounts_controller.py | 50 ++- erpnext/controllers/stock_controller.py | 1 + erpnext/controllers/taxes_and_totals.py | 207 +++++++++--- .../tests/test_item_wise_tax_details.py | 66 ++-- erpnext/patches.txt | 3 +- ...te_old_item_wise_tax_detail_data_format.py | 77 ----- ..._old_item_wise_tax_detail_data_to_table.py | 307 ++++++++++++++++++ .../public/js/controllers/taxes_and_totals.js | 39 --- erpnext/regional/italy/utils.py | 139 ++++---- .../vat_audit_report/vat_audit_report.py | 154 ++++----- .../regional/united_arab_emirates/utils.py | 2 +- .../selling/doctype/quotation/quotation.json | 9 + .../selling/doctype/quotation/quotation.py | 2 + .../doctype/sales_order/sales_order.json | 9 + .../doctype/sales_order/sales_order.py | 5 +- .../doctype/delivery_note/delivery_note.json | 9 + .../doctype/delivery_note/delivery_note.py | 5 +- erpnext/stock/doctype/item/item.py | 19 -- .../purchase_receipt/purchase_receipt.json | 10 + .../purchase_receipt/purchase_receipt.py | 5 +- 43 files changed, 1001 insertions(+), 622 deletions(-) create mode 100644 erpnext/accounts/doctype/item_wise_tax_detail/__init__.py create mode 100644 erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.json create mode 100644 erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.py delete mode 100644 erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py create mode 100644 erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_to_table.py diff --git a/erpnext/accounts/doctype/item_wise_tax_detail/__init__.py b/erpnext/accounts/doctype/item_wise_tax_detail/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.json b/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.json new file mode 100644 index 00000000000..397ff29e1b4 --- /dev/null +++ b/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.json @@ -0,0 +1,63 @@ +{ + "actions": [], + "creation": "2025-07-17 12:24:05.609186", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "item_row", + "tax_row", + "rate", + "amount", + "taxable_amount" + ], + "fields": [ + { + "fieldname": "item_row", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Item Row", + "reqd": 1 + }, + { + "fieldname": "tax_row", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Tax Row", + "reqd": 1 + }, + { + "fieldname": "rate", + "fieldtype": "Float", + "in_list_view": 1, + "label": "Tax Rate" + }, + { + "fieldname": "amount", + "fieldtype": "Currency", + "in_list_view": 1, + "label": "Tax Amount", + "options": "Company:company:default_currency" + }, + { + "fieldname": "taxable_amount", + "fieldtype": "Currency", + "in_list_view": 1, + "label": "Taxable Amount", + "options": "Company:company:default_currency" + } + ], + "grid_page_length": 50, + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2025-09-26 15:54:19.750714", + "modified_by": "Administrator", + "module": "Accounts", + "name": "Item Wise Tax Detail", + "owner": "Administrator", + "permissions": [], + "row_format": "Dynamic", + "sort_field": "creation", + "sort_order": "DESC", + "states": [] +} diff --git a/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.py b/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.py new file mode 100644 index 00000000000..0f8124f0f7f --- /dev/null +++ b/erpnext/accounts/doctype/item_wise_tax_detail/item_wise_tax_detail.py @@ -0,0 +1,27 @@ +# Copyright (c) 2025, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class ItemWiseTaxDetail(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from frappe.types import DF + + amount: DF.Currency + item_row: DF.Data + parent: DF.Data + parentfield: DF.Data + parenttype: DF.Data + rate: DF.Float + tax_row: DF.Data + taxable_amount: DF.Currency + # end: auto-generated types + + pass diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js index c0634354006..939cd0d113d 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.js +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js @@ -1455,7 +1455,6 @@ frappe.ui.form.on("Payment Entry", { $.each(frm.doc["taxes"] || [], function (i, tax) { frm.events.validate_taxes_and_charges(tax); frm.events.validate_inclusive_tax(tax); - tax.item_wise_tax_detail = {}; let tax_fields = [ "total", "tax_fraction_for_current_item", diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.json b/erpnext/accounts/doctype/pos_invoice/pos_invoice.json index ad3c9943b18..7578150b07e 100644 --- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.json +++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.json @@ -70,6 +70,7 @@ "taxes", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "section_break_43", "base_total_taxes_and_charges", "column_break_47", @@ -1602,6 +1603,14 @@ "fieldtype": "Data", "is_virtual": 1, "label": "Last Scanned Warehouse" + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "icon": "fa fa-file-text", diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py index 23caf1959f8..c26c784b262 100644 --- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py +++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py @@ -31,6 +31,7 @@ class POSInvoice(SalesInvoice): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pos_invoice_item.pos_invoice_item import POSInvoiceItem from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail @@ -99,6 +100,7 @@ class POSInvoice(SalesInvoice): is_opening: DF.Literal["No", "Yes"] is_pos: DF.Check is_return: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[POSInvoiceItem] language: DF.Data | None letter_head: DF.Link | None diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py index 96136a74f37..0874955ec47 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py @@ -17,7 +17,6 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_checks_for_pl_and_bs_accounts, ) from erpnext.controllers.sales_and_purchase_return import get_sales_invoice_item_from_consolidated_invoice -from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail class POSInvoiceMergeLog(Document): @@ -156,7 +155,6 @@ class POSInvoiceMergeLog(Document): sales_invoice.save() sales_invoice.submit() - self.consolidated_invoice = sales_invoice.name return sales_invoice @@ -207,7 +205,7 @@ class POSInvoiceMergeLog(Document): return return_invoices def merge_pos_invoice_into(self, invoice, data): - items, payments, taxes = [], [], [] + items, payments, taxes, item_tax_details = [], [], [], [] loyalty_amount_sum, loyalty_points_sum = 0, 0 @@ -217,6 +215,8 @@ class POSInvoiceMergeLog(Document): loyalty_amount_sum, loyalty_points_sum, idx = 0, 0, 1 for doc in data: + old_new_item_map = frappe._dict() + old_new_tax_map = frappe._dict() map_doc(doc, invoice, table_map={"doctype": invoice.doctype}) if doc.get("posting_date"): @@ -244,6 +244,7 @@ class POSInvoiceMergeLog(Document): if item.serial_and_batch_bundle: si_item.serial_and_batch_bundle = item.serial_and_batch_bundle items.append(si_item) + old_new_item_map[item.name] = si_item for tax in doc.get("taxes"): found = False @@ -253,7 +254,7 @@ class POSInvoiceMergeLog(Document): t.base_tax_amount = flt(t.base_tax_amount) + flt( tax.base_tax_amount_after_discount_amount ) - update_item_wise_tax_detail(t, tax) + old_new_tax_map[tax.name] = t found = True if not found: tax.charge_type = "Actual" @@ -263,8 +264,9 @@ class POSInvoiceMergeLog(Document): tax.included_in_print_rate = 0 tax.tax_amount = tax.tax_amount_after_discount_amount tax.base_tax_amount = tax.base_tax_amount_after_discount_amount - tax.item_wise_tax_detail = tax.item_wise_tax_detail + tax.dont_recompute_tax = 1 taxes.append(tax) + old_new_tax_map[tax.name] = tax for payment in doc.get("payments"): found = False @@ -281,6 +283,16 @@ class POSInvoiceMergeLog(Document): base_rounding_adjustment += doc.base_rounding_adjustment base_rounded_total += doc.base_rounded_total + for d in doc.get("item_wise_tax_details"): + row = frappe._dict( + item=old_new_item_map[d.item_row], + tax=old_new_tax_map[d.tax_row], + amount=d.amount, + rate=d.rate, + taxable_amount=d.taxable_amount, + ) + item_tax_details.append(row) + if loyalty_points_sum: invoice.redeem_loyalty_points = 1 invoice.loyalty_points = loyalty_points_sum @@ -342,6 +354,7 @@ class POSInvoiceMergeLog(Document): invoice.set("sales_partner", None) invoice.set("commission_rate", 0) invoice.set("total_commission", 0) + invoice._item_wise_tax_details = item_tax_details return invoice @@ -419,24 +432,6 @@ class POSInvoiceMergeLog(Document): si.cancel() -def update_item_wise_tax_detail(consolidate_tax_row, tax_row): - consolidated_tax_detail = json.loads(consolidate_tax_row.item_wise_tax_detail) - tax_row_detail = json.loads(tax_row.item_wise_tax_detail) - - if not consolidated_tax_detail: - consolidated_tax_detail = {} - - for item_code, tax_data in tax_row_detail.items(): - tax_data = ItemWiseTaxDetail(**tax_data) - if consolidated_tax_detail.get(item_code): - consolidated_tax_detail[item_code]["tax_amount"] += tax_data.tax_amount - consolidated_tax_detail[item_code]["net_amount"] += tax_data.net_amount - else: - consolidated_tax_detail.update({item_code: tax_data}) - - consolidate_tax_row.item_wise_tax_detail = json.dumps(consolidated_tax_detail) - - def get_all_unconsolidated_invoices(): filters = { "consolidated_invoice": ["in", ["", None]], diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py index 3c5c0c2abeb..5403b10476c 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/test_pos_invoice_merge_log.py @@ -164,20 +164,36 @@ class TestPOSInvoiceMergeLog(IntegrationTestCase): inv.load_from_db() consolidated_invoice = frappe.get_doc("Sales Invoice", inv.consolidated_invoice) - item_wise_tax_detail = json.loads(consolidated_invoice.get("taxes")[0].item_wise_tax_detail) - expected_item_wise_tax_detail = { - "_Test Item": { - "tax_rate": 9, - "tax_amount": 9, - "net_amount": 100, + + expected_item_wise_tax_details = [ + { + "item_row": consolidated_invoice.items[0].name, + "tax_row": consolidated_invoice.taxes[0].name, + "rate": 9.0, + "amount": 9.0, + "taxable_amount": 100.0, }, - "_Test Item 2": { - "tax_rate": 5, - "tax_amount": 5, - "net_amount": 100, + { + "item_row": consolidated_invoice.items[1].name, + "tax_row": consolidated_invoice.taxes[0].name, + "rate": 5.0, + "amount": 5.0, + "taxable_amount": 100.0, }, - } - self.assertEqual(item_wise_tax_detail, expected_item_wise_tax_detail) + ] + + actual = [ + { + "item_row": d.item_row, + "tax_row": d.tax_row, + "rate": d.rate, + "amount": d.amount, + "taxable_amount": d.taxable_amount, + } + for d in consolidated_invoice.get("item_wise_tax_details") + ] + + self.assertEqual(actual, expected_item_wise_tax_details) def test_consolidation_round_off_error_1(self): """ diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json index eb8380e5c82..d2efe60e459 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json @@ -112,6 +112,7 @@ "tax_withheld_vouchers", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "pricing_rule_details", "pricing_rules", "raw_materials_supplied", @@ -1670,6 +1671,14 @@ "options": "Company:company:default_currency", "print_hide": 1, "read_only": 1 + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index eae1818d20c..bdd936a520b 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -62,6 +62,7 @@ class PurchaseInvoice(BuyingController): from frappe.types import DF from erpnext.accounts.doctype.advance_tax.advance_tax import AdvanceTax + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.purchase_invoice_advance.purchase_invoice_advance import ( @@ -136,6 +137,7 @@ class PurchaseInvoice(BuyingController): is_paid: DF.Check is_return: DF.Check is_subcontracted: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[PurchaseInvoiceItem] language: DF.Data | None letter_head: DF.Link | None diff --git a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json index 0719727a272..fab5b6f6a21 100644 --- a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json +++ b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.json @@ -34,8 +34,7 @@ "base_net_amount", "base_tax_amount", "base_total", - "base_tax_amount_after_discount_amount", - "item_wise_tax_detail" + "base_tax_amount_after_discount_amount" ], "fields": [ { @@ -196,16 +195,6 @@ "print_hide": 1, "read_only": 1 }, - { - "fieldname": "item_wise_tax_detail", - "fieldtype": "Code", - "hidden": 1, - "label": "Item Wise Tax Detail", - "oldfieldname": "item_wise_tax_detail", - "oldfieldtype": "Small Text", - "print_hide": 1, - "read_only": 1 - }, { "fieldname": "accounting_dimensions_section", "fieldtype": "Section Break", @@ -279,7 +268,7 @@ "idx": 1, "istable": 1, "links": [], - "modified": "2025-04-15 13:14:48.936047", + "modified": "2025-07-24 15:08:44.433022", "modified_by": "Administrator", "module": "Accounts", "name": "Purchase Taxes and Charges", diff --git a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.py b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.py index 5b6062b32c9..4a198e001bc 100644 --- a/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.py +++ b/erpnext/accounts/doctype/purchase_taxes_and_charges/purchase_taxes_and_charges.py @@ -35,7 +35,6 @@ class PurchaseTaxesandCharges(Document): included_in_paid_amount: DF.Check included_in_print_rate: DF.Check is_tax_withholding_account: DF.Check - item_wise_tax_detail: DF.Code | None net_amount: DF.Currency parent: DF.Data parentfield: DF.Data diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json index 221ff6c610c..a889ca3c2ed 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json @@ -101,6 +101,7 @@ "discount_amount", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "pricing_rule_details", "pricing_rules", "packing_list", @@ -2238,6 +2239,14 @@ "hidden": 1, "label": "Has Subcontracted", "read_only": 1 + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 4a137da1230..fdea1b2d29e 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -65,6 +65,7 @@ class SalesInvoice(SellingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.sales_invoice_advance.sales_invoice_advance import SalesInvoiceAdvance @@ -146,6 +147,7 @@ class SalesInvoice(SellingController): is_opening: DF.Literal["No", "Yes"] is_pos: DF.Check is_return: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[SalesInvoiceItem] language: DF.Link | None letter_head: DF.Link | None diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index a3b6c42afef..9aa9d61951b 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -2077,12 +2077,12 @@ class TestSalesInvoice(ERPNextTestSuite): { "item": "_Test Item", "taxable_amount": 10000.0, - "Service Tax": {"tax_rate": 10.0, "tax_amount": 1000.0, "net_amount": 10000.0}, + "Service Tax": {"tax_rate": 10.0, "tax_amount": 1000.0, "taxable_amount": 10000.0}, }, { "item": "_Test Item 2", "taxable_amount": 5000.0, - "Service Tax": {"tax_rate": 10.0, "tax_amount": 500.0, "net_amount": 5000.0}, + "Service Tax": {"tax_rate": 10.0, "tax_amount": 500.0, "taxable_amount": 5000.0}, }, ] @@ -3980,29 +3980,29 @@ class TestSalesInvoice(ERPNextTestSuite): target_doc=si, args=json.dumps({"customer": dn1.customer, "merge_taxes": 1, "filtered_children": []}), ) - si.save().submit() + si.save() expected = [ { "charge_type": "Actual", "account_head": "Freight and Forwarding Charges - _TC", "tax_amount": 120.0, - "total": 1520.0, - "base_total": 1520.0, + "total": 1620.0, + "base_total": 1620.0, }, { "charge_type": "Actual", "account_head": "Marketing Expenses - _TC", "tax_amount": 150.0, - "total": 1670.0, - "base_total": 1670.0, + "total": 1770.0, + "base_total": 1770.0, }, { "charge_type": "Actual", "account_head": "Miscellaneous Expenses - _TC", "tax_amount": 60.0, - "total": 1610.0, - "base_total": 1610.0, + "total": 1830.0, + "base_total": 1830.0, }, ] actual = [ diff --git a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json index f740a6ba936..429bf759743 100644 --- a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json +++ b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.json @@ -31,7 +31,6 @@ "base_tax_amount", "base_total", "base_tax_amount_after_discount_amount", - "item_wise_tax_detail", "dont_recompute_tax" ], "fields": [ @@ -174,15 +173,6 @@ "options": "Company:company:default_currency", "read_only": 1 }, - { - "fieldname": "item_wise_tax_detail", - "fieldtype": "Code", - "hidden": 1, - "label": "Item Wise Tax Detail", - "oldfieldname": "item_wise_tax_detail", - "oldfieldtype": "Small Text", - "read_only": 1 - }, { "fieldname": "accounting_dimensions_section", "fieldtype": "Section Break", @@ -257,13 +247,14 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2024-11-22 19:17:31.898467", + "modified": "2025-07-24 15:08:34.381704", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Taxes and Charges", "owner": "Administrator", "permissions": [], + "row_format": "Dynamic", "sort_field": "creation", "sort_order": "ASC", "states": [] -} \ No newline at end of file +} diff --git a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.py b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.py index b6896e9b6c1..329511c2651 100644 --- a/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.py +++ b/erpnext/accounts/doctype/sales_taxes_and_charges/sales_taxes_and_charges.py @@ -33,7 +33,6 @@ class SalesTaxesandCharges(Document): dont_recompute_tax: DF.Check included_in_paid_amount: DF.Check included_in_print_rate: DF.Check - item_wise_tax_detail: DF.Code | None net_amount: DF.Currency parent: DF.Data parentfield: DF.Data diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py index c9d37b9ad1a..56b25cfe474 100644 --- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py +++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py @@ -16,7 +16,7 @@ from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register i get_group_by_and_display_fields, get_tax_accounts, ) -from erpnext.accounts.report.utils import get_query_columns, get_values_for_columns +from erpnext.accounts.report.utils import get_values_for_columns def execute(filters=None): @@ -96,15 +96,18 @@ def _execute(filters=None, additional_table_columns=None): } total_tax = 0 - for tax in tax_columns: - item_tax = itemised_tax.get(d.name, {}).get(tax, {}) + total_other_charges = 0 + for tax, details in itemised_tax.get(d.name, {}).items(): row.update( { - scrubbed_tax_fields[tax + " Rate"]: item_tax.get("tax_rate", 0), - scrubbed_tax_fields[tax + " Amount"]: item_tax.get("tax_amount", 0), + scrubbed_tax_fields[tax + " Rate"]: details.get("tax_rate", 0), + scrubbed_tax_fields[tax + " Amount"]: details.get("tax_amount", 0), } ) - total_tax += flt(item_tax.get("tax_amount")) + if details.get("is_other_charges"): + total_other_charges += flt(details.get("tax_amount")) + else: + total_tax += flt(details.get("tax_amount")) row.update( {"total_tax": total_tax, "total": d.base_net_amount + total_tax, "currency": company_currency} diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py index d5aa58758c8..cd0949721de 100644 --- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py +++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py @@ -4,7 +4,6 @@ import frappe from frappe import _ -from frappe.model.meta import get_field_precision from frappe.query_builder import functions as fn from frappe.utils import cstr, flt from frappe.utils.nestedset import get_descendants_of @@ -12,7 +11,6 @@ from frappe.utils.xlsxutils import handle_html from erpnext.accounts.report.sales_register.sales_register import get_mode_of_payments from erpnext.accounts.report.utils import get_values_for_columns -from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail from erpnext.selling.report.item_wise_sales_history.item_wise_sales_history import ( get_customer_details, ) @@ -30,18 +28,19 @@ def _execute(filters=None, additional_table_columns=None, additional_conditions= company_currency = frappe.get_cached_value("Company", filters.get("company"), "default_currency") item_list = get_items(filters, additional_table_columns, additional_conditions) - if item_list: - itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency) + if not item_list: + return columns, [], None, None, None, 0 - scrubbed_tax_fields = {} + itemised_tax, tax_columns = get_tax_accounts(item_list, columns, company_currency) + scrubbed_tax_fields = {} - for tax in tax_columns: - scrubbed_tax_fields.update( - { - tax + " Rate": frappe.scrub(tax + " Rate"), - tax + " Amount": frappe.scrub(tax + " Amount"), - } - ) + for tax in tax_columns: + scrubbed_tax_fields.update( + { + tax + " Rate": frappe.scrub(tax + " Rate"), + tax + " Amount": frappe.scrub(tax + " Amount"), + } + ) mode_of_payments = get_mode_of_payments(set(d.parent for d in item_list)) so_dn_map = get_delivery_notes_against_sales_order(item_list) @@ -99,18 +98,17 @@ def _execute(filters=None, additional_table_columns=None, additional_conditions= total_tax = 0 total_other_charges = 0 - for tax in tax_columns: - item_tax = itemised_tax.get(d.name, {}).get(tax, {}) + for tax, details in itemised_tax.get(d.name, {}).items(): row.update( { - scrubbed_tax_fields[tax + " Rate"]: item_tax.get("tax_rate", 0), - scrubbed_tax_fields[tax + " Amount"]: item_tax.get("tax_amount", 0), + scrubbed_tax_fields[tax + " Rate"]: details.get("tax_rate", 0), + scrubbed_tax_fields[tax + " Amount"]: details.get("tax_amount", 0), } ) - if item_tax.get("is_other_charges"): - total_other_charges += flt(item_tax.get("tax_amount")) + if details.get("is_other_charges"): + total_other_charges += flt(details.get("tax_amount")) else: - total_tax += flt(item_tax.get("tax_amount")) + total_tax += flt(details.get("tax_amount")) row.update( { @@ -544,124 +542,52 @@ def get_tax_accounts( doctype="Sales Invoice", tax_doctype="Sales Taxes and Charges", ): - import json - - item_row_map = {} - tax_columns = [] - invoice_item_row = {} - itemised_tax = {} - add_deduct_tax = "charge_type" - - tax_amount_precision = ( - get_field_precision(frappe.get_meta(tax_doctype).get_field("tax_amount"), currency=company_currency) - or 2 - ) - - for d in item_list: - invoice_item_row.setdefault(d.parent, []).append(d) - item_row_map.setdefault(d.parent, {}).setdefault(d.item_code or d.item_name, []).append(d) - - conditions = "" - if doctype == "Purchase Invoice": - conditions = ( - " and category in ('Total', 'Valuation and Total') and base_tax_amount_after_discount_amount != 0" - ) - add_deduct_tax = "add_deduct_tax" - - tax_details = frappe.db.sql( - f""" - select - name, parent, description, item_wise_tax_detail, account_head, - charge_type, {add_deduct_tax}, base_tax_amount_after_discount_amount - from `tab%s` - where - parenttype = %s and docstatus = 1 - and (description is not null and description != '') - and parent in (%s) - %s - order by description - """ - % (tax_doctype, "%s", ", ".join(["%s"] * len(invoice_item_row)), conditions), - tuple([doctype, *list(invoice_item_row)]), - ) - - account_doctype = frappe.qb.DocType("Account") + invoice_item_row = [d.name for d in item_list] + tax = frappe.qb.DocType("Item Wise Tax Detail") + taxes_and_charges = frappe.qb.DocType(tax_doctype) + account = frappe.qb.DocType("Account") query = ( - frappe.qb.from_(account_doctype) - .select(account_doctype.name) - .where(account_doctype.account_type == "Tax") + get_tax_details_query( + doctype, + tax_doctype, + ) + .left_join(account) + .on(taxes_and_charges.account_head == account.name) + .select(account.account_type) + .where(tax.item_row.isin(invoice_item_row)) ) - tax_accounts = query.run() + if doctype == "Purchase Invoice": + query = query.where( + (taxes_and_charges.category.isin(["Total", "Valuation and Total"])) + & (taxes_and_charges.base_tax_amount_after_discount_amount != 0) + ) - for ( - _name, - parent, - description, - item_wise_tax_detail, - account_head, - charge_type, - add_deduct_tax, - tax_amount, - ) in tax_details: - description = handle_html(description) - if description not in tax_columns and tax_amount: - # as description is text editor earlier and markup can break the column convention in reports - tax_columns.append(description) + tax_details = query.run(as_dict=True) - if item_wise_tax_detail: - try: - item_wise_tax_detail = json.loads(item_wise_tax_detail) + precision = frappe.get_precision(tax_doctype, "tax_amount", currency=company_currency) or 2 + tax_columns = set() + itemised_tax = {} - for item_code, tax_data in item_wise_tax_detail.items(): - itemised_tax.setdefault(item_code, frappe._dict()) + for row in tax_details: + description = handle_html(row.description) or row.account_head + rate = "NA" if row.rate == 0 else row.rate + tax_columns.add(description) + itemised_tax.setdefault(row.item_row, {}).setdefault( + description, + frappe._dict( + { + "tax_rate": rate, + "tax_amount": 0, + "is_other_charges": 0 if row.account_type == "Tax" else 1, + } + ), + ) - tax_data = ItemWiseTaxDetail(**tax_data) + itemised_tax[row.item_row][description].tax_amount += flt(row.amount, precision) - if charge_type == "Actual" and not tax_data.tax_rate: - tax_data.tax_rate = "NA" - - item_net_amount = sum( - [flt(d.base_net_amount) for d in item_row_map.get(parent, {}).get(item_code, [])] - ) - - for d in item_row_map.get(parent, {}).get(item_code, []): - item_tax_amount = ( - flt((tax_data.tax_amount * d.base_net_amount) / item_net_amount) - if item_net_amount - else 0 - ) - if item_tax_amount: - tax_value = flt(item_tax_amount, tax_amount_precision) - tax_value = ( - tax_value * -1 - if (doctype == "Purchase Invoice" and add_deduct_tax == "Deduct") - else tax_value - ) - - itemised_tax.setdefault(d.name, {})[description] = frappe._dict( - { - "tax_rate": tax_data.tax_rate, - "tax_amount": tax_value, - "is_other_charges": 0 if tuple([account_head]) in tax_accounts else 1, - } - ) - - except ValueError: - continue - elif charge_type == "Actual" and tax_amount: - for d in invoice_item_row.get(parent, []): - itemised_tax.setdefault(d.name, {})[description] = frappe._dict( - { - "tax_rate": "NA", - "tax_amount": flt( - (tax_amount * d.base_net_amount) / d.base_net_total, tax_amount_precision - ), - } - ) - - tax_columns.sort() + tax_columns = sorted(tax_columns) for desc in tax_columns: columns.append( { @@ -716,6 +642,30 @@ def get_tax_accounts( return itemised_tax, tax_columns +def get_tax_details_query(doctype, tax_doctype): + tax = frappe.qb.DocType("Item Wise Tax Detail") + taxes_and_charges = frappe.qb.DocType(tax_doctype) + + query = ( + frappe.qb.from_(tax) + .left_join(taxes_and_charges) + .on(tax.tax_row == taxes_and_charges.name) + .select( + tax.parent, + tax.item_row, + tax.rate, + tax.amount, + tax.taxable_amount, + taxes_and_charges.charge_type, + taxes_and_charges.account_head, + taxes_and_charges.description, + ) + .where(tax.parenttype == doctype) + ) + + return query + + def add_total_row( data, filters, diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json index 8900165ff7c..8a8a222da73 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.json +++ b/erpnext/buying/doctype/purchase_order/purchase_order.json @@ -102,6 +102,7 @@ "discount_amount", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "address_and_contact_tab", "section_addresses", "supplier_address", @@ -1323,6 +1324,14 @@ "label": "MPS", "options": "Master Production Schedule", "read_only": 1 + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py index 5495c557d1b..5c3e0d92257 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/purchase_order.py @@ -44,6 +44,7 @@ class PurchaseOrder(BuyingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.purchase_taxes_and_charges.purchase_taxes_and_charges import ( @@ -105,6 +106,7 @@ class PurchaseOrder(BuyingController): is_internal_supplier: DF.Check is_old_subcontracting_flow: DF.Check is_subcontracted: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[PurchaseOrderItem] language: DF.Data | None letter_head: DF.Link | None @@ -547,9 +549,6 @@ class PurchaseOrder(BuyingController): unlink_inter_company_doc(self.doctype, self.name, self.inter_company_order_reference) - def on_update(self): - pass - def update_status_updater(self): self.status_updater.append( { diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json index 2f4cae69e01..c9805ed4065 100644 --- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json +++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.json @@ -81,6 +81,7 @@ "disable_rounded_total", "tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "pricing_rule_details", "pricing_rules", "address_and_contact_tab", @@ -930,6 +931,14 @@ "hidden": 1, "label": "Has Unit Price Items", "no_copy": 1 + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, @@ -938,7 +947,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2025-03-03 17:39:38.459977", + "modified": "2025-07-23 02:22:43.526822", "modified_by": "Administrator", "module": "Buying", "name": "Supplier Quotation", @@ -1007,4 +1016,4 @@ "states": [], "timeline_field": "supplier", "title_field": "title" -} \ No newline at end of file +} diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py index cc72d2ab0d4..10cba8a8be8 100644 --- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py +++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py @@ -24,6 +24,7 @@ class SupplierQuotation(BuyingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.purchase_taxes_and_charges.purchase_taxes_and_charges import ( PurchaseTaxesandCharges, @@ -67,6 +68,7 @@ class SupplierQuotation(BuyingController): in_words: DF.Data | None incoterm: DF.Link | None is_subcontracted: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[SupplierQuotationItem] language: DF.Data | None letter_head: DF.Link | None diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index c1a9547499c..3902b2f7202 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -137,6 +137,11 @@ class AccountsController(TransactionBase): if self.doctype in relevant_docs: self.set_payment_schedule() + def on_update(self): + from erpnext.controllers.taxes_and_totals import process_item_wise_tax_details + + process_item_wise_tax_details(self) + def remove_bundle_for_non_stock_invoices(self): has_sabb = False if self.doctype in ("Sales Invoice", "Purchase Invoice") and not self.update_stock: @@ -1161,7 +1166,6 @@ class AccountsController(TransactionBase): if self.get("taxes_and_charges"): if not tax_master_doctype: tax_master_doctype = self.meta.get_field("taxes_and_charges").options - self.extend("taxes", get_taxes_and_charges(tax_master_doctype, self.get("taxes_and_charges"))) def append_taxes_from_item_tax_template(self): @@ -4102,35 +4106,47 @@ def check_if_child_table_updated(child_table_before_update, child_table_after_up return False -def merge_taxes(source_taxes, target_doc): - from erpnext.accounts.doctype.pos_invoice_merge_log.pos_invoice_merge_log import ( - update_item_wise_tax_detail, - ) - - existing_taxes = target_doc.get("taxes") or [] - idx = 1 - for tax in source_taxes: +def merge_taxes(source_doc, target_doc): + tax_map = {} + for tax in source_doc.get("taxes") or []: found = False - for t in existing_taxes: + for t in target_doc.get("taxes") or []: if t.account_head == tax.account_head and t.cost_center == tax.cost_center: t.tax_amount = flt(t.tax_amount) + flt(tax.tax_amount_after_discount_amount) t.base_tax_amount = flt(t.base_tax_amount) + flt(tax.base_tax_amount_after_discount_amount) - update_item_wise_tax_detail(t, tax) + tax_map[tax.name] = t found = True if not found: tax.charge_type = "Actual" - tax.idx = idx - idx += 1 tax.included_in_print_rate = 0 tax.dont_recompute_tax = 1 - tax.row_id = "" + tax.row_id = None + tax.idx = None tax.tax_amount = tax.tax_amount_after_discount_amount tax.base_tax_amount = tax.base_tax_amount_after_discount_amount - tax.item_wise_tax_detail = tax.item_wise_tax_detail - existing_taxes.append(tax) + tax_map[tax.name] = target_doc.append("taxes", tax) - target_doc.set("taxes", existing_taxes) + item_map = {d._old_name: d for d in target_doc.get("items") if d.get("_old_name")} + + item_tax_details = target_doc.get("_item_wise_tax_details") or [] + for row in source_doc.get("item_wise_tax_details"): + item = item_map.get(row.item_row) + tax = tax_map.get(row.tax_row) + if not (item and tax): + continue + + item_tax_details.append( + frappe._dict( + item=item, + tax=tax, + amount=row.amount, + rate=row.rate, + taxable_amount=row.taxable_amount, + ) + ) + + target_doc._item_wise_tax_details = item_tax_details @erpnext.allow_regional diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index af4e76495e9..12b24c420d4 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -71,6 +71,7 @@ class StockController(AccountsController): self.reset_conversion_factor() def on_update(self): + super().on_update() self.check_zero_rate() def reset_conversion_factor(self): diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 30b651aa82b..3b42b7d0c1d 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -6,12 +6,13 @@ import json import frappe from frappe import _, scrub -from frappe.model.document import Document +from frappe.model.document import Document, bulk_insert from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction import erpnext from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate from erpnext.accounts.doctype.pricing_rule.utils import get_applied_pricing_rules +from erpnext.accounts.utils import get_zero_cutoff from erpnext.controllers.accounts_controller import ( validate_conversion_rate, validate_inclusive_tax, @@ -21,8 +22,6 @@ from erpnext.deprecation_dumpster import deprecated from erpnext.stock.get_item_details import ItemDetailsCtx, _get_item_tax_template, get_item_tax_map from erpnext.utilities.regional import temporary_flag -ItemWiseTaxDetail = frappe._dict - class calculate_taxes_and_totals: def __init__(self, doc: Document): @@ -36,7 +35,6 @@ class calculate_taxes_and_totals: ) self._items = self.filter_rows() if self.doc.doctype == "Quotation" else self.doc.get("items") - get_round_off_applicable_accounts(self.doc.company, frappe.flags.round_off_applicable_accounts) self.calculate() @@ -83,7 +81,6 @@ class calculate_taxes_and_totals: self.calculate_taxes() self.adjust_grand_total_for_inclusive_tax() self.calculate_totals() - self._cleanup() self.calculate_total_net_weight() def calculate_tax_withholding_net_total(self): @@ -251,14 +248,12 @@ class calculate_taxes_and_totals: doc.set("base_" + f, val) def initialize_taxes(self): + self.reset_item_wise_tax_details() for tax in self.doc.get("taxes"): if not self.discount_amount_applied: validate_taxes_and_charges(tax) validate_inclusive_tax(tax, self.doc) - if not (self.doc.get("is_consolidated") or tax.get("dont_recompute_tax")): - tax.item_wise_tax_detail = {} - tax_fields = [ "net_amount", "total", @@ -278,6 +273,22 @@ class calculate_taxes_and_totals: self.doc.round_floats_in(tax) + def reset_item_wise_tax_details(self): + # Setting flag for adding rows + self.doc.update_item_wise_tax_details = True + dont_recompute_taxes = [d for d in self.doc.get("taxes") if d.get("dont_recompute_tax")] + + # Identify taxes that shouldn't be recomputed + item_wise_tax_details = [] + # retain tax_breakup for dont_recompute_taxes + for row in self.doc.get("_item_wise_tax_details") or []: + tax = row.get("tax") + if tax in dont_recompute_taxes: + item_wise_tax_details.append(row) + + self.doc._item_wise_tax_details = item_wise_tax_details + self.doc.item_wise_tax_details = [] + def determine_exclusive_rate(self): if not any(cint(tax.included_in_print_rate) for tax in self.doc.get("taxes")): return @@ -476,6 +487,60 @@ class calculate_taxes_and_totals: self._set_in_company_currency(tax, ["total"]) + self.adjust_rounding_in_item_wise_tax_details() + + def adjust_rounding_in_item_wise_tax_details(self): + if ignore_item_wise_tax_details(self.doc): + return + + if not self.doc.get("_item_wise_tax_details"): + return + + invalid_rows = [] + + # reset temporary attributes + for tax in self.doc.taxes: + tax._total_tax_breakup = 0 + tax._last_row_idx = None + + for idx, d in enumerate(self.doc._item_wise_tax_details): + tax = d.get("tax") + if not tax: + continue + tax._total_tax_breakup += d.amount or 0 + tax._last_row_idx = idx + + # Apply rounding difference to the last row + for tax in self.doc.taxes: + last_idx = tax._last_row_idx + if last_idx is None: + continue + + multiplier = -1 if tax.get("add_deduct_tax") == "Deduct" else 1 + expected_amount = tax.base_tax_amount_after_discount_amount * multiplier + actual_breakup = tax._total_tax_breakup + diff = flt(expected_amount - actual_breakup, 5) + + # TODO: fix rounding difference issues + if abs(diff) <= 0.5: + detail_row = self.doc._item_wise_tax_details[last_idx] + detail_row["amount"] = flt(detail_row["amount"] + diff, 5) + + else: + invalid_rows.append(f"Row {tax.idx} (Difference: {diff})") + + if self.doc.flags.ignore_validate: + return + + if invalid_rows: + message = ( + _("Item Wise Tax Details do not match with Taxes and Charges at the following rows:") + + "
" + + "
".join(invalid_rows) + ) + + frappe.throw(_(message)) + 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 # if tax/charges is for deduction, multiply by -1 @@ -533,41 +598,35 @@ class calculate_taxes_and_totals: # don't sum current net amount due to the field being a currency field current_tax_amount = tax_rate * item.qty - if not (self.doc.get("is_consolidated") or tax.get("dont_recompute_tax")): + if not tax.get("dont_recompute_tax"): self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount) return current_net_amount, current_tax_amount def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount, current_net_amount): # store tax breakup for each item - key = item.item_code or item.item_name - item_wise_tax_amount = current_tax_amount * self.doc.conversion_rate - if tax.charge_type != "On Item Quantity": - item_wise_net_amount = current_net_amount * self.doc.conversion_rate - else: - item_wise_net_amount = 0.0 - if frappe.flags.round_row_wise_tax: - item_wise_tax_amount = flt(item_wise_tax_amount, tax.precision("tax_amount")) - item_wise_net_amount = flt(item_wise_net_amount, tax.precision("net_amount")) - if tax_data := tax.item_wise_tax_detail.get(key): - item_wise_tax_amount += flt(tax_data.tax_amount, tax.precision("tax_amount")) - item_wise_net_amount += flt(tax_data.net_amount, tax.precision("net_amount")) - else: - tax.item_wise_tax_detail[key] = ItemWiseTaxDetail( - tax_rate=tax_rate, - tax_amount=flt(item_wise_tax_amount, tax.precision("tax_amount")), - net_amount=flt(item_wise_net_amount, tax.precision("net_amount")), - ) - else: - if tax_data := tax.item_wise_tax_detail.get(key): - item_wise_tax_amount += tax_data.tax_amount - item_wise_net_amount += tax_data.net_amount + multiplier = -1 if tax.get("add_deduct_tax") == "Deduct" else 1 + item_wise_tax_amount = flt( + current_tax_amount * self.doc.conversion_rate * multiplier, tax.precision("tax_amount") + ) - tax.item_wise_tax_detail[key] = ItemWiseTaxDetail( - tax_rate=tax_rate, - tax_amount=item_wise_tax_amount, - net_amount=item_wise_net_amount, + if tax.charge_type != "On Item Quantity": + item_wise_taxable_amount = flt( + current_net_amount * self.doc.conversion_rate * multiplier, tax.precision("tax_amount") ) + else: + item_wise_taxable_amount = 0.0 + + # maintaining a temp object with item and tax object because correct name will be available after insertion. + self.doc._item_wise_tax_details.append( + frappe._dict( + item=item, + tax=tax, + rate=tax_rate, + amount=item_wise_tax_amount, + taxable_amount=item_wise_taxable_amount, + ) + ) def round_off_totals(self, tax): if tax.account_head in frappe.flags.round_off_applicable_accounts: @@ -704,12 +763,6 @@ class calculate_taxes_and_totals: self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"]) - def _cleanup(self): - if not self.doc.get("is_consolidated"): - for tax in self.doc.get("taxes"): - if not tax.get("dont_recompute_tax"): - tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail) - def set_discount_amount(self): if self.doc.additional_discount_percentage: self.doc.discount_amount = flt( @@ -1150,30 +1203,45 @@ def get_itemised_tax_breakup_header(item_doctype, tax_accounts): @erpnext.allow_regional def get_itemised_tax_breakup_data(doc): - itemised_tax = get_itemised_tax(doc.taxes) + itemised_tax = get_itemised_tax(doc) itemised_tax_data = [] for item_code, taxes in itemised_tax.items(): - taxable_amount = next(iter(taxes.values())).get("net_amount") + taxable_amount = next(iter(taxes.values())).get("taxable_amount") itemised_tax_data.append(frappe._dict({"item": item_code, "taxable_amount": taxable_amount, **taxes})) return itemised_tax_data -def get_itemised_tax(taxes, with_tax_account=False): +def get_itemised_tax(doc, with_tax_account=False): itemised_tax = {} - for tax in taxes: + precision = doc.precision("tax_amount", "taxes") + + for row in doc.get("_item_wise_tax_details"): + item = row.get("item") + tax = row.get("tax") + if not item or not tax: + continue + + item_code = item.item_code or item.item_name if getattr(tax, "category", None) and tax.category == "Valuation": continue - item_tax_map = json.loads(tax.item_wise_tax_detail) if tax.item_wise_tax_detail else {} - if item_tax_map: - for item_code, tax_data in item_tax_map.items(): - tax_data = ItemWiseTaxDetail(**tax_data) - itemised_tax.setdefault(item_code, frappe._dict()) - itemised_tax[item_code][tax.description] = tax_data + tax_info = itemised_tax.setdefault(item_code, frappe._dict()).setdefault( + tax.description, + frappe._dict( + { + "tax_amount": 0.0, + "taxable_amount": 0.0, + "tax_rate": row.rate, + } + ), + ) - if with_tax_account: - itemised_tax[item_code][tax.description].tax_account = tax.account_head + tax_info.tax_amount += flt(row.amount, precision) + tax_info.taxable_amount += flt(row.taxable_amount, precision) + + if with_tax_account: + tax_info.tax_account = tax.account_head return itemised_tax @@ -1196,6 +1264,39 @@ def get_rounding_tax_settings(): return frappe.get_single_value("Accounts Settings", "round_row_wise_tax") +def ignore_item_wise_tax_details(doc): + """Ignore item wise tax details if the doctype does not have item_wise_tax_details field.""" + if not doc.meta.get_field("item_wise_tax_details"): + return True + + return False + + +def process_item_wise_tax_details(doc): + if ignore_item_wise_tax_details(doc): + return + + if not (doc.get("update_item_wise_tax_details") and doc.get("_item_wise_tax_details")): + return + + docs = [] + for row in doc.get("_item_wise_tax_details"): + tax_details = doc.append( + "item_wise_tax_details", + { + **row, + "docstatus": doc.docstatus, + "item_row": row.item.name, + "tax_row": row.tax.name, + }, + ) + tax_details.set_new_name() + docs.append(tax_details) + + bulk_insert("Item Wise Tax Detail", docs) + doc.update_item_wise_tax_details = False + + class init_landed_taxes_and_totals: def __init__(self, doc): self.doc = doc diff --git a/erpnext/controllers/tests/test_item_wise_tax_details.py b/erpnext/controllers/tests/test_item_wise_tax_details.py index e535aad6933..7628f77f7e3 100644 --- a/erpnext/controllers/tests/test_item_wise_tax_details.py +++ b/erpnext/controllers/tests/test_item_wise_tax_details.py @@ -73,32 +73,54 @@ class TestTaxesAndTotals(FrappeTestCase): "taxes", { "charge_type": "On Item Quantity", - "account_head": "_Test Account Shipping - _TC", + "account_head": "_Test Account Shipping Charges - _TC", "cost_center": "_Test Cost Center - _TC", "description": "Shipping", "rate": 50, }, ) - self.doc.set_missing_item_details() - calculate_taxes_and_totals(self.doc) + self.doc.save() - expected_values = { - "VAT": {"tax_rate": 10, "tax_amount": 10, "net_amount": 100}, - "Service Tax": {"tax_rate": 14, "tax_amount": 1.4, "net_amount": 10}, - "Customs Duty": {"tax_rate": 5, "tax_amount": 5.57, "net_amount": 111.4}, - "Shipping": {"tax_rate": 50, "tax_amount": 50, "net_amount": 0.0}, # net_amount: here qty - } + expected_values = [ + { + "item_row": self.doc.items[0].name, + "tax_row": self.doc.taxes[0].name, + "rate": 10.0, + "amount": 10.0, + "taxable_amount": 100.0, + }, + { + "item_row": self.doc.items[0].name, + "tax_row": self.doc.taxes[1].name, + "rate": 14.0, + "amount": 1.4, + "taxable_amount": 10.0, + }, + { + "item_row": self.doc.items[0].name, + "tax_row": self.doc.taxes[2].name, + "rate": 5.0, + "amount": 5.57, + "taxable_amount": 111.4, + }, + { + "item_row": self.doc.items[0].name, + "tax_row": self.doc.taxes[3].name, + "rate": 50.0, + "amount": 50.0, + "taxable_amount": 0.0, + }, + ] - for tax in self.doc.taxes: - self.assertIn(tax.description, expected_values) - item_wise_tax_detail = json.loads(tax.item_wise_tax_detail) - tax_detail = item_wise_tax_detail[self.doc.items[0].item_code] - self.assertAlmostEqual(tax_detail.get("tax_rate"), expected_values[tax.description]["tax_rate"]) - self.assertAlmostEqual( - tax_detail.get("tax_amount"), expected_values[tax.description]["tax_amount"] - ) - self.assertAlmostEqual( - tax_detail.get("net_amount"), expected_values[tax.description]["net_amount"] - ) - # Check if net_total is set for each tax - self.assertEqual(tax.net_amount, expected_values[tax.description]["net_amount"]) + actual_values = [ + { + "item_row": row.item_row, + "tax_row": row.tax_row, + "rate": row.rate, + "amount": row.amount, + "taxable_amount": row.taxable_amount, + } + for row in self.doc.item_wise_tax_details + ] + + self.assertEqual(actual_values, expected_values) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 4cfc3d2a232..51d4f66b9bb 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -391,7 +391,6 @@ erpnext.patches.v15_0.migrate_to_utm_analytics erpnext.patches.v15_0.update_sub_voucher_type_in_gl_entries erpnext.patches.v15_0.update_task_assignee_email_field_in_asset_maintenance_log erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter -erpnext.patches.v15_0.migrate_old_item_wise_tax_detail_data_format erpnext.patches.v15_0.set_is_exchange_gain_loss_in_payment_entry_deductions erpnext.patches.v14_0.update_stock_uom_in_work_order_item erpnext.patches.v15_0.enable_allow_existing_serial_no @@ -448,3 +447,5 @@ erpnext.patches.v15_0.toggle_legacy_controller_for_period_closing erpnext.patches.v16_0.update_serial_batch_entries erpnext.patches.v16_0.set_company_wise_warehouses erpnext.patches.v16_0.set_valuation_method_on_companies +erpnext.patches.v15_0.migrate_old_item_wise_tax_detail_data_to_table + diff --git a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py deleted file mode 100644 index 735420063d4..00000000000 --- a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py +++ /dev/null @@ -1,77 +0,0 @@ -import json - -import frappe -from frappe.utils import flt - -from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail - - -def execute(): - # Get all DocTypes that have the 'item_wise_tax_detail' field - doctypes_with_tax_details = frappe.get_all( - "DocField", filters={"fieldname": "item_wise_tax_detail"}, fields=["parent"], pluck="parent" - ) - for doctype in doctypes_with_tax_details: - migrated_count = 0 # Counter for migrated records per DocType - # Get all documents of this DocType that have data in 'item_wise_tax_detail' - docs = frappe.get_all( - doctype, - filters={"item_wise_tax_detail": ["is", "set"]}, - fields=["name", "item_wise_tax_detail"], - ) - for doc in docs: - if not doc.item_wise_tax_detail: - continue - - updated_tax_details = {} - needs_update = False - - try: - item_iterator = json.loads(doc.item_wise_tax_detail).items() - except AttributeError as e: - # This is stale data from 2009 found in a database - if isinstance(json.loads(doc.item_wise_tax_detail), int | float): - needs_update = False - else: - raise e - else: - for item, tax_data in item_iterator: - if isinstance(tax_data, list) and len(tax_data) == 2: - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=tax_data[0], - tax_amount=tax_data[1], - # can't be reliably reconstructed since it depends on the tax type - # (actual, net, previous line total, previous line net, etc) - net_amount=0.0, - ) - needs_update = True - # intermediate patch version of the originating PR - elif isinstance(tax_data, list) and len(tax_data) == 3: - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=tax_data[0], - tax_amount=tax_data[1], - net_amount=tax_data[2], - ) - needs_update = True - elif isinstance(tax_data, str): - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=flt(tax_data), - tax_amount=0.0, - net_amount=0.0, - ) - needs_update = True - else: - updated_tax_details[item] = tax_data - - if needs_update: - frappe.db.set_value( - doctype, - doc.name, - "item_wise_tax_detail", - json.dumps(updated_tax_details), - update_modified=False, - ) - migrated_count += 1 # Increment the counter for each migrated record - - frappe.db.commit() - print(f"Migrated {migrated_count} records for DocType: {doctype}") diff --git a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_to_table.py b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_to_table.py new file mode 100644 index 00000000000..186d470e01a --- /dev/null +++ b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_to_table.py @@ -0,0 +1,307 @@ +import click +import frappe +from frappe import parse_json +from frappe.model.document import bulk_insert +from frappe.utils import flt + +DOCTYPES_TO_PATCH = { + "Sales Taxes and Charges": [ + "Sales Invoice", + "POS Invoice", + "Sales Order", + "Delivery Note", + "Quotation", + ], + "Purchase Taxes and Charges": [ + "Purchase Invoice", + "Purchase Order", + "Purchase Receipt", + "Supplier Quotation", + ], +} + + +TAX_WITHHOLDING_DOCS = ( + "Purchase Invoice", + "Purchase Order", + "Purchase Receipt", +) + + +def execute(): + for tax_doctype, doctypes in DOCTYPES_TO_PATCH.items(): + for doctype in doctypes: + docnames = frappe.get_all( + tax_doctype, + filters={ + "item_wise_tax_detail": ["is", "set"], + "docstatus": ["=", 1], + "parenttype": ["=", doctype], + }, + pluck="parent", + ) + + total_docs = len(docnames) + if not total_docs: + continue + + chunk_size = 1000 + + with click.progressbar( + range(0, total_docs, chunk_size), label=f"Migrating {total_docs} {doctype}s" + ) as bar: + for index in bar: + chunk = docnames[index : index + chunk_size] + doc_info = get_doc_details(chunk, doctype) + docs = [d.name for d in doc_info] # valid invoices + + # Delete existing item-wise tax details to avoid duplicates + delete_existing_tax_details(docs, doctype) + + taxes = get_taxes_for_docs(docs, tax_doctype, doctype) + items = get_items_for_docs(docs, doctype) + compiled_docs = compile_docs(doc_info, taxes, items, doctype, tax_doctype) + rows_to_insert = [] + + for doc in compiled_docs: + if not (doc.taxes and doc.items): + continue + rows_to_insert.extend(ItemTax().get_item_wise_tax_details(doc)) + + if rows_to_insert: + bulk_insert("Item Wise Tax Detail", rows_to_insert, commit_chunks=True) + + +def get_taxes_for_docs(parents, tax_doctype, doctype): + tax = frappe.qb.DocType(tax_doctype) + + return ( + frappe.qb.from_(tax) + .select("*") + .where(tax.parenttype == doctype) + .where(tax.parent.isin(parents)) + .run(as_dict=True) + ) + + +def get_items_for_docs(parents, doctype): + item = frappe.qb.DocType(f"{doctype} Item") + additional_fields = [] + + if doctype in TAX_WITHHOLDING_DOCS: + additional_fields.append(item.apply_tds) + + return ( + frappe.qb.from_(item) + .select( + item.name, + item.parent, + item.item_code, + item.item_name, + item.base_net_amount, + item.qty, + item.item_tax_rate, + *additional_fields, + ) + .where(item.parenttype == doctype) + .where(item.parent.isin(parents)) + .run(as_dict=True) + ) + + +def get_doc_details(parents, doctype): + inv = frappe.qb.DocType(doctype) + additional_fields = [] + if doctype in TAX_WITHHOLDING_DOCS: + additional_fields.append(inv.base_tax_withholding_net_total) + + return ( + frappe.qb.from_(inv) + .select( + inv.name, + inv.base_net_total, + inv.company, + *additional_fields, + ) + .where(inv.name.isin(parents)) + .run(as_dict=True) + ) + + +def compile_docs(doc_info, taxes, items, doctype, tax_doctype): + """ + Compile docs, so that each one could be accessed as if it's a single doc. + """ + response = frappe._dict() + for doc in doc_info: + response[doc.name] = frappe._dict(**doc, taxes=[], items=[], doctype=doctype, tax_doctype=tax_doctype) + + for tax in taxes: + response[tax.parent]["taxes"].append(tax) + + for item in items: + response[item.parent]["items"].append(item) + + return response.values() + + +def delete_existing_tax_details(doc_names, doctype): + """ + Delete existing Item Wise Tax Detail records for the given documents + to avoid duplicates when re-running the migration. + """ + if not doc_names: + return + + frappe.db.delete("Item Wise Tax Detail", {"parent": ["in", doc_names], "parenttype": doctype}) + + +class ItemTax: + def get_item_wise_tax_details(self, doc): + """ + This method calculates tax amounts for each item-tax combination. + """ + item_wise_tax_details = [] + company_currency = frappe.get_cached_value("Company", doc.company, "default_currency") + precision = frappe.get_precision(doc.tax_doctype, "tax_amount", currency=company_currency) + + tax_differences = frappe._dict() + last_taxable_items = frappe._dict() + + # Initialize tax differences with expected amounts + for tax_row in doc.taxes: + if tax_row.base_tax_amount_after_discount_amount: + multiplier = -1 if tax_row.get("add_deduct_tax") == "Deduct" else 1 + tax_differences[tax_row.name] = tax_row.base_tax_amount_after_discount_amount * multiplier + + idx = 1 + for item in doc.get("items"): + item_proportion = item.base_net_amount / doc.base_net_total if doc.base_net_total else 0 + for tax_row in doc.taxes: + tax_rate = 0 + tax_amount = 0 + + if not tax_row.base_tax_amount_after_discount_amount: + continue + + charge_type = tax_row.charge_type + if tax_row.item_wise_tax_detail: + # tax rate + tax_rate = self._get_item_tax_rate(item, tax_row) + # tax amount + if tax_rate: + multiplier = ( + item.qty if charge_type == "On Item Quantity" else item.base_net_amount / 100 + ) + tax_amount = multiplier * tax_rate + else: + # eg: charge_type == actual + item_key = item.item_code or item.item_name + item_tax_detail = self._get_item_tax_details(tax_row).get(item_key, {}) + tax_amount = item_tax_detail.get("tax_amount", 0) * item_proportion + # Actual rows where no item_wise_tax_detail + elif charge_type == "Actual": + if tax_row.get("is_tax_withholding_account"): + if not item.get("apply_tds") or not doc.get("base_tax_withholding_net_total"): + item_proportion = 0 + else: + item_proportion = item.base_net_amount / doc.base_tax_withholding_net_total + + tax_amount = tax_row.base_tax_amount_after_discount_amount * item_proportion + + if tax_row.get("add_deduct_tax") == "Deduct": + tax_amount *= -1 + + tax_doc = get_item_tax_doc(item, tax_row, tax_rate, tax_amount, idx, precision) + item_wise_tax_details.append(tax_doc) + + # Update tax differences and track last taxable item + if tax_amount: + tax_differences[tax_row.name] -= tax_amount + last_taxable_items[tax_row.name] = tax_doc + + idx += 1 + + # Handle rounding errors by applying differences to last taxable items + self._handle_rounding_differences(tax_differences, last_taxable_items) + + return item_wise_tax_details + + def _handle_rounding_differences(self, tax_differences, last_taxable_items): + """ + Handle rounding errors by applying the difference to the last taxable item + """ + for tax_row, diff in tax_differences.items(): + if not diff or tax_row not in last_taxable_items: + continue + + rounded_difference = flt(diff, 5) + + if abs(rounded_difference) <= 0.5: + last_item_tax_doc = last_taxable_items[tax_row] + last_item_tax_doc.amount = flt(last_item_tax_doc.amount + rounded_difference, 5) + + def _get_item_tax_details(self, tax_row): + # temp cache + if not getattr(tax_row, "__tax_details", None): + tax_row.__tax_details = parse_item_wise_tax_details(tax_row.get("item_wise_tax_detail") or "{}") + + return tax_row.__tax_details + + def _get_item_tax_rate(self, item, tax_row): + # NOTE: Use item tax rate as same item code + # could have different tax rates in same invoice + + item_tax_rates = frappe.parse_json(item.item_tax_rate) + + if tax_row.account_head in item_tax_rates: + return item_tax_rates[tax_row.account_head] + + return tax_row.rate + + +def get_item_tax_doc(item, tax, rate, tax_value, idx, precision=2): + return frappe.get_doc( + { + "doctype": "Item Wise Tax Detail", + "name": frappe.generate_hash(), + "idx": idx, + "item_row": item.name, + "tax_row": tax.name, + "rate": rate, + "amount": flt(tax_value, precision), + "taxable_amount": item.base_net_amount, + "docstatus": tax.docstatus, + "parent": tax.parent, + "parenttype": tax.parenttype, + "parentfield": "item_wise_tax_details", + } + ) + + +def parse_item_wise_tax_details(item_wise_tax_detail): + updated_tax_details = {} + try: + item_iterator = parse_json(item_wise_tax_detail) + except Exception: + return updated_tax_details + else: + # This is stale data from 2009 found in a database + if isinstance(item_iterator, int | float): + return updated_tax_details + + for item, tax_data in item_iterator.items(): + if isinstance(tax_data, list) and len(tax_data) >= 2: + updated_tax_details[item] = frappe._dict( + tax_rate=tax_data[0] or 0, + tax_amount=tax_data[1] or 0, + ) + elif isinstance(tax_data, str): + updated_tax_details[item] = frappe._dict( + tax_rate=flt(tax_data), + tax_amount=0.0, + ) + elif isinstance(tax_data, dict): + updated_tax_details[item] = tax_data + + return updated_tax_details diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js index 3e36387c066..bfedf72764b 100644 --- a/erpnext/public/js/controllers/taxes_and_totals.js +++ b/erpnext/public/js/controllers/taxes_and_totals.js @@ -552,44 +552,9 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments { current_tax_amount = tax_rate * item.qty; } - if (!tax.dont_recompute_tax) { - this.set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount); - } - return current_tax_amount; } - set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount) { - // store tax breakup for each item - let tax_detail = tax.item_wise_tax_detail; - let key = item.item_code || item.item_name; - - if (typeof tax_detail == "string") { - tax.item_wise_tax_detail = JSON.parse(tax.item_wise_tax_detail); - tax_detail = tax.item_wise_tax_detail; - } - - let item_wise_tax_amount = current_tax_amount * this.frm.doc.conversion_rate; - let item_wise_net_amount = current_net_amount * this.frm.doc.conversion_rate; - if (frappe.flags.round_row_wise_tax) { - item_wise_tax_amount = flt(item_wise_tax_amount, precision("tax_amount", tax)); - item_wise_net_amount = flt(item_wise_net_amount, precision("net_amount", tax)); - if (tax_detail && tax_detail[key]) { - item_wise_tax_amount += flt(tax_detail[key].tax_amount, precision("tax_amount", tax)); - item_wise_net_amount += flt(tax_detail[key].net_amount, precision("net_amount", tax)); - } - } else if (tax_detail && tax_detail[key]) { - item_wise_tax_amount += tax_detail[key].tax_amount; - item_wise_net_amount += tax_detail[key].net_amount; - } - - tax_detail[key] = { - tax_rate: tax_rate, - tax_amount: flt(item_wise_tax_amount, precision("base_tax_amount", tax)), - net_amount: flt(item_wise_net_amount, precision("base_net_amount", tax)), - }; - } - round_off_totals(tax) { if (frappe.flags.round_off_applicable_accounts.includes(tax.account_head)) { tax.tax_amount = Math.round(tax.tax_amount); @@ -787,10 +752,6 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments { $.each(temporary_fields, function (i, fieldname) { delete tax[fieldname]; }); - - if (!tax.dont_recompute_tax) { - tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail); - } }); } } diff --git a/erpnext/regional/italy/utils.py b/erpnext/regional/italy/utils.py index 40b6746ab2a..2e8d38bebde 100644 --- a/erpnext/regional/italy/utils.py +++ b/erpnext/regional/italy/utils.py @@ -6,7 +6,7 @@ from frappe import _ from frappe.utils import cstr, flt from frappe.utils.file_manager import remove_file -from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail, get_itemised_tax +from erpnext.controllers.taxes_and_totals import get_itemised_tax from erpnext.regional.italy import state_codes from erpnext.stock.utils import get_default_stock_uom @@ -18,7 +18,7 @@ def update_itemised_tax_data(doc): if doc.doctype == "Purchase Invoice": return - itemised_tax = get_itemised_tax(doc.taxes) + itemised_tax = get_itemised_tax(doc) for row in doc.items: tax_rate = 0.0 @@ -79,7 +79,7 @@ def prepare_invoice(invoice, progressive_number): invoice.transmission_format_code = "FPR12" invoice.e_invoice_items = [item for item in invoice.items] - tax_data = get_invoice_summary(invoice.e_invoice_items, invoice.taxes) + tax_data = get_invoice_summary(invoice.e_invoice_items, invoice.taxes, invoice.item_wise_tax_details) invoice.tax_data = tax_data # Check if stamp duty (Bollo) of 2 EUR exists. @@ -140,8 +140,9 @@ def download_zip(files, output_filename): zip_stream.close() -def get_invoice_summary(items, taxes): +def get_invoice_summary(items, taxes, item_wise_tax_details): summary_data = frappe._dict() + taxes_wise_tax_details = {d.tax_row: d for d in item_wise_tax_details} for tax in taxes: # Include only VAT charges. if tax.charge_type == "Actual": @@ -151,91 +152,63 @@ def get_invoice_summary(items, taxes): if tax.charge_type in ["On Previous Row Total", "On Previous Row Amount"]: reference_row = next((row for row in taxes if row.idx == int(tax.row_id or 0)), None) if reference_row: - items.append( - frappe._dict( - idx=len(items) + 1, - item_code=reference_row.description, - item_name=reference_row.description, - description=reference_row.description, - rate=reference_row.tax_amount, - qty=1.0, - amount=reference_row.tax_amount, - stock_uom=get_default_stock_uom(), - tax_rate=tax.rate, - tax_amount=(reference_row.tax_amount * tax.rate) / 100, - net_amount=reference_row.tax_amount, - taxable_amount=reference_row.tax_amount, - item_tax_rate={tax.account_head: tax.rate}, - charges=True, - ) - ) + append_row_as_charges(items, tax, reference_row, summary_data) - # Check item tax rates if tax rate is zero. - if tax.rate == 0: - for item in items: - item_tax_rate = item.item_tax_rate - if isinstance(item.item_tax_rate, str): - item_tax_rate = json.loads(item.item_tax_rate) + for row in taxes_wise_tax_details.get(tax.name) or []: + update_summary_details(summary_data, tax, row.rate, row.amount, row.taxable_amount) - if item_tax_rate and tax.account_head in item_tax_rate: - key = cstr(item_tax_rate[tax.account_head]) - if key not in summary_data: - summary_data.setdefault( - key, - { - "tax_amount": 0.0, - "taxable_amount": 0.0, - "tax_exemption_reason": "", - "tax_exemption_law": "", - }, - ) - - summary_data[key]["tax_amount"] += item.tax_amount - summary_data[key]["taxable_amount"] += item.net_amount - if key == "0.0": - summary_data[key]["tax_exemption_reason"] = tax.tax_exemption_reason - summary_data[key]["tax_exemption_law"] = tax.tax_exemption_law - - if summary_data.get("0.0") and tax.charge_type in [ - "On Previous Row Total", - "On Previous Row Amount", - ]: - summary_data[key]["taxable_amount"] = tax.total - - if summary_data == {}: # Implies that Zero VAT has not been set on any item. - summary_data.setdefault( - "0.0", - { - "tax_amount": 0.0, - "taxable_amount": tax.total, - "tax_exemption_reason": tax.tax_exemption_reason, - "tax_exemption_law": tax.tax_exemption_law, - }, - ) - - else: - item_wise_tax_detail = json.loads(tax.item_wise_tax_detail) - # TODO: with net_amount stored inside item_wise_tax_detail, this entire block seems obsolete and redundant - for _item_code, tax_data in item_wise_tax_detail.items(): - tax_data = ItemWiseTaxDetail(**tax_data) - if tax_data.tax_rate != tax.rate: - continue - key = cstr(tax.rate) - if not summary_data.get(key): - summary_data.setdefault(key, {"tax_amount": 0.0, "taxable_amount": 0.0}) - summary_data[key]["tax_amount"] += tax_data.tax_amount - summary_data[key]["taxable_amount"] += tax_data.net_amount - - for item in items: - key = cstr(tax.rate) - if item.get("charges"): - if not summary_data.get(key): - summary_data.setdefault(key, {"taxable_amount": 0.0}) - summary_data[key]["taxable_amount"] += item.taxable_amount + if summary_data == {}: + # Implies that Zero VAT has not been set on any item. + update_summary_details(summary_data, tax, 0.0, 0.0, tax.total) return summary_data +def update_summary_details(summary_data, tax, rate, amount, taxable_amount): + key = cstr(rate) + summary_data.setdefault( + key, + { + "tax_amount": 0.0, + "taxable_amount": 0.0, + "tax_exemption_reason": "", + "tax_exemption_law": "", + }, + ) + + summary_data[key]["tax_amount"] += amount + summary_data[key]["taxable_amount"] += taxable_amount + + if key == "0.0": + summary_data[key]["tax_exemption_reason"] = tax.tax_exemption_reason + summary_data[key]["tax_exemption_law"] = tax.tax_exemption_law + + +def append_row_as_charges(items, tax, reference_row, summary_data): + rate = tax.rate + amount = (reference_row.tax_amount * tax.rate) / 100 + taxable_amount = reference_row.tax_amount + items.append( + frappe._dict( + idx=len(items) + 1, + item_code=reference_row.description, + item_name=reference_row.description, + description=reference_row.description, + rate=reference_row.tax_amount, + qty=1.0, + amount=reference_row.tax_amount, + stock_uom=get_default_stock_uom(), + tax_rate=rate, + tax_amount=amount, + net_amount=taxable_amount, + taxable_amount=taxable_amount, + item_tax_rate={tax.account_head: tax.rate}, + charges=True, + ) + ) + update_summary_details(summary_data, tax, rate, amount, taxable_amount) + + # Preflight for successful e-invoice export. def sales_invoice_validate(doc): # Validate company diff --git a/erpnext/regional/report/vat_audit_report/vat_audit_report.py b/erpnext/regional/report/vat_audit_report/vat_audit_report.py index 36931f4bb97..78fdc62e29c 100644 --- a/erpnext/regional/report/vat_audit_report/vat_audit_report.py +++ b/erpnext/regional/report/vat_audit_report/vat_audit_report.py @@ -8,7 +8,7 @@ import frappe from frappe import _ from frappe.utils import formatdate, get_link_to_form -from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail +from erpnext.accounts.report.item_wise_sales_register.item_wise_sales_register import get_tax_details_query def execute(filters=None): @@ -80,93 +80,54 @@ class VATAuditReport: def get_invoice_items(self, doctype): self.invoice_items = frappe._dict() - - items = frappe.db.sql( - """ - SELECT - item_code, parent, base_net_amount, is_zero_rated - FROM - `tab{} Item` - WHERE - parent in ({}) - """.format(doctype, ", ".join(["%s"] * len(self.invoices))), - tuple(self.invoices), - as_dict=1, + item_doctype = frappe.qb.DocType(doctype + " Item") + self.invoice_items = frappe._dict( + frappe.qb.from_(item_doctype) + .select( + item_doctype.name, + item_doctype.is_zero_rated, + ) + .where(item_doctype.parent.isin(list(self.invoices.keys()))) + .run(as_list=1) ) - for d in items: - self.invoice_items.setdefault(d.parent, {}).setdefault(d.item_code, {"net_amount": 0.0}) - self.invoice_items[d.parent][d.item_code]["net_amount"] += d.get("base_net_amount", 0) - self.invoice_items[d.parent][d.item_code]["is_zero_rated"] = d.is_zero_rated def get_items_based_on_tax_rate(self, doctype): self.items_based_on_tax_rate = frappe._dict() - self.item_tax_rate = frappe._dict() self.tax_doctype = ( "Purchase Taxes and Charges" if doctype == "Purchase Invoice" else "Sales Taxes and Charges" ) - self.tax_details = frappe.db.sql( - """ - SELECT - parent, account_head, item_wise_tax_detail - FROM - `tab{}` - WHERE - parenttype = {} and docstatus = 1 - and parent in ({}) - ORDER BY - account_head - """.format(self.tax_doctype, "%s", ", ".join(["%s"] * len(self.invoices.keys()))), - tuple([doctype, *list(self.invoices.keys())]), + taxes_and_charges = frappe.qb.DocType(self.tax_doctype) + item_wise_tax = frappe.qb.DocType("Item Wise Tax Detail") + invoice_names = list(self.invoices.keys()) + if not invoice_names: + return + + tax_details = ( + get_tax_details_query(doctype, self.tax_doctype) + .where(item_wise_tax.parent.isin(invoice_names)) + .where(taxes_and_charges.account_head.isin(self.sa_vat_accounts)) + .run(as_dict=True) ) - for parent, account, item_wise_tax_detail in self.tax_details: - if item_wise_tax_detail: - try: - if account in self.sa_vat_accounts: - item_wise_tax_detail = json.loads(item_wise_tax_detail) - else: - continue - for item_code, tax_data in item_wise_tax_detail.items(): - tax_data = ItemWiseTaxDetail(**tax_data) - is_zero_rated = self.invoice_items.get(parent).get(item_code).get("is_zero_rated") - # to skip items with non-zero tax rate in multiple rows - if tax_data.tax_rate == 0 and not is_zero_rated: - continue - tax_rate = self.get_item_amount_map(parent, item_code, tax_data) + for row in tax_details: + parent = row.parent + item = row.item_row + is_zero_rated = self.invoice_items.get(item) + if row.rate == 0 and not is_zero_rated: + continue - if tax_rate is not None: - rate_based_dict = self.items_based_on_tax_rate.setdefault(parent, {}).setdefault( - tax_rate, [] - ) - if item_code not in rate_based_dict: - rate_based_dict.append(item_code) - except ValueError: - continue - - # TODO: now that tax_data holds net_amount, this method seems almost obsolete and can be removactored, - # gross_amount can be calculated on the file as a list comprehension - def get_item_amount_map(self, parent, item_code, tax_data): - net_amount = self.invoice_items.get(parent).get(item_code).get("net_amount") - tax_rate = tax_data.tax_rate - tax_amount = tax_data.tax_amount - gross_amount = net_amount + tax_amount - - self.item_tax_rate.setdefault(parent, {}).setdefault( - item_code, - { - "tax_rate": tax_rate, - "gross_amount": 0.0, - "tax_amount": 0.0, - "net_amount": 0.0, - }, - ) - - self.item_tax_rate[parent][item_code]["net_amount"] += net_amount - self.item_tax_rate[parent][item_code]["tax_amount"] += tax_amount - self.item_tax_rate[parent][item_code]["gross_amount"] += gross_amount - - return tax_rate + self.items_based_on_tax_rate.setdefault(parent, {}).setdefault( + row.rate, + { + "gross_amount": 0.0, + "tax_amount": 0.0, + "net_amount": 0.0, + }, + ) + self.items_based_on_tax_rate[parent][row.rate]["tax_amount"] += row.amount + self.items_based_on_tax_rate[parent][row.rate]["net_amount"] += row.taxable_amount + self.items_based_on_tax_rate[parent][row.rate]["gross_amount"] += row.amount + row.taxable_amount def get_conditions(self): conditions = "" @@ -209,25 +170,30 @@ class VATAuditReport: def get_consolidated_data(self, doctype): consolidated_data_map = {} for inv, inv_data in self.invoices.items(): - if self.items_based_on_tax_rate.get(inv): - for rate, items in self.items_based_on_tax_rate.get(inv).items(): - row = {"tax_amount": 0.0, "gross_amount": 0.0, "net_amount": 0.0} + rate_details = self.items_based_on_tax_rate.get(inv, {}) + if not rate_details: + continue - consolidated_data_map.setdefault(rate, {"data": []}) - for item in items: - item_details = self.item_tax_rate.get(inv).get(item) - row["account"] = inv_data.get("account") - row["posting_date"] = formatdate(inv_data.get("posting_date"), "dd-mm-yyyy") - row["voucher_type"] = doctype - row["voucher_no"] = inv - row["party_type"] = "Customer" if doctype == "Sales Invoice" else "Supplier" - row["party"] = inv_data.get("party") - row["remarks"] = inv_data.get("remarks") - row["gross_amount"] += item_details.get("gross_amount") - row["tax_amount"] += item_details.get("tax_amount") - row["net_amount"] += item_details.get("net_amount") + for rate, item_details in rate_details.items(): + row = { + "tax_amount": 0.0, + "gross_amount": 0.0, + "net_amount": 0.0, + } - consolidated_data_map[rate]["data"].append(row) + row["account"] = inv_data.get("account") + row["posting_date"] = formatdate(inv_data.get("posting_date"), "dd-mm-yyyy") + row["voucher_type"] = doctype + row["voucher_no"] = inv + row["party_type"] = "Customer" if doctype == "Sales Invoice" else "Supplier" + row["party"] = inv_data.get("party") + row["remarks"] = inv_data.get("remarks") + row["gross_amount"] += item_details.get("gross_amount") + row["tax_amount"] += item_details.get("tax_amount") + row["net_amount"] += item_details.get("net_amount") + + consolidated_data_map.setdefault(rate, {"data": []}) + consolidated_data_map[rate]["data"].append(row) return consolidated_data_map diff --git a/erpnext/regional/united_arab_emirates/utils.py b/erpnext/regional/united_arab_emirates/utils.py index 4cc805c8616..28997542393 100644 --- a/erpnext/regional/united_arab_emirates/utils.py +++ b/erpnext/regional/united_arab_emirates/utils.py @@ -14,7 +14,7 @@ def update_itemised_tax_data(doc): if not meta.has_field("tax_rate"): return - itemised_tax = get_itemised_tax(doc.taxes) + itemised_tax = get_itemised_tax(doc) def determine_if_export(doc): if doc.doctype != "Sales Invoice": diff --git a/erpnext/selling/doctype/quotation/quotation.json b/erpnext/selling/doctype/quotation/quotation.json index 9c48908e26f..e8b8b5106b9 100644 --- a/erpnext/selling/doctype/quotation/quotation.json +++ b/erpnext/selling/doctype/quotation/quotation.json @@ -78,6 +78,7 @@ "referral_sales_partner", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "bundle_items_section", "packed_items", "pricing_rule_details", @@ -1107,6 +1108,14 @@ "fieldtype": "Data", "is_virtual": 1, "label": "Last Scanned Warehouse" + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "icon": "fa fa-shopping-cart", diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py index 81b0867cba7..e894e36b8ca 100644 --- a/erpnext/selling/doctype/quotation/quotation.py +++ b/erpnext/selling/doctype/quotation/quotation.py @@ -23,6 +23,7 @@ class Quotation(SellingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.sales_taxes_and_charges.sales_taxes_and_charges import ( @@ -72,6 +73,7 @@ class Quotation(SellingController): ignore_pricing_rule: DF.Check in_words: DF.Data | None incoterm: DF.Link | None + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[QuotationItem] language: DF.Link | None letter_head: DF.Link | None diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json index 564c3daf882..c76fcff4679 100644 --- a/erpnext/selling/doctype/sales_order/sales_order.json +++ b/erpnext/selling/doctype/sales_order/sales_order.json @@ -91,6 +91,7 @@ "discount_amount", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "packing_list", "packed_items", "pricing_rule_details", @@ -1696,6 +1697,14 @@ "fieldtype": "Check", "label": "Is Subcontracted", "print_hide": 1 + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index 53e545c1ab5..fbda1f0b41a 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -59,6 +59,7 @@ class SalesOrder(SellingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.payment_schedule.payment_schedule import PaymentSchedule from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.sales_taxes_and_charges.sales_taxes_and_charges import ( @@ -121,6 +122,7 @@ class SalesOrder(SellingController): inter_company_order_reference: DF.Link | None is_internal_customer: DF.Check is_subcontracted: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[SalesOrderItem] language: DF.Link | None letter_head: DF.Link | None @@ -632,9 +634,6 @@ class SalesOrder(SellingController): for item_code, warehouse in item_wh_list: update_bin_qty(item_code, warehouse, {"reserved_qty": get_reserved_qty(item_code, warehouse)}) - def on_update(self): - pass - def on_update_after_submit(self): self.calculate_commission() self.calculate_contribution() diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.json b/erpnext/stock/doctype/delivery_note/delivery_note.json index 32d772f3392..f3b44402dc8 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.json +++ b/erpnext/stock/doctype/delivery_note/delivery_note.json @@ -85,6 +85,7 @@ "discount_amount", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "packing_list", "packed_items", "product_bundle_help", @@ -1418,6 +1419,14 @@ "fieldtype": "Data", "is_virtual": 1, "label": "Last Scanned Warehouse" + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "icon": "fa fa-truck", diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py index 22868d82d6e..453344d500d 100644 --- a/erpnext/stock/doctype/delivery_note/delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/delivery_note.py @@ -30,6 +30,7 @@ class DeliveryNote(SellingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.sales_taxes_and_charges.sales_taxes_and_charges import ( SalesTaxesandCharges, @@ -87,6 +88,7 @@ class DeliveryNote(SellingController): is_internal_customer: DF.Check is_return: DF.Check issue_credit_note: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[DeliveryNoteItem] language: DF.Link | None letter_head: DF.Link | None @@ -853,7 +855,7 @@ def make_sales_invoice(source_name, target_doc=None, args=None): frappe.throw(_("All these items have already been Invoiced/Returned")) if args and args.get("merge_taxes"): - merge_taxes(source.get("taxes") or [], target) + merge_taxes(source, target) target.run_method("calculate_taxes_and_totals") @@ -869,6 +871,7 @@ def make_sales_invoice(source_name, target_doc=None, args=None): def update_item(source_doc, target_doc, source_parent): target_doc.qty = to_make_invoice_qty_map[source_doc.name] + target_doc._old_name = source_doc.name def get_pending_qty(item_row): pending_qty = item_row.qty - invoiced_qty_map.get(item_row.name, 0) diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 8a587d49b0c..fc973ac2449 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -575,25 +575,6 @@ class Item(Document): self.set_last_purchase_rate(new_name) self.recalculate_bin_qty(new_name) - for dt in ("Sales Taxes and Charges", "Purchase Taxes and Charges"): - for d in frappe.db.sql( - f"""select name, item_wise_tax_detail from `tab{dt}` - where ifnull(item_wise_tax_detail, '') != ''""", - as_dict=1, - ): - item_wise_tax_detail = json.loads(d.item_wise_tax_detail) - if isinstance(item_wise_tax_detail, dict) and old_name in item_wise_tax_detail: - item_wise_tax_detail[new_name] = item_wise_tax_detail[old_name] - item_wise_tax_detail.pop(old_name) - - frappe.db.set_value( - dt, - d.name, - "item_wise_tax_detail", - json.dumps(item_wise_tax_detail), - update_modified=False, - ) - def delete_old_bins(self, old_name): frappe.db.delete("Bin", {"item_code": old_name}) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json index 70192b0ba9a..ba761fb54ac 100755 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json @@ -98,6 +98,7 @@ "discount_amount", "sec_tax_breakup", "other_charges_calculation", + "item_wise_tax_details", "pricing_rule_details", "pricing_rules", "raw_material_details", @@ -1293,6 +1294,14 @@ "fieldtype": "Data", "is_virtual": 1, "label": "Last Scanned Warehouse" + }, + { + "fieldname": "item_wise_tax_details", + "fieldtype": "Table", + "hidden": 1, + "label": "Item Wise Tax Details", + "no_copy": 1, + "options": "Item Wise Tax Detail" } ], "grid_page_length": 50, @@ -1371,3 +1380,4 @@ "title_field": "title", "track_changes": 1 } + diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 36b759fedca..535b1f57c63 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -33,6 +33,7 @@ class PurchaseReceipt(BuyingController): if TYPE_CHECKING: from frappe.types import DF + from erpnext.accounts.doctype.item_wise_tax_detail.item_wise_tax_detail import ItemWiseTaxDetail from erpnext.accounts.doctype.pricing_rule_detail.pricing_rule_detail import PricingRuleDetail from erpnext.accounts.doctype.purchase_taxes_and_charges.purchase_taxes_and_charges import ( PurchaseTaxesandCharges, @@ -85,6 +86,7 @@ class PurchaseReceipt(BuyingController): is_old_subcontracting_flow: DF.Check is_return: DF.Check is_subcontracted: DF.Check + item_wise_tax_details: DF.Table[ItemWiseTaxDetail] items: DF.Table[PurchaseReceiptItem] language: DF.Data | None letter_head: DF.Link | None @@ -1359,7 +1361,7 @@ def make_purchase_invoice(source_name, target_doc=None, args=None): doc.run_method("set_missing_values") if args and args.get("merge_taxes"): - merge_taxes(source.get("taxes") or [], doc) + merge_taxes(source, doc) doc.run_method("calculate_taxes_and_totals") doc.set_payment_schedule() @@ -1372,6 +1374,7 @@ def make_purchase_invoice(source_name, target_doc=None, args=None): target_doc.conversion_factor, target_doc.precision("conversion_factor") ) returned_qty_map[source_doc.name] = returned_qty + target_doc._old_name = source_doc.name def get_pending_qty(item_row): qty = item_row.qty