From e52b9825e398fddef74037bd5ac7c14114aa3836 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Mon, 29 Jun 2026 16:29:45 +0530 Subject: [PATCH] fix(accounts): break last-purchase-rate ties deterministically in Gross Profit get_last_purchase_rate ordered by posting_date DESC only; same-date Purchase Invoices yielded an undefined last_purchase_rate that diverged between MariaDB and Postgres. Add purchase_invoice.name DESC tiebreaker. --- erpnext/accounts/report/gross_profit/gross_profit.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 8785144a8da..61968d603ad 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -895,7 +895,11 @@ class GrossProfitGenerator: if row.cost_center: query = query.where(purchase_invoice_item.cost_center == row.cost_center) - query = query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc).limit(1) + query = ( + query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc) + .orderby(purchase_invoice.name, order=frappe.qb.desc) + .limit(1) + ) last_purchase_rate = query.run() return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0