Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/report/budget_variance_report/budget_variance_report.py b/accounts/report/budget_variance_report/budget_variance_report.py
index 158ff3a..015e2c0 100644
--- a/accounts/report/budget_variance_report/budget_variance_report.py
+++ b/accounts/report/budget_variance_report/budget_variance_report.py
@@ -47,18 +47,20 @@
 			msgprint(_("Please specify") + ": " + label,
 				raise_exception=True)
 
-	columns = ["Cost Center:Link/Cost Center:100", "Account:Link/Account:100"]
+	columns = ["Cost Center:Link/Cost Center:120", "Account:Link/Account:120"]
 
 	group_months = False if filters["period"] == "Monthly" else True
 
 	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")))				
+				label = label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))
 			else:
-				columns.append(label % from_date.strftime("%b"))
+				label = label % from_date.strftime("%b")
+				
+			columns.append(label+":Float:120")
 
-	return columns + ["Total Target::80", "Total Actual::80", "Total Variance::80"]
+	return columns + ["Total Target::120", "Total Actual::120", "Total Variance::120"]
 
 #Get cost center & target details
 def get_costcenter_target_details(filters):
@@ -66,19 +68,17 @@
 		cc.parent_cost_center, bd.account, bd.budget_allocated 
 		from `tabCost Center` cc, `tabBudget Detail` bd 
 		where bd.parent=cc.name and bd.fiscal_year=%s and 
-		cc.company=%s and ifnull(cc.distribution_id, '')!='' 
-		order by cc.name""" % ('%s', '%s'), 
+		cc.company=%s order by cc.name""" % ('%s', '%s'), 
 		(filters.get("fiscal_year"), filters.get("company")), as_dict=1)
 
 #Get target distribution details of accounts of cost center
 def get_target_distribution_details(filters):
 	target_details = {}
 
-	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""", (filters["fiscal_year"]), as_dict=1):
-			target_details.setdefault(d.month, d)
+	for d in webnotes.conn.sql("""select bd.name, bdd.month, bdd.percentage_allocation \
+		from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd
+		where bdd.parent=bd.name and bd.fiscal_year=%s""", (filters["fiscal_year"]), as_dict=1):
+			target_details.setdefault(d.name, {}).setdefault(d.month, d.percentage_allocation)
 
 	return target_details
 
@@ -99,15 +99,19 @@
 	cam_map = {}
 
 	for ccd in costcenter_target_details:
-		for month in tdd:
+		for month_id in range(1, 13):
+			month = datetime.date(2013, month_id, 1).strftime('%B')
+			
 			cam_map.setdefault(ccd.name, {}).setdefault(ccd.account, {})\
-			.setdefault(month, webnotes._dict({
-				"target": 0.0, "actual": 0.0
-			}))
+				.setdefault(month, webnotes._dict({
+					"target": 0.0, "actual": 0.0
+				}))
 
 			tav_dict = cam_map[ccd.name][ccd.account][month]
-			tav_dict.target = flt(ccd.budget_allocated) * \
-				(tdd[month]["percentage_allocation"]/100)
+			month_percentage = ccd.distribution_id and \
+				tdd.get(ccd.distribution_id, {}).get(month, 0) or 100.0/12
+				
+			tav_dict.target = flt(flt(ccd.budget_allocated) * month_percentage /100)
 
 			for ad in actual_details:
 				if ad.month_name == month and ad.account == ccd.account \
diff --git a/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
index 96c147f..6fdafe0 100644
--- a/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
+++ b/selling/report/sales_person_target_variance_item_group_wise/sales_person_target_variance_item_group_wise.py
@@ -8,6 +8,7 @@
 import time
 from accounts.utils import get_fiscal_year
 from controllers.trends import get_period_date_ranges, get_period_month_ranges
+from webnotes.model.meta import get_field_precision
 
 def execute(filters=None):
 	if not filters: filters = {}
@@ -16,10 +17,7 @@
 	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():
 		for item_group, monthwise_data in salesperson_items.items():
 			row = [salesperson, item_group]
@@ -29,7 +27,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), precision)
+						value = flt(month_data.get(fieldname))
 						period_data[i] += value
 						totals[i] += value
 				period_data[2] = period_data[0] - period_data[1]
@@ -47,37 +45,37 @@
 			msgprint(_("Please specify") + ": " + label,
 				raise_exception=True)
 
-	columns = ["Sales Person:Link/Sales Person:80", "Item Group:Link/Item Group:80"]
+	columns = ["Sales Person:Link/Sales Person:120", "Item Group:Link/Item Group:120"]
 
 	group_months = False if filters["period"] == "Monthly" else True
 
 	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")))				
+				label = label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))
 			else:
-				columns.append(label % from_date.strftime("%b"))
+				label = label % from_date.strftime("%b")
+			
+			columns.append(label+":Float:120")
 
-	return columns + ["Total Target::80", "Total Achieved::80", "Total Variance::80"]
+	return columns + ["Total Target::120", "Total Achieved::120", "Total Variance::120"]
 
 #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""", 
+		where td.parent=sp.name and td.fiscal_year=%s order by sp.name""", 
 		(filters["fiscal_year"]), as_dict=1)
 
 #Get target distribution details of item group
 def get_target_distribution_details(filters):
 	target_details = {}
 	
-	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""", (filters["fiscal_year"]), as_dict=1):
-			target_details.setdefault(d.month, d)
+	for d in webnotes.conn.sql("""select bd.name, bdd.month, bdd.percentage_allocation 
+		from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd 
+		where bdd.parent=bd.name and bd.fiscal_year=%s""", (filters["fiscal_year"]), as_dict=1):
+			target_details.setdefault(d.name, {}).setdefault(d.month, d.percentage_allocation)
 
 	return target_details
 
@@ -94,32 +92,33 @@
 		(start_date, end_date), as_dict=1)
 
 def get_salesperson_item_month_map(filters):
+	import datetime
 	salesperson_details = get_salesperson_details(filters)
 	tdd = get_target_distribution_details(filters)
 	achieved_details = get_achieved_details(filters)
 
 	sim_map = {}
-
 	for sd in salesperson_details:
-		for month in tdd:
+		for month_id in range(1, 13):
+			month = datetime.date(2013, month_id, 1).strftime('%B')
 			sim_map.setdefault(sd.name, {}).setdefault(sd.item_group, {})\
-			.setdefault(month, webnotes._dict({
-				"target": 0.0, "achieved": 0.0
-			}))
+				.setdefault(month, webnotes._dict({
+					"target": 0.0, "achieved": 0.0
+				}))
 
 			tav_dict = sim_map[sd.name][sd.item_group][month]
-
+			month_percentage = sd.distribution_id and \
+				tdd.get(sd.distribution_id, {}).get(month, 0) or 100.0/12
+			
 			for ad in achieved_details:
 				if (filters["target_on"] == "Quantity"):
-					tav_dict.target = flt(sd.target_qty) * \
-						(tdd[month]["percentage_allocation"]/100)
+					tav_dict.target = flt(flt(sd.target_qty) * month_percentage/100, 2)
 					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 = flt(sd.target_amount) * \
-						(tdd[month]["percentage_allocation"]/100)
+					tav_dict.target = flt(flt(sd.target_amount) * month_percentage/100, 2)
 					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
diff --git a/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
index 4a8cc7c..829f781 100644
--- a/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
+++ b/selling/report/territory_target_variance_item_group_wise/territory_target_variance_item_group_wise.py
@@ -46,37 +46,36 @@
 			label = (" ".join(fieldname.split("_"))).title()
 			msgprint(_("Please specify") + ": " + label, raise_exception=True)
 
-	columns = ["Territory:Link/Territory:80", "Item Group:Link/Item Group:80"]
+	columns = ["Territory:Link/Territory:120", "Item Group:Link/Item Group:120"]
 
 	group_months = False if filters["period"] == "Monthly" else True
 
 	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")))				
+				label = label % (from_date.strftime("%b") + " - " + to_date.strftime("%b"))
 			else:
-				columns.append(label % from_date.strftime("%b"))
+				label = label % from_date.strftime("%b")
+			columns.append(label+":Float:120")
 
-	return columns + ["Total Target::80", "Total Achieved::80", "Total Variance::80"]
+	return columns + ["Total Target::120", "Total Achieved::120", "Total Variance::120"]
 
 #Get territory & item group details
 def get_territory_details(filters):
 	return webnotes.conn.sql("""select t.name, td.item_group, td.target_qty, 
 		td.target_amount, t.distribution_id 
 		from `tabTerritory` t, `tabTarget Detail` td 
-		where td.parent=t.name and td.fiscal_year=%s and 
-		ifnull(t.distribution_id, '')!='' order by t.name""", 
+		where td.parent=t.name and td.fiscal_year=%s order by t.name""", 
 		(filters["fiscal_year"]), as_dict=1)
 
 #Get target distribution details of item group
 def get_target_distribution_details(filters):
 	target_details = {}
 
-	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""", (filters["fiscal_year"]), as_dict=1):
-			target_details.setdefault(d.month, d)
+	for d in webnotes.conn.sql("""select bd.name, bdd.month, bdd.percentage_allocation 
+		from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd
+		where bdd.parent=bd.name and bd.fiscal_year=%s""", (filters["fiscal_year"]), as_dict=1):
+			target_details.setdefault(d.name, {}).setdefault(d.month, d.percentage_allocation)
 
 	return target_details
 
@@ -99,25 +98,27 @@
 	tim_map = {}
 
 	for td in territory_details:
-		for month in tdd:
+		for month_id in range(1, 13):
+			month = datetime.date(2013, month_id, 1).strftime('%B')
+			
 			tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\
-			.setdefault(month, webnotes._dict({
-				"target": 0.0, "achieved": 0.0
-			}))
+				.setdefault(month, webnotes._dict({
+					"target": 0.0, "achieved": 0.0
+				}))
 
 			tav_dict = tim_map[td.name][td.item_group][month]
+			month_percentage = td.distribution_id and \
+				tdd.get(td.distribution_id, {}).get(month, 0) or 100.0/12
 
 			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(flt(td.target_qty) * month_percentage /100)
 					if ad.month_name == month and get_item_group(ad.item_code) == td.item_group \
 						and ad.territory == td.name:
 							tav_dict.achieved += ad.qty
 
 				if (filters["target_on"] == "Amount"):
-					tav_dict.target = flt(td.target_amount) * \
-						(tdd[month]["percentage_allocation"]/100)
+					tav_dict.target = flt(flt(td.target_amount) * month_percentage / 100)
 					if ad.month_name == month and get_item_group(ad.item_code) == td.item_group \
 						and ad.territory == td.name:
 							tav_dict.achieved += ad.amount