mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-07 15:12:51 +00:00
[Report] Completed Budget variance Report and corrections in Territory Target Varinace & Sales Person Target Variance Report
This commit is contained in:
@@ -26,27 +26,27 @@ def execute(filters=None):
|
|||||||
|
|
||||||
columns = get_columns(filters)
|
columns = get_columns(filters)
|
||||||
period_month_ranges = get_period_month_ranges(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 = []
|
data = []
|
||||||
|
|
||||||
# for territory, territory_items in tim_map.items():
|
for cost_center, cost_center_items in cam_map.items():
|
||||||
# for item_group, monthwise_data in territory_items.items():
|
for account, monthwise_data in cost_center_items.items():
|
||||||
# row = [territory, item_group]
|
row = [cost_center, account]
|
||||||
# totals = [0, 0, 0]
|
totals = [0, 0, 0]
|
||||||
# for relevant_months in period_month_ranges:
|
for relevant_months in period_month_ranges:
|
||||||
# period_data = [0, 0, 0]
|
period_data = [0, 0, 0]
|
||||||
# for month in relevant_months:
|
for month in relevant_months:
|
||||||
# month_data = monthwise_data.get(month, {})
|
month_data = monthwise_data.get(month, {})
|
||||||
# for i, fieldname in enumerate(["target", "achieved", "variance"]):
|
for i, fieldname in enumerate(["target", "actual", "variance"]):
|
||||||
# value = flt(month_data.get(fieldname))
|
value = flt(month_data.get(fieldname))
|
||||||
# period_data[i] += value
|
period_data[i] += value
|
||||||
# totals[i] += value
|
totals[i] += value
|
||||||
# period_data[2] = period_data[0] - period_data[1]
|
period_data[2] = period_data[0] - period_data[1]
|
||||||
# row += period_data
|
row += period_data
|
||||||
# totals[2] = totals[0] - totals[1]
|
totals[2] = totals[0] - totals[1]
|
||||||
# row += totals
|
row += totals
|
||||||
# data.append(row)
|
data.append(row)
|
||||||
|
|
||||||
return columns, sorted(data, key=lambda x: (x[0], x[1]))
|
return columns, sorted(data, key=lambda x: (x[0], x[1]))
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ def get_columns(filters):
|
|||||||
msgprint(_("Please specify") + ": " + label,
|
msgprint(_("Please specify") + ": " + label,
|
||||||
raise_exception=True)
|
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
|
group_months = False if filters["period"] == "Monthly" else True
|
||||||
|
|
||||||
@@ -105,70 +105,60 @@ def get_period_month_ranges(filters):
|
|||||||
return period_month_ranges
|
return period_month_ranges
|
||||||
|
|
||||||
|
|
||||||
#Get cost center details
|
#Get cost center & target details
|
||||||
def get_costcenter_details(filters):
|
def get_costcenter_target_details(filters):
|
||||||
return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty,
|
return webnotes.conn.sql("""select cc.name, cc.distribution_id,
|
||||||
td.target_amount, t.distribution_id
|
cc.parent_cost_center, bd.account, bd.budget_allocated
|
||||||
from `tabTerritory` t, `tabTarget Detail` td
|
from `tabCost Center` cc, `tabBudget Detail` bd
|
||||||
where td.parent=t.name and td.fiscal_year=%s and
|
where bd.parent=cc.name and bd.fiscal_year=%s and
|
||||||
ifnull(t.distribution_id, '')!='' order by t.name""" %
|
cc.company_name=%s and ifnull(cc.distribution_id, '')!=''
|
||||||
('%s'), (filters.get("fiscal_year")), as_dict=1)
|
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):
|
def get_target_distribution_details(filters):
|
||||||
target_details = {}
|
target_details = {}
|
||||||
abc = []
|
|
||||||
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
||||||
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
||||||
`tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \
|
`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):
|
bd.fiscal_year=%s""" % ('%s'), (filters.get("fiscal_year")), as_dict=1):
|
||||||
target_details.setdefault(d.month, d)
|
target_details.setdefault(d.month, d)
|
||||||
|
|
||||||
return target_details
|
return target_details
|
||||||
|
|
||||||
#Get achieved details from sales order
|
#Get actual details from gl entry
|
||||||
def get_achieved_details(filters):
|
def get_actual_details(filters):
|
||||||
start_date, end_date = get_year_start_end_date(filters)
|
return webnotes.conn.sql("""select gl.account, gl.debit, gl.credit,
|
||||||
achieved_details = {}
|
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, \
|
def get_costcenter_account_month_map(filters):
|
||||||
MONTHNAME(so.transaction_date) as month_name \
|
costcenter_target_details = get_costcenter_target_details(filters)
|
||||||
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)
|
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:
|
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({
|
.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:
|
for ad in actual_details:
|
||||||
if (filters["target_on"] == "Quantity"):
|
if ad.month_name == month and ad.account == ccd.account \
|
||||||
tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100)
|
and ad.cost_center == ccd.name:
|
||||||
if ad == month and ''.join(get_item_group(achieved_details[month]["item_code"])) == td.item_group:
|
tav_dict.actual += ad.debit - ad.credit
|
||||||
tav_dict.achieved += achieved_details[month]["qty"]
|
|
||||||
|
|
||||||
if (filters["target_on"] == "Amount"):
|
return cam_map
|
||||||
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):
|
def get_year_start_end_date(filters):
|
||||||
return webnotes.conn.sql("""select year_start_date,
|
return webnotes.conn.sql("""select year_start_date,
|
||||||
@@ -176,9 +166,3 @@ def get_year_start_end_date(filters):
|
|||||||
as year_end_date
|
as year_end_date
|
||||||
from `tabFiscal Year`
|
from `tabFiscal Year`
|
||||||
where name=%s""", filters["fiscal_year"])[0]
|
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))
|
|
||||||
@@ -23,7 +23,6 @@ def execute(filters=None):
|
|||||||
columns = get_columns()
|
columns = get_columns()
|
||||||
item_list = get_items(filters)
|
item_list = get_items(filters)
|
||||||
aii_account_map = get_aii_accounts()
|
aii_account_map = get_aii_accounts()
|
||||||
webnotes.errprint(aii_account_map)
|
|
||||||
data = []
|
data = []
|
||||||
for d in item_list:
|
for d in item_list:
|
||||||
expense_head = d.expense_head or aii_account_map.get(d.company)
|
expense_head = d.expense_head or aii_account_map.get(d.company)
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ def get_salesperson_details(filters):
|
|||||||
#Get target distribution details of item group
|
#Get target distribution details of item group
|
||||||
def get_target_distribution_details(filters):
|
def get_target_distribution_details(filters):
|
||||||
target_details = {}
|
target_details = {}
|
||||||
abc = []
|
|
||||||
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
||||||
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
||||||
`tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \
|
`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
|
#Get achieved details from sales order
|
||||||
def get_achieved_details(filters):
|
def get_achieved_details(filters):
|
||||||
start_date, end_date = get_year_start_end_date(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, \
|
return webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date,
|
||||||
MONTHNAME(so.transaction_date) as month_name \
|
st.sales_person, MONTHNAME(so.transaction_date) as month_name
|
||||||
from `tabSales Order Item` soi, `tabSales Order` so \
|
from `tabSales Order Item` soi, `tabSales Order` so, `tabSales Team` st
|
||||||
where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \
|
where soi.parent=so.name and so.docstatus=1 and
|
||||||
so.transaction_date<=%s""" % ('%s', '%s'), \
|
st.parent=so.name and so.transaction_date>=%s and
|
||||||
(start_date, end_date), as_dict=1):
|
so.transaction_date<=%s""" % ('%s', '%s'),
|
||||||
achieved_details.setdefault(d.month_name, d)
|
(start_date, end_date), as_dict=1)
|
||||||
|
|
||||||
return achieved_details
|
|
||||||
|
|
||||||
def get_salesperson_item_month_map(filters):
|
def get_salesperson_item_month_map(filters):
|
||||||
salesperson_details = get_salesperson_details(filters)
|
salesperson_details = get_salesperson_details(filters)
|
||||||
@@ -160,13 +157,15 @@ def get_salesperson_item_month_map(filters):
|
|||||||
for ad in achieved_details:
|
for ad in achieved_details:
|
||||||
if (filters["target_on"] == "Quantity"):
|
if (filters["target_on"] == "Quantity"):
|
||||||
tav_dict.target = sd.target_qty*(tdd[month]["percentage_allocation"]/100)
|
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:
|
if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \
|
||||||
tav_dict.achieved += achieved_details[month]["qty"]
|
and ad.sales_person == sd.name:
|
||||||
|
tav_dict.achieved += ad.qty
|
||||||
|
|
||||||
if (filters["target_on"] == "Amount"):
|
if (filters["target_on"] == "Amount"):
|
||||||
tav_dict.target = sd.target_amount*(tdd[month]["percentage_allocation"]/100)
|
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:
|
if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == sd.item_group \
|
||||||
tav_dict.achieved += achieved_details[month]["amount"]
|
and ad.sales_person == sd.name:
|
||||||
|
tav_dict.achieved += ad.amount
|
||||||
|
|
||||||
return sim_map
|
return sim_map
|
||||||
|
|
||||||
|
|||||||
@@ -117,11 +117,11 @@ def get_territory_details(filters):
|
|||||||
#Get target distribution details of item group
|
#Get target distribution details of item group
|
||||||
def get_target_distribution_details(filters):
|
def get_target_distribution_details(filters):
|
||||||
target_details = {}
|
target_details = {}
|
||||||
abc = []
|
|
||||||
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
for d in webnotes.conn.sql("""select bdd.month, bdd.percentage_allocation \
|
||||||
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd, \
|
||||||
`tabTerritory` t where bdd.parent=bd.name and t.distribution_id=bd.name and \
|
`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)
|
target_details.setdefault(d.month, d)
|
||||||
|
|
||||||
return target_details
|
return target_details
|
||||||
@@ -129,17 +129,13 @@ def get_target_distribution_details(filters):
|
|||||||
#Get achieved details from sales order
|
#Get achieved details from sales order
|
||||||
def get_achieved_details(filters):
|
def get_achieved_details(filters):
|
||||||
start_date, end_date = get_year_start_end_date(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, \
|
return webnotes.conn.sql("""select soi.item_code, soi.qty, soi.amount, so.transaction_date,
|
||||||
MONTHNAME(so.transaction_date) as month_name \
|
so.territory, MONTHNAME(so.transaction_date) as month_name
|
||||||
from `tabSales Order Item` soi, `tabSales Order` so \
|
from `tabSales Order Item` soi, `tabSales Order` so
|
||||||
where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and \
|
where soi.parent=so.name and so.docstatus=1 and so.transaction_date>=%s and
|
||||||
so.transaction_date<=%s""" % ('%s', '%s'), \
|
so.transaction_date<=%s""" % ('%s', '%s'),
|
||||||
(start_date, end_date), as_dict=1):
|
(start_date, end_date), as_dict=1)
|
||||||
achieved_details.setdefault(d.month_name, d)
|
|
||||||
|
|
||||||
return achieved_details
|
|
||||||
|
|
||||||
def get_territory_item_month_map(filters):
|
def get_territory_item_month_map(filters):
|
||||||
territory_details = get_territory_details(filters)
|
territory_details = get_territory_details(filters)
|
||||||
@@ -160,13 +156,15 @@ def get_territory_item_month_map(filters):
|
|||||||
for ad in achieved_details:
|
for ad in achieved_details:
|
||||||
if (filters["target_on"] == "Quantity"):
|
if (filters["target_on"] == "Quantity"):
|
||||||
tav_dict.target = td.target_qty*(tdd[month]["percentage_allocation"]/100)
|
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:
|
if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \
|
||||||
tav_dict.achieved += achieved_details[month]["qty"]
|
and ad.territory == td.name:
|
||||||
|
tav_dict.achieved += ad.qty
|
||||||
|
|
||||||
if (filters["target_on"] == "Amount"):
|
if (filters["target_on"] == "Amount"):
|
||||||
tav_dict.target = td.target_amount*(tdd[month]["percentage_allocation"]/100)
|
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:
|
if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \
|
||||||
tav_dict.achieved += achieved_details[month]["amount"]
|
and ad.territory == td.name:
|
||||||
|
tav_dict.achieved += ad.amount
|
||||||
|
|
||||||
return tim_map
|
return tim_map
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user