[Fix] Sales Person Reports (#13987)
* Fix Sales Person Reports - Show quantity and amounts based on the delivered quantities if the Sales Order has been closed and all quantities weren't considered for further transactions. * Fix codacy * Modify as per the review comment
This commit is contained in:
		
							parent
							
								
									1b48fde986
								
							
						
					
					
						commit
						4f40b2dca5
					
				| @ -91,17 +91,22 @@ def get_achieved_details(filters, sales_person, all_sales_persons, target_item_g | |||||||
| 	start_date, end_date = get_fiscal_year(fiscal_year = filters["fiscal_year"])[1:] | 	start_date, end_date = get_fiscal_year(fiscal_year = filters["fiscal_year"])[1:] | ||||||
| 
 | 
 | ||||||
| 	item_details = frappe.db.sql(""" | 	item_details = frappe.db.sql(""" | ||||||
| 		select | 		SELECT st.sales_person, MONTHNAME(so.transaction_date) as month_name, | ||||||
| 			sum(soi.stock_qty * (st.allocated_percentage/100)) as qty, | 		CASE | ||||||
| 			sum(soi.base_net_amount * (st.allocated_percentage/100)) as amount, | 			WHEN so.status = "Closed" THEN sum(soi.delivered_qty * soi.conversion_factor * (st.allocated_percentage/100)) | ||||||
| 			st.sales_person, MONTHNAME(so.transaction_date) as month_name | 			ELSE sum(soi.stock_qty * (st.allocated_percentage/100)) | ||||||
|  | 		END as qty, | ||||||
|  | 		CASE | ||||||
|  | 			WHEN so.status = "Closed" THEN sum(soi.delivered_qty * soi.conversion_factor * soi.base_net_rate * (st.allocated_percentage/100)) | ||||||
|  | 			ELSE soi.base_net_amount * (st.allocated_percentage/100)) | ||||||
|  | 		END as amount | ||||||
| 		from | 		from | ||||||
| 			`tabSales Order Item` soi, `tabSales Order` so, `tabSales Team` st | 			`tabSales Order Item` soi, `tabSales Order` so, `tabSales Team` st | ||||||
| 		where | 		where | ||||||
| 			soi.parent=so.name and so.docstatus=1 and st.parent=so.name | 			soi.parent=so.name and so.docstatus=1 and st.parent=so.name | ||||||
| 			and so.transaction_date>=%s and so.transaction_date<=%s | 			and so.transaction_date>=%s and so.transaction_date<=%s | ||||||
| 			and exists(select name from `tabSales Person` where lft >= %s and rgt <= %s and name=st.sales_person) | 			and exists(SELECT name from `tabSales Person` where lft >= %s and rgt <= %s and name=st.sales_person) | ||||||
| 			and exists(select name from `tabItem Group` where lft >= %s and rgt <= %s and name=soi.item_group) | 			and exists(SELECT name from `tabItem Group` where lft >= %s and rgt <= %s and name=soi.item_group) | ||||||
| 		group by | 		group by | ||||||
| 			sales_person, month_name | 			sales_person, month_name | ||||||
| 			""", | 			""", | ||||||
|  | |||||||
| @ -15,11 +15,12 @@ def execute(filters=None): | |||||||
| 	data = [] | 	data = [] | ||||||
| 
 | 
 | ||||||
| 	for d in entries: | 	for d in entries: | ||||||
| 		data.append([ | 		if d.stock_qty > 0: | ||||||
| 			d.name, d.customer, d.territory, d.posting_date, d.item_code, | 			data.append([ | ||||||
| 			item_details.get(d.item_code, {}).get("item_group"), item_details.get(d.item_code, {}).get("brand"), | 				d.name, d.customer, d.territory, d.posting_date, d.item_code, | ||||||
| 			d.stock_qty, d.base_net_amount, d.sales_person, d.allocated_percentage, d.contribution_amt | 				item_details.get(d.item_code, {}).get("item_group"), item_details.get(d.item_code, {}).get("brand"), | ||||||
| 		]) | 				d.stock_qty, d.base_net_amount, d.sales_person, d.allocated_percentage, d.contribution_amt | ||||||
|  | 			]) | ||||||
| 
 | 
 | ||||||
| 	if data: | 	if data: | ||||||
| 		total_row = [""]*len(data[0]) | 		total_row = [""]*len(data[0]) | ||||||
| @ -40,18 +41,34 @@ def get_columns(filters): | |||||||
| 
 | 
 | ||||||
| def get_entries(filters): | def get_entries(filters): | ||||||
| 	date_field = filters["doc_type"] == "Sales Order" and "transaction_date" or "posting_date" | 	date_field = filters["doc_type"] == "Sales Order" and "transaction_date" or "posting_date" | ||||||
|  | 	if filters["doc_type"] == "Sales Order": | ||||||
|  | 		qty_field = "delivered_qty" | ||||||
|  | 	else: | ||||||
|  | 		qty_field = "qty" | ||||||
| 	conditions, values = get_conditions(filters, date_field) | 	conditions, values = get_conditions(filters, date_field) | ||||||
|  | 
 | ||||||
| 	entries = frappe.db.sql(""" | 	entries = frappe.db.sql(""" | ||||||
| 		select | 		select | ||||||
| 			dt.name, dt.customer, dt.territory, dt.%s as posting_date, dt_item.item_code, | 			dt.name, dt.customer, dt.territory, dt.%s as posting_date, dt_item.item_code, | ||||||
| 			dt_item.stock_qty, dt_item.base_net_amount, st.sales_person, st.allocated_percentage, | 			st.sales_person, st.allocated_percentage, | ||||||
| 			dt_item.base_net_amount*st.allocated_percentage/100 as contribution_amt | 		CASE  | ||||||
|  | 			WHEN dt.status = "Closed" THEN dt_item.%s * dt_item.conversion_factor | ||||||
|  | 			ELSE dt_item.stock_qty | ||||||
|  | 		END as stock_qty, | ||||||
|  | 		CASE | ||||||
|  | 			WHEN dt.status = "Closed" THEN (dt_item.base_net_rate * dt_item.%s * dt_item.conversion_factor) | ||||||
|  | 			ELSE dt_item.base_net_amount | ||||||
|  | 		END as base_net_amount, | ||||||
|  | 		CASE | ||||||
|  | 			WHEN dt.status = "Closed" THEN ((dt_item.base_net_rate * dt_item.%s * dt_item.conversion_factor) * st.allocated_percentage/100) | ||||||
|  | 			ELSE dt_item.base_net_amount * st.allocated_percentage/100 | ||||||
|  | 		END as contribution_amt | ||||||
| 		from | 		from | ||||||
| 			`tab%s` dt, `tab%s Item` dt_item, `tabSales Team` st | 			`tab%s` dt, `tab%s Item` dt_item, `tabSales Team` st | ||||||
| 		where | 		where | ||||||
| 			st.parent = dt.name and dt.name = dt_item.parent and st.parenttype = %s | 			st.parent = dt.name and dt.name = dt_item.parent and st.parenttype = %s | ||||||
| 			and dt.docstatus = 1 %s order by st.sales_person, dt.name desc | 			and dt.docstatus = 1 %s order by st.sales_person, dt.name desc | ||||||
| 		""" %(date_field, filters["doc_type"], filters["doc_type"], '%s', conditions), | 		""" %(date_field, qty_field, qty_field, qty_field, filters["doc_type"], filters["doc_type"], '%s', conditions), | ||||||
| 			tuple([filters["doc_type"]] + values), as_dict=1) | 			tuple([filters["doc_type"]] + values), as_dict=1) | ||||||
| 
 | 
 | ||||||
| 	return entries | 	return entries | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user