[trend analyzer] cleanup

This commit is contained in:
Nabin Hait 2013-06-20 13:03:10 +05:30
parent 616657d4a2
commit b8ebbcafa9
3 changed files with 100 additions and 126 deletions

View File

@ -16,33 +16,39 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import webnotes import webnotes
from webnotes.utils import cint, add_days, add_months, cstr from webnotes.utils import add_days, add_months, cstr, getdate
from datetime import datetime from webnotes import _
def get_columns(filters, trans): def get_columns(filters, trans):
validate_filters(filters)
if not (filters.get("period") and filters.get("based_on")): # based_on_cols, based_on_select, based_on_group_by, addl_tables
webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
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) 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) pwc, query_pwc = period_wise_colums_query(filters, trans)
# group_by_cols
grbc = group_wise_column(filters.get("group_by")) grbc = group_wise_column(filters.get("group_by"))
columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
if grbc: if grbc:
columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] 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, # conditions
"grbc": grbc, "sup_tab": sup_tab} details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns,
"basedon": based, "grbc": grbc, "sup_tab": sup_tab}
return details 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 = [] data = []
inc, cond= '','' inc, cond= '',''
query_details = details["query_bon"] + details["query_pwc"] query_details = details["query_bon"] + details["query_pwc"]
@ -98,15 +104,15 @@ def get_data(filters, tab, details):
and t1.docstatus = 1 and %s = %s and %s = %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"), "%s", "%s", sel_col, "%s", details["basedon"], "%s"),
(filters.get("company"), filters.get("fiscal_year"), row[i][0], data1[d][0]), (filters.get("company"), filters.get("fiscal_year"),
as_list=1) row[i][0], data1[d][0]), as_list=1)
des[ind] = row[i] des[ind] = row[i]
for j in range(1,len(details["columns"])-inc): for j in range(1,len(details["columns"])-inc):
des[j+inc] = row1[0][j] des[j+inc] = row1[0][j]
data.append(des) data.append(des)
else: else:
data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s
where t2.parent = t1.name and t1.company = %s where t2.parent = t1.name and t1.company = %s
and t1.fiscal_year = %s and t1.docstatus = 1 %s and t1.fiscal_year = %s and t1.docstatus = 1 %s
@ -118,20 +124,13 @@ def get_data(filters, tab, details):
return data return data
def get_mon(date): def get_mon(dt):
"""convert srting formated date into date and retrieve month abbrevation""" return getdate(dt).strftime("%b")
return (datetime.strptime(date, '%Y-%m-%d')).strftime("%b")
def period_wise_colums_query(filters, trans): def period_wise_colums_query(filters, trans):
from datetime import datetime
query_details = '' query_details = ''
pwc = [] pwc = []
ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year` where name = '%s' ysd = webnotes.conn.get_value("Fiscal year", filters.get("fiscal_year"), "year_start_date")
"""%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']: if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']:
trans_date = 'posting_date' trans_date = 'posting_date'
@ -141,27 +140,13 @@ def period_wise_colums_query(filters, trans):
if filters.get("period") == "Monthly": if filters.get("period") == "Monthly":
month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
for month in range(start_month-1,len(month_name)): for month_idx in range(ysd.month-1,len(month_name)) + range(0, ysd.month-1):
pwc.append(month_name[month]+' (Qty):Float:120') query_details = get_monthly_conditions(month_name, month_idx, trans_date,
pwc.append(month_name[month]+' (Amt):Currency:120') pwc, query_details)
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)}
elif filters.get("period") == "Quarterly": 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) 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]] bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]]
@ -178,7 +163,7 @@ def period_wise_colums_query(filters, trans):
elif filters.get("period") == "Half-yearly": 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) first_half_end = add_days(add_months(first_half_start,6),-1)
second_half_start = add_days(first_half_end,1) second_half_start = add_days(first_half_end,1)
second_half_end = add_days(add_months(second_half_start,6),-1) second_half_end = add_days(add_months(second_half_start,6),-1)
@ -201,6 +186,17 @@ def period_wise_colums_query(filters, trans):
query_details += 'SUM(t2.qty), SUM(t1.grand_total)' query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
return pwc, query_details 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): def basedon_wise_colums_query(based_on, trans):
sup_tab = '' sup_tab = ''
@ -242,19 +238,16 @@ def basedon_wise_colums_query(based_on, trans):
based = 't1.territory' based = 't1.territory'
elif based_on == "Project": elif based_on == "Project":
if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']: if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']:
bon = ["Project:Link/Project:120"] bon = ["Project:Link/Project:120"]
query_details = "t1.project_name," query_details = "t1.project_name,"
based = 't1.project_name' based = 't1.project_name'
elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']: elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
bon = ["Project:Link/Project:120"] bon = ["Project:Link/Project:120"]
query_details = "t2.project_name," query_details = "t2.project_name,"
based = 't2.project_name' based = 't2.project_name'
else: 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 return bon, query_details, based, sup_tab

View File

@ -28,7 +28,4 @@ def execute(filters=None):
details = get_columns(filters, trans) details = get_columns(filters, trans)
data = get_data(filters, tab, details) data = get_data(filters, tab, details)
if not data:
webnotes.msgprint("Data not found for selected criterias")
return details["columns"], data return details["columns"], data

View File

@ -1,14 +1,14 @@
[ [
{ {
"creation": "2013-01-29 19:25:41", "creation": "2013-05-16 10:59:15",
"docstatus": 0, "docstatus": 0,
"modified": "2013-01-29 16:27:57", "modified": "2013-06-20 11:23:01",
"modified_by": "Administrator", "modified_by": "Administrator",
"owner": "Administrator" "owner": "Administrator"
}, },
{ {
"allow_attach": 1, "allow_attach": 1,
"allow_rename": 1, "allow_rename": 0,
"autoname": "field:serial_no", "autoname": "field:serial_no",
"description": "Distinct unit of an Item", "description": "Distinct unit of an Item",
"doctype": "DocType", "doctype": "DocType",
@ -31,7 +31,9 @@
"parent": "Serial No", "parent": "Serial No",
"parentfield": "permissions", "parentfield": "permissions",
"parenttype": "DocType", "parenttype": "DocType",
"permlevel": 0,
"read": 1, "read": 1,
"report": 1,
"submit": 0 "submit": 0
}, },
{ {
@ -43,12 +45,14 @@
"fieldname": "details", "fieldname": "details",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Details", "label": "Details",
"oldfieldtype": "Section Break" "oldfieldtype": "Section Break",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break0", "fieldname": "column_break0",
"fieldtype": "Column Break" "fieldtype": "Column Break",
"read_only": 0
}, },
{ {
"default": "In Store", "default": "In Store",
@ -75,6 +79,7 @@
"no_copy": 1, "no_copy": 1,
"oldfieldname": "serial_no", "oldfieldname": "serial_no",
"oldfieldtype": "Data", "oldfieldtype": "Data",
"read_only": 0,
"reqd": 1, "reqd": 1,
"search_index": 1 "search_index": 1
}, },
@ -88,13 +93,15 @@
"oldfieldname": "item_code", "oldfieldname": "item_code",
"oldfieldtype": "Link", "oldfieldtype": "Link",
"options": "Item", "options": "Item",
"read_only": 0,
"reqd": 1, "reqd": 1,
"search_index": 0 "search_index": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break1", "fieldname": "column_break1",
"fieldtype": "Column Break" "fieldtype": "Column Break",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
@ -146,12 +153,14 @@
"doctype": "DocField", "doctype": "DocField",
"fieldname": "purchase_details", "fieldname": "purchase_details",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Purchase Details" "label": "Purchase Details",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break2", "fieldname": "column_break2",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -160,7 +169,8 @@
"fieldtype": "Select", "fieldtype": "Select",
"label": "Purchase Document Type", "label": "Purchase Document Type",
"no_copy": 1, "no_copy": 1,
"options": "\nPurchase Receipt\nStock Entry" "options": "\nPurchase Receipt\nStock Entry",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
@ -168,7 +178,8 @@
"fieldtype": "Data", "fieldtype": "Data",
"hidden": 0, "hidden": 0,
"label": "Purchase Document No", "label": "Purchase Document No",
"no_copy": 1 "no_copy": 1,
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
@ -179,6 +190,7 @@
"no_copy": 1, "no_copy": 1,
"oldfieldname": "purchase_date", "oldfieldname": "purchase_date",
"oldfieldtype": "Date", "oldfieldtype": "Date",
"read_only": 0,
"reqd": 0, "reqd": 0,
"search_index": 0 "search_index": 0
}, },
@ -188,6 +200,7 @@
"fieldtype": "Time", "fieldtype": "Time",
"label": "Incoming Time", "label": "Incoming Time",
"no_copy": 1, "no_copy": 1,
"read_only": 0,
"reqd": 1 "reqd": 1
}, },
{ {
@ -200,6 +213,7 @@
"oldfieldname": "purchase_rate", "oldfieldname": "purchase_rate",
"oldfieldtype": "Currency", "oldfieldtype": "Currency",
"options": "Company:company:default_currency", "options": "Company:company:default_currency",
"read_only": 0,
"reqd": 1, "reqd": 1,
"search_index": 0 "search_index": 0
}, },
@ -207,6 +221,7 @@
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break3", "fieldname": "column_break3",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -220,6 +235,7 @@
"oldfieldname": "warehouse", "oldfieldname": "warehouse",
"oldfieldtype": "Link", "oldfieldtype": "Link",
"options": "Warehouse", "options": "Warehouse",
"read_only": 0,
"reqd": 0, "reqd": 0,
"search_index": 1 "search_index": 1
}, },
@ -230,7 +246,8 @@
"in_filter": 1, "in_filter": 1,
"label": "Supplier", "label": "Supplier",
"no_copy": 1, "no_copy": 1,
"options": "Supplier" "options": "Supplier",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
@ -254,12 +271,14 @@
"fieldname": "delivery_details", "fieldname": "delivery_details",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Delivery Details", "label": "Delivery Details",
"oldfieldtype": "Column Break" "oldfieldtype": "Column Break",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break4", "fieldname": "column_break4",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -318,12 +337,14 @@
"oldfieldname": "is_cancelled", "oldfieldname": "is_cancelled",
"oldfieldtype": "Select", "oldfieldtype": "Select",
"options": "\nYes\nNo", "options": "\nYes\nNo",
"read_only": 0,
"report_hide": 1 "report_hide": 1
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break5", "fieldname": "column_break5",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -378,12 +399,14 @@
"doctype": "DocField", "doctype": "DocField",
"fieldname": "warranty_amc_details", "fieldname": "warranty_amc_details",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Warranty / AMC Details" "label": "Warranty / AMC Details",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break6", "fieldname": "column_break6",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -396,6 +419,7 @@
"oldfieldname": "maintenance_status", "oldfieldname": "maintenance_status",
"oldfieldtype": "Select", "oldfieldtype": "Select",
"options": "\nUnder Warranty\nOut of Warranty\nUnder AMC\nOut of AMC", "options": "\nUnder Warranty\nOut of Warranty\nUnder AMC\nOut of AMC",
"read_only": 0,
"search_index": 1, "search_index": 1,
"width": "150px" "width": "150px"
}, },
@ -406,12 +430,14 @@
"label": "Warranty Period (Days)", "label": "Warranty Period (Days)",
"oldfieldname": "warranty_period", "oldfieldname": "warranty_period",
"oldfieldtype": "Int", "oldfieldtype": "Int",
"read_only": 0,
"width": "150px" "width": "150px"
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "column_break7", "fieldname": "column_break7",
"fieldtype": "Column Break", "fieldtype": "Column Break",
"read_only": 0,
"width": "50%" "width": "50%"
}, },
{ {
@ -422,6 +448,7 @@
"label": "Warranty Expiry Date", "label": "Warranty Expiry Date",
"oldfieldname": "warranty_expiry_date", "oldfieldname": "warranty_expiry_date",
"oldfieldtype": "Date", "oldfieldtype": "Date",
"read_only": 0,
"width": "150px" "width": "150px"
}, },
{ {
@ -432,6 +459,7 @@
"label": "AMC Expiry Date", "label": "AMC Expiry Date",
"oldfieldname": "amc_expiry_date", "oldfieldname": "amc_expiry_date",
"oldfieldtype": "Date", "oldfieldtype": "Date",
"read_only": 0,
"search_index": 0, "search_index": 0,
"width": "150px" "width": "150px"
}, },
@ -439,13 +467,15 @@
"doctype": "DocField", "doctype": "DocField",
"fieldname": "more_info", "fieldname": "more_info",
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "More Info" "label": "More Info",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
"fieldname": "serial_no_details", "fieldname": "serial_no_details",
"fieldtype": "Text Editor", "fieldtype": "Text Editor",
"label": "Serial No Details" "label": "Serial No Details",
"read_only": 0
}, },
{ {
"doctype": "DocField", "doctype": "DocField",
@ -454,6 +484,7 @@
"in_filter": 1, "in_filter": 1,
"label": "Company", "label": "Company",
"options": "link:Company", "options": "link:Company",
"read_only": 0,
"reqd": 1, "reqd": 1,
"search_index": 1 "search_index": 1
}, },
@ -464,6 +495,7 @@
"in_filter": 1, "in_filter": 1,
"label": "Fiscal Year", "label": "Fiscal Year",
"options": "link:Fiscal Year", "options": "link:Fiscal Year",
"read_only": 0,
"reqd": 1, "reqd": 1,
"search_index": 1 "search_index": 1
}, },
@ -487,54 +519,10 @@
"read_only": 1, "read_only": 1,
"report_hide": 1 "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, "cancel": 1,
"create": 1, "create": 1,
"doctype": "DocPerm", "doctype": "DocPerm",
"permlevel": 0,
"report": 1,
"role": "System Manager", "role": "System Manager",
"write": 1 "write": 1
}, },
@ -542,8 +530,6 @@
"cancel": 1, "cancel": 1,
"create": 1, "create": 1,
"doctype": "DocPerm", "doctype": "DocPerm",
"permlevel": 0,
"report": 1,
"role": "Material Master Manager", "role": "Material Master Manager",
"write": 1 "write": 1
}, },
@ -552,17 +538,15 @@
"cancel": 0, "cancel": 0,
"create": 0, "create": 0,
"doctype": "DocPerm", "doctype": "DocPerm",
"match": "", "role": "Material Manager",
"permlevel": 1, "write": 0
"role": "System Manager"
}, },
{ {
"amend": 0, "amend": 0,
"cancel": 0, "cancel": 0,
"create": 0, "create": 0,
"doctype": "DocPerm", "doctype": "DocPerm",
"match": "", "role": "Material User",
"permlevel": 1, "write": 0
"role": "Sales Master Manager"
} }
] ]