diff --git a/accounts/search_criteria/trend_analyzer/trend_analyzer.py b/accounts/search_criteria/trend_analyzer/trend_analyzer.py index 9bdde392b4..6bc4cf6f69 100644 --- a/accounts/search_criteria/trend_analyzer/trend_analyzer.py +++ b/accounts/search_criteria/trend_analyzer/trend_analyzer.py @@ -155,8 +155,10 @@ for r in res: for d in range(len(colnames) - cr): r.append(flt(main_det[0][d])) out.append(r) + if group_by: flag = 1 + # check for root nodes 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 = is_grp and cstr(is_grp[0][0]) or '' @@ -165,11 +167,11 @@ for r in res: 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()}))] + for des in range(len(det)): t_row = ['' for i in range(len(colnames))] 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()})) - webnotes.errprint(cstr(r[col_idx[based_on]]).strip()) for d in range(len(col_names)): t_row[col_idx[col_names[d]]] = flt(gr_det[0][d]) out.append(t_row) \ No newline at end of file diff --git a/selling/report/quotation_trends/quotation_trends.py b/selling/report/quotation_trends/quotation_trends.py index 6a81d97f16..7f944e0f23 100644 --- a/selling/report/quotation_trends/quotation_trends.py +++ b/selling/report/quotation_trends/quotation_trends.py @@ -22,19 +22,18 @@ def execute(filters=None): if not filters: filters ={} # Global data + trans = "Quotation" + tab = ["tabQuotation","tabQuotation Item"] 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]) - columns, query_bon, query_pwc, group_by = get_columns(filters, year_start_date, start_month) - - ptab = "tabQuotation" - ctab = "tabQuotation Item" - data = get_data(filters, ptab, ctab, query_bon, query_pwc, group_by) + columns, query_bon, query_pwc, group_by, gby = get_columns(filters, year_start_date, start_month, trans) + data = get_data(columns,filters, tab, query_bon, query_pwc, group_by, gby) return columns, data -def get_columns(filters, year_start_date, start_month): +def get_columns(filters, year_start_date, start_month, trans): columns, pwc, bon, gby = [], [], [], [] query_bon, query_pwc = '', '' @@ -44,11 +43,13 @@ def get_columns(filters, year_start_date, start_month): if not (period and based_on): webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1) + elif based_on == grby: webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1) + else: bon,query_bon,group_by = base_wise_column(based_on, bon) - pwc,query_pwc = period_wise_column_and_query(filters, period, pwc, year_start_date,start_month) + pwc,query_pwc = period_wise_column_and_query(filters, period, pwc, year_start_date,start_month, trans) gby = gruoup_wise_column(grby) if gby: @@ -56,49 +57,95 @@ def get_columns(filters, year_start_date, start_month): else: columns = bon + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] - return columns, query_bon, query_pwc, group_by + return columns, query_bon, query_pwc, group_by,gby -def get_data(filters, ptab, ctab, query_bon, query_pwc, group_by): +def get_data(columns, filters, tab, query_bon, query_pwc, group_by,gby): + 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 = [] + inc = '' + + if filters.get("group_by"): + sel_col = '' + if filters.get("group_by") == 'Item': + sel_col = 't2.item_code' + + elif filters.get("group_by") == 'Customer': + sel_col = 't1.customer' + + if filters.get('based_on') in ['Item','Customer']: + inc = 2 + else : + inc = 1 + + ind = columns.index(gby[0]) + + data1 = 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 group by %s """%(query_bon, query_pwc, tab[0], tab[1], + filters.get("company"), filters.get("fiscal_year"), group_by), as_list=1) + + for d in range(len(data1)): + + dt = data1[d] + dt.insert(ind,'') + data.append(dt) + + 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"), group_by, data1[d][0]),as_list=1) + + for i in range(len(row)): + des = ['' for q in range(len(columns))] + + 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], group_by, + data1[d][0]),as_list=1) + + des[ind] = row[i] + for j in range(1,len(columns)-inc): + des[j+inc] = row1[0][j] + data.append(des) + else: + 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) + where t2.parent = t1.name and t1.company = '%s' and t1.fiscal_year = '%s' + and t1.docstatus = 1 group by %s + """%(query_details, tab[0], tab[1], 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): +def period_wise_column_and_query(filters, period, pwc, year_start_date, start_month, trans): query_details = '' + if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']: + trans_date = 'posting_date' + else: + trans_date = 'transaction_date' if 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(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)} + + query_details += """ + Sum(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t2.qty ELSE NULL END), + 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)} 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)} + + query_details += """ + Sum(CASE WHEN MONTH(t1.%(trans)s)= %(mon_num)s THEN t2.qty ELSE NULL END), + 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", @@ -110,9 +157,9 @@ def period_wise_column_and_query(filters, period, pwc, year_start_date, start_mo 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]} + 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", @@ -123,11 +170,12 @@ def period_wise_column_and_query(filters, period, pwc, year_start_date, start_mo 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} + query_details = """ + SUM(CASE WHEN t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t2.qty ELSE NULL END), + SUM(CASE WHEN t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s' THEN t1.grand_total ELSE NULL END), + SUM(CASE WHEN t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s' THEN t2.qty ELSE NULL END), + SUM(CASE WHEN t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s' THEN t1.grand_total ELSE NULL END), + """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start, "she": second_half_end} else: pwc = [filters.get("fiscal_year")+"(qty):Float:120", filters.get("fiscal_year")+"(amt):Currency:120"] diff --git a/selling/report/quotation_trends/quotation_trends.txt b/selling/report/quotation_trends/quotation_trends.txt index a135c34560..eebffcf84b 100644 --- a/selling/report/quotation_trends/quotation_trends.txt +++ b/selling/report/quotation_trends/quotation_trends.txt @@ -2,12 +2,12 @@ { "creation": "2013-06-07 16:01:16", "docstatus": 0, - "modified": "2013-06-07 16:01:16", + "modified": "2013-06-12 16:31:23", "modified_by": "Administrator", "owner": "Administrator" }, { - "add_total_row": 1, + "add_total_row": 0, "doctype": "Report", "is_standard": "Yes", "name": "__common__",