mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-25 07:54:46 +00:00
refactor: rewrite Stock Ledger Report queries in QB
This commit is contained in:
@@ -318,20 +318,25 @@ def get_inventory_dimension_fields():
|
|||||||
|
|
||||||
|
|
||||||
def get_items(filters):
|
def get_items(filters):
|
||||||
|
item = frappe.qb.DocType("Item")
|
||||||
|
query = frappe.qb.from_(item).select(item.name)
|
||||||
conditions = []
|
conditions = []
|
||||||
if filters.get("item_code"):
|
|
||||||
conditions.append("item.name=%(item_code)s")
|
if item_code := filters.get("item_code"):
|
||||||
|
conditions.append(item.name == item_code)
|
||||||
else:
|
else:
|
||||||
if filters.get("brand"):
|
if brand := filters.get("brand"):
|
||||||
conditions.append("item.brand=%(brand)s")
|
conditions.append(item.brand == brand)
|
||||||
if filters.get("item_group"):
|
if item_group := filters.get("item_group"):
|
||||||
conditions.append(get_item_group_condition(filters.get("item_group")))
|
if condition := get_item_group_condition(item_group, item):
|
||||||
|
conditions.append(condition)
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
if conditions:
|
if conditions:
|
||||||
items = frappe.db.sql_list(
|
for condition in conditions:
|
||||||
"""select name from `tabItem` item where {}""".format(" and ".join(conditions)), filters
|
query = query.where(condition)
|
||||||
)
|
items = [r[0] for r in query.run()]
|
||||||
|
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
@@ -343,29 +348,22 @@ def get_item_details(items, sl_entries, include_uom):
|
|||||||
if not items:
|
if not items:
|
||||||
return item_details
|
return item_details
|
||||||
|
|
||||||
cf_field = cf_join = ""
|
item = frappe.qb.DocType("Item")
|
||||||
|
query = (
|
||||||
|
frappe.qb.from_(item)
|
||||||
|
.select(item.name, item.item_name, item.description, item.item_group, item.brand, item.stock_uom)
|
||||||
|
.where(item.name.isin(items))
|
||||||
|
)
|
||||||
|
|
||||||
if include_uom:
|
if include_uom:
|
||||||
cf_field = ", ucd.conversion_factor"
|
ucd = frappe.qb.DocType("UOM Conversion Detail")
|
||||||
cf_join = (
|
query = (
|
||||||
"left join `tabUOM Conversion Detail` ucd on ucd.parent=item.name and ucd.uom=%s"
|
query.left_join(ucd)
|
||||||
% frappe.db.escape(include_uom)
|
.on((ucd.parent == item.name) & (ucd.uom == include_uom))
|
||||||
|
.select(ucd.conversion_factor)
|
||||||
)
|
)
|
||||||
|
|
||||||
res = frappe.db.sql(
|
res = query.run(as_dict=True)
|
||||||
"""
|
|
||||||
select
|
|
||||||
item.name, item.item_name, item.description, item.item_group, item.brand, item.stock_uom {cf_field}
|
|
||||||
from
|
|
||||||
`tabItem` item
|
|
||||||
{cf_join}
|
|
||||||
where
|
|
||||||
item.name in ({item_codes})
|
|
||||||
""".format(
|
|
||||||
cf_field=cf_field, cf_join=cf_join, item_codes=",".join(["%s"] * len(items))
|
|
||||||
),
|
|
||||||
items,
|
|
||||||
as_dict=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
for item in res:
|
for item in res:
|
||||||
item_details.setdefault(item.name, item)
|
item_details.setdefault(item.name, item)
|
||||||
@@ -440,16 +438,28 @@ def get_warehouse_condition(warehouse):
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_item_group_condition(item_group):
|
def get_item_group_condition(item_group, item_table=None):
|
||||||
item_group_details = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"], as_dict=1)
|
item_group_details = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"], as_dict=1)
|
||||||
if item_group_details:
|
if item_group_details:
|
||||||
return (
|
if item_table:
|
||||||
"item.item_group in (select ig.name from `tabItem Group` ig \
|
ig = frappe.qb.DocType("Item Group")
|
||||||
where ig.lft >= %s and ig.rgt <= %s and item.item_group = ig.name)"
|
return item_table.item_group.isin(
|
||||||
% (item_group_details.lft, item_group_details.rgt)
|
(
|
||||||
)
|
frappe.qb.from_(ig)
|
||||||
|
.select(ig.name)
|
||||||
return ""
|
.where(
|
||||||
|
(ig.lft >= item_group_details.lft)
|
||||||
|
& (ig.rgt <= item_group_details.rgt)
|
||||||
|
& (item_table.item_group == ig.name)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
"item.item_group in (select ig.name from `tabItem Group` ig \
|
||||||
|
where ig.lft >= %s and ig.rgt <= %s and item.item_group = ig.name)"
|
||||||
|
% (item_group_details.lft, item_group_details.rgt)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def check_inventory_dimension_filters_applied(filters) -> bool:
|
def check_inventory_dimension_filters_applied(filters) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user