From 5c34a5aaed2c7f10c3d5686bdaefec5e2bff7f7f Mon Sep 17 00:00:00 2001 From: ljain112 Date: Tue, 18 Mar 2025 12:49:29 +0530 Subject: [PATCH] fix: correct invoice order in payment reconcillaiton --- erpnext/accounts/utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 78a38ac7518..91311b118b0 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -9,8 +9,8 @@ import frappe import frappe.defaults from frappe import _, qb, throw from frappe.model.meta import get_field_precision -from frappe.query_builder import AliasedQuery, Criterion, Table -from frappe.query_builder.functions import Count, Round, Sum +from frappe.query_builder import AliasedQuery, Case, Criterion, Table +from frappe.query_builder.functions import Count, Max, Round, Sum from frappe.query_builder.utils import DocType from frappe.utils import ( add_days, @@ -2008,6 +2008,15 @@ class QueryPaymentLedger: .select( ple.against_voucher_no.as_("voucher_no"), Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"), + Max( + Case().when( + ( + (ple.voucher_no == ple.against_voucher_no) + & (ple.voucher_type == ple.against_voucher_type) + ), + (ple.posting_date), + ) + ).as_("invoice_date"), ) .where(ple.delinked == 0) .where(Criterion.all(filter_on_against_voucher_no)) @@ -2015,7 +2024,7 @@ class QueryPaymentLedger: .where(Criterion.all(self.dimensions_filter)) .where(Criterion.all(self.voucher_posting_date)) .groupby(ple.against_voucher_type, ple.against_voucher_no, ple.party_type, ple.party) - .orderby(ple.posting_date, ple.voucher_no) + .orderby(ple.invoice_date, ple.voucher_no) .having(qb.Field("amount_in_account_currency") > 0) .limit(self.limit) .run()