refactor(stock): convert stock settings checks to ORM/qb

Replace the get_all(limit=1, pluck) existence checks with frappe.db.exists and
the no-own-valuation-method Stock Ledger Entry EXISTS with a frappe.qb
subquery (null-or-empty valuation_method preserved). Same result on MariaDB;
valid under Postgres.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-19 22:11:38 +05:30
parent 3e801a2067
commit f9732efb23

View File

@@ -120,7 +120,7 @@ class StockSettings(Document):
if not doc_before_save:
return
if not frappe.get_all("Serial and Batch Bundle", filters={"docstatus": 1}, limit=1, pluck="name"):
if not frappe.db.exists("Serial and Batch Bundle", {"docstatus": 1}):
return
if doc_before_save.do_not_use_batchwise_valuation and not self.do_not_use_batchwise_valuation:
@@ -142,7 +142,7 @@ class StockSettings(Document):
doc_before_save.enable_serial_and_batch_no_for_item
and not self.enable_serial_and_batch_no_for_item
):
if frappe.get_all("Serial and Batch Bundle", filters={"docstatus": 1}, limit=1, pluck="name"):
if frappe.db.exists("Serial and Batch Bundle", {"docstatus": 1}):
frappe.throw(
_(
"Cannot disable Serial and Batch No for Item, as there are existing records for serial / batch."
@@ -170,11 +170,20 @@ class StockSettings(Document):
if previous_valuation_method and previous_valuation_method != self.valuation_method:
# check if there are any stock ledger entries against items
# which does not have it's own valuation method
sle = frappe.db.sql(
"""select name from `tabStock Ledger Entry` sle
where exists(select name from tabItem
where name=sle.item_code and (valuation_method is null or valuation_method='')) limit 1
"""
sle_dt = frappe.qb.DocType("Stock Ledger Entry")
item = frappe.qb.DocType("Item")
sle = (
frappe.qb.from_(sle_dt)
.select(sle_dt.name)
.where(
sle_dt.item_code.isin(
frappe.qb.from_(item)
.select(item.name)
.where(item.valuation_method.isnull() | (item.valuation_method == ""))
)
)
.limit(1)
.run()
)
if sle: