attr_val_map = {}
for row in attribute_list:
name = row.get("parent")
if not attr_val_map.get(name):
attr_val_map[name] = {}
attr_val_map[name][row.get("attribute")] = row.get("attribute_value")
return attr_val_map
i want edit this report to get item by warehouses
To modify the report to get items by warehouses, you can follow these steps:
Add a warehouse filter to the execute function:
python
def execute(filters=None):
columns = get_columns(filters.item)
data = get_data(filters.item, filters.warehouse)
return columns, data
Modify the get_data function to include the warehouse parameter:
python
def get_data(item, warehouse):
if not item:
return []
item_dicts = []
variant_results = frappe.db.get_all(
"Item", fields=["name"], filters={"variant_of": ["=", item], "disabled": 0}
)
if not variant_results:
frappe.msgprint(_("There aren't any item variants for the selected item"))
return []
else:
variant_list = [variant["name"] for variant in variant_results]
order_count_map = get_open_sales_orders_count(variant_list)
stock_details_map = get_stock_details_map(variant_list, warehouse)
buying_price_map = get_buying_price_map(variant_list)
selling_price_map = get_selling_price_map(variant_list)
attr_val_map = get_attribute_values_map(variant_list)
attributes = frappe.db.get_all(
"Item Variant Attribute",
fields=["attribute"],
filters={"parent": ["in", variant_list]},
group_by="attribute",
)
attribute_list = [row.get("attribute") for row in attributes]
# Prepare dicts
variant_dicts = [{"variant_name": d["name"]} for d in variant_results]
for item_dict in variant_dicts:
name = item_dict.get("variant_name")
for attribute in attribute_list:
attr_dict = attr_val_map.get(name)
if attr_dict and attr_dict.get(attribute):
item_dict[frappe.scrub(attribute)] = attr_val_map.get(name).get(attribute)
item_dict["open_orders"] = order_count_map.get(name) or 0
if stock_details_map.get(name):
stock_details = stock_details_map.get(name).get(warehouse, {})
item_dict["current_stock"] = stock_details.get("Inventory") or 0
item_dict["in_production"] = stock_details.get("In Production") or 0
else:
item_dict["current_stock"] = item_dict["in_production"] = 0
item_dict["avg_buying_price_list_rate"] = buying_price_map.get(name) or 0
item_dict["avg_selling_price_list_rate"] = selling_price_map.get(name) or 0
item_dicts.append(item_dict)
return item_dicts
Modify the get_columns function to include the warehouse column:
Modify the get_stock_details_map function to get the stock details for the specific warehouse:
python
def get_stock_details_map(variant_list, warehouse):
stock_details = frappe.db.get_all(
"Bin",
fields=[
"sum(planned_qty) as planned_qty",
"sum(actual_qty) as actual_qty",
"sum(projected_qty) as projected_qty",
"item_code",
"warehouse",
],
filters={"item_code": ["in", variant_list], "warehouse": warehouse},
group_by=["item_code", "warehouse"],
)
stock_details_map = {}
for row in stock_details:
name = row.get("item_code")
warehouse = row.get("warehouse")
if not stock_details_map.get(name):
stock_details_map[name] = {}
stock_details_map[name][warehouse] = {
"Inventory": row.get("actual_qty"),
"In Production": row.get("planned_qty"),
}
return stock_details_map
With these modifications, you can now select a warehouse in the filter and get the items' stock details specific to that warehouse in the report.
query-report/Item Variant Details
Traceback
code
Traceback (most recent call last):
File "apps/frappe/frappe/app.py", line 95, in application
response = frappe.api.handle()
File "apps/frappe/frappe/api.py", line 54, in handle
return frappe.handler.handle()
File "apps/frappe/frappe/handler.py", line 47, in handle
data = execute_cmd(cmd)
File "apps/frappe/frappe/handler.py", line 85, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File "apps/frappe/frappe/__init__.py", line 1622, in call
return fn(*args, **newargs)
File "apps/frappe/frappe/__init__.py", line 802, in wrapper_fn
retval = fn(*args, **get_newargs(fn, kwargs))
File "apps/frappe/frappe/desk/query_report.py", line 232, in run
result = generate_report_result(report, filters, user, custom_columns, is_tree, parent_field)
File "apps/frappe/frappe/__init__.py", line 802, in wrapper_fn
retval = fn(*args, **get_newargs(fn, kwargs))
File "apps/frappe/frappe/desk/query_report.py", line 90, in generate_report_result
res = get_report_result(report, filters) or []
File "apps/frappe/frappe/desk/query_report.py", line 71, in get_report_result
res = report.execute_script_report(filters)
File "apps/frappe/frappe/core/doctype/report/report.py", line 131, in execute_script_report
res = self.execute_module(filters)
File "apps/frappe/frappe/core/doctype/report/report.py", line 148, in execute_module
return frappe.get_attr(method_name)(frappe._dict(filters))
File "apps/erpnext/erpnext/stock/report/item_variant_details/item_variant_details.py", line 11, in execute
data = get_data(filters.item, filters.warehouse)
File "apps/erpnext/erpnext/stock/report/item_variant_details/item_variant_details.py", line 30, in get_data
stock_details_map = get_stock_details_map(variant_list)
TypeError: get_stock_details_map() missing 1 required positional argument: 'warehouse'
Apologies for the oversight. It seems that the get_stock_details_map function is missing the warehouse parameter. To fix this issue, you need to modify the function definition and the function call in the get_data function. Here's how you can do it:
Modify the get_stock_details_map function to include the warehouse parameter:
python
def get_stock_details_map(variant_list, warehouse):
stock_details = frappe.db.get_all(
"Bin",
fields=[
"sum(planned_qty) as planned_qty",
"sum(actual_qty) as actual_qty",
"sum(projected_qty) as projected_qty",
"item_code",
"warehouse",
],
filters={"item_code": ["in", variant_list], "warehouse": warehouse},
group_by=["item_code", "warehouse"],
)
stock_details_map = {}
for row in stock_details:
name = row.get("item_code")
warehouse = row.get("warehouse")
if not stock_details_map.get(name):
stock_details_map[name] = {}
stock_details_map[name][warehouse] = {
"Inventory": row.get("actual_qty"),
"In Production": row.get("planned_qty"),
}
return stock_details_map
Modify the get_data function to include the warehouse parameter in the get_stock_details_map function call:
python
def get_data(item, warehouse):
if not item:
return []
item_dicts = []
variant_results = frappe.db.get_all(
"Item", fields=["name"], filters={"variant_of": ["=", item], "disabled": 0}
)
if not variant_results:
frappe.msgprint(_("There aren't any item variants for the selected item"))
return []
else:
variant_list = [variant["name"] for variant in variant_results]
order_count_map = get_open_sales_orders_count(variant_list)
stock_details_map = get_stock_details_map(variant_list, warehouse) # Include warehouse parameter
buying_price_map = get_buying_price_map(variant_list)
selling_price_map = get_selling_price_map(variant_list)
attr_val_map = get_attribute_values_map(variant_list)
attributes = frappe.db.get_all(
"Item Variant Attribute",
fields=["attribute"],
filters={"parent": ["in", variant_list]},
group_by="attribute",
)
attribute_list = [row.get("attribute") for row in attributes]
# Prepare dicts
variant_dicts = [{"variant_name": d["name"]} for d in variant_results]
for item_dict in variant_dicts:
name = item_dict.get("variant_name")
for attribute in attribute_list:
attr_dict = attr_val_map.get(name)
if attr_dict and attr_dict.get(attribute):
item_dict[frappe.scrub(attribute)] = attr_val_map.get(name).get(attribute)
item_dict["open_orders"] = order_count_map.get(name) or 0
if stock_details_map.get(name):
stock_details = stock_details_map.get(name).get(warehouse, {}) # Include warehouse parameter
item_dict["current_stock"] = stock_details.get("Inventory") or 0
item_dict["in_production"] = stock_details.get("In Production") or 0
else:
item_dict["current_stock"] = item_dict["in_production"] = 0
item_dict["avg_buying_price_list_rate"] = buying_price_map.get(name) or 0
item_dict["avg_selling_price_list_rate"] = selling_price_map.get(name) or 0
item_dicts.append(item_dict)
return item_dicts
With these modifications, the get_stock_details_map function will have a warehouse parameter, and the `get_data