import frappe from frappe import _ def execute(filters=None): columns =…
- User
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 []
codeitem_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
The code is an implementation of a report in Frappe that generates a list of item variants along with their attributes, stock details, open sales orders, and average buying and selling prices.
The
executefunction is the entry point of the report. It takes filters as input and returns the columns and data for the report.The
get_datafunction retrieves the required data for each item variant, including its attributes, stock details, open sales orders, and average buying and selling prices. It uses several helper functions to retrieve specific data, such asget_open_sales_orders_count,get_stock_details_map,get_buying_price_map, andget_selling_price_map.The
get_columnsfunction defines the columns for the report. It includes the variant name and attributes, as well as additional columns for average buying price, average selling price, current stock, in production quantity, and open sales orders. - User
- Assistant
The error occurred because the
filtersparameter passed to theget_datafunction is not in the correct format. It should be a dictionary, but it is currently a string representation of a dictionary.To fix this issue, you need to convert the
filtersparameter from a string to a dictionary. You can use thejson.loads()function to accomplish this.Here's the modified code:
pythonimport json def execute(filters=None): filters = json.loads(filters) if filters else {} columns = get_columns(filters.get("item")) data = get_data(filters.get("item"), filters.get("warehouse")) return columns, dataWith this change, the
filtersparameter will be converted to a dictionary before passing it to theget_columnsandget_datafunctions. This should resolve the error you encountered.