mirror of
https://github.com/frappe/erpnext.git
synced 2026-04-27 02:28:30 +00:00
[trend analyzer] cleanup
This commit is contained in:
@@ -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 @@ def get_data(filters, tab, details):
|
||||
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 @@ def get_data(filters, tab, details):
|
||||
|
||||
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 @@ def period_wise_colums_query(filters, trans):
|
||||
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 @@ def period_wise_colums_query(filters, trans):
|
||||
|
||||
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 @@ def period_wise_colums_query(filters, trans):
|
||||
|
||||
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 @@ def basedon_wise_colums_query(based_on, trans):
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user