mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-14 01:20:41 +00:00
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:
@@ -383,7 +383,7 @@ class PurchaseReceipt(BuyingController):
|
|||||||
self.update_received_qty_if_from_pp()
|
self.update_received_qty_if_from_pp()
|
||||||
|
|
||||||
def update_received_qty_if_from_pp(self):
|
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]
|
items_from_po = [item.purchase_order_item for item in self.items if item.purchase_order_item]
|
||||||
if items_from_po:
|
if items_from_po:
|
||||||
@@ -404,7 +404,9 @@ class PurchaseReceipt(BuyingController):
|
|||||||
frappe.qb.from_(table)
|
frappe.qb.from_(table)
|
||||||
.select(
|
.select(
|
||||||
table.production_plan_sub_assembly_item,
|
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))
|
.where(table.production_plan_sub_assembly_item.isin(result))
|
||||||
.groupby(table.production_plan_sub_assembly_item)
|
.groupby(table.production_plan_sub_assembly_item)
|
||||||
|
|||||||
Reference in New Issue
Block a user