[target report] cleanup
This commit is contained in:
		
							parent
							
								
									739a7fdf74
								
							
						
					
					
						commit
						0930b94264
					
				| @ -26,17 +26,23 @@ from utilities import build_filter_conditions | |||||||
| 
 | 
 | ||||||
| class FiscalYearError(webnotes.ValidationError): pass | class FiscalYearError(webnotes.ValidationError): pass | ||||||
| 
 | 
 | ||||||
| def get_fiscal_year(date, verbose=1): | def get_fiscal_year(date=None, fiscal_year=None, verbose=1): | ||||||
| 	return get_fiscal_years(date, verbose=1)[0] | 	return get_fiscal_years(date, fiscal_year, verbose=1)[0] | ||||||
| 	 | 	 | ||||||
| def get_fiscal_years(date, verbose=1): | def get_fiscal_years(date=None, fiscal_year=None, verbose=1): | ||||||
| 	# if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) | 	# if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) | ||||||
|  | 	cond = "" | ||||||
|  | 	if fiscal_year: | ||||||
|  | 		cond = "name = '%s'" % fiscal_year | ||||||
|  | 	else: | ||||||
|  | 		cond = "'%s' >= year_start_date and '%s' < adddate(year_start_date, interval 1 year)" % \ | ||||||
|  | 			(date, date) | ||||||
| 	fy = webnotes.conn.sql("""select name, year_start_date,  | 	fy = webnotes.conn.sql("""select name, year_start_date,  | ||||||
| 		subdate(adddate(year_start_date, interval 1 year), interval 1 day)  | 		subdate(adddate(year_start_date, interval 1 year), interval 1 day)  | ||||||
| 			as year_end_date | 			as year_end_date | ||||||
| 		from `tabFiscal Year` | 		from `tabFiscal Year` | ||||||
| 		where %s >= year_start_date and %s < adddate(year_start_date, interval 1 year) | 		where %s | ||||||
| 		order by year_start_date desc""", (date, date)) | 		order by year_start_date desc""" % cond) | ||||||
| 	 | 	 | ||||||
| 	if not fy: | 	if not fy: | ||||||
| 		error_msg = """%s not in any Fiscal Year""" % formatdate(date) | 		error_msg = """%s not in any Fiscal Year""" % formatdate(date) | ||||||
|  | |||||||
| @ -20,6 +20,7 @@ import calendar | |||||||
| from webnotes import _, msgprint | from webnotes import _, msgprint | ||||||
| from webnotes.utils import flt | from webnotes.utils import flt | ||||||
| import time | import time | ||||||
|  | from accounts.utils import get_fiscal_year | ||||||
| 
 | 
 | ||||||
| def execute(filters=None): | def execute(filters=None): | ||||||
| 	if not filters: filters = {} | 	if not filters: filters = {} | ||||||
| @ -54,8 +55,7 @@ def get_columns(filters): | |||||||
| 	for fieldname in ["fiscal_year", "period", "target_on"]: | 	for fieldname in ["fiscal_year", "period", "target_on"]: | ||||||
| 		if not filters.get(fieldname): | 		if not filters.get(fieldname): | ||||||
| 			label = (" ".join(fieldname.split("_"))).title() | 			label = (" ".join(fieldname.split("_"))).title() | ||||||
| 			msgprint(_("Please specify") + ": " + label, | 			msgprint(_("Please specify") + ": " + label, raise_exception=True) | ||||||
| 				raise_exception=True) |  | ||||||
| 
 | 
 | ||||||
| 	columns = ["Territory:Link/Territory:80", "Item Group:Link/Item Group:80"] | 	columns = ["Territory:Link/Territory:80", "Item Group:Link/Item Group:80"] | ||||||
| 
 | 
 | ||||||
| @ -72,8 +72,8 @@ def get_columns(filters): | |||||||
| 
 | 
 | ||||||
| def get_period_date_ranges(filters): | def get_period_date_ranges(filters): | ||||||
| 	from dateutil.relativedelta import relativedelta | 	from dateutil.relativedelta import relativedelta | ||||||
|  | 	year_start_date, year_end_date = get_fiscal_year(fiscal_year = filters["fiscal_year"])[1:] | ||||||
| 	 | 	 | ||||||
| 	year_start_date, year_end_date = get_year_start_end_date(filters) |  | ||||||
| 
 | 
 | ||||||
| 	increment = { | 	increment = { | ||||||
| 		"Monthly": 1, | 		"Monthly": 1, | ||||||
| @ -111,8 +111,8 @@ def get_territory_details(filters): | |||||||
| 		td.target_amount, t.distribution_id  | 		td.target_amount, t.distribution_id  | ||||||
| 		from `tabTerritory` t, `tabTarget Detail` td  | 		from `tabTerritory` t, `tabTarget Detail` td  | ||||||
| 		where td.parent=t.name and td.fiscal_year=%s and  | 		where td.parent=t.name and td.fiscal_year=%s and  | ||||||
| 		ifnull(t.distribution_id, '')!='' order by t.name""" % | 		ifnull(t.distribution_id, '')!='' order by t.name""",  | ||||||
| 		('%s'), (filters.get("fiscal_year")), as_dict=1) | 		filters.get("fiscal_year"), as_dict=1) | ||||||
| 
 | 
 | ||||||
| #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): | ||||||
| @ -128,7 +128,7 @@ 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_fiscal_year(fiscal_year = filters["fiscal_year"])[1:] | ||||||
| 
 | 
 | ||||||
| 	return 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,  | ||||||
| 		so.territory, MONTHNAME(so.transaction_date) as month_name  | 		so.territory, MONTHNAME(so.transaction_date) as month_name  | ||||||
| @ -148,35 +148,26 @@ def get_territory_item_month_map(filters): | |||||||
| 		for month in tdd: | 		for month in tdd: | ||||||
| 			tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ | 			tim_map.setdefault(td.name, {}).setdefault(td.item_group, {})\ | ||||||
| 			.setdefault(month, webnotes._dict({ | 			.setdefault(month, webnotes._dict({ | ||||||
| 				"target": 0.0, "achieved": 0.0, "variance": 0.0 | 				"target": 0.0, "achieved": 0.0 | ||||||
| 			})) | 			})) | ||||||
| 
 | 
 | ||||||
| 			tav_dict = tim_map[td.name][td.item_group][month] | 			tav_dict = tim_map[td.name][td.item_group][month] | ||||||
| 
 | 
 | ||||||
| 			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 = flt(td.target_qty) * (tdd[month]["percentage_allocation"]/100) | ||||||
| 					if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \ | 					if ad.month_name == month and get_item_group(ad.item_code) == td.item_group \ | ||||||
| 						and ad.territory == td.name: | 						and ad.territory == td.name: | ||||||
| 							tav_dict.achieved += ad.qty | 							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 = flt(td.target_amount) * \ | ||||||
| 					if ad.month_name == month and ''.join(get_item_group(ad.item_code)) == td.item_group \ | 						(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: | 						and ad.territory == td.name: | ||||||
| 							tav_dict.achieved += ad.amount | 							tav_dict.achieved += ad.amount | ||||||
| 
 | 
 | ||||||
| 	return tim_map | 	return tim_map | ||||||
| 
 | 
 | ||||||
| def get_year_start_end_date(filters): |  | ||||||
| 	return webnotes.conn.sql("""select year_start_date,  |  | ||||||
| 		subdate(adddate(year_start_date, interval 1 year), interval 1 day)  |  | ||||||
| 			as year_end_date |  | ||||||
| 		from `tabFiscal Year` |  | ||||||
| 		where name=%s""", filters["fiscal_year"])[0] |  | ||||||
| 
 |  | ||||||
| def get_item_group(item_name): | def get_item_group(item_name): | ||||||
| 	"""Get Item Group of an item""" | 	return webnotes.conn.get_value("Item", item_name, "item_group") | ||||||
| 
 |  | ||||||
| 	return webnotes.conn.sql_list("select item_group from `tabItem` where name=%s""" % |  | ||||||
| 		('%s'), (item_name)) |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user