2012-02-23 07:05:32 +00:00
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-06-08 09:07:15 +00:00
report . customize _filters = function ( ) {
this . hide _all _filters ( ) ;
this . add _filter ( { fieldname : 'transaction' , label : 'Transaction' , fieldtype : 'Select' , options : 'Quotation' + NEWLINE + 'Sales Order' + NEWLINE + 'Delivery Note' + NEWLINE + 'Sales Invoice' + NEWLINE + 'Purchase Order' + NEWLINE + 'Purchase Receipt' + NEWLINE + 'Purchase Invoice' , report _default : 'Delivery Note' , 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 } ) ;
2011-10-20 13:50:16 +00:00
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 } ) ;
2011-06-08 09:07:15 +00:00
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 : 'order_type' , label : 'Order Type' , fieldtype : 'Select' , options : NEWLINE + 'Sales' + NEWLINE + 'Maintenance' , ignore : 1 , parent : 'Profile' , single _select : 1 } ) ;
this . add _filter ( { fieldname : 'company' , label : 'Company' , fieldtype : 'Link' , options : 'Company' , report _default : sys _defaults . company , ignore : 1 , parent : 'Profile' } ) ;
2011-09-27 05:27:14 +00:00
this . add _filter ( { fieldname : 'fiscal_year' , label : 'Fiscal Year' , fieldtype : 'Link' , options : 'Fiscal Year' , report _default : sys _defaults . fiscal _year , ignore : 1 , parent : 'Profile' , in _first _page : 1 } ) ;
2011-06-08 09:07:15 +00:00
// Add Filters
this . add _filter ( { fieldname : 'item' , label : 'Item' , fieldtype : 'Link' , options : 'Item' , ignore : 1 , parent : 'Profile' } ) ;
this . add _filter ( { fieldname : 'item_group' , label : 'Item Group' , fieldtype : 'Link' , options : 'Item Group' , ignore : 1 , parent : 'Profile' } ) ;
this . add _filter ( { fieldname : 'customer' , label : 'Customer' , fieldtype : 'Link' , options : 'Customer' , ignore : 1 , parent : 'Profile' } ) ;
this . add _filter ( { fieldname : 'customer_group' , label : 'Customer Group' , fieldtype : 'Link' , options : 'Customer Group' , 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_type' , label : 'Supplier Type' , fieldtype : 'Link' , options : 'Supplier Type' , ignore : 1 , parent : 'Profile' } ) ;
2011-10-20 13:50:16 +00:00
this . add _filter ( { fieldname : 'project' , label : 'Project' , fieldtype : 'Link' , options : 'Project' , ignore : 1 , parent : 'Profile' } ) ;
2011-06-08 09:07:15 +00:00
}
this . mytabs . tabs [ 'Select Columns' ] . hide ( ) ;
report . aftertableprint = function ( t ) {
$yt ( t , '*' , 1 , { whiteSpace : 'pre' } ) ;
}
var validate _values = function ( trans , based _on , order _type ) {
if ( ! fiscal _year ) {
msgprint ( "Please select Fiscal Year" ) ;
return 0 ;
}
if ( ( in _list ( [ 'Quotation' , 'Sales Order' , 'Delivery Note' , 'Sales Invoice' ] , trans ) && in _list ( [ 'Supplier' , 'Supplier Type' ] , based _on ) ) || ( in _list ( [ 'Purchase Order' , 'Purchase Receipt' , 'Purchase Invoice' ] , trans ) && in _list ( [ 'Customer' , 'Customer Group' , 'Territory' ] , based _on ) ) ) {
msgprint ( "Sorry! You cannot fetch " + trans + " trend based on " + based _on ) ;
return 0 ;
}
if ( in _list ( [ 'Purchase Order' , 'Purchase Receipt' , 'Purchase Invoice' ] , trans ) && order _type ) {
msgprint ( "Please deselect Order Type for " + trans ) ;
return 0 ;
}
return 1 ;
}
report . get _query = function ( ) {
trans = this . get _filter ( 'Profile' , 'Transaction' ) . get _value ( ) ;
order _type = this . get _filter ( 'Profile' , 'Order Type' ) . get _value ( ) ;
based _on = this . get _filter ( 'Profile' , 'Based On' ) . get _value ( ) ;
company = this . get _filter ( 'Profile' , 'Company' ) . get _value ( ) ;
fiscal _year = this . get _filter ( 'Profile' , 'Fiscal Year' ) . get _value ( ) ;
if ( validate _values ( trans , based _on , order _type ) ) {
col = '' ;
add _cond = '' ;
add _col = '' ;
add _tables = '' ;
sp _cond = '' ;
2012-05-04 07:22:56 +00:00
trans _det = trans + ' Item'
2011-06-08 09:07:15 +00:00
if ( order _type != '' ) add _code += ' AND t1.order_type = ' + order _type ;
switch ( based _on ) {
case 'Item' : item = this . get _filter ( 'Profile' , 'Item' ) . get _value ( ) ;
col = 'DISTINCT t2.item_code, t3.item_name' ;
add _tables = ',tabItem t3' ;
add _cond += ' AND t2.item_code = t3.name' ;
if ( item ) add _cond += ' AND t2.item_code = "' + item + '"' ;
break ;
case 'Customer' : cust = this . get _filter ( 'Profile' , 'Customer' ) . get _value ( ) ;
col = 'DISTINCT t1.customer, t3.territory' ;
add _tables = ',tabCustomer t3' ;
add _cond += ' AND t1.customer = t3.name' ;
if ( cust ) add _cond += ' AND t1.customer = "' + cust + '"' ;
break ;
case 'Supplier' : supp = this . get _filter ( 'Profile' , 'Supplier' ) . get _value ( ) ;
col = 'DISTINCT t1.supplier, t3.supplier_type' ;
add _tables = ',tabSupplier t3' ;
add _cond += ' AND t1.supplier = t3.name' ;
if ( supp ) add _cond += ' AND t1.supplier = "' + supp + '"' ;
break ;
case 'Supplier Type' : supp _type = this . get _filter ( 'Profile' , 'Supplier Type' ) . get _value ( ) ;
col = 'DISTINCT t3.supplier_type' ;
add _tables = ',tabSupplier t3' ;
add _cond += ' AND t1.supplier = t3.name' ;
if ( supp _type ) add _cond += ' AND t1.supplier_type = "' + supp _type + '"' ;
break ;
2011-10-20 13:50:16 +00:00
case 'Project' : pro = this . get _filter ( 'Profile' , 'Project' ) . get _value ( ) ;
2012-03-30 06:59:06 +00:00
if ( inList ( [ 'Purchase Order' , 'Purchase Receipt' , 'Purchase Invoice' ] , trans ) ) {
2011-10-21 09:25:16 +00:00
col = 'DISTINCT t2.project_name' ;
if ( pro ) add _cond += ' AND t2.project_name = "' + pro + '"' ;
} else {
col = 'DISTINCT t1.project_name' ;
if ( pro ) add _cond += ' AND t1.project_name = "' + pro + '"' ;
}
2011-10-20 13:50:16 +00:00
break ;
2011-06-08 09:07:15 +00:00
case 'Item Group' : ig = this . get _filter ( 'Profile' , 'Item Group' ) . get _value ( ) ;
if ( ig ) sp _cond += ' AND parent.name = "' + ig + '"' ;
break ;
case 'Customer Group' : cg = this . get _filter ( 'Profile' , 'Customer Group' ) . get _value ( ) ;
if ( cg ) sp _cond += ' AND parent.name = "' + cg + '"' ;
break ;
case 'Territory' : ter = this . get _filter ( 'Profile' , 'Territory' ) . get _value ( ) ;
if ( ter ) sp _cond += ' AND parent.name = "' + ter + '"' ;
break ;
2011-10-20 13:50:16 +00:00
2011-06-08 09:07:15 +00:00
}
2011-10-20 13:50:16 +00:00
if ( based _on == 'Item' || based _on == 'Customer' || based _on == 'Supplier' || based _on == 'Supplier Type' || based _on == 'Project' )
2011-06-08 09:07:15 +00:00
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' ;
return q ;
}
2011-09-27 05:27:14 +00:00
}