From b6e16c18916db5f4c9087e8744d806913073439f Mon Sep 17 00:00:00 2001 From: venkat102 Date: Mon, 28 Oct 2024 12:42:58 +0530 Subject: [PATCH 1/2] fix: calculate tds with net amount when invoice exceeds single threshold amount (cherry picked from commit ef694a40a1945fb4bd1f56a962a9245a6e76706b) --- .../tax_withholding_category.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py index d1bb24617956..187e5026e240 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py @@ -482,7 +482,7 @@ def get_tds_amount(ldc, parties, inv, tax_details, vouchers): payment_entry_filters.pop("apply_tax_withholding_amount", None) payment_entry_filters.pop("tax_withholding_category", None) - supp_credit_amt = frappe.db.get_value("Purchase Invoice", invoice_filters, field) or 0.0 + supp_inv_credit_amt = frappe.db.get_value("Purchase Invoice", invoice_filters, field) or 0.0 supp_jv_credit_amt = ( frappe.db.get_value( @@ -506,7 +506,7 @@ def get_tds_amount(ldc, parties, inv, tax_details, vouchers): group_by="payment_type", ) - supp_credit_amt += supp_jv_credit_amt + supp_credit_amt = supp_jv_credit_amt supp_credit_amt += inv.tax_withholding_net_total for type in payment_entry_amounts: @@ -524,18 +524,18 @@ def get_tds_amount(ldc, parties, inv, tax_details, vouchers): tax_withholding_net_total = inv.tax_withholding_net_total if (threshold and tax_withholding_net_total >= threshold) or ( - cumulative_threshold and supp_credit_amt >= cumulative_threshold + cumulative_threshold and (supp_credit_amt + supp_inv_credit_amt) >= cumulative_threshold ): + # Get net total again as TDS is calculated on net total + # Grand is used to just check for threshold breach + net_total = ( + frappe.db.get_value("Purchase Invoice", invoice_filters, "sum(tax_withholding_net_total)") or 0.0 + ) + supp_credit_amt += net_total + if (cumulative_threshold and supp_credit_amt >= cumulative_threshold) and cint( tax_details.tax_on_excess_amount ): - # Get net total again as TDS is calculated on net total - # Grand is used to just check for threshold breach - net_total = ( - frappe.db.get_value("Purchase Invoice", invoice_filters, "sum(tax_withholding_net_total)") - or 0.0 - ) - net_total += inv.tax_withholding_net_total supp_credit_amt = net_total - cumulative_threshold if ldc and is_valid_certificate(ldc, inv.get("posting_date") or inv.get("transaction_date"), 0): From 577be19f98223549bdd3bae4146003dc8a577ca0 Mon Sep 17 00:00:00 2001 From: venkat102 Date: Mon, 28 Oct 2024 12:43:55 +0530 Subject: [PATCH 2/2] test: add unit test to validate purchase invoice exceeding single threshold value (cherry picked from commit 94badb464db4e598fd6fec72d3ee4d914d02d87d) --- .../test_tax_withholding_category.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py index b4550d2bb532..83b53aa7ae6f 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/test_tax_withholding_category.py @@ -121,6 +121,46 @@ def test_tax_withholding_category_checks(self): for d in reversed(invoices): d.cancel() + def test_cumulative_threshold_with_party_ledger_amount_on_net_total(self): + invoices = [] + frappe.db.set_value( + "Supplier", "Test TDS Supplier3", "tax_withholding_category", "Advance TDS Category" + ) + + # Invoice with tax and without exceeding single and cumulative thresholds + for _ in range(2): + pi = create_purchase_invoice(supplier="Test TDS Supplier3", rate=1000, do_not_save=True) + pi.apply_tds = 1 + pi.append( + "taxes", + { + "category": "Total", + "charge_type": "Actual", + "account_head": "_Test Account VAT - _TC", + "cost_center": "Main - _TC", + "tax_amount": 500, + "description": "Test", + "add_deduct_tax": "Add", + }, + ) + pi.save() + pi.submit() + invoices.append(pi) + + # Third Invoice exceeds single threshold and not exceeding cumulative threshold + pi1 = create_purchase_invoice(supplier="Test TDS Supplier3", rate=6000) + pi1.apply_tds = 1 + pi1.save() + pi1.submit() + invoices.append(pi1) + + # Cumulative threshold is 10,000 + # Threshold calculation should be only on the third invoice + self.assertEqual(pi1.taxes[0].tax_amount, 800) + + for d in reversed(invoices): + d.cancel() + def test_cumulative_threshold_tcs(self): frappe.db.set_value( "Customer", "Test TCS Customer", "tax_withholding_category", "Cumulative Threshold TCS"