fix(stock): guard production-plan received-qty division against a zero divisor (Postgres)

update_received_qty_if_from_pp divides received_qty by (qty / fg_item_qty) over
Purchase Order Items. Both qty and fg_item_qty are Float with no non-zero
constraint, so a zero qty (or fg_item_qty) drives the divisor to 0.

MariaDB returns NULL for x/0 (dropped by the surrounding Sum); PostgreSQL
raises `division by zero` and aborts the Purchase Receipt submit/cancel.
Wrapping both divisors in NullIf(..., 0) makes the zero row contribute NULL on
both engines, leaving MariaDB output unchanged.
This commit is contained in:
Mihir Kandoi
2026-06-23 18:41:44 +05:30
parent 334f1cc6f0
commit 727f8d0967

View File

@@ -383,7 +383,7 @@ class PurchaseReceipt(BuyingController):
self.update_received_qty_if_from_pp()
def update_received_qty_if_from_pp(self):
from frappe.query_builder.functions import Coalesce, Sum
from frappe.query_builder.functions import Coalesce, NullIf, Sum
items_from_po = [item.purchase_order_item for item in self.items if item.purchase_order_item]
if items_from_po:
@@ -404,7 +404,9 @@ class PurchaseReceipt(BuyingController):
frappe.qb.from_(table)
.select(
table.production_plan_sub_assembly_item,
Sum(table.received_qty / (table.qty / table.fg_item_qty)).as_("received_qty"),
Sum(table.received_qty / NullIf(table.qty / NullIf(table.fg_item_qty, 0), 0)).as_(
"received_qty"
),
)
.where(table.production_plan_sub_assembly_item.isin(result))
.groupby(table.production_plan_sub_assembly_item)