refactor: replace SQL query with Query Builder in fetch_items_with_pending_qty method

This commit is contained in:
ljain112
2025-10-09 17:51:51 +05:30
parent f00a63b69d
commit f1f61ff61b

View File

@@ -358,18 +358,25 @@ class StatusUpdater(Document):
self.check_overflow_with_allowance(item, args) self.check_overflow_with_allowance(item, args)
def fetch_items_with_pending_qty(self, args, item_field, items): def fetch_items_with_pending_qty(self, args, item_field, items):
return frappe.db.sql( doctype = frappe.qb.DocType(args["target_dt"])
"""select name,`{item_code}` as item_code, `{target_ref_field}`, item_field = doctype[item_field]
`{target_field}`, parenttype, parent from `tab{target_dt}` target_ref_field = doctype[args["target_ref_field"]]
where `{target_ref_field}` < `{target_field}` target_field = doctype[args["target_field"]]
and name in %(names)s and docstatus=1""".format(
item_code=item_field, return (
target_ref_field=args["target_ref_field"], frappe.qb.from_(doctype)
target_field=args["target_field"], .select(
target_dt=args["target_dt"], doctype.name,
), item_field.as_("item_code"),
{"names": items}, target_ref_field,
as_dict=1, 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): def check_overflow_with_allowance(self, item, args):