Merge pull request #517 from akhileshdarjee/master

[Report] Cleanup of Territory Target Variance, Sales Person Target Variance, Budget Variance Report & Item Prices
diff --git a/accounts/report/budget_variance_report/budget_variance_report.py b/accounts/report/budget_variance_report/budget_variance_report.py
index 99e303b..42bc663 100644
--- a/accounts/report/budget_variance_report/budget_variance_report.py
+++ b/accounts/report/budget_variance_report/budget_variance_report.py
@@ -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 @@
 				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 @@
 
 	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 @@
 
 	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 @@
 	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 @@
 		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]
\ No newline at end of file
+	return cam_map
\ No newline at end of file
diff --git a/stock/doctype/sales_bom/__init__.py b/selling/doctype/sales_bom/__init__.py
similarity index 100%
rename from stock/doctype/sales_bom/__init__.py
rename to selling/doctype/sales_bom/__init__.py
diff --git a/stock/doctype/sales_bom/locale/_messages_doc.json b/selling/doctype/sales_bom/locale/_messages_doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/_messages_doc.json
rename to selling/doctype/sales_bom/locale/_messages_doc.json
diff --git a/stock/doctype/sales_bom/locale/ar-doc.json b/selling/doctype/sales_bom/locale/ar-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/ar-doc.json
rename to selling/doctype/sales_bom/locale/ar-doc.json
diff --git a/stock/doctype/sales_bom/locale/de-doc.json b/selling/doctype/sales_bom/locale/de-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/de-doc.json
rename to selling/doctype/sales_bom/locale/de-doc.json
diff --git a/stock/doctype/sales_bom/locale/es-doc.json b/selling/doctype/sales_bom/locale/es-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/es-doc.json
rename to selling/doctype/sales_bom/locale/es-doc.json
diff --git a/stock/doctype/sales_bom/locale/fr-doc.json b/selling/doctype/sales_bom/locale/fr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/fr-doc.json
rename to selling/doctype/sales_bom/locale/fr-doc.json
diff --git a/stock/doctype/sales_bom/locale/hi-doc.json b/selling/doctype/sales_bom/locale/hi-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/hi-doc.json
rename to selling/doctype/sales_bom/locale/hi-doc.json
diff --git a/stock/doctype/sales_bom/locale/hr-doc.json b/selling/doctype/sales_bom/locale/hr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/hr-doc.json
rename to selling/doctype/sales_bom/locale/hr-doc.json
diff --git a/stock/doctype/sales_bom/locale/nl-doc.json b/selling/doctype/sales_bom/locale/nl-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/nl-doc.json
rename to selling/doctype/sales_bom/locale/nl-doc.json
diff --git a/stock/doctype/sales_bom/locale/pt-BR-doc.json b/selling/doctype/sales_bom/locale/pt-BR-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/pt-BR-doc.json
rename to selling/doctype/sales_bom/locale/pt-BR-doc.json
diff --git a/stock/doctype/sales_bom/locale/pt-doc.json b/selling/doctype/sales_bom/locale/pt-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/pt-doc.json
rename to selling/doctype/sales_bom/locale/pt-doc.json
diff --git a/stock/doctype/sales_bom/locale/sr-doc.json b/selling/doctype/sales_bom/locale/sr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/sr-doc.json
rename to selling/doctype/sales_bom/locale/sr-doc.json
diff --git a/stock/doctype/sales_bom/locale/ta-doc.json b/selling/doctype/sales_bom/locale/ta-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/ta-doc.json
rename to selling/doctype/sales_bom/locale/ta-doc.json
diff --git a/stock/doctype/sales_bom/locale/th-doc.json b/selling/doctype/sales_bom/locale/th-doc.json
similarity index 100%
rename from stock/doctype/sales_bom/locale/th-doc.json
rename to selling/doctype/sales_bom/locale/th-doc.json
diff --git a/stock/doctype/sales_bom/sales_bom.js b/selling/doctype/sales_bom/sales_bom.js
similarity index 100%
rename from stock/doctype/sales_bom/sales_bom.js
rename to selling/doctype/sales_bom/sales_bom.js
diff --git a/stock/doctype/sales_bom/sales_bom.py b/selling/doctype/sales_bom/sales_bom.py
similarity index 100%
rename from stock/doctype/sales_bom/sales_bom.py
rename to selling/doctype/sales_bom/sales_bom.py
diff --git a/stock/doctype/sales_bom/sales_bom.txt b/selling/doctype/sales_bom/sales_bom.txt
similarity index 100%
rename from stock/doctype/sales_bom/sales_bom.txt
rename to selling/doctype/sales_bom/sales_bom.txt
diff --git a/stock/doctype/sales_bom/test_sales_bom.py b/selling/doctype/sales_bom/test_sales_bom.py
similarity index 100%
rename from stock/doctype/sales_bom/test_sales_bom.py
rename to selling/doctype/sales_bom/test_sales_bom.py
diff --git a/stock/doctype/sales_bom_item/__init__.py b/selling/doctype/sales_bom_item/__init__.py
similarity index 100%
rename from stock/doctype/sales_bom_item/__init__.py
rename to selling/doctype/sales_bom_item/__init__.py
diff --git a/stock/doctype/sales_bom_item/locale/_messages_doc.json b/selling/doctype/sales_bom_item/locale/_messages_doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/_messages_doc.json
rename to selling/doctype/sales_bom_item/locale/_messages_doc.json
diff --git a/stock/doctype/sales_bom_item/locale/ar-doc.json b/selling/doctype/sales_bom_item/locale/ar-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/ar-doc.json
rename to selling/doctype/sales_bom_item/locale/ar-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/de-doc.json b/selling/doctype/sales_bom_item/locale/de-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/de-doc.json
rename to selling/doctype/sales_bom_item/locale/de-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/es-doc.json b/selling/doctype/sales_bom_item/locale/es-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/es-doc.json
rename to selling/doctype/sales_bom_item/locale/es-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/fr-doc.json b/selling/doctype/sales_bom_item/locale/fr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/fr-doc.json
rename to selling/doctype/sales_bom_item/locale/fr-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/hi-doc.json b/selling/doctype/sales_bom_item/locale/hi-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/hi-doc.json
rename to selling/doctype/sales_bom_item/locale/hi-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/hr-doc.json b/selling/doctype/sales_bom_item/locale/hr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/hr-doc.json
rename to selling/doctype/sales_bom_item/locale/hr-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/nl-doc.json b/selling/doctype/sales_bom_item/locale/nl-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/nl-doc.json
rename to selling/doctype/sales_bom_item/locale/nl-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/pt-BR-doc.json b/selling/doctype/sales_bom_item/locale/pt-BR-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/pt-BR-doc.json
rename to selling/doctype/sales_bom_item/locale/pt-BR-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/pt-doc.json b/selling/doctype/sales_bom_item/locale/pt-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/pt-doc.json
rename to selling/doctype/sales_bom_item/locale/pt-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/sr-doc.json b/selling/doctype/sales_bom_item/locale/sr-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/sr-doc.json
rename to selling/doctype/sales_bom_item/locale/sr-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/ta-doc.json b/selling/doctype/sales_bom_item/locale/ta-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/ta-doc.json
rename to selling/doctype/sales_bom_item/locale/ta-doc.json
diff --git a/stock/doctype/sales_bom_item/locale/th-doc.json b/selling/doctype/sales_bom_item/locale/th-doc.json
similarity index 100%
rename from stock/doctype/sales_bom_item/locale/th-doc.json
rename to selling/doctype/sales_bom_item/locale/th-doc.json
diff --git a/stock/doctype/sales_bom_item/sales_bom_item.py b/selling/doctype/sales_bom_item/sales_bom_item.py
similarity index 100%
rename from stock/doctype/sales_bom_item/sales_bom_item.py
rename to selling/doctype/sales_bom_item/sales_bom_item.py
diff --git a/stock/doctype/sales_bom_item/sales_bom_item.txt b/selling/doctype/sales_bom_item/sales_bom_item.txt
similarity index 100%
rename from stock/doctype/sales_bom_item/sales_bom_item.txt
rename to selling/doctype/sales_bom_item/sales_bom_item.txt
diff --git a/selling/locale/_messages_js.json b/selling/locale/_messages_js.json
new file mode 100644
index 0000000..0637a08
--- /dev/null
+++ b/selling/locale/_messages_js.json
@@ -0,0 +1 @@
+[]
\ No newline at end of file
diff --git a/selling/locale/_messages_py.json b/selling/locale/_messages_py.json
new file mode 100644
index 0000000..f5b4a01
--- /dev/null
+++ b/selling/locale/_messages_py.json
@@ -0,0 +1,7 @@
+[
+ "is not a Stock Item", 
+ "Item", 
+ "Please check", 
+ "reached its end of life on", 
+ "is a cancelled Item"
+]
\ No newline at end of file
diff --git a/selling/locale/ar-py.json b/selling/locale/ar-py.json
new file mode 100644
index 0000000..b8dd151
--- /dev/null
+++ b/selling/locale/ar-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/de-py.json b/selling/locale/de-py.json
new file mode 100644
index 0000000..68b58d7
--- /dev/null
+++ b/selling/locale/de-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/es-py.json b/selling/locale/es-py.json
new file mode 100644
index 0000000..7d145d1
--- /dev/null
+++ b/selling/locale/es-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/fr-py.json b/selling/locale/fr-py.json
new file mode 100644
index 0000000..c466df4
--- /dev/null
+++ b/selling/locale/fr-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/hi-py.json b/selling/locale/hi-py.json
new file mode 100644
index 0000000..58d4e65
--- /dev/null
+++ b/selling/locale/hi-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/hr-py.json b/selling/locale/hr-py.json
new file mode 100644
index 0000000..24f60bb
--- /dev/null
+++ b/selling/locale/hr-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/nl-py.json b/selling/locale/nl-py.json
new file mode 100644
index 0000000..7964d37
--- /dev/null
+++ b/selling/locale/nl-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/pt-BR-py.json b/selling/locale/pt-BR-py.json
new file mode 100644
index 0000000..0bf350c
--- /dev/null
+++ b/selling/locale/pt-BR-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/pt-py.json b/selling/locale/pt-py.json
new file mode 100644
index 0000000..cfcbcec
--- /dev/null
+++ b/selling/locale/pt-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/sr-py.json b/selling/locale/sr-py.json
new file mode 100644
index 0000000..ed1672f
--- /dev/null
+++ b/selling/locale/sr-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/ta-py.json b/selling/locale/ta-py.json
new file mode 100644
index 0000000..355cc0e
--- /dev/null
+++ b/selling/locale/ta-py.json
@@ -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"
+}
\ No newline at end of file
diff --git a/selling/locale/th-py.json b/selling/locale/th-py.json
new file mode 100644
index 0000000..5f31a59
--- /dev/null
+++ b/selling/locale/th-py.json
@@ -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"
+}
\ 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 73cc718..90f8a94 100644
--- a/selling/page/selling_home/selling_home.js
+++ b/selling/page/selling_home/selling_home.js
@@ -162,13 +162,11 @@
 			},
 			{
 				"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"),
@@ -186,6 +184,10 @@
 				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"
 			},
diff --git "a/selling/report/sales_person_target_variance_\050item_group_wise\051/__init__.py" b/selling/report/available_stock_for_packing_items/__init__.py
similarity index 100%
copy from "selling/report/sales_person_target_variance_\050item_group_wise\051/__init__.py"
copy to selling/report/available_stock_for_packing_items/__init__.py
diff --git a/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.py b/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.py
new file mode 100644
index 0000000..d766c44
--- /dev/null
+++ b/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.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 <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
\ No newline at end of file
diff --git a/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.txt b/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.txt
new file mode 100644
index 0000000..5cf4133
--- /dev/null
+++ b/selling/report/available_stock_for_packing_items/available_stock_for_packing_items.txt
@@ -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"
+ }
+]
\ No newline at end of file
diff --git "a/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.js" "b/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.js"
deleted file mode 100644
index 09f0d55..0000000
--- "a/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.js"
+++ /dev/null
@@ -1,25 +0,0 @@
-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_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.txt" "b/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.txt"
deleted file mode 100644
index 955cdec..0000000
--- "a/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.txt"
+++ /dev/null
@@ -1,21 +0,0 @@
-[
- {
-  "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/sales_person_target_variance_\050item_group_wise\051/__init__.py" b/selling/report/sales_person_target_variance_item_group_wise/__init__.py
similarity index 100%
rename from "selling/report/sales_person_target_variance_\050item_group_wise\051/__init__.py"
rename to selling/report/sales_person_target_variance_item_group_wise/__init__.py
diff --git "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js" b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.js
similarity index 86%
copy from "selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js"
copy to selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.js
index 58718a4..c7a5d66 100644
--- "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js"
+++ b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.js
@@ -1,4 +1,4 @@
-wn.query_reports["Territory Target Variance (Item Group-Wise)"] = {
+wn.query_reports["Sales Person Target Variance Item Group-Wise"] = {
 	"filters": [
 		{
 			fieldname: "fiscal_year",
diff --git "a/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.py" b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
similarity index 66%
rename from "selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.py"
rename to selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
index 8f5931d..ae3819c 100644
--- "a/selling/report/sales_person_target_variance_\050item_group_wise\051/sales_person_target_variance_\050item_group_wise\051.py"
+++ b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
@@ -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 @@
 				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 @@
 
 	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 @@
 
 	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 @@
 	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 @@
 		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))
\ No newline at end of file
+	return webnotes.conn.get_value("Item", item_name, "item_group")
\ 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 0000000..e587c2c
--- /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-21 12:14:15", 
+  "docstatus": 0, 
+  "modified": "2013-06-21 12:14:15", 
+  "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_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.txt" "b/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.txt"
deleted file mode 100644
index 7fff64a..0000000
--- "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.txt"
+++ /dev/null
@@ -1,21 +0,0 @@
-[
- {
-  "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
diff --git "a/selling/report/territory_target_variance_\050item_group_wise\051/__init__.py" b/selling/report/territory_target_variance_item_group_wise/__init__.py
similarity index 100%
rename from "selling/report/territory_target_variance_\050item_group_wise\051/__init__.py"
rename to selling/report/territory_target_variance_item_group_wise/__init__.py
diff --git "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js" b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.js
similarity index 86%
rename from "selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js"
rename to selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.js
index 58718a4..146b17d 100644
--- "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.js"
+++ b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.js
@@ -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",
diff --git "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.py" b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
similarity index 77%
rename from "selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.py"
rename to selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
index 022d19e..109acbd 100644
--- "a/selling/report/territory_target_variance_\050item_group_wise\051/territory_target_variance_\050item_group_wise\051.py"
+++ b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
@@ -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 @@
 				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 @@
 
 	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 @@
 
 	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 @@
 		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 @@
 	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 @@
 
 			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
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 0000000..6e16b64
--- /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-21 12:15:00", 
+  "docstatus": 0, 
+  "modified": "2013-06-21 12:15:00", 
+  "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
diff --git a/stock/report/item_prices/item_prices.py b/stock/report/item_prices/item_prices.py
index 3bdb746..fb69086 100644
--- a/stock/report/item_prices/item_prices.py
+++ b/stock/report/item_prices/item_prices.py
@@ -24,16 +24,22 @@
 	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 @@
 	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 @@
 	
 	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
\ No newline at end of file
+	return item_val_rate_map