diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 48821c2bb77402b72c8969b2cf73b4426736e147..df9aef69bd05b5ca2ca9a5337a70e3e802d6d861 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -2704,13 +2704,14 @@ def add_income_discount_loss(pe, doc, total_discount_percent) -> float: """Add loss on income discount in base currency.""" precision = doc.precision("total") base_loss_on_income = doc.get("base_total") * (total_discount_percent / 100) + positive_negative = -1 if pe.payment_type == "Pay" else 1 pe.append( "deductions", { "account": frappe.get_cached_value("Company", pe.company, "default_discount_account"), "cost_center": pe.cost_center or frappe.get_cached_value("Company", pe.company, "cost_center"), - "amount": flt(base_loss_on_income, precision), + "amount": flt(base_loss_on_income, precision) * positive_negative, }, ) @@ -2722,6 +2723,7 @@ def add_tax_discount_loss(pe, doc, total_discount_percentage) -> float: tax_discount_loss = {} base_total_tax_loss = 0 precision = doc.precision("tax_amount_after_discount_amount", "taxes") + positive_negative = -1 if pe.payment_type == "Pay" else 1 # The same account head could be used more than once for tax in doc.get("taxes", []): @@ -2744,8 +2746,9 @@ def add_tax_discount_loss(pe, doc, total_discount_percentage) -> float: "deductions", { "account": account, - "cost_center": pe.cost_center or frappe.get_cached_value("Company", pe.company, "cost_center"), - "amount": flt(loss, precision), + "cost_center": pe.cost_center + or frappe.get_cached_value("Company", pe.company, "cost_center"), + "amount": flt(loss, precision) * positive_negative, }, ) diff --git a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py index 15dbaf9e9c451be3721c117795c9fd1c60de3fd1..26a1235b2edbf10482b9f829352ac98d42b59f4c 100644 --- a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py @@ -282,6 +282,48 @@ class TestPaymentEntry(FrappeTestCase): self.assertEqual(si.payment_schedule[0].paid_amount, 200.0) self.assertEqual(si.payment_schedule[1].paid_amount, 36.0) + def test_payment_entry_against_payment_terms_with_discount_on_pi(self): + pi = make_purchase_invoice(do_not_save=1) + create_payment_terms_template_with_discount() + pi.payment_terms_template = "Test Discount Template" + + frappe.db.set_value("Company", pi.company, "default_discount_account", "Write Off - _TC") + + pi.append( + "taxes", + { + "charge_type": "On Net Total", + "account_head": "_Test Account Service Tax - _TC", + "cost_center": "_Test Cost Center - _TC", + "description": "Service Tax", + "rate": 18, + }, + ) + pi.save() + pi.submit() + + frappe.db.set_single_value("Accounts Settings", "book_tax_discount_loss", 1) + pe_with_tax_loss = get_payment_entry("Purchase Invoice", pi.name, bank_account="_Test Cash - _TC") + + self.assertEqual(pe_with_tax_loss.references[0].payment_term, "30 Credit Days with 10% Discount") + self.assertEqual(pe_with_tax_loss.payment_type, "Pay") + self.assertEqual(pe_with_tax_loss.references[0].allocated_amount, 295.0) + self.assertEqual(pe_with_tax_loss.paid_amount, 265.5) + self.assertEqual(pe_with_tax_loss.difference_amount, 0) + self.assertEqual(pe_with_tax_loss.deductions[0].amount, -25.0) # Loss on Income + self.assertEqual(pe_with_tax_loss.deductions[1].amount, -4.5) # Loss on Tax + self.assertEqual(pe_with_tax_loss.deductions[1].account, "_Test Account Service Tax - _TC") + + frappe.db.set_single_value("Accounts Settings", "book_tax_discount_loss", 0) + pe = get_payment_entry("Purchase Invoice", pi.name, bank_account="_Test Cash - _TC") + + self.assertEqual(pe.references[0].payment_term, "30 Credit Days with 10% Discount") + self.assertEqual(pe.payment_type, "Pay") + self.assertEqual(pe.references[0].allocated_amount, 295.0) + self.assertEqual(pe.paid_amount, 265.5) + self.assertEqual(pe.deductions[0].amount, -29.5) + self.assertEqual(pe.difference_amount, 0) + def test_payment_entry_against_payment_terms_with_discount(self): si = create_sales_invoice(do_not_save=1, qty=1, rate=200) create_payment_terms_template_with_discount()