From ade8799358896043fdd77869dd03b0c90f45c536 Mon Sep 17 00:00:00 2001 From: Lakshit Jain <108322669+ljain112@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:31:56 +0530 Subject: [PATCH] fix: check if tds deducted based on Purchase Taxes and Charges (#45161) (cherry picked from commit 37a5767be5aa120dc61d8d9e3965e3ae1866f05c) --- .../tax_withholding_category.py | 16 ++++++ .../test_tax_withholding_category.py | 53 +++++++++++++++++++ 2 files changed, 69 insertions(+) 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 325fefed804..ace139ce15a 100644 --- a/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py +++ b/erpnext/accounts/doctype/tax_withholding_category/tax_withholding_category.py @@ -302,6 +302,10 @@ def get_tax_amount(party_type, parties, inv, tax_details, posting_date, pan_no=N tax_amount = 0 if party_type == "Supplier": + # if tds account is changed. + if not tax_deducted: + tax_deducted = is_tax_deducted_on_the_basis_of_inv(vouchers) + ldc = get_lower_deduction_certificate(inv.company, posting_date, tax_details, pan_no) if tax_deducted: net_total = inv.tax_withholding_net_total @@ -336,6 +340,18 @@ def get_tax_amount(party_type, parties, inv, tax_details, posting_date, pan_no=N return tax_amount, tax_deducted, tax_deducted_on_advances, voucher_wise_amount +def is_tax_deducted_on_the_basis_of_inv(vouchers): + return frappe.db.exists( + "Purchase Taxes and Charges", + { + "parent": ["in", vouchers], + "is_tax_withholding_account": 1, + "parenttype": "Purchase Invoice", + "base_tax_amount_after_discount_amount": [">", 0], + }, + ) + + def get_invoice_vouchers(parties, tax_details, company, party_type="Supplier"): doctype = "Purchase Invoice" if party_type == "Supplier" else "Sales Invoice" field = ( 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 f9f34380d55..f2ebfc60cd7 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 @@ -61,6 +61,49 @@ class TestTaxWithholdingCategory(FrappeTestCase): for d in reversed(invoices): d.cancel() + def test_tds_with_account_changed(self): + frappe.db.set_value( + "Supplier", "Test TDS Supplier", "tax_withholding_category", "Multi Account TDS Category" + ) + invoices = [] + + # create invoices for lower than single threshold tax rate + for _ in range(2): + pi = create_purchase_invoice(supplier="Test TDS Supplier") + pi.submit() + invoices.append(pi) + + # create another invoice whose total when added to previously created invoice, + # surpasses cumulative threshhold + pi = create_purchase_invoice(supplier="Test TDS Supplier") + pi.submit() + + # assert equal tax deduction on total invoice amount until now + self.assertEqual(pi.taxes_and_charges_deducted, 3000) + self.assertEqual(pi.grand_total, 7000) + invoices.append(pi) + + # account changed + + frappe.db.set_value( + "Tax Withholding Account", + {"parent": "Multi Account TDS Category"}, + "account", + "_Test Account VAT - _TC", + ) + + # TDS should be on invoice only even though account is changed + pi = create_purchase_invoice(supplier="Test TDS Supplier", rate=5000) + pi.submit() + + # assert equal tax deduction on total invoice amount until now + self.assertEqual(pi.taxes_and_charges_deducted, 500) + invoices.append(pi) + + # delete invoices to avoid clashing + for d in reversed(invoices): + d.cancel() + def test_single_threshold_tds(self): invoices = [] frappe.db.set_value( @@ -1061,6 +1104,16 @@ def create_tax_withholding_category_records(): consider_party_ledger_amount=1, ) + create_tax_withholding_category( + category_name="Multi Account TDS Category", + rate=10, + from_date=from_date, + to_date=to_date, + account="TDS - _TC", + single_threshold=0, + cumulative_threshold=30000, + ) + def create_tax_withholding_category( category_name,