From f75ebb5a1bc940db2d5d47e4da060a8841d26cc1 Mon Sep 17 00:00:00 2001 From: Akhilesh Darjee Date: Tue, 11 Jun 2013 10:23:10 +0530 Subject: [PATCH 1/5] Report partially completed - Territory Target Variance(Item-Group Wise) --- selling/page/selling_home/selling_home.js | 4 + .../__init__.py | 0 ...itory_target_variance_(item_group_wise).js | 25 ++++++ ...itory_target_variance_(item_group_wise).py | 89 +++++++++++++++++++ ...tory_target_variance_(item_group_wise).txt | 21 +++++ 5 files changed, 139 insertions(+) create mode 100644 selling/report/territory_target_variance_(item_group_wise)/__init__.py create mode 100644 selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).js create mode 100644 selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py create mode 100644 selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).txt diff --git a/selling/page/selling_home/selling_home.js b/selling/page/selling_home/selling_home.js index 682978bd17..7d0162d2ab 100644 --- a/selling/page/selling_home/selling_home.js +++ b/selling/page/selling_home/selling_home.js @@ -165,6 +165,10 @@ wn.module_page["Selling"] = [ "label":wn._("Item-wise Sales History"), route: "query-report/Item-wise Sales History", }, + { + "label":wn._("Territory Target Variance (Item Group-Wise)"), + route: "query-report/Territory Target Variance (Item Group-Wise)", + }, ] } ] diff --git a/selling/report/territory_target_variance_(item_group_wise)/__init__.py b/selling/report/territory_target_variance_(item_group_wise)/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).js b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).js new file mode 100644 index 0000000000..58718a4e0b --- /dev/null +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).js @@ -0,0 +1,25 @@ +wn.query_reports["Territory Target Variance (Item Group-Wise)"] = { + "filters": [ + { + fieldname: "fiscal_year", + label: "Fiscal Year", + fieldtype: "Link", + options: "Fiscal Year", + default: sys_defaults.fiscal_year + }, + { + fieldname: "period", + label: "Period", + fieldtype: "Select", + options: "Monthly\nQuarterly\nHalf-Yearly\nYearly", + default: "Monthly" + }, + { + fieldname: "target_on", + label: "Target On", + fieldtype: "Select", + options: "Quantity\nAmount", + default: "Quantity" + }, + ] +} \ No newline at end of file diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py new file mode 100644 index 0000000000..790c6f02ad --- /dev/null +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py @@ -0,0 +1,89 @@ +# ERPNext - web based ERP (http://erpnext.com) +# Copyright (C) 2012 Web Notes Technologies Pvt Ltd +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from __future__ import unicode_literals +import webnotes +import calendar +from webnotes import msgprint +from webnotes.utils import cint, cstr, add_months + +def execute(filters=None): + if not filters: filters = {} + + columns = get_columns(filters) + + data = [] + + return columns, data + +def get_columns(filters): + """return columns based on filters""" + + if not filters.get("period"): + msgprint("Please select the Period", raise_exception=1) + + mo = cint(cstr(webnotes.conn.get_value("Fiscal Year", filters["fiscal_year"], "year_start_date")).split("-")[1]) + period_months = [] + if (filters["period"] == "Monthly" or "Yearly"): + for x in range(0,12): + period_months.append(mo) + if (mo!=12): + mo += 1 + else: + mo = 1 + + columns = ["Territory:Link/Territory:80"] + ["Item Group:Link/Item Group:80"] + + period = [] + + if (filters["period"] == "Monthly" or "Yearly"): + for i in (0,12): + period.append("Target (" + "i" + ")::80") + period.append("Achieved (" + "i" + ")::80") + period.append("Variance (" + "i" + ")::80") + + columns = columns + [(p) for p in period] + \ + ["Total Target::80"] + ["Total Achieved::80"] + ["Total Variance::80"] + + return columns + +def get_conditions(filters): + conditions = "" + + if filters.get("fiscal_year"): + conditions += " and posting_date <= '%s'" % filters["fiscal_year"] + else: + webnotes.msgprint("Please enter Fiscal Year", raise_exception=1) + + if filters.get("target_on"): + conditions += " and posting_date <= '%s'" % filters["target_on"] + else: + webnotes.msgprint("Please select Target On", raise_exception=1) + + return conditions + + +#get territory details +def get_territory_details(filters): + conditions = get_conditions(filters) + return webnotes.conn.sql("""select item_code, batch_no, warehouse, + posting_date, actual_qty + from `tabStock Ledger Entry` + where ifnull(is_cancelled, 'No') = 'No' %s order by item_code, warehouse""" % + conditions, as_dict=1) + +def get_month_abbr(month_number): + return 0 \ No newline at end of file diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).txt b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).txt new file mode 100644 index 0000000000..7fff64a861 --- /dev/null +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).txt @@ -0,0 +1,21 @@ +[ + { + "creation": "2013-06-07 15:13:13", + "docstatus": 0, + "modified": "2013-06-07 15:13:13", + "modified_by": "Administrator", + "owner": "Administrator" + }, + { + "doctype": "Report", + "is_standard": "Yes", + "name": "__common__", + "ref_doctype": "Sales Order", + "report_name": "Territory Target Variance (Item Group-Wise)", + "report_type": "Script Report" + }, + { + "doctype": "Report", + "name": "Territory Target Variance (Item Group-Wise)" + } +] \ No newline at end of file From 85e1026325253f15d133a6b2fd3accd35726433f Mon Sep 17 00:00:00 2001 From: Akhilesh Darjee Date: Tue, 11 Jun 2013 11:17:56 +0530 Subject: [PATCH 2/5] Fixed selling_home.js conflicted during merge --- selling/page/selling_home/selling_home.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/selling/page/selling_home/selling_home.js b/selling/page/selling_home/selling_home.js index 5996625577..e3663c93e4 100644 --- a/selling/page/selling_home/selling_home.js +++ b/selling/page/selling_home/selling_home.js @@ -159,16 +159,18 @@ wn.module_page["Selling"] = [ }, { "label":wn._("Sales Person-wise Transaction Summary"), - route: "query-report/Sales Person-wise Transaction Summary", + route: "query-report/Sales Person-wise Transaction Summary" }, { "label":wn._("Item-wise Sales History"), - route: "query-report/Item-wise Sales History", + route: "query-report/Item-wise Sales History" }, { "label":wn._("Territory Target Variance (Item Group-Wise)"), route: "query-report/Territory Target Variance (Item Group-Wise)", + doctype: "Sales Order" }, + { "label":wn._("Customers Not Buying Since Long Time"), route: "query-report/Customers Not Buying Since Long Time", doctype: "Sales Order" From f841a7bc144074d496f0c935524a0d87ce1fc2c0 Mon Sep 17 00:00:00 2001 From: Akhilesh Darjee Date: Mon, 17 Jun 2013 19:25:48 +0530 Subject: [PATCH 3/5] Completed territory_target_variance(item_group_wise) report --- ...itory_target_variance_(item_group_wise).py | 193 +++++++++++++----- 1 file changed, 145 insertions(+), 48 deletions(-) diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py index 790c6f02ad..844d4f3e05 100644 --- a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py @@ -17,73 +17,170 @@ from __future__ import unicode_literals import webnotes import calendar -from webnotes import msgprint -from webnotes.utils import cint, cstr, add_months +from webnotes import _, msgprint +from webnotes.utils import cint, cstr, add_months, flt +import time +import calendar def execute(filters=None): if not filters: filters = {} columns = get_columns(filters) + period_month_ranges = get_period_month_ranges(filters) + + target_on = "Quantity" if (filters.get("target_on")=="Quantity") else "Amount" + tim_map = get_territory_item_month_map(filters, target_on) data = [] - - return columns, data + + for territory, territory_items in tim_map.items(): + for item_group, monthwise_data in territory_items.items(): + row = [territory, item_group] + totals = [0, 0, 0] + for relevant_months in period_month_ranges: + period_data = [0, 0, 0] + for month in relevant_months: + month_data = monthwise_data.get(month, {}) + for i, fieldname in enumerate(["target", "achieved", "variance"]): + value = flt(month_data.get(fieldname)) + period_data[i] += value + totals[i] += value + period_data[2] = period_data[0] - period_data[1] + row += period_data + totals[2] = totals[0] - totals[1] + row += totals + data.append(row) + + return columns, sorted(data, key=lambda x: (x[0], x[1])) def get_columns(filters): - """return columns based on filters""" - - if not filters.get("period"): - msgprint("Please select the Period", raise_exception=1) + for fieldname in ["fiscal_year", "period", "target_on"]: + if not filters.get(fieldname): + label = (" ".join(fieldname.split("_"))).title() + msgprint(_("Please specify") + ": " + label, + raise_exception=True) - mo = cint(cstr(webnotes.conn.get_value("Fiscal Year", filters["fiscal_year"], "year_start_date")).split("-")[1]) - period_months = [] - if (filters["period"] == "Monthly" or "Yearly"): - for x in range(0,12): - period_months.append(mo) - if (mo!=12): - mo += 1 + columns = ["Territory:Link/Territory:80", "Item Group:Link/Item Group:80"] + + group_months = False if filters["period"] == "Monthly" else True + + for from_date, to_date in get_period_date_ranges(filters): + for label in ["Target (%s)", "Achieved (%s)", "Variance (%s)"]: + if group_months: + columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))) else: - mo = 1 + columns.append(label % from_date.strftime("%b")) - columns = ["Territory:Link/Territory:80"] + ["Item Group:Link/Item Group:80"] + return columns + ["Total Target::80", "Total Achieved::80", "Total Variance::80"] - period = [] +def get_period_date_ranges(filters): + from dateutil.relativedelta import relativedelta - if (filters["period"] == "Monthly" or "Yearly"): - for i in (0,12): - period.append("Target (" + "i" + ")::80") - period.append("Achieved (" + "i" + ")::80") - period.append("Variance (" + "i" + ")::80") + year_start_date, year_end_date = get_year_start_end_date(filters) - columns = columns + [(p) for p in period] + \ - ["Total Target::80"] + ["Total Achieved::80"] + ["Total Variance::80"] + increment = { + "Monthly": 1, + "Quarterly": 3, + "Half-Yearly": 6, + "Yearly": 12 + }.get(filters["period"]) - return columns + period_date_ranges = [] + for i in xrange(1, 13, increment): + period_end_date = year_start_date + relativedelta(months=increment, + days=-1) + period_date_ranges.append([year_start_date, period_end_date]) + year_start_date = period_end_date + relativedelta(days=1) -def get_conditions(filters): - conditions = "" - - if filters.get("fiscal_year"): - conditions += " and posting_date <= '%s'" % filters["fiscal_year"] - else: - webnotes.msgprint("Please enter Fiscal Year", raise_exception=1) - - if filters.get("target_on"): - conditions += " and posting_date <= '%s'" % filters["target_on"] - else: - webnotes.msgprint("Please select Target On", raise_exception=1) + return period_date_ranges - return conditions +def get_period_month_ranges(filters): + from dateutil.relativedelta import relativedelta + period_month_ranges = [] + + for start_date, end_date in get_period_date_ranges(filters): + months_in_this_period = [] + while start_date <= end_date: + months_in_this_period.append(start_date.strftime("%B")) + start_date += relativedelta(months=1) + period_month_ranges.append(months_in_this_period) + + return period_month_ranges -#get territory details +#Get territory & item group details def get_territory_details(filters): - conditions = get_conditions(filters) - return webnotes.conn.sql("""select item_code, batch_no, warehouse, - posting_date, actual_qty - from `tabStock Ledger Entry` - where ifnull(is_cancelled, 'No') = 'No' %s order by item_code, warehouse""" % - conditions, as_dict=1) + return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, td.target_amount, + t.distribution_id from `tabTerritory` t, `tabTarget Detail` td + where td.parent=t.name and td.fiscal_year=%s and + ifnull(t.distribution_id, '')!='' order by t.name""" % + ('%s'), (filters.get("fiscal_year")), as_dict=1) -def get_month_abbr(month_number): - return 0 \ No newline at end of file +#Get target distribution details of item group +def get_target_distribution_details(filters): + target_details = {} + abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ + from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ + `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ + bd.fiscal_year=%s """ % ('%s'), (filters.get("fiscal_year")), as_dict=1): + target_details.setdefault(d.month, d) + + return target_details + +#Get achieved details from sales order +def get_achieved_details(filters): + start_date, end_date = get_year_start_end_date(filters) + achieved_details = {} + + for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ + MONTHNAME(so.transaction_date) as month_name \ + from `tabSales Order Item` soi, `tabSales Order` so \ + where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ + so.transaction_date<=%s""" % ('%s', '%s'), \ + (start_date, end_date), as_dict=1): + achieved_details.setdefault(d.month_name, d) + + return achieved_details + +def get_territory_item_month_map(filters, target_on): + territory_details = get_territory_details(filters) + tdd = get_target_distribution_details(filters) + achieved_details = get_achieved_details(filters) + + ti_map = {} + + for td in territory_details: + for month in tdd: + ti_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ + .setdefault(month, webnotes._dict({ + "target": 0.0, "achieved": 0.0, "variance": 0.0 + })) + + tav_dict = ti_map[td.name][td.item_group][month] + + for ad in achieved_details: + if (target_on == "Quantity"): + tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: + tav_dict.achieved += achieved_details[month]["qty"] + + if (target_on == "Amount"): + tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: + tav_dict.achieved += achieved_details[month]["amount"] + + return ti_map + +def get_year_start_end_date(filters): + return webnotes.conn.sql("""select year_start_date, + subdate(adddate(year_start_date, interval 1 year), interval 1 day) + as year_end_date + from `tabFiscal Year` + where name=%s""", filters["fiscal_year"])[0] + +def get_item_group(item_name): + """Get Item Group of an item""" + + return webnotes.conn.sql_list("select item_group from `tabItem` where name=%s""" % + ('%s'), (item_name)) \ No newline at end of file From 4ddc2618e8611f39665cddb49a6d93e646a80317 Mon Sep 17 00:00:00 2001 From: Akhilesh Darjee Date: Tue, 18 Jun 2013 18:46:02 +0530 Subject: [PATCH 4/5] [Report] Completed Sales Person Target Variance (Item-Group wise) --- accounts/page/accounts_home/accounts_home.js | 5 + .../report/budget_variance_report/__init__.py | 0 .../budget_variance_report.js | 25 +++ .../budget_variance_report.py | 184 ++++++++++++++++++ .../budget_variance_report.txt | 21 ++ selling/page/selling_home/selling_home.js | 5 + .../__init__.py | 0 ...erson_target_variance_(item_group_wise).js | 25 +++ ...erson_target_variance_(item_group_wise).py | 184 ++++++++++++++++++ ...rson_target_variance_(item_group_wise).txt | 21 ++ ...itory_target_variance_(item_group_wise).py | 26 ++- 11 files changed, 482 insertions(+), 14 deletions(-) create mode 100644 accounts/report/budget_variance_report/__init__.py create mode 100644 accounts/report/budget_variance_report/budget_variance_report.js create mode 100644 accounts/report/budget_variance_report/budget_variance_report.py create mode 100644 accounts/report/budget_variance_report/budget_variance_report.txt create mode 100644 selling/report/sales_person_target_variance_(item_group_wise)/__init__.py create mode 100644 selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).js create mode 100644 selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py create mode 100644 selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).txt diff --git a/accounts/page/accounts_home/accounts_home.js b/accounts/page/accounts_home/accounts_home.js index b920bfdbab..b4846746a4 100644 --- a/accounts/page/accounts_home/accounts_home.js +++ b/accounts/page/accounts_home/accounts_home.js @@ -252,6 +252,11 @@ wn.module_page["Accounts"] = [ route: "query-report/Item-wise Purchase Register", doctype: "Purchase Invoice" }, + { + "label":wn._("Budget Variance Report"), + route: "query-report/Budget Variance Report", + doctype: "Cost Center" + }, ] } ] diff --git a/accounts/report/budget_variance_report/__init__.py b/accounts/report/budget_variance_report/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/accounts/report/budget_variance_report/budget_variance_report.js b/accounts/report/budget_variance_report/budget_variance_report.js new file mode 100644 index 0000000000..a0516050ce --- /dev/null +++ b/accounts/report/budget_variance_report/budget_variance_report.js @@ -0,0 +1,25 @@ +wn.query_reports["Budget Variance Report"] = { + "filters": [ + { + fieldname: "fiscal_year", + label: "Fiscal Year", + fieldtype: "Link", + options: "Fiscal Year", + default: sys_defaults.fiscal_year + }, + { + fieldname: "period", + label: "Period", + fieldtype: "Select", + options: "Monthly\nQuarterly\nHalf-Yearly\nYearly", + default: "Monthly" + }, + { + fieldname: "company", + label: "Company", + fieldtype: "Link", + options: "Company", + default: sys_defaults.company + }, + ] +} \ No newline at end of file diff --git a/accounts/report/budget_variance_report/budget_variance_report.py b/accounts/report/budget_variance_report/budget_variance_report.py new file mode 100644 index 0000000000..ac38d9f34d --- /dev/null +++ b/accounts/report/budget_variance_report/budget_variance_report.py @@ -0,0 +1,184 @@ +# ERPNext - web based ERP (http://erpnext.com) +# Copyright (C) 2012 Web Notes Technologies Pvt Ltd +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from __future__ import unicode_literals +import webnotes +import calendar +from webnotes import _, msgprint +from webnotes.utils import flt +import time + +def execute(filters=None): + if not filters: filters = {} + + columns = get_columns(filters) + period_month_ranges = get_period_month_ranges(filters) + # tim_map = get_territory_item_month_map(filters) + + data = [] + + # for territory, territory_items in tim_map.items(): + # for item_group, monthwise_data in territory_items.items(): + # row = [territory, item_group] + # totals = [0, 0, 0] + # for relevant_months in period_month_ranges: + # period_data = [0, 0, 0] + # for month in relevant_months: + # month_data = monthwise_data.get(month, {}) + # for i, fieldname in enumerate(["target", "achieved", "variance"]): + # value = flt(month_data.get(fieldname)) + # period_data[i] += value + # totals[i] += value + # period_data[2] = period_data[0] - period_data[1] + # row += period_data + # totals[2] = totals[0] - totals[1] + # row += totals + # data.append(row) + + return columns, sorted(data, key=lambda x: (x[0], x[1])) + +def get_columns(filters): + for fieldname in ["fiscal_year", "period", "company"]: + if not filters.get(fieldname): + label = (" ".join(fieldname.split("_"))).title() + msgprint(_("Please specify") + ": " + label, + raise_exception=True) + + columns = ["Cost Center:Link/Cost Center:80"] + + group_months = False if filters["period"] == "Monthly" else True + + for from_date, to_date in get_period_date_ranges(filters): + for label in ["Target (%s)", "Actual (%s)", "Variance (%s)"]: + if group_months: + columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))) + else: + columns.append(label % from_date.strftime("%b")) + + return columns + ["Total Target::80", "Total Actual::80", "Total Variance::80"] + +def get_period_date_ranges(filters): + from dateutil.relativedelta import relativedelta + + year_start_date, year_end_date = get_year_start_end_date(filters) + + increment = { + "Monthly": 1, + "Quarterly": 3, + "Half-Yearly": 6, + "Yearly": 12 + }.get(filters["period"]) + + period_date_ranges = [] + for i in xrange(1, 13, increment): + period_end_date = year_start_date + relativedelta(months=increment, + days=-1) + period_date_ranges.append([year_start_date, period_end_date]) + year_start_date = period_end_date + relativedelta(days=1) + + return period_date_ranges + +def get_period_month_ranges(filters): + from dateutil.relativedelta import relativedelta + period_month_ranges = [] + + for start_date, end_date in get_period_date_ranges(filters): + months_in_this_period = [] + while start_date <= end_date: + months_in_this_period.append(start_date.strftime("%B")) + start_date += relativedelta(months=1) + period_month_ranges.append(months_in_this_period) + + return period_month_ranges + + +#Get cost center details +def get_costcenter_details(filters): + return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, + td.target_amount, t.distribution_id + from `tabTerritory` t, `tabTarget Detail` td + where td.parent=t.name and td.fiscal_year=%s and + ifnull(t.distribution_id, '')!='' order by t.name""" % + ('%s'), (filters.get("fiscal_year")), as_dict=1) + +#Get target distribution details of item group +def get_target_distribution_details(filters): + target_details = {} + abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ + from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ + `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ + bd.fiscal_year=%s """ % ('%s'), (filters.get("fiscal_year")), as_dict=1): + target_details.setdefault(d.month, d) + + return target_details + +#Get achieved details from sales order +def get_achieved_details(filters): + start_date, end_date = get_year_start_end_date(filters) + achieved_details = {} + + for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ + MONTHNAME(so.transaction_date) as month_name \ + from `tabSales Order Item` soi, `tabSales Order` so \ + where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ + so.transaction_date<=%s""" % ('%s', '%s'), \ + (start_date, end_date), as_dict=1): + achieved_details.setdefault(d.month_name, d) + + return achieved_details + +def get_territory_item_month_map(filters): + territory_details = get_territory_details(filters) + tdd = get_target_distribution_details(filters) + achieved_details = get_achieved_details(filters) + + tim_map = {} + + for td in territory_details: + for month in tdd: + tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ + .setdefault(month, webnotes._dict({ + "target": 0.0, "achieved": 0.0, "variance": 0.0 + })) + + tav_dict = tim_map[td.name][td.item_group][month] + + for ad in achieved_details: + if (filters["target_on"] == "Quantity"): + tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: + tav_dict.achieved += achieved_details[month]["qty"] + + if (filters["target_on"] == "Amount"): + tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: + tav_dict.achieved += achieved_details[month]["amount"] + + return tim_map + +def get_year_start_end_date(filters): + return webnotes.conn.sql("""select year_start_date, + subdate(adddate(year_start_date, interval 1 year), interval 1 day) + as year_end_date + from `tabFiscal Year` + where name=%s""", filters["fiscal_year"])[0] + +def get_item_group(item_name): + """Get Item Group of an item""" + + return webnotes.conn.sql_list("select item_group from `tabItem` where name=%s""" % + ('%s'), (item_name)) \ No newline at end of file diff --git a/accounts/report/budget_variance_report/budget_variance_report.txt b/accounts/report/budget_variance_report/budget_variance_report.txt new file mode 100644 index 0000000000..b89cb4545f --- /dev/null +++ b/accounts/report/budget_variance_report/budget_variance_report.txt @@ -0,0 +1,21 @@ +[ + { + "creation": "2013-06-18 12:56:36", + "docstatus": 0, + "modified": "2013-06-18 12:56:36", + "modified_by": "Administrator", + "owner": "Administrator" + }, + { + "doctype": "Report", + "is_standard": "Yes", + "name": "__common__", + "ref_doctype": "Cost Center", + "report_name": "Budget Variance Report", + "report_type": "Script Report" + }, + { + "doctype": "Report", + "name": "Budget Variance Report" + } +] \ No newline at end of file diff --git a/selling/page/selling_home/selling_home.js b/selling/page/selling_home/selling_home.js index e3663c93e4..0792f0a8e6 100644 --- a/selling/page/selling_home/selling_home.js +++ b/selling/page/selling_home/selling_home.js @@ -170,6 +170,11 @@ wn.module_page["Selling"] = [ route: "query-report/Territory Target Variance (Item Group-Wise)", doctype: "Sales Order" }, + { + "label":wn._("Sales Person Target Variance (Item Group-Wise)"), + route: "query-report/Sales Person Target Variance (Item Group-Wise)", + doctype: "Sales Order" + }, { "label":wn._("Customers Not Buying Since Long Time"), route: "query-report/Customers Not Buying Since Long Time", diff --git a/selling/report/sales_person_target_variance_(item_group_wise)/__init__.py b/selling/report/sales_person_target_variance_(item_group_wise)/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).js b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).js new file mode 100644 index 0000000000..09f0d55aed --- /dev/null +++ b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).js @@ -0,0 +1,25 @@ +wn.query_reports["Sales Person Target Variance (Item Group-Wise)"] = { + "filters": [ + { + fieldname: "fiscal_year", + label: "Fiscal Year", + fieldtype: "Link", + options: "Fiscal Year", + default: sys_defaults.fiscal_year + }, + { + fieldname: "period", + label: "Period", + fieldtype: "Select", + options: "Monthly\nQuarterly\nHalf-Yearly\nYearly", + default: "Monthly" + }, + { + fieldname: "target_on", + label: "Target On", + fieldtype: "Select", + options: "Quantity\nAmount", + default: "Quantity" + }, + ] +} \ No newline at end of file diff --git a/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py new file mode 100644 index 0000000000..3163829131 --- /dev/null +++ b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py @@ -0,0 +1,184 @@ +# ERPNext - web based ERP (http://erpnext.com) +# Copyright (C) 2012 Web Notes Technologies Pvt Ltd +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from __future__ import unicode_literals +import webnotes +import calendar +from webnotes import _, msgprint +from webnotes.utils import flt +import time + +def execute(filters=None): + if not filters: filters = {} + + columns = get_columns(filters) + period_month_ranges = get_period_month_ranges(filters) + sim_map = get_salesperson_item_month_map(filters) + + data = [] + + for salesperson, salesperson_items in sim_map.items(): + for item_group, monthwise_data in salesperson_items.items(): + row = [salesperson, item_group] + totals = [0, 0, 0] + for relevant_months in period_month_ranges: + period_data = [0, 0, 0] + for month in relevant_months: + month_data = monthwise_data.get(month, {}) + for i, fieldname in enumerate(["target", "achieved", "variance"]): + value = flt(month_data.get(fieldname)) + period_data[i] += value + totals[i] += value + period_data[2] = period_data[0] - period_data[1] + row += period_data + totals[2] = totals[0] - totals[1] + row += totals + data.append(row) + + return columns, sorted(data, key=lambda x: (x[0], x[1])) + +def get_columns(filters): + for fieldname in ["fiscal_year", "period", "target_on"]: + if not filters.get(fieldname): + label = (" ".join(fieldname.split("_"))).title() + msgprint(_("Please specify") + ": " + label, + raise_exception=True) + + columns = ["Sales Person:Link/Sales Person:80", "Item Group:Link/Item Group:80"] + + group_months = False if filters["period"] == "Monthly" else True + + for from_date, to_date in get_period_date_ranges(filters): + for label in ["Target (%s)", "Achieved (%s)", "Variance (%s)"]: + if group_months: + columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))) + else: + columns.append(label % from_date.strftime("%b")) + + return columns + ["Total Target::80", "Total Achieved::80", "Total Variance::80"] + +def get_period_date_ranges(filters): + from dateutil.relativedelta import relativedelta + + year_start_date, year_end_date = get_year_start_end_date(filters) + + increment = { + "Monthly": 1, + "Quarterly": 3, + "Half-Yearly": 6, + "Yearly": 12 + }.get(filters["period"]) + + period_date_ranges = [] + for i in xrange(1, 13, increment): + period_end_date = year_start_date + relativedelta(months=increment, + days=-1) + period_date_ranges.append([year_start_date, period_end_date]) + year_start_date = period_end_date + relativedelta(days=1) + + return period_date_ranges + +def get_period_month_ranges(filters): + from dateutil.relativedelta import relativedelta + period_month_ranges = [] + + for start_date, end_date in get_period_date_ranges(filters): + months_in_this_period = [] + while start_date <= end_date: + months_in_this_period.append(start_date.strftime("%B")) + start_date += relativedelta(months=1) + period_month_ranges.append(months_in_this_period) + + return period_month_ranges + + +#Get sales person & item group details +def get_salesperson_details(filters): + return webnotes.conn.sql("""select sp.name, td.item_group, td.target_qty, + td.target_amount, sp.distribution_id + from `tabSales Person` sp, `tabTarget Detail` td + where td.parent=sp.name and td.fiscal_year=%s and + ifnull(sp.distribution_id, '')!='' order by sp.name""" % + ('%s'), (filters.get("fiscal_year")), as_dict=1) + +#Get target distribution details of item group +def get_target_distribution_details(filters): + target_details = {} + abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ + from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ + `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ + bd.fiscal_year=%s """ % ('%s'), (filters.get("fiscal_year")), as_dict=1): + target_details.setdefault(d.month, d) + + return target_details + +#Get achieved details from sales order +def get_achieved_details(filters): + start_date, end_date = get_year_start_end_date(filters) + achieved_details = {} + + for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ + MONTHNAME(so.transaction_date) as month_name \ + from `tabSales Order Item` soi, `tabSales Order` so \ + where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ + so.transaction_date<=%s""" % ('%s', '%s'), \ + (start_date, end_date), as_dict=1): + achieved_details.setdefault(d.month_name, d) + + return achieved_details + +def get_salesperson_item_month_map(filters): + salesperson_details = get_salesperson_details(filters) + tdd = get_target_distribution_details(filters) + achieved_details = get_achieved_details(filters) + + sim_map = {} + + for sd in salesperson_details: + for month in tdd: + sim_map.setdefault(sd.name, {}).setdefault(sd.item_group, {})\ + .setdefault(month, webnotes._dict({ + "target": 0.0, "achieved": 0.0, "variance": 0.0 + })) + + tav_dict = sim_map[sd.name][sd.item_group][month] + + for ad in achieved_details: + if (filters["target_on"] == "Quantity"): + tav_dict.target = sd.target_qty*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == sd.item_group: + tav_dict.achieved += achieved_details[month]["qty"] + + if (filters["target_on"] == "Amount"): + tav_dict.target = sd.target_amount*(tdd[month]["percentage_allocation"]/100) + if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == sd.item_group: + tav_dict.achieved += achieved_details[month]["amount"] + + return sim_map + +def get_year_start_end_date(filters): + return webnotes.conn.sql("""select year_start_date, + subdate(adddate(year_start_date, interval 1 year), interval 1 day) + as year_end_date + from `tabFiscal Year` + where name=%s""", filters["fiscal_year"])[0] + +def get_item_group(item_name): + """Get Item Group of an item""" + + return webnotes.conn.sql_list("select item_group from `tabItem` where name=%s""" % + ('%s'), (item_name)) \ No newline at end of file diff --git a/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).txt b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).txt new file mode 100644 index 0000000000..955cdec477 --- /dev/null +++ b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).txt @@ -0,0 +1,21 @@ +[ + { + "creation": "2013-06-18 12:09:40", + "docstatus": 0, + "modified": "2013-06-18 12:09:40", + "modified_by": "Administrator", + "owner": "Administrator" + }, + { + "doctype": "Report", + "is_standard": "Yes", + "name": "__common__", + "ref_doctype": "Sales Order", + "report_name": "Sales Person Target Variance (Item Group-Wise)", + "report_type": "Script Report" + }, + { + "doctype": "Report", + "name": "Sales Person Target Variance (Item Group-Wise)" + } +] \ No newline at end of file diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py index 844d4f3e05..fe5e628c65 100644 --- a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py @@ -18,18 +18,15 @@ from __future__ import unicode_literals import webnotes import calendar from webnotes import _, msgprint -from webnotes.utils import cint, cstr, add_months, flt +from webnotes.utils import flt import time -import calendar def execute(filters=None): if not filters: filters = {} columns = get_columns(filters) period_month_ranges = get_period_month_ranges(filters) - - target_on = "Quantity" if (filters.get("target_on")=="Quantity") else "Amount" - tim_map = get_territory_item_month_map(filters, target_on) + tim_map = get_territory_item_month_map(filters) data = [] @@ -110,8 +107,9 @@ def get_period_month_ranges(filters): #Get territory & item group details def get_territory_details(filters): - return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, td.target_amount, - t.distribution_id from `tabTerritory` t, `tabTarget Detail` td + return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, + td.target_amount, t.distribution_id + from `tabTerritory` t, `tabTarget Detail` td where td.parent=t.name and td.fiscal_year=%s and ifnull(t.distribution_id, '')!='' order by t.name""" % ('%s'), (filters.get("fiscal_year")), as_dict=1) @@ -143,34 +141,34 @@ def get_achieved_details(filters): return achieved_details -def get_territory_item_month_map(filters, target_on): +def get_territory_item_month_map(filters): territory_details = get_territory_details(filters) tdd = get_target_distribution_details(filters) achieved_details = get_achieved_details(filters) - ti_map = {} + tim_map = {} for td in territory_details: for month in tdd: - ti_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ + tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ .setdefault(month, webnotes._dict({ "target": 0.0, "achieved": 0.0, "variance": 0.0 })) - tav_dict = ti_map[td.name][td.item_group][month] + tav_dict = tim_map[td.name][td.item_group][month] for ad in achieved_details: - if (target_on == "Quantity"): + if (filters["target_on"] == "Quantity"): tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100) if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: tav_dict.achieved += achieved_details[month]["qty"] - if (target_on == "Amount"): + if (filters["target_on"] == "Amount"): tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100) if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: tav_dict.achieved += achieved_details[month]["amount"] - return ti_map + return tim_map def get_year_start_end_date(filters): return webnotes.conn.sql("""select year_start_date, From 92571c692939c17537cdb27bedea89343a038fb1 Mon Sep 17 00:00:00 2001 From: Akhilesh Darjee Date: Wed, 19 Jun 2013 18:15:20 +0530 Subject: [PATCH 5/5] [Report] Completed Budget variance Report and corrections in Territory Target Varinace & Sales Person Target Variance Report --- .../budget_variance_report.py | 128 ++++++++---------- .../item_wise_purchase_register.py | 1 - ...erson_target_variance_(item_group_wise).py | 31 ++--- ...itory_target_variance_(item_group_wise).py | 30 ++-- 4 files changed, 85 insertions(+), 105 deletions(-) diff --git a/accounts/report/budget_variance_report/budget_variance_report.py b/accounts/report/budget_variance_report/budget_variance_report.py index ac38d9f34d..99e303bd20 100644 --- a/accounts/report/budget_variance_report/budget_variance_report.py +++ b/accounts/report/budget_variance_report/budget_variance_report.py @@ -26,27 +26,27 @@ def execute(filters=None): columns = get_columns(filters) period_month_ranges = get_period_month_ranges(filters) - # tim_map = get_territory_item_month_map(filters) + cam_map = get_costcenter_account_month_map(filters) data = [] - # for territory, territory_items in tim_map.items(): - # for item_group, monthwise_data in territory_items.items(): - # row = [territory, item_group] - # totals = [0, 0, 0] - # for relevant_months in period_month_ranges: - # period_data = [0, 0, 0] - # for month in relevant_months: - # month_data = monthwise_data.get(month, {}) - # for i, fieldname in enumerate(["target", "achieved", "variance"]): - # value = flt(month_data.get(fieldname)) - # period_data[i] += value - # totals[i] += value - # period_data[2] = period_data[0] - period_data[1] - # row += period_data - # totals[2] = totals[0] - totals[1] - # row += totals - # data.append(row) + for cost_center, cost_center_items in cam_map.items(): + for account, monthwise_data in cost_center_items.items(): + row = [cost_center, account] + totals = [0, 0, 0] + for relevant_months in period_month_ranges: + period_data = [0, 0, 0] + for month in relevant_months: + month_data = monthwise_data.get(month, {}) + for i, fieldname in enumerate(["target", "actual", "variance"]): + value = flt(month_data.get(fieldname)) + period_data[i] += value + totals[i] += value + period_data[2] = period_data[0] - period_data[1] + row += period_data + totals[2] = totals[0] - totals[1] + row += totals + data.append(row) return columns, sorted(data, key=lambda x: (x[0], x[1])) @@ -57,7 +57,7 @@ def get_columns(filters): msgprint(_("Please specify") + ": " + label, raise_exception=True) - columns = ["Cost Center:Link/Cost Center:80"] + columns = ["Cost Center:Link/Cost Center:100", "Account:Link/Account:100"] group_months = False if filters["period"] == "Monthly" else True @@ -105,80 +105,64 @@ def get_period_month_ranges(filters): return period_month_ranges -#Get cost center details -def get_costcenter_details(filters): - return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, - td.target_amount, t.distribution_id - from `tabTerritory` t, `tabTarget Detail` td - where td.parent=t.name and td.fiscal_year=%s and - ifnull(t.distribution_id, '')!='' order by t.name""" % - ('%s'), (filters.get("fiscal_year")), as_dict=1) +#Get cost center & target details +def get_costcenter_target_details(filters): + return webnotes.conn.sql("""select cc.name, cc.distribution_id, + cc.parent_cost_center, bd.account, bd.budget_allocated + from `tabCost Center` cc, `tabBudget Detail` bd + where bd.parent=cc.name and bd.fiscal_year=%s and + cc.company_name=%s and ifnull(cc.distribution_id, '')!='' + order by cc.name""" % ('%s', '%s'), + (filters.get("fiscal_year"), filters.get("company")), as_dict=1) -#Get target distribution details of item group +#Get target distribution details of accounts of cost center def get_target_distribution_details(filters): target_details = {} - abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ - `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ - bd.fiscal_year=%s """ % ('%s'), (filters.get("fiscal_year")), as_dict=1): + `tabCost Center` cc where bdd.parent=bd.name and cc.distribution_id=bd.name and \ + bd.fiscal_year=%s""" % ('%s'), (filters.get("fiscal_year")), as_dict=1): target_details.setdefault(d.month, d) return target_details -#Get achieved details from sales order -def get_achieved_details(filters): - start_date, end_date = get_year_start_end_date(filters) - achieved_details = {} +#Get actual details from gl entry +def get_actual_details(filters): + return webnotes.conn.sql("""select gl.account, gl.debit, gl.credit, + gl.cost_center, MONTHNAME(gl.posting_date) as month_name + from `tabGL Entry` gl, `tabBudget Detail` bd + where gl.fiscal_year=%s and company=%s and is_cancelled='No' + and bd.account=gl.account""" % ('%s', '%s'), + (filters.get("fiscal_year"), filters.get("company")), as_dict=1) - for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ - MONTHNAME(so.transaction_date) as month_name \ - from `tabSales Order Item` soi, `tabSales Order` so \ - where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ - so.transaction_date<=%s""" % ('%s', '%s'), \ - (start_date, end_date), as_dict=1): - achieved_details.setdefault(d.month_name, d) - - return achieved_details - -def get_territory_item_month_map(filters): - territory_details = get_territory_details(filters) +def get_costcenter_account_month_map(filters): + costcenter_target_details = get_costcenter_target_details(filters) tdd = get_target_distribution_details(filters) - achieved_details = get_achieved_details(filters) + actual_details = get_actual_details(filters) - tim_map = {} + cam_map = {} - for td in territory_details: + for ccd in costcenter_target_details: for month in tdd: - tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ + cam_map.setdefault(ccd.name, {}).setdefault(ccd.account, {})\ .setdefault(month, webnotes._dict({ - "target": 0.0, "achieved": 0.0, "variance": 0.0 + "target": 0.0, "actual": 0.0, "variance": 0.0 })) - tav_dict = tim_map[td.name][td.item_group][month] + tav_dict = cam_map[ccd.name][ccd.account][month] + tav_dict.target = ccd.budget_allocated*(tdd[month]["percentage_allocation"]/100) - for ad in achieved_details: - if (filters["target_on"] == "Quantity"): - tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: - tav_dict.achieved += achieved_details[month]["qty"] - - if (filters["target_on"] == "Amount"): - tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: - tav_dict.achieved += achieved_details[month]["amount"] - - return tim_map + for ad in actual_details: + if ad.month_name == month and ad.account == ccd.account \ + and ad.cost_center == ccd.name: + tav_dict.actual += ad.debit - ad.credit + + return cam_map def get_year_start_end_date(filters): return webnotes.conn.sql("""select year_start_date, subdate(adddate(year_start_date, interval 1 year), interval 1 day) as year_end_date from `tabFiscal Year` - where name=%s""", filters["fiscal_year"])[0] - -def get_item_group(item_name): - """Get Item Group of an item""" - - return webnotes.conn.sql_list("select item_group from `tabItem` where name=%s""" % - ('%s'), (item_name)) \ No newline at end of file + where name=%s""", filters["fiscal_year"])[0] \ No newline at end of file diff --git a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py index ad9d79504b..f7afb3dfb6 100644 --- a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py +++ b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py @@ -23,7 +23,6 @@ def execute(filters=None): columns = get_columns() item_list = get_items(filters) aii_account_map = get_aii_accounts() - webnotes.errprint(aii_account_map) data = [] for d in item_list: expense_head = d.expense_head or aii_account_map.get(d.company) diff --git a/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py index 3163829131..8f5931d826 100644 --- a/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py +++ b/selling/report/sales_person_target_variance_(item_group_wise)/sales_person_target_variance_(item_group_wise).py @@ -117,7 +117,7 @@ def get_salesperson_details(filters): #Get target distribution details of item group def get_target_distribution_details(filters): target_details = {} - abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ @@ -129,17 +129,14 @@ def get_target_distribution_details(filters): #Get achieved details from sales order def get_achieved_details(filters): start_date, end_date = get_year_start_end_date(filters) - achieved_details = {} - - for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ - MONTHNAME(so.transaction_date) as month_name \ - from `tabSales Order Item` soi, `tabSales Order` so \ - where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ - so.transaction_date<=%s""" % ('%s', '%s'), \ - (start_date, end_date), as_dict=1): - achieved_details.setdefault(d.month_name, d) - - return achieved_details + + return webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, + st.sales_person, MONTHNAME(so.transaction_date) as month_name + from `tabSales Order Item` soi, `tabSales Order` so, `tabSales Team` st + where soi.parent=so.name and so.docstatus=1 and + st.parent=so.name and so.transaction_date>=%s and + so.transaction_date<=%s""" % ('%s', '%s'), + (start_date, end_date), as_dict=1) def get_salesperson_item_month_map(filters): salesperson_details = get_salesperson_details(filters) @@ -160,13 +157,15 @@ def get_salesperson_item_month_map(filters): for ad in achieved_details: if (filters["target_on"] == "Quantity"): tav_dict.target = sd.target_qty*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == sd.item_group: - tav_dict.achieved += achieved_details[month]["qty"] + if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \ + and ad.sales_person == sd.name: + tav_dict.achieved += ad.qty if (filters["target_on"] == "Amount"): tav_dict.target = sd.target_amount*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == sd.item_group: - tav_dict.achieved += achieved_details[month]["amount"] + if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \ + and ad.sales_person == sd.name: + tav_dict.achieved += ad.amount return sim_map diff --git a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py index fe5e628c65..079f8e82a2 100644 --- a/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py +++ b/selling/report/territory_target_variance_(item_group_wise)/territory_target_variance_(item_group_wise).py @@ -117,11 +117,11 @@ def get_territory_details(filters): #Get target distribution details of item group def get_target_distribution_details(filters): target_details = {} - abc = [] + for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \ from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \ `tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \ - bd.fiscal_year=%s """ % ('%s'), (filters.get("fiscal_year")), as_dict=1): + bd.fiscal_year=%s""" % ('%s'), (filters.get("fiscal_year")), as_dict=1): target_details.setdefault(d.month, d) return target_details @@ -129,17 +129,13 @@ def get_target_distribution_details(filters): #Get achieved details from sales order def get_achieved_details(filters): start_date, end_date = get_year_start_end_date(filters) - achieved_details = {} - for d in webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, \ - MONTHNAME(so.transaction_date) as month_name \ - from `tabSales Order Item` soi, `tabSales Order` so \ - where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \ - so.transaction_date<=%s""" % ('%s', '%s'), \ - (start_date, end_date), as_dict=1): - achieved_details.setdefault(d.month_name, d) - - return achieved_details + return webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date, + so.territory, MONTHNAME(so.transaction_date) as month_name + from `tabSales Order Item` soi, `tabSales Order` so + where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and + so.transaction_date<=%s""" % ('%s', '%s'), + (start_date, end_date), as_dict=1) def get_territory_item_month_map(filters): territory_details = get_territory_details(filters) @@ -160,13 +156,15 @@ def get_territory_item_month_map(filters): for ad in achieved_details: if (filters["target_on"] == "Quantity"): tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: - tav_dict.achieved += achieved_details[month]["qty"] + if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \ + and ad.territory == td.name: + tav_dict.achieved += ad.qty if (filters["target_on"] == "Amount"): tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100) - if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group: - tav_dict.achieved += achieved_details[month]["amount"] + if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \ + and ad.territory == td.name: + tav_dict.achieved += ad.amount return tim_map