From 899c18df180cea20a0fb88e730ba047b1d256ddd Mon Sep 17 00:00:00 2001 From: Sugesh393 Date: Wed, 5 Feb 2025 11:00:55 +0530 Subject: [PATCH 1/2] fix: validate payment request total of partly paid invoice --- erpnext/accounts/doctype/payment_request/payment_request.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 51fb99657ca..b8db4273afe 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -780,7 +780,10 @@ def get_existing_paid_amount(doctype, name): frappe.qb.from_(PL) .left_join(PER) .on( - (PER.reference_doctype == PL.against_voucher_type) & (PER.reference_name == PL.against_voucher_no) + (PL.against_voucher_type == PER.reference_doctype) + & (PL.against_voucher_no == PER.reference_name) + & (PL.voucher_type == PER.parenttype) + & (PL.voucher_no == PER.parent) ) .select(Abs(Sum(PL.amount)).as_("total_paid_amount")) .where(PL.against_voucher_type.eq(doctype)) From f8472c32d9f5a051ac676f512eef43defeb81988 Mon Sep 17 00:00:00 2001 From: Sugesh393 Date: Thu, 6 Feb 2025 11:59:29 +0530 Subject: [PATCH 2/2] test: add unit test to validate payment request grand_total for partly paid invoice --- .../payment_request/test_payment_request.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/erpnext/accounts/doctype/payment_request/test_payment_request.py b/erpnext/accounts/doctype/payment_request/test_payment_request.py index 70c38ede6af..d3745b2b4b9 100644 --- a/erpnext/accounts/doctype/payment_request/test_payment_request.py +++ b/erpnext/accounts/doctype/payment_request/test_payment_request.py @@ -709,6 +709,45 @@ class TestPaymentRequest(IntegrationTestCase): self.assertEqual(pr.grand_total, si.outstanding_amount) + def test_partial_paid_invoice_with_more_payment_entry(self): + pi = make_purchase_invoice(currency="INR", qty=1, rate=500) + pi.submit() + pi_1 = make_purchase_invoice(currency="INR", qty=1, rate=300) + pi_1.submit() + + pr = make_payment_request(dt="Purchase Invoice", dn=pi.name, mute_email=1, submit_doc=0, return_doc=1) + pr.grand_total = 200 + pr.submit() + pr.create_payment_entry() + pr_1 = make_payment_request( + dt="Purchase Invoice", dn=pi.name, mute_email=1, submit_doc=0, return_doc=1 + ) + pr_1.grand_total = 200 + pr_1.submit() + pr_1.create_payment_entry() + + pe = get_payment_entry(dt="Purchase Invoice", dn=pi.name) + pe.paid_amount = 200 + pe.references[0].reference_doctype = pi.doctype + pe.references[0].reference_name = pi.name + pe.references[0].grand_total = pi.grand_total + pe.references[0].outstanding_amount = pi.outstanding_amount + pe.references[0].allocated_amount = 100 + pe.append( + "references", + { + "reference_doctype": pi_1.doctype, + "reference_name": pi_1.name, + "grand_total": pi_1.grand_total, + "outstanding_amount": pi_1.outstanding_amount, + "allocated_amount": 100, + }, + ) + + pr_2 = make_payment_request(dt="Purchase Invoice", dn=pi.name, mute_email=1) + pi.load_from_db() + self.assertEqual(pr_2.grand_total, pi.outstanding_amount) + def test_partial_paid_invoice_with_submitted_payment_entry(self): pi = make_purchase_invoice(currency="INR", qty=1, rate=5000)