From 727f8d0967d6d8fe170ed282a38bdbff257fcf86 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Tue, 23 Jun 2026 18:41:44 +0530 Subject: [PATCH] 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. --- erpnext/stock/doctype/purchase_receipt/purchase_receipt.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index a99c536dd5b..fbb9a38150c 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -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)