From 1979ad058608a8e4379614e472d385d999139f5b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 20 Oct 2011 19:20:16 +0530 Subject: [PATCH] Project wise analysis included in trend analyzer --- .../trend_analyzer/trend_analyzer.js | 10 +- .../trend_analyzer/trend_analyzer.py | 131 +++++++++--------- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.js b/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.js index 9a87abbd11d..a99de236e29 100644 --- a/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.js +++ b/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.js @@ -5,7 +5,7 @@ report.customize_filters = function() { this.add_filter({fieldname:'period', label:'Period', fieldtype:'Select', options:'Monthly'+NEWLINE+'Quarterly'+NEWLINE+'Half Yearly'+NEWLINE+'Annual',report_default:'Quarterly',ignore : 1, parent:'Profile',in_first_page:1,single_select:1}); - this.add_filter({fieldname:'based_on', label:'Based On', fieldtype:'Select', options:'Item'+NEWLINE+'Item Group'+NEWLINE+'Customer'+NEWLINE+'Customer Group'+NEWLINE+'Territory'+NEWLINE+'Supplier'+NEWLINE+'Supplier Type', ignore : 1, parent:'Profile', report_default:'Item', in_first_page : 1,single_select:1}); + this.add_filter({fieldname:'based_on', label:'Based On', fieldtype:'Select', options:'Item'+NEWLINE+'Item Group'+NEWLINE+'Customer'+NEWLINE+'Customer Group'+NEWLINE+'Territory'+NEWLINE+'Supplier'+NEWLINE+'Supplier Type'+NEWLINE+'Project', ignore : 1, parent:'Profile', report_default:'Item', in_first_page : 1,single_select:1}); this.add_filter({fieldname:'group_by', label:'Group By', fieldtype:'Select', options:NEWLINE+'Item'+NEWLINE+'Customer'+NEWLINE+'Supplier', ignore : 1, parent:'Profile',single_select:1}); @@ -24,6 +24,7 @@ report.customize_filters = function() { this.add_filter({fieldname:'territory', label:'Territory', fieldtype:'Link', options:'Territory', ignore : 1, parent:'Profile'}); this.add_filter({fieldname:'supplier', label:'Supplier', fieldtype:'Link', options:'Supplier', ignore : 1, parent:'Profile'}); this.add_filter({fieldname:'supplier_type', label:'Supplier Type', fieldtype:'Link', options:'Supplier Type', ignore : 1, parent:'Profile'}); + this.add_filter({fieldname:'project', label:'Project', fieldtype:'Link', options:'Project', ignore : 1, parent:'Profile'}); } @@ -99,6 +100,10 @@ report.get_query = function() { add_cond += ' AND t1.supplier = t3.name'; if(supp_type) add_cond += ' AND t1.supplier_type = "'+supp_type+'"'; break; + case 'Project' : pro = this.get_filter('Profile', 'Project').get_value(); + col = 'DISTINCT t1.project_name'; + if(pro) add_cond += ' AND t1.project_name = "'+pro+'"'; + break; case 'Item Group' : ig = this.get_filter('Profile', 'Item Group').get_value(); if(ig) sp_cond += ' AND parent.name = "'+ig+'"'; break; @@ -108,10 +113,11 @@ report.get_query = function() { case 'Territory' : ter = this.get_filter('Profile', 'Territory').get_value(); if(ter) sp_cond += ' AND parent.name = "'+ter+'"'; break; + } - if(based_on == 'Item' || based_on == 'Customer' || based_on == 'Supplier' || based_on == 'Supplier Type') + if(based_on == 'Item' || based_on == 'Customer' || based_on == 'Supplier' || based_on == 'Supplier Type' || based_on == 'Project') var q ='SELECT '+col+' FROM `tab'+trans+'` t1, `tab'+trans_det+'` t2 '+add_tables+' WHERE t1.fiscal_year = "'+fiscal_year+'" and t1.company = "'+company+'" and t2.parent = t1.name '+add_cond; else var q = 'SELECT CONCAT(REPEAT(" ", COUNT(parent.name) - 1), node.name) AS "Name" FROM `tab'+based_on+'` node,`tab'+based_on+'` parent WHERE node.lft BETWEEN parent.lft and parent.rgt and node.docstatus !=2 '+sp_cond+' GROUP BY node.name ORDER BY node.lft'; diff --git a/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.py b/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.py index 9edf04563b7..42d5d2f04b3 100644 --- a/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.py +++ b/erpnext/analysis/search_criteria/trend_analyzer/trend_analyzer.py @@ -17,7 +17,7 @@ customer_group = filter_values.get('customer_group') territory = filter_values.get('territory') supplier = filter_values.get('supplier') supplier_type = filter_values.get('supplier_type') - +project = filter_values.get('project') # ********************************************* SET DEFAULTS ************************************************** @@ -41,41 +41,41 @@ col_names.append('Total (Amt)') # ********************************************* VALIDATIONS *************************************************** if (based_on in ['Customer','Customer Group','Territory'] and group_by == 'Supplier') or (based_on in ['Supplier','Supplier Type'] and group_by == 'Customer'): - msgprint("Sorry! You cannot group Trend Analyzer based on %s by %s" % (based_on,group_by)) - raise Exception + msgprint("Sorry! You cannot group Trend Analyzer based on %s by %s" % (based_on,group_by)) + raise Exception if based_on == group_by: - msgprint("Based On and Group By value cannot be same for Trend Analyzer") - raise Exception + msgprint("Based On and Group By value cannot be same for Trend Analyzer") + raise Exception # ********************************************** ADD COLUMNS ********************************************** cols = [[based_on, 'Data', '300px', '']] cr = 1 if based_on == 'Item': - cols.append(['Item Name','Data','200px','']) - cr = 2 + cols.append(['Item Name','Data','200px','']) + cr = 2 elif based_on == 'Customer': - cols.append(['Territory','Link','150px','Territory']) - cr = 2 + cols.append(['Territory','Link','150px','Territory']) + cr = 2 elif based_on == 'Supplier': - cols.append(['Supplier Type','Link','150px','Supplier Type']) - cr = 2 + cols.append(['Supplier Type','Link','150px','Supplier Type']) + cr = 2 if group_by: - cr += 1 + cr += 1 if group_by: - cols.append([group_by,'Data','150px','']) + cols.append([group_by,'Data','150px','']) for c in col_names: - cols.append([c,'Currency','150px','']) + cols.append([c,'Currency','150px','']) for c in cols: - colnames.append(c[0]) - coltypes.append(c[1]) - colwidths.append(c[2]) - coloptions.append(c[3]) - col_idx[c[0]] = len(colnames)-1 + colnames.append(c[0]) + coltypes.append(c[1]) + colwidths.append(c[2]) + coloptions.append(c[3]) + col_idx[c[0]] = len(colnames)-1 # ******************************************* ADDITIONAL CONDITION ************************************************ @@ -86,75 +86,78 @@ if order_type: add_cond += ' AND t1.order_type = "%s"' % order_type # Item if item or based_on == 'Item': - add_cond += ' AND t2.item_code = "%s"' % (based_on != 'Item' and item or '%(value)s') + add_cond += ' AND t2.item_code = "%s"' % (based_on != 'Item' and item or '%(value)s') # Item Group if item_group or based_on == 'Item Group': - add_tab += ' ,`tabItem` t3, `tabItem Group` t4 ' - add_cond += ' AND t3.name = t2.item_code AND t3.item_group = t4.name and (t4.name = "%s" or t4.name IN (SELECT t5.name FROM `tabItem Group` t5,`tabItem Group` t6 WHERE t5.lft BETWEEN t6.lft and t6.rgt and t5.docstatus !=2 and ifnull(t5.is_group,"No") = "No" and t6.name = "%s"))' % (based_on != 'Item Group' and item_group or '%(value)s', based_on != 'Item Group' and item_group or '%(value)s') + add_tab += ' ,`tabItem` t3, `tabItem Group` t4 ' + add_cond += ' AND t3.name = t2.item_code AND t3.item_group = t4.name and (t4.name = "%s" or t4.name IN (SELECT t5.name FROM `tabItem Group` t5,`tabItem Group` t6 WHERE t5.lft BETWEEN t6.lft and t6.rgt and t5.docstatus !=2 and ifnull(t5.is_group,"No") = "No" and t6.name = "%s"))' % (based_on != 'Item Group' and item_group or '%(value)s', based_on != 'Item Group' and item_group or '%(value)s') # Customer if customer or based_on == 'Customer': - add_cond += ' AND t1.customer = "%s"' % (based_on != 'Customer' and customer or '%(value)s') + add_cond += ' AND t1.customer = "%s"' % (based_on != 'Customer' and customer or '%(value)s') # Customer Group if customer_group or based_on == 'Customer Group': - add_tab += ' ,`tabCustomer` t7, `tabCustomer Group` t8 ' - add_cond += ' AND t7.name = t1.customer AND t7.customer_group = t8.name and (t8.name = "%s" or t8.name IN (SELECT t9.name FROM `tabCustomer Group` t9,`tabCustomer Group` t10 WHERE t9.lft BETWEEN t10.lft and t10.rgt and t9.docstatus !=2 and ifnull(t9.is_group,"No") = "No" and t10.name = "%s"))' % (based_on != 'Customer Group' and customer_group or '%(value)s', based_on != 'Customer Group' and customer_group or '%(value)s') - + add_tab += ' ,`tabCustomer` t7, `tabCustomer Group` t8 ' + add_cond += ' AND t7.name = t1.customer AND t7.customer_group = t8.name and (t8.name = "%s" or t8.name IN (SELECT t9.name FROM `tabCustomer Group` t9,`tabCustomer Group` t10 WHERE t9.lft BETWEEN t10.lft and t10.rgt and t9.docstatus !=2 and ifnull(t9.is_group,"No") = "No" and t10.name = "%s"))' % (based_on != 'Customer Group' and customer_group or '%(value)s', based_on != 'Customer Group' and customer_group or '%(value)s') + # Territory if territory or based_on == 'Territory': - add_tab += ' ,`tabTerritory` t11 ' - add_cond += ' AND t1.territory = t11.name AND t1.territory = t11.name and (t11.name = "%s" or t11.name IN (SELECT t12.name FROM `tabTerritory` t12,`tabTerritory` t13 WHERE t12.lft BETWEEN t13.lft and t13.rgt and t12.docstatus !=2 and ifnull(t12.is_group,"No") = "No" and t13.name = "%s"))' % (based_on != 'Territory' and territory or '%(value)s', based_on != 'Territory' and territory or '%(value)s') + add_tab += ' ,`tabTerritory` t11 ' + add_cond += ' AND t1.territory = t11.name AND t1.territory = t11.name and (t11.name = "%s" or t11.name IN (SELECT t12.name FROM `tabTerritory` t12,`tabTerritory` t13 WHERE t12.lft BETWEEN t13.lft and t13.rgt and t12.docstatus !=2 and ifnull(t12.is_group,"No") = "No" and t13.name = "%s"))' % (based_on != 'Territory' and territory or '%(value)s', based_on != 'Territory' and territory or '%(value)s') # Supplier if supplier or based_on == 'Supplier': - add_cond += ' AND t1.supplier = "%s"' % (based_on != 'Supplier' and supplier or '%(value)s') - + add_cond += ' AND t1.supplier = "%s"' % (based_on != 'Supplier' and supplier or '%(value)s') + # Supplier Type if supplier_type or based_on == 'Supplier Type': - add_tab += ' ,`tabSupplier` t14, `tabSupplier Type` t15 ' - add_cond += ' AND t14.name = t1.supplier AND t14.supplier_type = t15.name and t15.name = "%s"' % (based_on != 'Supplier Type' and supplier_type or '%(value)s') + add_tab += ' ,`tabSupplier` t14, `tabSupplier Type` t15 ' + add_cond += ' AND t14.name = t1.supplier AND t14.supplier_type = t15.name and t15.name = "%s"' % (based_on != 'Supplier Type' and supplier_type or '%(value)s') - +# Project +if project or based_on == 'Project': + add_cond += ' AND t1.project_name = "%s"' % (based_on != 'Project' and project or '%(value)s') + # Column to be seleted for group by condition # ============================================== sel_col = '' if group_by == 'Item': - sel_col = 't2.item_code' + sel_col = 't2.item_code' elif group_by == 'Customer': - sel_col = 't1.customer' + sel_col = 't1.customer' elif group_by == 'Supplier': - sel_col = 't1.supplier' - - + sel_col = 't1.supplier' + + # ********************************************** Result Set ************************************************ for r in res: - main_det = sql("SELECT %s FROM %s WHERE %s" % (query_val, add_tab, add_cond % {'value':cstr(r[col_idx[based_on]]).strip()})) - if group_by: - for col in range(cr,cr+1): # this would make all first row blank. just for look - r.append('') - if main_det[0][len(colnames) - cr - 1]: - 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 '' - if is_grp != 'No': - flag = 0 + main_det = sql("SELECT %s FROM %s WHERE %s" % (query_val, add_tab, add_cond % {'value':cstr(r[col_idx[based_on]]).strip()})) + if group_by: + for col in range(cr,cr+1): # this would make all first row blank. just for look + r.append('') + if main_det[0][len(colnames) - cr - 1]: + 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 '' + if is_grp != 'No': + flag = 0 - 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()}))] + 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()})) - 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 + 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()})) + for d in range(len(col_names)): + t_row[col_idx[col_names[d]]] = flt(gr_det[0][d]) + out.append(t_row)