fix: enhance sorting and optimize GL entry retrieval

This commit is contained in:
ljain112
2026-03-09 18:50:42 +05:30
parent e5eb5406da
commit 93ebec90ef

View File

@@ -5,6 +5,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.utils import flt, getdate from frappe.utils import flt, getdate
from pypika import Tuple
from erpnext.accounts.utils import get_currency_precision from erpnext.accounts.utils import get_currency_precision
@@ -141,7 +142,7 @@ def get_result(filters, tds_accounts, tax_category_map, net_total_map):
else: else:
entries[key] = row entries[key] = row
out = list(entries.values()) out = list(entries.values())
out.sort(key=lambda x: (x["section_code"], x["transaction_date"])) out.sort(key=lambda x: (x["section_code"], x["transaction_date"], x["ref_no"]))
return out return out
@@ -174,14 +175,9 @@ def get_gle_map(net_total_map):
if not net_total_map: if not net_total_map:
return {} return {}
voucher_types = set()
voucher_nos = set()
for doctype, voucher_no in net_total_map.keys():
voucher_types.add(doctype)
voucher_nos.add(voucher_no)
gle = frappe.qb.DocType("GL Entry") gle = frappe.qb.DocType("GL Entry")
voucher_pairs = list(net_total_map.keys())
rows = ( rows = (
frappe.qb.from_(gle) frappe.qb.from_(gle)
.select( .select(
@@ -196,8 +192,7 @@ def get_gle_map(net_total_map):
gle.party_type, gle.party_type,
) )
.where(gle.is_cancelled == 0) .where(gle.is_cancelled == 0)
.where(gle.voucher_type.isin(voucher_types)) .where(Tuple(gle.voucher_type, gle.voucher_no).isin(voucher_pairs))
.where(gle.voucher_no.isin(voucher_nos))
).run(as_dict=True) ).run(as_dict=True)
gle_map = {} gle_map = {}
@@ -492,8 +487,11 @@ def get_doc_info(vouchers, doctype, tax_category_map, net_total_map=None, filter
value.grand_total = entry.paid_amount_after_tax value.grand_total = entry.paid_amount_after_tax
value.base_total = entry.base_paid_amount value.base_total = entry.base_paid_amount
else: else:
party_list = journal_entry_party_map.get(entry.name) party_list = journal_entry_party_map.get(entry.name, [])
value.party = party_list[0] if party_list else None if party and party in party_list:
value.party = party
elif party_list:
value.party = sorted(party_list)[0]
value.party_type = party_type value.party_type = party_type
value.base_tax_withholding_net_total = entry.total_debit value.base_tax_withholding_net_total = entry.total_debit
value.grand_total = entry.total_debit value.grand_total = entry.total_debit