mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-07 15:12:51 +00:00
Project wise analysis included in trend analyzer
This commit is contained in:
@@ -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:'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});
|
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:'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', 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:'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';
|
add_cond += ' AND t1.supplier = t3.name';
|
||||||
if(supp_type) add_cond += ' AND t1.supplier_type = "'+supp_type+'"';
|
if(supp_type) add_cond += ' AND t1.supplier_type = "'+supp_type+'"';
|
||||||
break;
|
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();
|
case 'Item Group' : ig = this.get_filter('Profile', 'Item Group').get_value();
|
||||||
if(ig) sp_cond += ' AND parent.name = "'+ig+'"';
|
if(ig) sp_cond += ' AND parent.name = "'+ig+'"';
|
||||||
break;
|
break;
|
||||||
@@ -108,10 +113,11 @@ report.get_query = function() {
|
|||||||
case 'Territory' : ter = this.get_filter('Profile', 'Territory').get_value();
|
case 'Territory' : ter = this.get_filter('Profile', 'Territory').get_value();
|
||||||
if(ter) sp_cond += ' AND parent.name = "'+ter+'"';
|
if(ter) sp_cond += ' AND parent.name = "'+ter+'"';
|
||||||
break;
|
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;
|
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
|
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';
|
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';
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ customer_group = filter_values.get('customer_group')
|
|||||||
territory = filter_values.get('territory')
|
territory = filter_values.get('territory')
|
||||||
supplier = filter_values.get('supplier')
|
supplier = filter_values.get('supplier')
|
||||||
supplier_type = filter_values.get('supplier_type')
|
supplier_type = filter_values.get('supplier_type')
|
||||||
|
project = filter_values.get('project')
|
||||||
|
|
||||||
|
|
||||||
# ********************************************* SET DEFAULTS **************************************************
|
# ********************************************* SET DEFAULTS **************************************************
|
||||||
@@ -41,41 +41,41 @@ col_names.append('Total (Amt)')
|
|||||||
|
|
||||||
# ********************************************* VALIDATIONS ***************************************************
|
# ********************************************* VALIDATIONS ***************************************************
|
||||||
if (based_on in ['Customer','Customer Group','Territory'] and group_by == 'Supplier') or (based_on in ['Supplier','Supplier Type'] and group_by == 'Customer'):
|
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))
|
msgprint("Sorry! You cannot group Trend Analyzer based on %s by %s" % (based_on,group_by))
|
||||||
raise Exception
|
raise Exception
|
||||||
|
|
||||||
if based_on == group_by:
|
if based_on == group_by:
|
||||||
msgprint("Based On and Group By value cannot be same for Trend Analyzer")
|
msgprint("Based On and Group By value cannot be same for Trend Analyzer")
|
||||||
raise Exception
|
raise Exception
|
||||||
|
|
||||||
|
|
||||||
# ********************************************** ADD COLUMNS **********************************************
|
# ********************************************** ADD COLUMNS **********************************************
|
||||||
cols = [[based_on, 'Data', '300px', '']]
|
cols = [[based_on, 'Data', '300px', '']]
|
||||||
cr = 1
|
cr = 1
|
||||||
if based_on == 'Item':
|
if based_on == 'Item':
|
||||||
cols.append(['Item Name','Data','200px',''])
|
cols.append(['Item Name','Data','200px',''])
|
||||||
cr = 2
|
cr = 2
|
||||||
elif based_on == 'Customer':
|
elif based_on == 'Customer':
|
||||||
cols.append(['Territory','Link','150px','Territory'])
|
cols.append(['Territory','Link','150px','Territory'])
|
||||||
cr = 2
|
cr = 2
|
||||||
elif based_on == 'Supplier':
|
elif based_on == 'Supplier':
|
||||||
cols.append(['Supplier Type','Link','150px','Supplier Type'])
|
cols.append(['Supplier Type','Link','150px','Supplier Type'])
|
||||||
cr = 2
|
cr = 2
|
||||||
if group_by:
|
if group_by:
|
||||||
cr += 1
|
cr += 1
|
||||||
|
|
||||||
if group_by:
|
if group_by:
|
||||||
cols.append([group_by,'Data','150px',''])
|
cols.append([group_by,'Data','150px',''])
|
||||||
|
|
||||||
for c in col_names:
|
for c in col_names:
|
||||||
cols.append([c,'Currency','150px',''])
|
cols.append([c,'Currency','150px',''])
|
||||||
|
|
||||||
for c in cols:
|
for c in cols:
|
||||||
colnames.append(c[0])
|
colnames.append(c[0])
|
||||||
coltypes.append(c[1])
|
coltypes.append(c[1])
|
||||||
colwidths.append(c[2])
|
colwidths.append(c[2])
|
||||||
coloptions.append(c[3])
|
coloptions.append(c[3])
|
||||||
col_idx[c[0]] = len(colnames)-1
|
col_idx[c[0]] = len(colnames)-1
|
||||||
|
|
||||||
|
|
||||||
# ******************************************* ADDITIONAL CONDITION ************************************************
|
# ******************************************* ADDITIONAL CONDITION ************************************************
|
||||||
@@ -86,75 +86,78 @@ if order_type: add_cond += ' AND t1.order_type = "%s"' % order_type
|
|||||||
|
|
||||||
# Item
|
# Item
|
||||||
if item or based_on == '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
|
# Item Group
|
||||||
if item_group or based_on == 'Item Group':
|
if item_group or based_on == 'Item Group':
|
||||||
add_tab += ' ,`tabItem` t3, `tabItem Group` t4 '
|
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_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
|
# Customer
|
||||||
if customer or based_on == '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
|
# Customer Group
|
||||||
if customer_group or based_on == 'Customer Group':
|
if customer_group or based_on == 'Customer Group':
|
||||||
add_tab += ' ,`tabCustomer` t7, `tabCustomer Group` t8 '
|
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_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
|
# Territory
|
||||||
if territory or based_on == 'Territory':
|
if territory or based_on == 'Territory':
|
||||||
add_tab += ' ,`tabTerritory` t11 '
|
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_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
|
# Supplier
|
||||||
if supplier or based_on == '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
|
# Supplier Type
|
||||||
if supplier_type or based_on == 'Supplier Type':
|
if supplier_type or based_on == 'Supplier Type':
|
||||||
add_tab += ' ,`tabSupplier` t14, `tabSupplier Type` t15 '
|
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_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
|
# Column to be seleted for group by condition
|
||||||
# ==============================================
|
# ==============================================
|
||||||
sel_col = ''
|
sel_col = ''
|
||||||
if group_by == 'Item':
|
if group_by == 'Item':
|
||||||
sel_col = 't2.item_code'
|
sel_col = 't2.item_code'
|
||||||
elif group_by == 'Customer':
|
elif group_by == 'Customer':
|
||||||
sel_col = 't1.customer'
|
sel_col = 't1.customer'
|
||||||
elif group_by == 'Supplier':
|
elif group_by == 'Supplier':
|
||||||
sel_col = 't1.supplier'
|
sel_col = 't1.supplier'
|
||||||
|
|
||||||
|
|
||||||
# ********************************************** Result Set ************************************************
|
# ********************************************** Result Set ************************************************
|
||||||
for r in res:
|
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()}))
|
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:
|
if group_by:
|
||||||
for col in range(cr,cr+1): # this would make all first row blank. just for look
|
for col in range(cr,cr+1): # this would make all first row blank. just for look
|
||||||
r.append('')
|
r.append('')
|
||||||
if main_det[0][len(colnames) - cr - 1]:
|
if main_det[0][len(colnames) - cr - 1]:
|
||||||
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
|
# 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 ''
|
||||||
if is_grp != 'No':
|
if is_grp != 'No':
|
||||||
flag = 0
|
flag = 0
|
||||||
|
|
||||||
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()}))
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user