[Reports][Sales and Purchase Trends][Completed]
diff --git a/controllers/trends.py b/controllers/trends.py
index 6c33235..dee9ec8 100644
--- a/controllers/trends.py
+++ b/controllers/trends.py
@@ -18,51 +18,45 @@
 import webnotes
 from webnotes.utils import cint, add_days, add_months, cstr
 
-def get_columns(filters, year_start_date, start_month, trans):
-	columns, pwc, bon, grbc = [], [], [], []
-	query_bon, query_pwc = '', ''
+def get_columns(filters, trans):
 
-	period = filters.get("period")
-	based_on = filters.get("based_on")
-	grby = filters.get("group_by")
-
-	if not (period and based_on):
+	if not (filters.get("period") and filters.get("based_on")):
 		webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
 
-	elif based_on == grby:
+	elif filters.get("based_on") == filters.get("group_by"):
 		webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1)
 
 	else: 
-		bon, query_bon, basedon, sup_tab = bon_columns_qdata(based_on, bon, trans)
-		pwc, query_pwc = pw_column_qdata(filters, period, pwc, year_start_date,start_month, trans)
-		grbc = grp_column(grby)
-	
-	if grbc:	
-		columns = bon + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] 
-	else:
+		bon, query_bon, basedon, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
+		pwc, query_pwc = period_wise_colums_query(filters, trans)
+		grbc = group_wise_column(filters.get("group_by"))
+
 		columns = bon + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
+		if grbc:	
+			columns = bon + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] 
 
-	return columns, query_bon, query_pwc, basedon, grbc, sup_tab
+		details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": basedon, 
+			"grbc": grbc, "sup_tab": sup_tab}
 
-def get_data(columns, filters, tab,  query_bon, query_pwc, basedon, grbc, sup_tab):
+	return details
+
+def get_data(filters, tab, details):
 	
-	query_details =  query_bon + query_pwc + 'SUM(t2.qty), SUM(t1.grand_total) '
-	query_pwc = query_pwc + 'SUM(t2.qty), SUM(t1.grand_total)'
 	data = []
 	inc, cond= '',''
+	query_details =  details["query_bon"] + details["query_pwc"]
 	
-	if query_bon in ["t1.project_name,", "t2.project_name,"]:
-		cond = 'and '+ query_bon[:-1] +' IS Not NULL'
+	if details["query_bon"] in ["t1.project_name,", "t2.project_name,"]:
+		cond = 'and '+ details["query_bon"][:-1] +' IS Not NULL'
 
 	if filters.get("group_by"):
 		sel_col = ''
+		ind = details["columns"].index(details["grbc"][0])
 
 		if filters.get("group_by") == 'Item':
 			sel_col = 't2.item_code'
-
 		elif filters.get("group_by") == 'Customer':
 			sel_col = 't1.customer'
-
 		elif filters.get("group_by") == 'Supplier':
 			sel_col = 't1.supplier'
 
@@ -71,13 +65,14 @@
 		else :
 			inc = 1
 
-		ind = columns.index(grbc[0])
-
-		data1 = webnotes.conn.sql(""" select %s %s from `%s` t1, `%s` t2 %s
-			where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' 
-			and t1.docstatus = 1 %s group by %s 
-		"""%(query_bon, query_pwc, tab[0], tab[1], sup_tab,filters.get("company"), filters.get("fiscal_year"), 
-		cond, basedon), as_list=1)
+		data1 = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
+						where t2.parent = t1.name and t1.company = %s 
+						and t1.fiscal_year = %s and t1.docstatus = 1 %s 
+						group by %s 
+					""" % (query_details, tab[0], tab[1], details["sup_tab"], "%s", 
+						"%s", cond, details["basedon"]), (filters.get("company"), 
+						filters["fiscal_year"]),
+					as_list=1)
 
 		for d in range(len(data1)):
 			#to add blanck column
@@ -86,44 +81,64 @@
 			data.append(dt)
 
 			#to get distinct value of col specified by group_by in filter
-			row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2
-				where t2.parent = t1.name and t1.company = '%s'	and t1.fiscal_year = '%s' 
-				and t1.docstatus = 1 and %s = '%s' 
-			"""%(sel_col, tab[0], tab[1], filters.get("company"), filters.get("fiscal_year"), 
-			basedon, data1[d][0]),as_list=1)
-			
+			row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2 %s
+							where t2.parent = t1.name and t1.company = %s	
+							and t1.fiscal_year = %s 	and t1.docstatus = 1 
+							and %s = %s 
+						"""%(sel_col, tab[0], tab[1], details["sup_tab"], "%s", 
+							"%s", details["basedon"], "%s"),
+							(filters.get("company"), filters.get("fiscal_year"), 
+							data1[d][0]),
+						as_list=1)
+
 			for i in range(len(row)):
-				des = ['' for q in range(len(columns))]
+				des = ['' for q in range(len(details["columns"]))]
 				
 				#get data for each group_by filter 
-				row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 
-					where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' 
-					and t1.docstatus = 1 and %s = '%s' and %s ='%s' 
-				"""%(sel_col, query_pwc, tab[0], tab[1], filters.get("company"), filters.get("fiscal_year"), 
-				sel_col, row[i][0], basedon, data1[d][0]),as_list=1)
+				row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s
+								where t2.parent = t1.name and t1.company = %s 
+								and t1.fiscal_year = %s and t1.docstatus = 1 
+								and %s = %s and %s = %s 
+							"""%(sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"], 
+								"%s", "%s", sel_col, "%s", details["basedon"], "%s"), 
+								(filters.get("company"), filters.get("fiscal_year"), row[i][0], 
+								data1[d][0]),
+							as_list=1)
 
 				des[ind] = row[i]
-				for j in range(1,len(columns)-inc):	
+				for j in range(1,len(details["columns"])-inc):	
 					des[j+inc] = row1[0][j]
 				data.append(des)
 	else:
 
 		data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
-			where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' 
-			and t1.docstatus = 1 %s group by %s	
-		"""%(query_details, tab[0], tab[1], sup_tab, filters.get("company"), 
-		filters.get("fiscal_year"), cond,basedon), as_list=1)
+						where t2.parent = t1.name and t1.company = %s 
+						and t1.fiscal_year = %s and t1.docstatus = 1 
+						%s group by %s	
+					"""%(query_details, tab[0], tab[1], details["sup_tab"], "%s", 
+						"%s", cond,details["basedon"]), (filters.get("company"), 
+						filters.get("fiscal_year")), 
+					as_list=1)
 
 	return data
 
-def pw_column_qdata(filters, period, pwc, year_start_date, start_month, trans):
+def period_wise_colums_query(filters, trans):
+
 	query_details = ''
+	pwc = []
+	ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year` 
+					where name = '%s' 
+				"""%filters.get("fiscal_year"))[0][0]
+
+	year_start_date = ysd.strftime('%Y-%m-%d')
+	start_month = cint(year_start_date.split('-')[1])
+
 	if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
 		trans_date = 'posting_date'
 	else:
 		trans_date = 'transaction_date'
 
-	if period == "Monthly":
+	if filters.get("period") == "Monthly":
 		month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
 
 		for month in range(start_month-1,len(month_name)):
@@ -144,23 +159,23 @@
 				SUM(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t1.grand_total ELSE NULL END),
 			"""%{"trans": trans_date, "mon_num": cstr(month+1)}
 	
-	elif period == "Quarterly":
-		pwc = ["Q1(qty):Float:120", "Q1(amt):Currency:120", "Q2(qty):Float:120", "Q2(amt):Currency:120", 
-		"Q3(qty):Float:120", "Q3(amt):Currency:120", "Q4(qty):Float:120", "Q4(amt):Currency:120"]
+	elif filters.get("period") == "Quarterly":
+		pwc = ["Q1 (Qty):Float:120", "Q1 (Amt):Currency:120", "Q2 (Qty):Float:120", "Q2 (Amt):Currency:120", 
+		"Q3 (Qty):Float:120", "Q3 (Amt):Currency:120", "Q4 (Qty):Float:120", "Q4 (Amt):Currency:120"]
 
 		first_qsd, second_qsd, third_qsd, fourth_qsd = year_start_date, add_months(year_start_date,3), add_months(year_start_date,6), add_months(year_start_date,9)
 		first_qed, second_qed, third_qed, fourth_qed = add_days(add_months(first_qsd,3),-1), add_days(add_months(second_qsd,3),-1), add_days(add_months(third_qsd,3),-1), add_days(add_months(fourth_qsd,3),-1)
-
 		bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]] 
+
 		for d in bet_dates:
 			query_details += """
 				SUM(CASE WHEN t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s' THEN t2.qty ELSE NULL END), 
 				SUM(CASE WHEN t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s' THEN t1.grand_total ELSE NULL END),
 			"""%{"trans": trans_date, "sd": d[0],"ed": d[1]}
 
-	elif period == "Half-yearly":
-		pwc = ["Fisrt Half(qty):Float:120", "Fisrt Half(amt):Currency:120", "Second Half(qty):Float:120",
-		 	"Second Half(amt):Currency:120"]
+	elif filters.get("period") == "Half-yearly":
+		pwc = ["Fisrt Half (Qty):Float:120", "Fisrt Half (Amt):Currency:120", "Second Half (Qty):Float:120",
+		 	"Second Half (Amt):Currency:120"]
 
 		first_half_start = year_start_date
 		first_half_end = add_days(add_months(first_half_start,6),-1)
@@ -176,12 +191,13 @@
 		"she": second_half_end}	 
 	
 	else:
-		pwc = [filters.get("fiscal_year")+"(qty):Float:120", filters.get("fiscal_year")+"(amt):Currency:120"]
+		pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
 		query_details = " SUM(t2.qty), SUM(t1.grand_total),"
 
+	query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
 	return pwc, query_details
 
-def bon_columns_qdata(based_on, bon, trans):
+def basedon_wise_colums_query(based_on, trans):
 	sup_tab = ''
 
 	if based_on == "Item":
@@ -234,11 +250,11 @@
 			basedon = 't2.project_name'
 
 		else:
-			webnotes.msgprint("No Information Available", raise_exception=1)
+			webnotes.msgprint("Information Not Available", raise_exception=1)
 
 	return bon, query_details, basedon, sup_tab
 
-def grp_column(group_by):
+def group_wise_column(group_by):
 	if group_by:
 		return [group_by+":Link/"+group_by+":120"]
 	else: