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()
)
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)