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.
This commit is contained in:
Mihir Kandoi
2026-06-25 14:39:07 +05:30
parent b10cf2fb65
commit 5fb16ca20c

View File

@@ -1121,17 +1121,20 @@ def insert_item_price(ctx: ItemDetailsCtx):
or getdate() or getdate()
) )
item_prices = frappe.get_all( ip = frappe.qb.DocType("Item Price")
"Item Price", item_prices = (
filters={ frappe.qb.from_(ip)
"item_code": ctx.item_code, .select(ip.name, ip.price_list_rate, ip.valid_from, ip.valid_upto)
"price_list": ctx.price_list, .where(
"currency": ctx.currency, (ip.item_code == ctx.item_code)
"uom": ctx.stock_uom, & (ip.price_list == ctx.price_list)
}, & (ip.currency == ctx.currency)
fields=["name", "price_list_rate", "valid_from", "valid_upto"], & (ip.uom == ctx.stock_uom)
order_by="valid_from desc, creation desc", )
) .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( item_price = next(
( (
row row
@@ -1233,6 +1236,7 @@ def get_item_price(
& (ip.price_list == pctx.price_list) & (ip.price_list == pctx.price_list)
& (IfNull(ip.uom, "").isin(["", pctx.uom])) & (IfNull(ip.uom, "").isin(["", pctx.uom]))
) )
.orderby(ip.valid_from.isnull(), order=frappe.qb.asc)
.orderby(ip.valid_from, order=frappe.qb.desc) .orderby(ip.valid_from, order=frappe.qb.desc)
.orderby(IfNull(ip.batch_no, ""), order=frappe.qb.desc) .orderby(IfNull(ip.batch_no, ""), order=frappe.qb.desc)
.orderby(ip.uom, order=frappe.qb.desc) .orderby(ip.uom, order=frappe.qb.desc)