[trend analyzer] cleanup
diff --git a/controllers/trends.py b/controllers/trends.py
index 4edb784..c0636cb 100644
--- a/controllers/trends.py
+++ b/controllers/trends.py
@@ -16,33 +16,39 @@
 
 from __future__ import unicode_literals
 import webnotes
-from webnotes.utils import cint, add_days, add_months, cstr
-from datetime import datetime
+from webnotes.utils import add_days, add_months, cstr, getdate
+from webnotes import _
 
 def get_columns(filters, trans):
+	validate_filters(filters)
+	
+	# based_on_cols, based_on_select, based_on_group_by, addl_tables
+	bonc, query_bon, based, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans)
+	# period_cols, period_select
+	pwc, query_pwc = period_wise_colums_query(filters, trans)
+	
+	# group_by_cols
+	grbc = group_wise_column(filters.get("group_by"))
 
-	if not (filters.get("period") and filters.get("based_on")):
-		webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
+	columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
+	if grbc:	
+		columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] 
 
-	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: 
-		bonc, query_bon, based, 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 = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
-		if grbc:	
-			columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] 
-
-		details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": based, 
-			"grbc": grbc, "sup_tab": sup_tab}
+	# conditions
+	details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, 
+		"basedon": based, "grbc": grbc, "sup_tab": sup_tab}
 
 	return details
 
-def get_data(filters, tab, details):
+def validate_filters(filters):
+	for f in ["Fiscal Year", "Based On", "Period", "Company"]:
+		if not filters.get(f.lower().replace(" ", "_")):
+			webnotes.msgprint(f + _(" is mandatory"), raise_exception=1)
 	
+	if filters.get("based_on") == filters.get("group_by"):
+		webnotes.msgprint("'Based On' and 'Group By' can not be same", raise_exception=1)
+
+def get_data(filters, tab, details):
 	data = []
 	inc, cond= '',''
 	query_details =  details["query_bon"] + details["query_pwc"]
@@ -96,17 +102,17 @@
 				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"], 
+						""" % (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)
+							(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(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 
@@ -118,20 +124,13 @@
 
 	return data
 
-def get_mon(date):
-	"""convert srting formated date into date and retrieve month abbrevation"""
-	return (datetime.strptime(date, '%Y-%m-%d')).strftime("%b")
+def get_mon(dt):
+	return getdate(dt).strftime("%b")
 
 def period_wise_colums_query(filters, trans):
-	from datetime import datetime
-
 	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])
+	ysd = webnotes.conn.get_value("Fiscal year", filters.get("fiscal_year"), "year_start_date")
 
 	if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
 		trans_date = 'posting_date'
@@ -141,27 +140,13 @@
 	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)):
-			pwc.append(month_name[month]+' (Qty):Float:120')
-			pwc.append(month_name[month]+' (Amt):Currency:120')
-
-			query_details += """
-				Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
-				SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
-			"""%{"trans": trans_date,"mon_num": cstr(month+1)}
-
-		for month in range(0, start_month-1):
-			pwc.append(month_name[month]+' (Qty):Float:120')
-			pwc.append(month_name[month]+' (Amt):Currency:120')
-
-			query_details += """
-				Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)),
-				SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)),
-			"""%{"trans": trans_date, "mon_num": cstr(month+1)}
+		for month_idx in range(ysd.month-1,len(month_name)) + range(0, ysd.month-1):
+			query_details = get_monthly_conditions(month_name, month_idx, trans_date, 
+				pwc, query_details)
 	
 	elif filters.get("period") == "Quarterly":
 
-		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_qsd, second_qsd, third_qsd, fourth_qsd = ysd, add_months(ysd,3), add_months(ysd,6), add_months(ysd,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]] 
 		
@@ -178,7 +163,7 @@
 
 	elif filters.get("period") == "Half-yearly":
 
-		first_half_start = year_start_date
+		first_half_start = ysd
 		first_half_end = add_days(add_months(first_half_start,6),-1)
 		second_half_start = add_days(first_half_end,1)
 		second_half_end = add_days(add_months(second_half_start,6),-1)
@@ -200,6 +185,17 @@
 
 	query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
 	return pwc, query_details
+	
+def get_monthly_conditions(month_list, month_idx, trans_date, pwc, query_details):
+	pwc += [month_list[month_idx] + ' (Qty):Float:120', 
+		month_list[month_idx] + ' (Amt):Currency:120']
+
+	query_details += """
+		Sum(IF(MONTH(t1.%(trans_date)s)= %(mon_num)s, t2.qty, NULL)),
+		SUM(IF(MONTH(t1.%(trans_date)s)= %(mon_num)s, t1.grand_total, NULL)),
+	""" % {"trans_date": trans_date, "mon_num": cstr(month_idx+1)}
+	
+	return query_details
 
 def basedon_wise_colums_query(based_on, trans):
 	sup_tab = ''
@@ -242,19 +238,16 @@
 		based = 't1.territory'
 
 	elif based_on == "Project":
-
 		if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
 			bon = ["Project:Link/Project:120"]
 			query_details = "t1.project_name,"
 			based = 't1.project_name'
-
 		elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
 			bon = ["Project:Link/Project:120"]
 			query_details = "t2.project_name,"
 			based = 't2.project_name'
-
 		else:
-			webnotes.msgprint("Information Not Available", raise_exception=1)
+			webnotes.msgprint("Project-wise data is not available for Quotation", raise_exception=1)
 
 	return bon, query_details, based, sup_tab
 
diff --git a/selling/report/quotation_trends/quotation_trends.py b/selling/report/quotation_trends/quotation_trends.py
index e341752..c6a54c7 100644
--- a/selling/report/quotation_trends/quotation_trends.py
+++ b/selling/report/quotation_trends/quotation_trends.py
@@ -16,7 +16,7 @@
 
 from __future__ import unicode_literals
 import webnotes
-from controllers.trends	import get_columns,get_data
+from controllers.trends	import get_columns, get_data
 
 def execute(filters=None):
 	if not filters: filters ={}
@@ -27,8 +27,5 @@
 
 	details = get_columns(filters, trans)
 	data = get_data(filters, tab, details)
-	
-	if not data:
-		webnotes.msgprint("Data not found for selected criterias")
 
 	return details["columns"], data 
\ No newline at end of file
diff --git a/stock/doctype/serial_no/serial_no.txt b/stock/doctype/serial_no/serial_no.txt
index 8e891b8..33160c7 100644
--- a/stock/doctype/serial_no/serial_no.txt
+++ b/stock/doctype/serial_no/serial_no.txt
@@ -1,14 +1,14 @@
 [
  {
-  "creation": "2013-01-29 19:25:41", 
+  "creation": "2013-05-16 10:59:15", 
   "docstatus": 0, 
-  "modified": "2013-01-29 16:27:57", 
+  "modified": "2013-06-20 11:23:01", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
  {
   "allow_attach": 1, 
-  "allow_rename": 1, 
+  "allow_rename": 0, 
   "autoname": "field:serial_no", 
   "description": "Distinct unit of an Item", 
   "doctype": "DocType", 
@@ -31,7 +31,9 @@
   "parent": "Serial No", 
   "parentfield": "permissions", 
   "parenttype": "DocType", 
+  "permlevel": 0, 
   "read": 1, 
+  "report": 1, 
   "submit": 0
  }, 
  {
@@ -43,12 +45,14 @@
   "fieldname": "details", 
   "fieldtype": "Section Break", 
   "label": "Details", 
-  "oldfieldtype": "Section Break"
+  "oldfieldtype": "Section Break", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break0", 
-  "fieldtype": "Column Break"
+  "fieldtype": "Column Break", 
+  "read_only": 0
  }, 
  {
   "default": "In Store", 
@@ -75,6 +79,7 @@
   "no_copy": 1, 
   "oldfieldname": "serial_no", 
   "oldfieldtype": "Data", 
+  "read_only": 0, 
   "reqd": 1, 
   "search_index": 1
  }, 
@@ -88,13 +93,15 @@
   "oldfieldname": "item_code", 
   "oldfieldtype": "Link", 
   "options": "Item", 
+  "read_only": 0, 
   "reqd": 1, 
   "search_index": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break1", 
-  "fieldtype": "Column Break"
+  "fieldtype": "Column Break", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
@@ -146,12 +153,14 @@
   "doctype": "DocField", 
   "fieldname": "purchase_details", 
   "fieldtype": "Section Break", 
-  "label": "Purchase Details"
+  "label": "Purchase Details", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break2", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -160,7 +169,8 @@
   "fieldtype": "Select", 
   "label": "Purchase Document Type", 
   "no_copy": 1, 
-  "options": "\nPurchase Receipt\nStock Entry"
+  "options": "\nPurchase Receipt\nStock Entry", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
@@ -168,7 +178,8 @@
   "fieldtype": "Data", 
   "hidden": 0, 
   "label": "Purchase Document No", 
-  "no_copy": 1
+  "no_copy": 1, 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
@@ -179,6 +190,7 @@
   "no_copy": 1, 
   "oldfieldname": "purchase_date", 
   "oldfieldtype": "Date", 
+  "read_only": 0, 
   "reqd": 0, 
   "search_index": 0
  }, 
@@ -188,6 +200,7 @@
   "fieldtype": "Time", 
   "label": "Incoming Time", 
   "no_copy": 1, 
+  "read_only": 0, 
   "reqd": 1
  }, 
  {
@@ -200,6 +213,7 @@
   "oldfieldname": "purchase_rate", 
   "oldfieldtype": "Currency", 
   "options": "Company:company:default_currency", 
+  "read_only": 0, 
   "reqd": 1, 
   "search_index": 0
  }, 
@@ -207,6 +221,7 @@
   "doctype": "DocField", 
   "fieldname": "column_break3", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -220,6 +235,7 @@
   "oldfieldname": "warehouse", 
   "oldfieldtype": "Link", 
   "options": "Warehouse", 
+  "read_only": 0, 
   "reqd": 0, 
   "search_index": 1
  }, 
@@ -230,7 +246,8 @@
   "in_filter": 1, 
   "label": "Supplier", 
   "no_copy": 1, 
-  "options": "Supplier"
+  "options": "Supplier", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
@@ -254,12 +271,14 @@
   "fieldname": "delivery_details", 
   "fieldtype": "Section Break", 
   "label": "Delivery Details", 
-  "oldfieldtype": "Column Break"
+  "oldfieldtype": "Column Break", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break4", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -318,12 +337,14 @@
   "oldfieldname": "is_cancelled", 
   "oldfieldtype": "Select", 
   "options": "\nYes\nNo", 
+  "read_only": 0, 
   "report_hide": 1
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break5", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -378,12 +399,14 @@
   "doctype": "DocField", 
   "fieldname": "warranty_amc_details", 
   "fieldtype": "Section Break", 
-  "label": "Warranty / AMC Details"
+  "label": "Warranty / AMC Details", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break6", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -396,6 +419,7 @@
   "oldfieldname": "maintenance_status", 
   "oldfieldtype": "Select", 
   "options": "\nUnder Warranty\nOut of Warranty\nUnder AMC\nOut of AMC", 
+  "read_only": 0, 
   "search_index": 1, 
   "width": "150px"
  }, 
@@ -406,12 +430,14 @@
   "label": "Warranty Period (Days)", 
   "oldfieldname": "warranty_period", 
   "oldfieldtype": "Int", 
+  "read_only": 0, 
   "width": "150px"
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "column_break7", 
   "fieldtype": "Column Break", 
+  "read_only": 0, 
   "width": "50%"
  }, 
  {
@@ -422,6 +448,7 @@
   "label": "Warranty Expiry Date", 
   "oldfieldname": "warranty_expiry_date", 
   "oldfieldtype": "Date", 
+  "read_only": 0, 
   "width": "150px"
  }, 
  {
@@ -432,6 +459,7 @@
   "label": "AMC Expiry Date", 
   "oldfieldname": "amc_expiry_date", 
   "oldfieldtype": "Date", 
+  "read_only": 0, 
   "search_index": 0, 
   "width": "150px"
  }, 
@@ -439,13 +467,15 @@
   "doctype": "DocField", 
   "fieldname": "more_info", 
   "fieldtype": "Section Break", 
-  "label": "More Info"
+  "label": "More Info", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
   "fieldname": "serial_no_details", 
   "fieldtype": "Text Editor", 
-  "label": "Serial No Details"
+  "label": "Serial No Details", 
+  "read_only": 0
  }, 
  {
   "doctype": "DocField", 
@@ -454,6 +484,7 @@
   "in_filter": 1, 
   "label": "Company", 
   "options": "link:Company", 
+  "read_only": 0, 
   "reqd": 1, 
   "search_index": 1
  }, 
@@ -464,6 +495,7 @@
   "in_filter": 1, 
   "label": "Fiscal Year", 
   "options": "link:Fiscal Year", 
+  "read_only": 0, 
   "reqd": 1, 
   "search_index": 1
  }, 
@@ -488,53 +520,9 @@
   "report_hide": 1
  }, 
  {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 1, 
-  "report": 0, 
-  "role": "Material Manager", 
-  "write": 0
- }, 
- {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
-  "role": "Material Manager", 
-  "write": 0
- }, 
- {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 1, 
-  "report": 0, 
-  "role": "Material User", 
-  "write": 0
- }, 
- {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
-  "role": "Material User", 
-  "write": 0
- }, 
- {
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
   "role": "System Manager", 
   "write": 1
  }, 
@@ -542,8 +530,6 @@
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
   "role": "Material Master Manager", 
   "write": 1
  }, 
@@ -552,17 +538,15 @@
   "cancel": 0, 
   "create": 0, 
   "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 1, 
-  "role": "System Manager"
+  "role": "Material Manager", 
+  "write": 0
  }, 
  {
   "amend": 0, 
   "cancel": 0, 
   "create": 0, 
   "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 1, 
-  "role": "Sales Master Manager"
+  "role": "Material User", 
+  "write": 0
  }
 ]
\ No newline at end of file