fix(stock): guard traceability qty division against a zero divisor (Postgres)

get_materials divides stock_entry_detail.qty by a CASE that returns
fg_completed_qty when it is > 0 and otherwise the injected sabb_data.qty. The
code explicitly anticipates fg_completed_qty <= 0 (the else branch), and
neither fg_completed_qty nor sabb_data.qty is constrained non-zero, so the
divisor can be 0. MariaDB returns NULL for x/0; PostgreSQL raises
`division by zero` and aborts the report. Wrapping the CASE in NullIf(..., 0)
makes the divisor NULL instead of 0 — unchanged on MariaDB, valid on Postgres.
This commit is contained in:
Mihir Kandoi
2026-06-23 19:38:27 +05:30
parent 20e6a6e149
commit 3859919263

View File

@@ -4,6 +4,7 @@
import frappe
from frappe import _
from frappe.query_builder import Case
from frappe.query_builder.functions import NullIf
def execute(filters: dict | None = None):
@@ -300,9 +301,12 @@ class ReportData:
(
(
stock_entry_detail.qty
/ Case()
.when(stock_entry.fg_completed_qty > 0, stock_entry.fg_completed_qty)
.else_(sabb_data.qty)
/ NullIf(
Case()
.when(stock_entry.fg_completed_qty > 0, stock_entry.fg_completed_qty)
.else_(sabb_data.qty),
0,
)
)
* sabb_data.qty
).as_("qty"),