fix: Show achieved amount and variance for parent item groups

This commit is contained in:
Nabin Hait
2024-05-16 14:27:52 +05:30
parent 0363afcfd0
commit 8ac11ae88d

View File

@@ -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)