Merge pull request #517 from akhileshdarjee/master

[Report] Cleanup of Territory Target Variance, Sales Person Target Variance, Budget Variance Report & Item Prices
This commit is contained in:
Nabin Hait 2013-06-25 02:53:58 -07:00
commit df6ad717b2
62 changed files with 311 additions and 175 deletions

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"])
cam_map = get_costcenter_account_month_map(filters)
precision = webnotes.conn.get_value("Global Defaults", None, "float_precision") or 2
data = []
for cost_center, cost_center_items in cam_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", "actual", "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)", "Actual (%s)", "Variance (%s)"]:
if group_months:
columns.append(label % (from_date.strftime("%b") + " - " + to_date.strftime("%b")))
@ -70,41 +73,6 @@ def get_columns(filters):
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 & target details
def get_costcenter_target_details(filters):
return webnotes.conn.sql("""select cc.name, cc.distribution_id,
@ -122,7 +90,7 @@ 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, \
`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""", (filters["fiscal_year"]), as_dict=1):
target_details.setdefault(d.month, d)
return target_details
@ -147,22 +115,16 @@ def get_costcenter_account_month_map(filters):
for month in tdd:
cam_map.setdefault(ccd.name, {}).setdefault(ccd.account, {})\
.setdefault(month, webnotes._dict({
"target": 0.0, "actual": 0.0, "variance": 0.0
"target": 0.0, "actual": 0.0
}))
tav_dict = cam_map[ccd.name][ccd.account][month]
tav_dict.target = ccd.budget_allocated*(tdd[month]["percentage_allocation"]/100)
tav_dict.target = flt(ccd.budget_allocated) * \
(tdd[month]["percentage_allocation"]/100)
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]
return cam_map

View File

@ -0,0 +1 @@
[]

View File

@ -0,0 +1,7 @@
[
"is not a Stock Item",
"Item",
"Please check",
"reached its end of life on",
"is a cancelled Item"
]

View File

@ -0,0 +1,7 @@
{
"Item": "\u0628\u0646\u062f",
"Please check": "\u064a\u0631\u062c\u0649 \u0645\u0631\u0627\u062c\u0639\u0629",
"is a cancelled Item": "\u0647\u0648 \u0628\u0646\u062f \u0625\u0644\u063a\u0627\u0621",
"is not a Stock Item": "\u0644\u064a\u0633 \u0627\u0644\u0625\u063a\u0644\u0627\u0642 \u0644\u0644\u0633\u0647\u0645",
"reached its end of life on": "\u0648\u0635\u0644 \u0625\u0644\u0649 \u0646\u0647\u0627\u064a\u062a\u0647 \u0645\u0646 \u0627\u0644\u062d\u064a\u0627\u0629 \u0639\u0644\u0649"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Artikel",
"Please check": "Bitte \u00fcberpr\u00fcfen Sie",
"is a cancelled Item": "ist ein gestempeltes",
"is not a Stock Item": "ist kein Lagerartikel",
"reached its end of life on": "erreichte Ende des Lebens auf"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Art\u00edculo",
"Please check": "Por favor, compruebe",
"is a cancelled Item": "Es un Tema cancelado",
"is not a Stock Item": "no es un elemento de serie",
"reached its end of life on": "llegado al final de su vida en la"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Article",
"Please check": "S&#39;il vous pla\u00eet v\u00e9rifier",
"is a cancelled Item": "est un \u00e9l\u00e9ment annul\u00e9e",
"is not a Stock Item": "n&#39;est pas un \u00e9l\u00e9ment de Stock",
"reached its end of life on": "atteint la fin de sa vie sur"
}

View File

@ -0,0 +1,7 @@
{
"Item": "\u092e\u0926",
"Please check": "\u0915\u0943\u092a\u092f\u093e \u091c\u093e\u0901\u091a \u0915\u0930\u0947\u0902",
"is a cancelled Item": "\u0930\u0926\u094d\u0926 \u0906\u0907\u091f\u092e",
"is not a Stock Item": "\u0938\u094d\u091f\u0949\u0915 \u0906\u0907\u091f\u092e \u0928\u0939\u0940\u0902 \u0939\u0948",
"reached its end of life on": "\u092a\u0930 \u0905\u092a\u0928\u0947 \u091c\u0940\u0935\u0928 \u0915\u0947 \u0905\u0902\u0924 \u0924\u0915 \u092a\u0939\u0941\u0901\u091a"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Stavka",
"Please check": "Molimo provjerite",
"is a cancelled Item": "je otkazan artikla",
"is not a Stock Item": "nije katalo\u0161ki artikla",
"reached its end of life on": "dosegla svoj kraj \u017eivota na"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Item",
"Please check": "Controleer",
"is a cancelled Item": "is een geannuleerde artikel",
"is not a Stock Item": "is niet een Stock Item",
"reached its end of life on": "het einde van zijn leven op"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Item",
"Please check": "Por favor, verifique",
"is a cancelled Item": "\u00e9 um Item cancelado",
"is not a Stock Item": "n\u00e3o \u00e9 um Item de Estoque",
"reached its end of life on": "chegou ao fim de vida em"
}

View File

@ -0,0 +1,7 @@
{
"Item": "Item",
"Please check": "Por favor, verifique",
"is a cancelled Item": "\u00e9 um item cancelado",
"is not a Stock Item": "n\u00e3o \u00e9 um item de estoque",
"reached its end of life on": "chegou ao fim da vida na"
}

View File

@ -0,0 +1,7 @@
{
"Item": "\u0421\u0442\u0430\u0432\u043a\u0430",
"Please check": "\u041c\u043e\u043b\u0438\u043c\u043e \u0432\u0430\u0441 \u0434\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u0435",
"is a cancelled Item": "\u0458\u0435 \u043e\u0442\u043a\u0430\u0437\u0430\u043d\u0430 \u0448\u0438\u0444\u0440\u0430",
"is not a Stock Item": "\u043d\u0438\u0458\u0435 \u0431\u0435\u0440\u0437\u0430 \u0448\u0438\u0444\u0440\u0430",
"reached its end of life on": "\u0434\u043e\u0441\u0442\u0438\u0433\u0430\u043e \u0441\u0432\u043e\u0458 \u043a\u0440\u0430\u0458 \u0436\u0438\u0432\u043e\u0442\u0430 \u043d\u0430"
}

View File

@ -0,0 +1,7 @@
{
"Item": "\u0b89\u0bb0\u0bc1\u0baa\u0bcd\u0baa\u0b9f\u0bbf",
"Please check": "\u0b9a\u0bb0\u0bbf\u0baa\u0bbe\u0bb0\u0bcd\u0b95\u0bcd\u0b95\u0bb5\u0bc1\u0bae\u0bcd",
"is a cancelled Item": "\u0b92\u0bb0\u0bc1 \u0bb0\u0ba4\u0bcd\u0ba4\u0bc1 \u0b89\u0bb0\u0bc1\u0baa\u0bcd\u0baa\u0b9f\u0bbf \u0b89\u0bb3\u0bcd\u0bb3\u0ba4\u0bc1",
"is not a Stock Item": "\u0b92\u0bb0\u0bc1 \u0baa\u0b99\u0bcd\u0b95\u0bc1 \u0baa\u0bc6\u0bbe\u0bb0\u0bc1\u0bb3\u0bcd \u0b85\u0bb2\u0bcd\u0bb2",
"reached its end of life on": "\u0bb5\u0bbe\u0bb4\u0bcd\u0b95\u0bcd\u0b95\u0bc8 \u0b85\u0ba4\u0ba9\u0bcd \u0b87\u0bb1\u0bc1\u0ba4\u0bbf\u0baf\u0bbf\u0bb2\u0bcd \u0b85\u0b9f\u0bc8\u0ba8\u0bcd\u0ba4\u0ba4\u0bc1"
}

View File

@ -0,0 +1,7 @@
{
"Item": "\u0e0a\u0e34\u0e49\u0e19",
"Please check": "\u0e01\u0e23\u0e38\u0e13\u0e32\u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a",
"is a cancelled Item": "\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e22\u0e01\u0e40\u0e25\u0e34\u0e01\u0e40\u0e1b\u0e47\u0e19",
"is not a Stock Item": "\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49\u0e40\u0e1b\u0e47\u0e19\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e2a\u0e15\u0e47\u0e2d\u0e01",
"reached its end of life on": "\u0e16\u0e36\u0e07\u0e08\u0e38\u0e14\u0e2a\u0e34\u0e49\u0e19\u0e2a\u0e38\u0e14\u0e02\u0e2d\u0e07\u0e0a\u0e35\u0e27\u0e34\u0e15\u0e40\u0e21\u0e37\u0e48\u0e2d"
}

View File

@ -162,13 +162,11 @@ wn.module_page["Selling"] = [
},
{
"label":wn._("Territory Target Variance (Item Group-Wise)"),
route: "query-report/Territory Target Variance (Item Group-Wise)",
doctype: "Sales Order"
route: "query-report/Territory Target Variance Item Group-Wise"
},
{
"label":wn._("Sales Person Target Variance (Item Group-Wise)"),
route: "query-report/Sales Person Target Variance (Item Group-Wise)",
doctype: "Sales Order"
route: "query-report/Sales Person Target Variance Item Group-Wise"
},
{
"label":wn._("Customers Not Buying Since Long Time"),
@ -185,6 +183,10 @@ wn.module_page["Selling"] = [
route: "query-report/Sales Order Trends",
doctype: "Sales Order"
},
{
"label":wn._("Available Stock for Packing Items"),
route: "query-report/Available Stock for Packing Items",
},
{
"label":wn._("Pending SO Items For Purchase Request"),
route: "query-report/Pending SO Items For Purchase Request"

View File

@ -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 <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import webnotes
from webnotes.utils import cint
def execute(filters=None):
if not filters: filters = {}
columns = get_columns()
item_warehouse_quantity_map = get_item_warehouse_quantity_map()
data = []
for item, warehouse in item_warehouse_quantity_map.items():
item_details = get_item_details(item)[0]
total = 0
total_qty = 0
for wh, item_qty in warehouse.items():
total += 1
row = [item, item_details.item_name, item_details.description, \
item_details.stock_uom, wh]
for quantity in item_qty.items():
max_qty = []
max_qty.append(quantity[1])
max_qty = min(max_qty)
total_qty += cint(max_qty.qty)
row += [max_qty.qty]
data.append(row)
if (total == len(warehouse)):
row = ["", "", "Total", "", "", total_qty]
data.append(row)
return columns, data
def get_columns():
columns = ["Item Code:Link/Item:100", "Item Name::100", "Description::120", \
"UOM:Link/UOM:80", "Warehouse:Link/Warehouse:100", "Quantity::100"]
return columns
def get_sales_bom():
return webnotes.conn.sql("""select name from `tabSales BOM`""", as_dict=1)
def get_sales_bom_items(item):
return webnotes.conn.sql("""select parent, item_code, qty from `tabSales BOM Item`
where parent=%s""", (item), as_dict=1)
def get_item_details(item):
return webnotes.conn.sql("""select name, item_name, description, stock_uom
from `tabItem` where name=%s""", (item), as_dict=1)
def get_item_warehouse_quantity(item):
return webnotes.conn.sql("""select item_code, warehouse, actual_qty from `tabBin`
where item_code=%s""", (item), as_dict=1)
def get_item_warehouse_quantity_map():
iwq_map = {}
sales_bom = get_sales_bom()
for item in sales_bom:
child_item = get_sales_bom_items(item.name)
for child in child_item:
item_warehouse_quantity = get_item_warehouse_quantity(child.item_code)
for iwq in item_warehouse_quantity:
iwq_map.setdefault(item.name, {}).setdefault(iwq.warehouse, {}).\
setdefault(child.item_code, webnotes._dict({
"qty" : 0
}))
q_dict = iwq_map[item.name][iwq.warehouse][child.item_code]
q_dict.qty = cint(iwq.actual_qty / child.qty)
return iwq_map

View File

@ -0,0 +1,21 @@
[
{
"creation": "2013-06-21 13:40:05",
"docstatus": 0,
"modified": "2013-06-21 15:06:40",
"modified_by": "Administrator",
"owner": "Administrator"
},
{
"doctype": "Report",
"is_standard": "Yes",
"name": "__common__",
"ref_doctype": "Sales BOM",
"report_name": "Available Stock for Packing Items",
"report_type": "Script Report"
},
{
"doctype": "Report",
"name": "Available Stock for Packing Items"
}
]

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(fiscal_year = filters["fiscal_year"])[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
@ -149,35 +117,27 @@ def get_salesperson_item_month_map(filters):
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
"target": 0.0, "achieved": 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_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,19 +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():
@ -40,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 +63,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,41 +72,6 @@ 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_fiscal_year(fiscal_year = filters["fiscal_year"])[1:]
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,
@ -112,7 +79,7 @@ def get_territory_details(filters):
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""",
filters.get("fiscal_year"), as_dict=1)
(filters["fiscal_year"]), as_dict=1)
#Get target distribution details of item group
def get_target_distribution_details(filters):
@ -121,7 +88,7 @@ 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
@ -155,7 +122,8 @@ def get_territory_item_month_map(filters):
for ad in achieved_details:
if (filters["target_on"] == "Quantity"):
tav_dict.target = flt(td.target_qty) * (tdd[month]["percentage_allocation"]/100)
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

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"
}
]

View File

@ -24,16 +24,22 @@ def execute(filters=None):
columns = get_columns(filters)
item_map = get_item_details()
pl = get_price_list()
last_purchase_rate = get_last_purchase_rate()
bom_rate = get_item_bom_rate()
val_rate_map = get_valuation_rate()
precision = webnotes.conn.get_value("Global Defaults", None, "float_precision") or 2
data = []
for item in sorted(item_map):
data.append([item, item_map[item]["item_name"],
item_map[item]["description"], item_map[item]["stock_uom"],
flt(item_map[item]["last_purchase_rate"]), val_rate_map.get(item, 0),
pl.get(item, {}).get("selling"), pl.get(item, {}).get("buying"),
bom_rate.get(item, 0), flt(item_map[item]["standard_rate"])
flt(last_purchase_rate.get(item, 0), precision),
flt(val_rate_map.get(item, 0), precision),
pl.get(item, {}).get("selling"),
pl.get(item, {}).get("buying"),
flt(bom_rate.get(item, 0), precision),
flt(item_map[item]["standard_rate"], precision)
])
return columns, data
@ -53,7 +59,7 @@ def get_item_details():
item_map = {}
for i in webnotes.conn.sql("select name, item_name, description, \
stock_uom, standard_rate, last_purchase_rate from tabItem \
stock_uom, standard_rate from tabItem \
order by item_code", as_dict=1):
item_map.setdefault(i.name, i)
@ -84,25 +90,61 @@ def get_price_list():
return item_rate_map
def get_last_purchase_rate():
item_last_purchase_rate_map = {}
query = """select * from (select
result.item_code,
result.purchase_rate
from (
(select
po_item.item_code,
po_item.item_name,
po.transaction_date as posting_date,
po_item.purchase_ref_rate,
po_item.discount_rate,
po_item.purchase_rate
from `tabPurchase Order` po, `tabPurchase Order Item` po_item
where po.name = po_item.parent and po.docstatus = 1)
union
(select
pr_item.item_code,
pr_item.item_name,
pr.posting_date,
pr_item.purchase_ref_rate,
pr_item.discount_rate,
pr_item.purchase_rate
from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
where pr.name = pr_item.parent and pr.docstatus = 1)
) result
order by result.item_code asc, result.posting_date desc) result_wrapper
group by item_code"""
for d in webnotes.conn.sql(query, as_dict=1):
item_last_purchase_rate_map.setdefault(d.item_code, d.purchase_rate)
return item_last_purchase_rate_map
def get_item_bom_rate():
"""Get BOM rate of an item from BOM"""
bom_map = {}
item_bom_map = {}
for b in webnotes.conn.sql("""select item, (total_cost/quantity) as bom_rate
from `tabBOM` where is_active=1 and is_default=1""", as_dict=1):
bom_map.setdefault(b.item, flt(b.bom_rate))
item_bom_map.setdefault(b.item, flt(b.bom_rate))
return bom_map
return item_bom_map
def get_valuation_rate():
"""Get an average valuation rate of an item from all warehouses"""
val_rate_map = {}
item_val_rate_map = {}
for d in webnotes.conn.sql("""select item_code,
sum(actual_qty*valuation_rate)/sum(actual_qty) as val_rate
from tabBin where actual_qty > 0 group by item_code""", as_dict=1):
val_rate_map.setdefault(d.item_code, d.val_rate)
item_val_rate_map.setdefault(d.item_code, d.val_rate)
return val_rate_map
return item_val_rate_map