From 8ac11ae88df920d28c897c3919b3ab8d37d1b9a9 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 16 May 2024 14:27:52 +0530 Subject: [PATCH] fix: Show achieved amount and variance for parent item groups --- .../item_group_wise_sales_target_variance.py | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/erpnext/selling/report/sales_partner_target_variance_based_on_item_group/item_group_wise_sales_target_variance.py b/erpnext/selling/report/sales_partner_target_variance_based_on_item_group/item_group_wise_sales_target_variance.py index 5046ec52c95..b837d67e1c0 100644 --- a/erpnext/selling/report/sales_partner_target_variance_based_on_item_group/item_group_wise_sales_target_variance.py +++ b/erpnext/selling/report/sales_partner_target_variance_based_on_item_group/item_group_wise_sales_target_variance.py @@ -164,9 +164,10 @@ def prepare_data( rows = {} target_qty_amt_field = "target_qty" if filters.get("target_on") == "Quantity" else "target_amount" - qty_or_amount_field = "stock_qty" if filters.get("target_on") == "Quantity" else "base_net_amount" + item_group_parent_child_map = get_item_group_parent_child_map() + for d in sales_users_data: key = (d.parent, d.item_group) dist_data = get_periodwise_distribution_data(d.distribution_id, period_list, filters.get("period")) @@ -191,7 +192,11 @@ def prepare_data( r.get(sales_field) == d.parent and period.from_date <= r.get(date_field) and r.get(date_field) <= period.to_date - and (not sales_user_wise_item_groups.get(d.parent) or r.item_group == d.item_group) + and ( + not sales_user_wise_item_groups.get(d.parent) + or r.item_group == d.item_group + or r.item_group in item_group_parent_child_map.get(d.item_group, []) + ) ): details[p_key] += r.get(qty_or_amount_field, 0) details[variance_key] = details.get(p_key) - details.get(target_key) @@ -204,6 +209,25 @@ def prepare_data( return rows +def get_item_group_parent_child_map(): + """ + Returns a dict of all item group parents and leaf children associated with them. + """ + + item_groups = frappe.get_all( + "Item Group", fields=["name", "parent_item_group"], order_by="lft desc, rgt desc" + ) + item_group_parent_child_map = {} + + for item_group in item_groups: + children = item_group_parent_child_map.get(item_group.name, []) + if not children: + children = [item_group.name] + item_group_parent_child_map.setdefault(item_group.parent_item_group, []).extend(children) + + return item_group_parent_child_map + + def get_actual_data(filters, sales_users_or_territory_data, date_field, sales_field): fiscal_year = get_fiscal_year(fiscal_year=filters.get("fiscal_year"), as_dict=1)