From df0994c0d306654a15f86bd75d0c6caebdee9d41 Mon Sep 17 00:00:00 2001 From: ljain112 Date: Sun, 6 Jul 2025 14:09:05 +0530 Subject: [PATCH] fix: query voucher outstanding only when all the conditions are true. --- erpnext/accounts/utils.py | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 3a395d837f7..007fcf03ffb 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1869,40 +1869,42 @@ def create_payment_ledger_entry( def update_voucher_outstanding(voucher_type, voucher_no, account, party_type, party): + if not (voucher_type in OUTSTANDING_DOCTYPES and party_type and party): + return + ple = frappe.qb.DocType("Payment Ledger Entry") vouchers = [frappe._dict({"voucher_type": voucher_type, "voucher_no": voucher_no})] common_filter = [] + common_filter.append(ple.party_type == party_type) + common_filter.append(ple.party == party) + if account: common_filter.append(ple.account == account) - if party_type: - common_filter.append(ple.party_type == party_type) - - if party: - common_filter.append(ple.party == party) - ple_query = QueryPaymentLedger() # on cancellation outstanding can be an empty list voucher_outstanding = ple_query.get_voucher_outstandings(vouchers, common_filter=common_filter) - if voucher_type in OUTSTANDING_DOCTYPES and party_type and party and voucher_outstanding: - outstanding = voucher_outstanding[0] - ref_doc = frappe.get_lazy_doc(voucher_type, voucher_no) - outstanding_amount = flt( - outstanding["outstanding_in_account_currency"], ref_doc.precision("outstanding_amount") - ) + if not voucher_outstanding: + return - # Didn't use db_set for optimisation purpose - ref_doc.outstanding_amount = outstanding_amount - frappe.db.set_value( - voucher_type, - voucher_no, - "outstanding_amount", - outstanding_amount, - ) + outstanding = voucher_outstanding[0] + ref_doc = frappe.get_lazy_doc(voucher_type, voucher_no) + outstanding_amount = flt( + outstanding["outstanding_in_account_currency"], ref_doc.precision("outstanding_amount") + ) - ref_doc.set_status(update=True) - ref_doc.notify_update() + # Didn't use db_set for optimisation purpose + ref_doc.outstanding_amount = outstanding_amount + frappe.db.set_value( + voucher_type, + voucher_no, + "outstanding_amount", + outstanding_amount, + ) + + ref_doc.set_status(update=True) + ref_doc.notify_update() def delink_original_entry(pl_entry, partial_cancel=False):