Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
For license information, please see license.txt
import frappe
from frappe import _
def execute(filters=None):
columns = get_columns(filters.item)
data = get_data(filters.item, filters.warehouse)
return columns, data
def get_data(item, warehouse):
if not item:
return []
code
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=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
Assistant
To address the issue of getting stock numbers in the wrong row for other items, you need to modify the code in the get_stock_details_map function.
Currently, the stock details map is being populated with the warehouse as the key and the stock details as the value. However, since multiple items can have the same warehouse, the values may be overwritten, leading to incorrect stock numbers in the report.
To fix this, you can modify the code as follows:
python
User
and what's the deference between this and the past
Assistant
In the previous version of the get_stock_details_map function, the code was overwriting the stock details for each item with the warehouse as the key and the stock details as the value. This caused the stock numbers to be displayed in the wrong row for other items.
In the modified version of the function, the stock details map is structured differently to account for multiple items and their respective stock details. Instead of overwriting the values for each warehouse, the stock details for each item are stored in a nested dictionary. The item code (name) is used as the key in the outer dictionary, and the warehouse is used as the key in the inner dictionary. This structure ensures that the stock details for each item are stored correctly and are not overwritten by stock details from other items.
By making this modification, you can retrieve the correct stock numbers for each item based on the selected warehouse in the report.
order_count_map = {}
for row in open_sales_orders:
item_code = row.get("item_code")
if order_count_map.get(item_code) is None:
order_count_map[item_code] = 1
else:
order_count_map[item_code] += 1
return order_count_map
def get_stock_details_map(variant_list, warehouse=None):
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},
)
code
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
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
now this report run without error but after i make edit for get stock by warehouse i get the number in the wrong row for other item
def get_stock_details_map(variant_list, warehouse=None):
stock_details = frappe.db.get_all(
"Bin",
fields=[
"planned_qty",
"actual_qty",
"projected_qty",
"item_code",
"warehouse",
],
filters={"item_code": ["in", variant_list], "warehouse": 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
In this modified code, the stock details are stored under each item's name in a nested dictionary, with the warehouse as the key and the stock details as the value. This ensures that the stock details for each item are stored correctly and do not overwrite each other.
With this modification, the stock numbers should be displayed correctly in the report for each item.