From f9732efb2329c359d54acb4f4c43acddb77778c5 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Fri, 19 Jun 2026 22:11:38 +0530 Subject: [PATCH] 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) --- .../doctype/stock_settings/stock_settings.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/doctype/stock_settings/stock_settings.py b/erpnext/stock/doctype/stock_settings/stock_settings.py index 03bf5b0cca7..b1676d8df79 100644 --- a/erpnext/stock/doctype/stock_settings/stock_settings.py +++ b/erpnext/stock/doctype/stock_settings/stock_settings.py @@ -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: