From 5fb16ca20cc1acbfdde031d07f0d97aeb976e83f Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Thu, 25 Jun 2026 14:39:07 +0530 Subject: [PATCH] fix(postgres): get_item_price picks undated price on Postgres Both Item Price lookups order by valid_from desc and take the first valid row. MariaDB sorts NULL valid_from last; Postgres first, so the undated base price was winning over a dated one. Order by (valid_from IS NULL) asc, valid_from desc (the get_all is converted to qb since its order_by won't take such an expression). MariaDB output is unchanged. --- erpnext/stock/get_item_details.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 11990db26b8..4ec75996608 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -1121,17 +1121,20 @@ def insert_item_price(ctx: ItemDetailsCtx): or getdate() ) - item_prices = frappe.get_all( - "Item Price", - filters={ - "item_code": ctx.item_code, - "price_list": ctx.price_list, - "currency": ctx.currency, - "uom": ctx.stock_uom, - }, - fields=["name", "price_list_rate", "valid_from", "valid_upto"], - order_by="valid_from desc, creation desc", - ) + ip = frappe.qb.DocType("Item Price") + item_prices = ( + frappe.qb.from_(ip) + .select(ip.name, ip.price_list_rate, ip.valid_from, ip.valid_upto) + .where( + (ip.item_code == ctx.item_code) + & (ip.price_list == ctx.price_list) + & (ip.currency == ctx.currency) + & (ip.uom == ctx.stock_uom) + ) + .orderby(ip.valid_from.isnull(), order=frappe.qb.asc) + .orderby(ip.valid_from, order=frappe.qb.desc) + .orderby(ip.creation, order=frappe.qb.desc) + ).run(as_dict=True) item_price = next( ( row @@ -1233,6 +1236,7 @@ def get_item_price( & (ip.price_list == pctx.price_list) & (IfNull(ip.uom, "").isin(["", pctx.uom])) ) + .orderby(ip.valid_from.isnull(), order=frappe.qb.asc) .orderby(ip.valid_from, order=frappe.qb.desc) .orderby(IfNull(ip.batch_no, ""), order=frappe.qb.desc) .orderby(ip.uom, order=frappe.qb.desc)