[Report]Cleanup of territory target variance, sales person target variance & budget variance reports

This commit is contained in:
Akhilesh Darjee
2013-06-21 12:47:51 +05:30
parent e6974d10c2
commit 1ded5c618e
11 changed files with 106 additions and 187 deletions

View File

@@ -167,12 +167,12 @@ wn.module_page["Selling"] = [
},
{
"label":wn._("Territory Target Variance (Item Group-Wise)"),
route: "query-report/Territory Target Variance (Item Group-Wise)",
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)",
route: "query-report/Sales Person Target Variance Item Group-Wise",
doctype: "Sales Order"
},
{

View File

@@ -1,4 +1,4 @@
wn.query_reports["Sales Person Target Variance (Item Group-Wise)"] = {
wn.query_reports["Sales Person Target Variance Item Group-Wise"] = {
"filters": [
{
fieldname: "fiscal_year",

View File

@@ -16,18 +16,21 @@
from __future__ import unicode_literals
import webnotes
import calendar
from webnotes import _, msgprint
from webnotes.utils import flt
import time
from accounts.utils import get_fiscal_year
from controllers.trends import get_period_date_ranges, get_period_month_ranges
def execute(filters=None):
if not filters: filters = {}
columns = get_columns(filters)
period_month_ranges = get_period_month_ranges(filters)
period_month_ranges = get_period_month_ranges(filters["period"], filters["fiscal_year"])
sim_map = get_salesperson_item_month_map(filters)
precision = webnotes.conn.get_value("Global Defaults", None, "float_precision") or 2
data = []
for salesperson, salesperson_items in sim_map.items():
@@ -39,7 +42,7 @@ def execute(filters=None):
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))
value = flt(month_data.get(fieldname), precision)
period_data[i] += value
totals[i] += value
period_data[2] = period_data[0] - period_data[1]
@@ -61,7 +64,7 @@ def get_columns(filters):
group_months = False if filters["period"] == "Monthly" else True
for from_date, to_date in get_period_date_ranges(filters):
for from_date, to_date in get_period_date_ranges(filters["period"], filters["fiscal_year"]):
for label in ["Target (%s)", "Achieved (%s)", "Variance (%s)"]:
if group_months:
columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b")))
@@ -70,49 +73,14 @@ def get_columns(filters):
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)
ifnull(sp.distribution_id, '')!='' order by sp.name""",
(filters["fiscal_year"]), as_dict=1)
#Get target distribution details of item group
def get_target_distribution_details(filters):
@@ -121,14 +89,14 @@ def get_target_distribution_details(filters):
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""", (filters["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)
start_date, end_date = get_fiscal_year(filters)[1:]
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
@@ -156,28 +124,18 @@ 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_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \
tav_dict.target = flt(sd.target_qty)*(tdd[month]["percentage_allocation"]/100)
if ad.month_name == month and 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_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \
tav_dict.target = flt(sd.target_amount)*(tdd[month]["percentage_allocation"]/100)
if ad.month_name == month and get_item_group(ad.item_code) == sd.item_group \
and ad.sales_person == sd.name:
tav_dict.achieved += ad.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))
return webnotes.conn.get_value("Item", item_name, item_group)

View File

@@ -1,8 +1,8 @@
[
{
"creation": "2013-06-18 12:09:40",
"creation": "2013-06-21 12:14:15",
"docstatus": 0,
"modified": "2013-06-18 12:09:40",
"modified": "2013-06-21 12:14:15",
"modified_by": "Administrator",
"owner": "Administrator"
},
@@ -11,11 +11,11 @@
"is_standard": "Yes",
"name": "__common__",
"ref_doctype": "Sales Order",
"report_name": "Sales Person Target Variance (Item Group-Wise)",
"report_name": "Sales Person Target Variance Item Group-Wise",
"report_type": "Script Report"
},
{
"doctype": "Report",
"name": "Sales Person Target Variance (Item Group-Wise)"
"name": "Sales Person Target Variance Item Group-Wise"
}
]

View File

@@ -1,4 +1,4 @@
wn.query_reports["Territory Target Variance (Item Group-Wise)"] = {
wn.query_reports["Territory Target Variance Item Group-Wise"] = {
"filters": [
{
fieldname: "fiscal_year",

View File

@@ -16,18 +16,21 @@
from __future__ import unicode_literals
import webnotes
import calendar
from webnotes import _, msgprint
from webnotes.utils import flt
import time
from accounts.utils import get_fiscal_year
from controllers.trends import get_period_date_ranges, get_period_month_ranges
def execute(filters=None):
if not filters: filters = {}
columns = get_columns(filters)
period_month_ranges = get_period_month_ranges(filters)
period_month_ranges = get_period_month_ranges(filters["period"], filters["fiscal_year"])
tim_map = get_territory_item_month_map(filters)
precision = webnotes.conn.get_value("Global Defaults", None, "float_precision") or 2
data = []
for territory, territory_items in tim_map.items():
@@ -39,7 +42,7 @@ def execute(filters=None):
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))
value = flt(month_data.get(fieldname), precision)
period_data[i] += value
totals[i] += value
period_data[2] = period_data[0] - period_data[1]
@@ -61,7 +64,7 @@ def get_columns(filters):
group_months = False if filters["period"] == "Monthly" else True
for from_date, to_date in get_period_date_ranges(filters):
for from_date, to_date in get_period_date_ranges(filters["period"], filters["fiscal_year"]):
for label in ["Target (%s)", "Achieved (%s)", "Variance (%s)"]:
if group_months:
columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b")))
@@ -70,49 +73,14 @@ def get_columns(filters):
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 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
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)
ifnull(t.distribution_id, '')!='' order by t.name""",
(filters["fiscal_year"]), as_dict=1)
#Get target distribution details of item group
def get_target_distribution_details(filters):
@@ -121,14 +89,14 @@ def get_target_distribution_details(filters):
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""", (filters["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)
start_date, end_date = get_fiscal_year(filters)[1:]
return webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date,
so.territory, MONTHNAME(so.transaction_date) as month_name
@@ -155,28 +123,18 @@ 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_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \
tav_dict.target = flt(td.target_qty)*(tdd[month]["percentage_allocation"]/100)
if ad.month_name == month and 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_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \
tav_dict.target = flt(td.target_amount)*(tdd[month]["percentage_allocation"]/100)
if ad.month_name == month and get_item_group(ad.item_code) == td.item_group \
and ad.territory == td.name:
tav_dict.achieved += ad.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))
return webnotes.conn.get_value("Item", item_name, item_group)

View File

@@ -1,8 +1,8 @@
[
{
"creation": "2013-06-07 15:13:13",
"creation": "2013-06-21 12:15:00",
"docstatus": 0,
"modified": "2013-06-07 15:13:13",
"modified": "2013-06-21 12:15:00",
"modified_by": "Administrator",
"owner": "Administrator"
},
@@ -11,11 +11,11 @@
"is_standard": "Yes",
"name": "__common__",
"ref_doctype": "Sales Order",
"report_name": "Territory Target Variance (Item Group-Wise)",
"report_name": "Territory Target Variance Item Group-Wise",
"report_type": "Script Report"
},
{
"doctype": "Report",
"name": "Territory Target Variance (Item Group-Wise)"
"name": "Territory Target Variance Item Group-Wise"
}
]