mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-01 11:19:09 +00:00
[Report][Quotation Trend]
This commit is contained in:
@@ -155,10 +155,8 @@ for r in res:
|
|||||||
for d in range(len(colnames) - cr):
|
for d in range(len(colnames) - cr):
|
||||||
r.append(flt(main_det[0][d]))
|
r.append(flt(main_det[0][d]))
|
||||||
out.append(r)
|
out.append(r)
|
||||||
|
|
||||||
if group_by:
|
if group_by:
|
||||||
flag = 1
|
flag = 1
|
||||||
# check for root nodes
|
|
||||||
if based_on in ['Item Group','Customer Group','Territory']:
|
if based_on in ['Item Group','Customer Group','Territory']:
|
||||||
is_grp = sql("select is_group from `tab%s` where name = '%s'" % (based_on, cstr(r[col_idx[based_on]]).strip()))
|
is_grp = sql("select is_group from `tab%s` where name = '%s'" % (based_on, cstr(r[col_idx[based_on]]).strip()))
|
||||||
is_grp = is_grp and cstr(is_grp[0][0]) or ''
|
is_grp = is_grp and cstr(is_grp[0][0]) or ''
|
||||||
@@ -167,11 +165,11 @@ for r in res:
|
|||||||
|
|
||||||
if flag == 1:
|
if flag == 1:
|
||||||
det = [x[0] for x in sql("SELECT DISTINCT %s FROM %s where %s" % (sel_col, add_tab, add_cond % {'value':cstr(r[col_idx[based_on]]).strip()}))]
|
det = [x[0] for x in sql("SELECT DISTINCT %s FROM %s where %s" % (sel_col, add_tab, add_cond % {'value':cstr(r[col_idx[based_on]]).strip()}))]
|
||||||
|
|
||||||
for des in range(len(det)):
|
for des in range(len(det)):
|
||||||
t_row = ['' for i in range(len(colnames))]
|
t_row = ['' for i in range(len(colnames))]
|
||||||
t_row[col_idx[group_by]] = cstr(det[des])
|
t_row[col_idx[group_by]] = cstr(det[des])
|
||||||
gr_det = sql("SELECT %s FROM %s WHERE %s = '%s' and %s" % (query_val, add_tab, sel_col, cstr(det[des]), add_cond % {'value':cstr(r[col_idx[based_on]]).strip()}))
|
gr_det = sql("SELECT %s FROM %s WHERE %s = '%s' and %s" % (query_val, add_tab, sel_col, cstr(det[des]), add_cond % {'value':cstr(r[col_idx[based_on]]).strip()}))
|
||||||
|
webnotes.errprint(cstr(r[col_idx[based_on]]).strip())
|
||||||
for d in range(len(col_names)):
|
for d in range(len(col_names)):
|
||||||
t_row[col_idx[col_names[d]]] = flt(gr_det[0][d])
|
t_row[col_idx[col_names[d]]] = flt(gr_det[0][d])
|
||||||
out.append(t_row)
|
out.append(t_row)
|
||||||
@@ -19,21 +19,21 @@ wn.query_reports["Quotation Trends"] = {
|
|||||||
"label": "Group By",
|
"label": "Group By",
|
||||||
"fieldtype": "Select",
|
"fieldtype": "Select",
|
||||||
"options": "Item"+NEWLINE+"Customer",
|
"options": "Item"+NEWLINE+"Customer",
|
||||||
"default": "Customer"
|
"default": ""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname":"fiscal_year",
|
"fieldname":"fiscal_year",
|
||||||
"label": "Fiscal Year",
|
"label": "Fiscal Year",
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"options":'Fiscal Year',
|
"options":'Fiscal Year',
|
||||||
"default": "Fiscal Year"
|
"default": sys_defaults.fiscal_year
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname":"company",
|
"fieldname":"company",
|
||||||
"label": "Company",
|
"label": "Company",
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"options": "Company",
|
"options": "Company",
|
||||||
"default": "Company"
|
"default": sys_defaults.company
|
||||||
},
|
},
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -16,78 +16,154 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import webnotes
|
import webnotes
|
||||||
from webnotes.utils import getdate, cint
|
from webnotes.utils import cint, add_days, add_months, cstr
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
if not filters: filters ={}
|
if not filters: filters ={}
|
||||||
|
|
||||||
period = filters.get("period")
|
# Global data
|
||||||
based_on = filters.get("based_on")
|
ysd = webnotes.conn.sql("select year_start_date from `tabFiscal Year` where name = '%s'"%filters.get("fiscal_year"))[0][0]
|
||||||
group_by = filters.get("group_by")
|
year_start_date = ysd.strftime('%Y-%m-%d')
|
||||||
|
start_month = cint(year_start_date.split('-')[1])
|
||||||
|
|
||||||
columns = get_columns(filters, period, based_on, group_by)
|
columns, query_bon, query_pwc, group_by = get_columns(filters, year_start_date, start_month)
|
||||||
data = []
|
|
||||||
|
ptab = "tabQuotation"
|
||||||
|
ctab = "tabQuotation Item"
|
||||||
|
data = get_data(filters, ptab, ctab, query_bon, query_pwc, group_by)
|
||||||
|
|
||||||
return columns, data
|
return columns, data
|
||||||
|
|
||||||
def get_columns(filters, period, based_on, group_by):
|
def get_columns(filters, year_start_date, start_month):
|
||||||
columns = []
|
columns, pwc, bon, gby = [], [], [], []
|
||||||
pwc = []
|
query_bon, query_pwc = '', ''
|
||||||
bon = []
|
|
||||||
gby = []
|
period = filters.get("period")
|
||||||
|
based_on = filters.get("based_on")
|
||||||
|
grby = filters.get("group_by")
|
||||||
|
|
||||||
if not (period and based_on):
|
if not (period and based_on):
|
||||||
webnotes.msgprint("Value missing in 'Period' or 'Based On'",raise_exception=1)
|
webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1)
|
||||||
elif based_on == group_by:
|
elif based_on == grby:
|
||||||
webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'")
|
webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1)
|
||||||
else:
|
else:
|
||||||
pwc = period_wise_column(filters, period, pwc)
|
bon,query_bon,group_by = base_wise_column(based_on, bon)
|
||||||
bon = base_wise_column(based_on, bon)
|
pwc,query_pwc = period_wise_column_and_query(filters, period, pwc, year_start_date,start_month)
|
||||||
gby = gruoup_wise_column(group_by)
|
gby = gruoup_wise_column(grby)
|
||||||
|
|
||||||
if gby:
|
if gby:
|
||||||
columns = bon + gby + pwc
|
columns = bon + gby + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
|
||||||
else:
|
else:
|
||||||
columns = bon + pwc
|
columns = bon + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"]
|
||||||
return columns
|
|
||||||
|
|
||||||
|
return columns, query_bon, query_pwc, group_by
|
||||||
|
|
||||||
def period_wise_column(filters, period, pwc):
|
def get_data(filters, ptab, ctab, query_bon, query_pwc, group_by):
|
||||||
|
query_details = query_bon + query_pwc + 'SUM(t2.qty), SUM(t1.grand_total) '
|
||||||
|
query_pwc = query_pwc + 'SUM(t2.qty), SUM(t1.grand_total)'
|
||||||
|
if not filters.get("group_by"):
|
||||||
|
data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2
|
||||||
|
where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' and t1.docstatus = 1
|
||||||
|
group by %s
|
||||||
|
"""%(query_details, ptab, ctab, filters.get("company"), filters.get("fiscal_year"), group_by), as_list=1)
|
||||||
|
|
||||||
|
# No coma is included between %s and t2.item_code cause it's already bounded with query_bon
|
||||||
|
if filters.get("group_by") == 'Item':
|
||||||
|
data = webnotes.conn.sql(""" select %s t2.item_code, %s from `%s` t1, `%s` t2
|
||||||
|
where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' and t1.docstatus = 1
|
||||||
|
group by %s, %s"""%( query_bon, query_pwc, ptab, ctab, filters.get("company"), filters.get("fiscal_year"),
|
||||||
|
group_by,'t2.item_code'), as_list=1)
|
||||||
|
|
||||||
|
if filters.get("group_by") == 'Customer':
|
||||||
|
data = webnotes.conn.sql(""" select %s t1.customer_name, %s from `%s` t1, `%s` t2
|
||||||
|
where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' and t1.docstatus = 1
|
||||||
|
group by %s, %s"""%(query_bon, query_pwc, ptab, ctab, filters.get("company"), filters.get("fiscal_year"), group_by, 't1.customer_name'), as_list=1)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
def period_wise_column_and_query(filters, period, pwc, year_start_date, start_month):
|
||||||
|
query_details = ''
|
||||||
|
|
||||||
if period == "Monthly":
|
if 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(0,len(month_name)):
|
for month in range(start_month-1,len(month_name)):
|
||||||
pwc.append(month_name[month]+' (Qty):Float:120')
|
pwc.append(month_name[month]+' (Qty):Float:120')
|
||||||
pwc.append(month_name[month]+' (Amt):Currency:120')
|
pwc.append(month_name[month]+' (Amt):Currency:120')
|
||||||
|
query_details += """Sum(CASE WHEN MONTH(t1.transaction_date)= %(mon_num)s THEN t2.qty ELSE NULL END),
|
||||||
|
SUM(CASE WHEN MONTH(t1.transaction_date)= %(mon_num)s THEN t1.grand_total ELSE NULL END),
|
||||||
|
"""%{"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(CASE WHEN MONTH(t1.transaction_date)= %(mon_num)s THEN t2.qty ELSE NULL END),
|
||||||
|
SUM(CASE WHEN MONTH(t1.transaction_date)= %(mon_num)s THEN t1.grand_total ELSE NULL END),
|
||||||
|
"""%{"mon_num": cstr(month+1)}
|
||||||
|
|
||||||
elif period == "Quarterly":
|
elif period == "Quarterly":
|
||||||
pwc = ["Q1(qty):Float:120", "Q1(amt):Currency:120", "Q2(qty):Float:120", "Q2(amt):Currency:120",
|
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"
|
"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.transaction_date BETWEEN '%(sd)s' AND '%(ed)s' THEN t2.qty ELSE NULL END),
|
||||||
|
SUM(CASE WHEN t1.transaction_date BETWEEN '%(sd)s' AND '%(ed)s' THEN t1.grand_total ELSE NULL END),
|
||||||
|
"""%{"sd": d[0],"ed": d[1]}
|
||||||
|
|
||||||
elif period == "Half-yearly":
|
elif period == "Half-yearly":
|
||||||
pwc = ["Fisrt Half(qty):Float:120", "Fisrt Half(amt):Currency:120", "Second Half(qty):Float:120",
|
pwc = ["Fisrt Half(qty):Float:120", "Fisrt Half(amt):Currency:120", "Second Half(qty):Float:120",
|
||||||
"Second Half(amt):Currency:120"
|
"Second Half(amt):Currency:120"]
|
||||||
]
|
|
||||||
|
first_half_start = year_start_date
|
||||||
|
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)
|
||||||
|
|
||||||
|
query_details = """ SUM(CASE WHEN t1.transaction_date BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t2.qty ELSE NULL END),
|
||||||
|
SUM(CASE WHEN t1.transaction_date BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t1.grand_total ELSE NULL END),
|
||||||
|
SUM(CASE WHEN t1.transaction_date BETWEEN '%(shs)s' AND '%(she)s' THEN t2.qty ELSE NULL END),
|
||||||
|
SUM(CASE WHEN t1.transaction_date BETWEEN '%(shs)s' AND '%(she)s' THEN t1.grand_total ELSE NULL END),
|
||||||
|
"""%{"fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start, "she": second_half_end}
|
||||||
|
|
||||||
else:
|
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),"
|
||||||
|
|
||||||
return pwc
|
return pwc, query_details
|
||||||
|
|
||||||
def base_wise_column(based_on, bon):
|
def base_wise_column(based_on, bon):
|
||||||
if based_on == "Item":
|
if based_on == "Item":
|
||||||
bon = ["Item:Link/Item:120", "Item Name:Data:120"]
|
bon = ["Item:Link/Item:120", "Item Name:Data:120"]
|
||||||
|
query_details = "t2.item_code, t2.item_name,"
|
||||||
|
group_by = 't2.item_code'
|
||||||
|
|
||||||
elif based_on == "Item Group":
|
elif based_on == "Item Group":
|
||||||
bon = ["Item Group:Link/Item Group:120"]
|
bon = ["Item Group:Link/Item Group:120"]
|
||||||
|
query_details = "t2.item_group,"
|
||||||
|
group_by = 't2.item_group'
|
||||||
|
|
||||||
elif based_on == "Customer":
|
elif based_on == "Customer":
|
||||||
bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
|
bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"]
|
||||||
|
query_details = "t1.customer_name, t1.territory, "
|
||||||
|
group_by = 't1.customer_name'
|
||||||
|
|
||||||
elif based_on == "Customer Group":
|
elif based_on == "Customer Group":
|
||||||
bon = ["Customer Group:Link/Customer Group"]
|
bon = ["Customer Group:Link/Customer Group"]
|
||||||
|
query_details = "t1.customer_group, "
|
||||||
|
group_by = 't1.customer_group'
|
||||||
|
|
||||||
elif based_on == "Territory":
|
elif based_on == "Territory":
|
||||||
bon = ["Territory:Link/Territory:120"]
|
bon = ["Territory:Link/Territory:120"]
|
||||||
|
query_details = "t1.territory, "
|
||||||
|
group_by = 't1.territory'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
bon = ["Project:Link/Project:120"]
|
bon = ["Project:Link/Project:120"]
|
||||||
return bon
|
return bon, query_details, group_by
|
||||||
|
|
||||||
def gruoup_wise_column(group_by):
|
def gruoup_wise_column(group_by):
|
||||||
if group_by:
|
if group_by:
|
||||||
|
|||||||
Reference in New Issue
Block a user