2011-06-08 09:07:15 +00:00
# ********************************************* INITIALIZATION *******************************************
out = [ ]
# Filter Values
# =============================================
based_on = filter_values . get ( ' based_on ' )
group_by = filter_values . get ( ' group_by ' )
trans = filter_values . get ( ' transaction ' )
period = filter_values . get ( ' period ' )
order_type = filter_values . get ( ' order_type ' )
company = filter_values . get ( ' company ' )
fiscal_year = filter_values . get ( ' fiscal_year ' )
item = filter_values . get ( ' item ' )
item_group = filter_values . get ( ' item_group ' )
customer = filter_values . get ( ' customer ' )
customer_group = filter_values . get ( ' customer_group ' )
territory = filter_values . get ( ' territory ' )
supplier = filter_values . get ( ' supplier ' )
supplier_type = filter_values . get ( ' supplier_type ' )
2011-10-20 13:50:16 +00:00
project = filter_values . get ( ' project ' )
2011-06-08 09:07:15 +00:00
# ********************************************* SET DEFAULTS **************************************************
# Details Table
# --------------
if trans == ' Sales Invoice ' : trans = ' Receivable Voucher '
elif trans == ' Purchase Invoice ' : trans = ' Payable Voucher '
trans_det = trans + ' Detail '
if trans == ' Receivable Voucher ' : trans_det = ' RV Detail '
elif trans == ' Payable Voucher ' : trans_det = ' PV Detail '
elif trans == ' Purchase Order ' : trans_det = ' PO Detail '
col_names , query_val = get_obj ( ' TA Control ' ) . get_single_year_query_value ( fiscal_year , period , trans , trans_det )
query_val + = ' SUM(t2.qty), SUM(t2.amount) '
col_names . append ( ' Total (Qty) ' )
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 ' ) :
2011-10-20 13:50:16 +00:00
msgprint ( " Sorry! You cannot group Trend Analyzer based on %s by %s " % ( based_on , group_by ) )
raise Exception
2011-06-08 09:07:15 +00:00
if based_on == group_by :
2011-10-20 13:50:16 +00:00
msgprint ( " Based On and Group By value cannot be same for Trend Analyzer " )
raise Exception
2011-06-08 09:07:15 +00:00
# ********************************************** ADD COLUMNS **********************************************
cols = [ [ based_on , ' Data ' , ' 300px ' , ' ' ] ]
cr = 1
if based_on == ' Item ' :
2011-10-20 13:50:16 +00:00
cols . append ( [ ' Item Name ' , ' Data ' , ' 200px ' , ' ' ] )
cr = 2
2011-06-08 09:07:15 +00:00
elif based_on == ' Customer ' :
2011-10-20 13:50:16 +00:00
cols . append ( [ ' Territory ' , ' Link ' , ' 150px ' , ' Territory ' ] )
cr = 2
2011-06-08 09:07:15 +00:00
elif based_on == ' Supplier ' :
2011-10-20 13:50:16 +00:00
cols . append ( [ ' Supplier Type ' , ' Link ' , ' 150px ' , ' Supplier Type ' ] )
cr = 2
2011-06-08 09:07:15 +00:00
if group_by :
2011-10-20 13:50:16 +00:00
cr + = 1
2011-06-08 09:07:15 +00:00
if group_by :
2011-10-20 13:50:16 +00:00
cols . append ( [ group_by , ' Data ' , ' 150px ' , ' ' ] )
2011-06-08 09:07:15 +00:00
for c in col_names :
2011-10-20 13:50:16 +00:00
cols . append ( [ c , ' Currency ' , ' 150px ' , ' ' ] )
2011-06-08 09:07:15 +00:00
for c in cols :
2011-10-20 13:50:16 +00:00
colnames . append ( c [ 0 ] )
coltypes . append ( c [ 1 ] )
colwidths . append ( c [ 2 ] )
coloptions . append ( c [ 3 ] )
col_idx [ c [ 0 ] ] = len ( colnames ) - 1
2011-06-08 09:07:15 +00:00
# ******************************************* ADDITIONAL CONDITION ************************************************
add_cond = ' t2.parent = t1.name AND t1.company = " %s " AND t1.fiscal_year = " %s " and t1.docstatus = 1 ' % ( company , fiscal_year )
add_tab = ' `tab ' + trans + ' ` t1, `tab ' + trans_det + ' ` t2 '
if order_type : add_cond + = ' AND t1.order_type = " %s " ' % order_type
# Item
if item or based_on == ' Item ' :
2011-10-20 13:50:16 +00:00
add_cond + = ' AND t2.item_code = " %s " ' % ( based_on != ' Item ' and item or ' %(value)s ' )
2011-06-08 09:07:15 +00:00
# Item Group
if item_group or based_on == ' Item Group ' :
2011-10-20 13:50:16 +00:00
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 ' )
2011-06-08 09:07:15 +00:00
# Customer
if customer or based_on == ' Customer ' :
2011-10-20 13:50:16 +00:00
add_cond + = ' AND t1.customer = " %s " ' % ( based_on != ' Customer ' and customer or ' %(value)s ' )
2011-06-08 09:07:15 +00:00
# Customer Group
if customer_group or based_on == ' Customer Group ' :
2011-10-20 13:50:16 +00:00
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 ' )
2011-06-08 09:07:15 +00:00
# Territory
if territory or based_on == ' Territory ' :
2011-10-20 13:50:16 +00:00
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 ' )
2011-06-08 09:07:15 +00:00
# Supplier
if supplier or based_on == ' Supplier ' :
2011-10-20 13:50:16 +00:00
add_cond + = ' AND t1.supplier = " %s " ' % ( based_on != ' Supplier ' and supplier or ' %(value)s ' )
2011-06-08 09:07:15 +00:00
# Supplier Type
if supplier_type or based_on == ' Supplier Type ' :
2011-10-20 13:50:16 +00:00
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 ' )
2011-06-08 09:07:15 +00:00
2011-10-20 13:50:16 +00:00
# Project
if project or based_on == ' Project ' :
2011-10-21 09:25:16 +00:00
if trans in [ ' Purchase Order ' , ' Purchase Receipt ' , ' Payable Voucher ' ] :
add_cond + = ' AND t2.project_name = " %s " ' % ( based_on != ' Project ' and project or ' %(value)s ' )
else :
add_cond + = ' AND t1.project_name = " %s " ' % ( based_on != ' Project ' and project or ' %(value)s ' )
2011-10-20 13:50:16 +00:00
2011-06-08 09:07:15 +00:00
# Column to be seleted for group by condition
# ==============================================
sel_col = ' '
if group_by == ' Item ' :
2011-10-20 13:50:16 +00:00
sel_col = ' t2.item_code '
2011-06-08 09:07:15 +00:00
elif group_by == ' Customer ' :
2011-10-20 13:50:16 +00:00
sel_col = ' t1.customer '
2011-06-08 09:07:15 +00:00
elif group_by == ' Supplier ' :
2011-10-20 13:50:16 +00:00
sel_col = ' t1.supplier '
2011-06-08 09:07:15 +00:00
# ********************************************** Result Set ************************************************
for r in res :
2011-10-20 13:50:16 +00:00
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 ( ) } ) ) ]
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 )