refactor(stock): build item-group filter via qb in item_dashboard

Replace the raw EXISTS subquery (items within an item-group subtree) with
frappe.qb (item_group.isin(subquery)). Same result on MariaDB; valid under
Postgres' stricter SQL.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-19 22:10:54 +05:30
parent 884f57d5f6
commit 497a0abb07

View File

@@ -24,13 +24,19 @@ def get_data(
filters.append(["warehouse", "=", warehouse])
if item_group:
lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"])
items = frappe.db.sql_list(
"""
select i.name from `tabItem` i
where exists(select name from `tabItem Group`
where name=i.item_group and lft >=%s and rgt<=%s)
""",
(lft, rgt),
item = frappe.qb.DocType("Item")
item_group_dt = frappe.qb.DocType("Item Group")
items = (
frappe.qb.from_(item)
.select(item.name)
.where(
item.item_group.isin(
frappe.qb.from_(item_group_dt)
.select(item_group_dt.name)
.where((item_group_dt.lft >= lft) & (item_group_dt.rgt <= rgt))
)
)
.run(pluck="name")
)
filters.append(["item_code", "in", items])
try: