From f1f61ff61b907de50e7ae909c3f50b6e9044c002 Mon Sep 17 00:00:00 2001 From: ljain112 Date: Thu, 9 Oct 2025 17:51:51 +0530 Subject: [PATCH] refactor: replace SQL query with Query Builder in fetch_items_with_pending_qty method --- erpnext/controllers/status_updater.py | 31 ++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py index c366a4a7cf2..538887977d3 100644 --- a/erpnext/controllers/status_updater.py +++ b/erpnext/controllers/status_updater.py @@ -358,18 +358,25 @@ class StatusUpdater(Document): self.check_overflow_with_allowance(item, args) def fetch_items_with_pending_qty(self, args, item_field, items): - return frappe.db.sql( - """select name,`{item_code}` as item_code, `{target_ref_field}`, - `{target_field}`, parenttype, parent from `tab{target_dt}` - where `{target_ref_field}` < `{target_field}` - and name in %(names)s and docstatus=1""".format( - item_code=item_field, - target_ref_field=args["target_ref_field"], - target_field=args["target_field"], - target_dt=args["target_dt"], - ), - {"names": items}, - as_dict=1, + doctype = frappe.qb.DocType(args["target_dt"]) + item_field = doctype[item_field] + target_ref_field = doctype[args["target_ref_field"]] + target_field = doctype[args["target_field"]] + + return ( + frappe.qb.from_(doctype) + .select( + doctype.name, + item_field.as_("item_code"), + target_ref_field, + target_field, + doctype.parenttype, + doctype.parent, + ) + .where(target_ref_field < target_field) + .where(doctype.name.isin(items)) + .where(doctype.docstatus == 1) + .run(as_dict=True) ) def check_overflow_with_allowance(self, item, args):