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 f2f1e4cfbaa2..d4e704a7e330 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 @@ -209,31 +209,32 @@ def get_actual_data(filters, sales_users_or_territory_data, date_field, sales_fi parent_doc = frappe.qb.DocType(filters.get("doctype")) child_doc = frappe.qb.DocType(filters.get("doctype") + " Item") - sales_team = frappe.qb.DocType("Sales Team") - - query = ( - frappe.qb.from_(parent_doc) - .inner_join(child_doc) - .on(child_doc.parent == parent_doc.name) - .inner_join(sales_team) - .on(sales_team.parent == parent_doc.name) - .select( - child_doc.item_group, - (child_doc.stock_qty * sales_team.allocated_percentage / 100).as_("stock_qty"), - (child_doc.base_net_amount * sales_team.allocated_percentage / 100).as_("base_net_amount"), - sales_team.sales_person, - parent_doc[date_field], - ) - .where( - (parent_doc.docstatus == 1) - & (parent_doc[date_field].between(fiscal_year.year_start_date, fiscal_year.year_end_date)) - ) - ) + + query = frappe.qb.from_(parent_doc).inner_join(child_doc).on(child_doc.parent == parent_doc.name) if sales_field == "sales_person": - query = query.where(sales_team.sales_person.isin(sales_users_or_territory_data)) + sales_team = frappe.qb.DocType("Sales Team") + stock_qty = child_doc.stock_qty * sales_team.allocated_percentage / 100 + net_amount = child_doc.base_net_amount * sales_team.allocated_percentage / 100 + sales_field_col = sales_team[sales_field] + + query = query.inner_join(sales_team).on(sales_team.parent == parent_doc.name) else: - query = query.where(parent_doc[sales_field].isin(sales_users_or_territory_data)) + stock_qty = child_doc.stock_qty + net_amount = child_doc.base_net_amount + sales_field_col = parent_doc[sales_field] + + query = query.select( + child_doc.item_group, + parent_doc[date_field], + (stock_qty).as_("stock_qty"), + (net_amount).as_("base_net_amount"), + sales_field_col, + ).where( + (parent_doc.docstatus == 1) + & (parent_doc[date_field].between(fiscal_year.year_start_date, fiscal_year.year_end_date)) + & (sales_field_col.isin(sales_users_or_territory_data)) + ) return query.run(as_dict=True)