2013-08-05 14:59:54 +05:30
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
// License: GNU General Public License v3. See license.txt
2012-02-23 12:35:32 +05:30
2012-04-13 19:04:55 +05:30
// tree of chart of accounts / cost centers
// multiple companies
// add node
// edit node
// see ledger
2012-02-21 17:26:50 +05:30
2012-04-13 19:04:55 +05:30
pscript [ 'onload_Accounts Browser' ] = function ( wrapper ) {
2013-06-14 11:33:01 +05:30
console . log ( $ ( wrapper ) . html ( ) ) ;
2013-05-23 16:03:49 +05:30
wn . ui . make _app _page ( {
parent : wrapper ,
single _column : true
} )
2013-06-14 11:33:01 +05:30
2013-05-23 16:03:49 +05:30
wrapper . appframe . add _module _icon ( "Accounts" ) ;
var main = $ ( wrapper ) . find ( ".layout-main" ) ,
chart _area = $ ( "<div>" )
. css ( { "margin-bottom" : "15px" } )
. appendTo ( main ) ,
2013-09-30 16:38:30 -03:00
help _area = $ ( '<div class="well">' +
'<h4>' + wn . _ ( 'Quick Help' ) + '</h4>' +
'<ol>' +
'<li>' + wn . _ ( 'To add child nodes, explore tree and click on the node under which you want to add more nodes.' ) + '</li>' +
'<li>' +
wn . _ ( 'Accounting Entries can be made against leaf nodes, called' ) +
'<b>' + wn . _ ( 'Ledgers' ) + '</b>.' + wn . _ ( 'Entries against' ) +
'<b>' + wn . _ ( 'Groups' ) + '</b>' + wn . _ ( 'are not allowed.' ) +
'</li>' +
'<li>' + wn . _ ( 'Please do NOT create Account (Ledgers) for Customers and Suppliers. They are created directly from the Customer / Supplier masters.' ) + '</li>' +
'<li>' +
'<b>' + wn . _ ( 'To create a Bank Account:' ) + '</b>' +
wn . _ ( 'Go to the appropriate group (usually Application of Funds > Current Assets > Bank Accounts)' ) +
wn . _ ( 'and create a new Account Ledger (by clicking on Add Child) of type "Bank or Cash"' ) +
'</li>' +
'<li>' +
'<b>' + wn . _ ( 'To create a Tax Account:' ) + '</b>' +
wn . _ ( 'Go to the appropriate group (usually Source of Funds > Current Liabilities > Taxes and Duties)' ) +
wn . _ ( 'and create a new Account Ledger (by clicking on Add Child) of type "Tax" and do mention the Tax rate.' ) +
'</li>' +
'</ol>' +
'<p>' + wn . _ ( 'Please setup your chart of accounts before you start Accounting Entries' ) + '</p></div>' ) . appendTo ( main ) ;
2012-09-10 14:13:01 +05:30
if ( wn . boot . profile . can _create . indexOf ( "Company" ) !== - 1 ) {
2013-09-30 16:38:30 -03:00
wrapper . appframe . add _button ( wn . _ ( 'New Company' ) , function ( ) { newdoc ( 'Company' ) ; } ,
2012-09-10 14:13:01 +05:30
'icon-plus' ) ;
}
2012-05-22 16:54:46 +05:30
wrapper . appframe . add _button ( 'Refresh' , function ( ) {
2012-04-16 18:36:46 +05:30
wrapper . $company _select . change ( ) ;
} , 'icon-refresh' ) ;
2012-04-13 12:01:23 +05:30
2012-04-13 19:04:55 +05:30
// company-select
2013-05-23 16:03:49 +05:30
wrapper . $company _select = wrapper . appframe . add _select ( "Company" , [ ] )
2012-04-13 19:04:55 +05:30
. change ( function ( ) {
var ctype = wn . get _route ( ) [ 1 ] || 'Account' ;
2013-05-23 16:03:49 +05:30
erpnext . account _chart = new erpnext . AccountsChart ( ctype , $ ( this ) . val ( ) ,
chart _area . get ( 0 ) ) ;
2012-12-03 15:45:03 +05:30
pscript . set _title ( wrapper , ctype , $ ( this ) . val ( ) ) ;
2012-04-13 19:04:55 +05:30
} )
// load up companies
2013-07-29 19:30:39 +05:30
return wn . call ( {
2012-04-13 19:04:55 +05:30
method : 'accounts.page.accounts_browser.accounts_browser.get_companies' ,
callback : function ( r ) {
wrapper . $company _select . empty ( ) ;
$ . each ( r . message , function ( i , v ) {
$ ( '<option>' ) . html ( v ) . attr ( 'value' , v ) . appendTo ( wrapper . $company _select ) ;
} ) ;
2013-07-26 13:02:18 +05:30
wrapper . $company _select . val ( wn . defaults . get _default ( "company" ) || r [ 0 ] ) . change ( ) ;
2012-04-13 19:04:55 +05:30
}
} ) ;
2011-06-08 14:37:15 +05:30
}
2012-12-03 15:45:03 +05:30
pscript . set _title = function ( wrapper , ctype , val ) {
if ( val ) {
wrapper . appframe . set _title ( 'Chart of ' + ctype + 's' + " - " + cstr ( val ) ) ;
} else {
wrapper . appframe . set _title ( 'Chart of ' + ctype + 's' ) ;
}
}
2012-04-13 19:04:55 +05:30
pscript [ 'onshow_Accounts Browser' ] = function ( wrapper ) {
// set route
var ctype = wn . get _route ( ) [ 1 ] || 'Account' ;
2012-07-11 13:18:58 +05:30
2012-04-13 19:04:55 +05:30
if ( erpnext . account _chart && erpnext . account _chart . ctype != ctype ) {
wrapper . $company _select . change ( ) ;
2012-12-03 15:45:03 +05:30
}
pscript . set _title ( wrapper , ctype ) ;
2012-04-13 19:04:55 +05:30
}
erpnext . AccountsChart = Class . extend ( {
init : function ( ctype , company , wrapper ) {
2013-05-23 16:03:49 +05:30
$ ( wrapper ) . empty ( ) ;
2012-04-13 19:04:55 +05:30
var me = this ;
me . ctype = ctype ;
2012-12-06 12:09:52 +05:30
me . can _create = wn . model . can _create ( this . ctype ) ;
2012-12-03 14:11:24 +05:30
me . can _delete = wn . model . can _delete ( this . ctype ) ;
2012-12-06 12:09:52 +05:30
me . can _write = wn . model . can _write ( this . ctype ) ;
2012-12-03 14:11:24 +05:30
2012-04-16 18:36:46 +05:30
me . company = company ;
2012-04-13 19:04:55 +05:30
this . tree = new wn . ui . Tree ( {
2013-05-23 16:03:49 +05:30
parent : $ ( wrapper ) ,
2013-06-24 15:43:18 +05:30
label : ctype === "Account" ? "Accounts" : "Cost Centers" ,
2012-05-22 16:54:46 +05:30
args : { ctype : ctype , comp : company } ,
2012-04-13 19:04:55 +05:30
method : 'accounts.page.accounts_browser.accounts_browser.get_children' ,
click : function ( link ) {
if ( me . cur _toolbar )
$ ( me . cur _toolbar ) . toggle ( false ) ;
if ( ! link . toolbar )
me . make _link _toolbar ( link ) ;
if ( link . toolbar ) {
me . cur _toolbar = link . toolbar ;
2012-12-06 12:09:52 +05:30
$ ( me . cur _toolbar ) . toggle ( true ) ;
2012-04-13 19:04:55 +05:30
}
2012-05-22 16:54:46 +05:30
// bold
$ ( '.balance-bold' ) . removeClass ( 'balance-bold' ) ; // deselect
$ ( link ) . parent ( ) . find ( '.balance-area:first' ) . addClass ( 'balance-bold' ) ; // select
2012-12-06 12:09:52 +05:30
2012-05-22 16:54:46 +05:30
} ,
onrender : function ( treenode ) {
2013-03-13 12:53:00 +05:30
if ( ctype == 'Account' && treenode . data ) {
if ( treenode . data . balance ) {
2013-01-23 19:39:51 +05:30
treenode . parent . append ( '<span class="balance-area">'
2013-03-13 12:53:00 +05:30
+ format _currency ( treenode . data . balance , treenode . data . currency )
+ '</span>' ) ;
2012-06-29 19:07:20 +05:30
}
2012-05-31 13:32:45 +05:30
}
2012-04-13 19:04:55 +05:30
}
} ) ;
2012-05-22 16:54:46 +05:30
this . tree . rootnode . $a . click ( ) ;
2012-04-13 19:04:55 +05:30
} ,
make _link _toolbar : function ( link ) {
var data = $ ( link ) . data ( 'node-data' ) ;
if ( ! data ) return ;
2012-04-17 12:40:37 +05:30
link . toolbar = $ ( '<span class="tree-node-toolbar"></span>' ) . insertAfter ( link ) ;
2012-08-23 18:33:58 +05:30
var node _links = [ ] ;
2012-04-13 19:04:55 +05:30
// edit
2012-12-06 14:58:44 +05:30
if ( wn . model . can _read ( this . ctype ) !== - 1 ) {
2013-09-30 16:38:30 -03:00
node _links . push ( '<a onclick="erpnext.account_chart.open();">' + wn . _ ( 'Edit' ) + '</a>' ) ;
2012-04-17 12:40:37 +05:30
}
2013-06-11 17:48:59 +05:30
if ( data . expandable && wn . boot . profile . in _create . indexOf ( this . ctype ) !== - 1 ) {
2013-09-30 16:38:30 -03:00
node _links . push ( '<a onclick="erpnext.account_chart.new_node();">' + wn . _ ( 'Add Child' ) + '</a>' ) ;
2012-08-23 18:33:58 +05:30
} else if ( this . ctype === 'Account' && wn . boot . profile . can _read . indexOf ( "GL Entry" ) !== - 1 ) {
2013-09-30 16:38:30 -03:00
node _links . push ( '<a onclick="erpnext.account_chart.show_ledger();">' + wn . _ ( 'View Ledger' ) + '</a>' ) ;
2012-08-23 18:33:58 +05:30
}
2012-12-03 14:11:24 +05:30
2012-12-06 12:09:52 +05:30
if ( this . can _write ) {
2013-09-30 16:38:30 -03:00
node _links . push ( '<a onclick="erpnext.account_chart.rename()">' + wn . _ ( 'Rename' ) + '</a>' ) ;
2012-12-03 14:11:24 +05:30
} ;
2012-12-06 12:09:52 +05:30
if ( this . can _delete ) {
2013-09-30 16:38:30 -03:00
node _links . push ( '<a onclick="erpnext.account_chart.delete()">' + wn . _ ( 'Delete' ) + '</a>' ) ;
2012-12-03 14:11:24 +05:30
} ;
2012-08-23 18:33:58 +05:30
link . toolbar . append ( node _links . join ( " | " ) ) ;
2012-04-13 19:04:55 +05:30
} ,
2012-12-06 12:09:52 +05:30
open : function ( ) {
var node = this . selected _node ( ) ;
wn . set _route ( "Form" , this . ctype , node . data ( "label" ) ) ;
} ,
2012-04-16 18:36:46 +05:30
show _ledger : function ( ) {
var me = this ;
var node = me . selected _node ( ) ;
2013-08-08 18:49:58 +05:30
wn . route _options = {
"account" : node . data ( 'label' ) ,
"from_date" : sys _defaults . year _start _date ,
"to_date" : sys _defaults . year _end _date
} ;
wn . set _route ( "general-ledger" ) ;
2012-04-16 18:36:46 +05:30
} ,
2012-12-03 14:11:24 +05:30
rename : function ( ) {
2012-12-06 12:09:52 +05:30
var node = this . selected _node ( ) ;
wn . model . rename _doc ( this . ctype , node . data ( 'label' ) , function ( new _name ) {
2013-03-26 17:15:49 +05:30
node . parents ( "ul:first" ) . parent ( ) . find ( ".tree-link:first" ) . trigger ( "reload" ) ;
2012-12-06 12:09:52 +05:30
} ) ;
2012-12-03 14:11:24 +05:30
} ,
delete : function ( ) {
2012-12-06 12:09:52 +05:30
var node = this . selected _node ( ) ;
wn . model . delete _doc ( this . ctype , node . data ( 'label' ) , function ( ) {
2012-12-03 14:11:24 +05:30
node . parent ( ) . remove ( ) ;
} ) ;
} ,
2012-04-16 18:36:46 +05:30
new _node : function ( ) {
if ( this . ctype == 'Account' ) {
this . new _account ( ) ;
} else {
this . new _cost _center ( ) ;
}
} ,
selected _node : function ( ) {
return this . tree . $w . find ( '.tree-link.selected' ) ;
} ,
2012-04-13 19:04:55 +05:30
new _account : function ( ) {
2012-04-16 18:36:46 +05:30
var me = this ;
// the dialog
var d = new wn . ui . Dialog ( {
2013-09-30 16:38:30 -03:00
title : wn . _ ( 'New Account' ) ,
2012-04-16 18:36:46 +05:30
fields : [
2013-09-30 16:38:30 -03:00
{ fieldtype : 'Data' , fieldname : 'account_name' , label : wn . _ ( 'New Account Name' ) , reqd : true ,
description : wn . _ ( "Name of new Account. Note: Please don't create accounts for Customers and Suppliers," ) +
wn . _ ( "they are created automatically from the Customer and Supplier master" ) } ,
{ fieldtype : 'Select' , fieldname : 'group_or_ledger' , label : wn . _ ( 'Group or Ledger' ) ,
options : 'Group\nLedger' , description : wn . _ ( 'Further accounts can be made under Groups,' ) +
wn . _ ( 'but entries can be made against Ledger' ) } ,
{ fieldtype : 'Select' , fieldname : 'account_type' , label : wn . _ ( 'Account Type' ) ,
2012-04-16 18:36:46 +05:30
options : [ '' , 'Fixed Asset Account' , 'Bank or Cash' , 'Expense Account' , 'Tax' ,
2012-11-15 15:11:15 +05:30
'Income Account' , 'Chargeable' ] . join ( '\n' ) ,
2013-09-30 16:38:30 -03:00
description : wn . _ ( "Optional. This setting will be used to filter in various transactions." ) } ,
{ fieldtype : 'Float' , fieldname : 'tax_rate' , label : wn . _ ( 'Tax Rate' ) } ,
{ fieldtype : 'Button' , fieldname : 'create_new' , label : wn . _ ( 'Create New' ) }
2012-04-16 18:36:46 +05:30
]
} )
2012-04-13 19:04:55 +05:30
2012-04-16 18:36:46 +05:30
var fd = d . fields _dict ;
// account type if ledger
$ ( fd . group _or _ledger . input ) . change ( function ( ) {
if ( $ ( this ) . val ( ) == 'Group' ) {
$ ( fd . account _type . wrapper ) . toggle ( false ) ;
$ ( fd . tax _rate . wrapper ) . toggle ( false ) ;
} else {
$ ( fd . account _type . wrapper ) . toggle ( true ) ;
if ( fd . account _type . get _value ( ) == 'Tax' ) {
$ ( fd . tax _rate . wrapper ) . toggle ( true ) ;
}
}
} ) ;
// tax rate if tax
$ ( fd . account _type . input ) . change ( function ( ) {
if ( $ ( this ) . val ( ) == 'Tax' ) {
2012-11-29 18:29:56 +05:30
$ ( fd . tax _rate . wrapper ) . toggle ( true ) ;
2012-04-16 18:36:46 +05:30
} else {
2012-11-29 18:29:56 +05:30
$ ( fd . tax _rate . wrapper ) . toggle ( false ) ;
2012-04-16 18:36:46 +05:30
}
} )
// create
$ ( fd . create _new . input ) . click ( function ( ) {
var btn = this ;
$ ( btn ) . set _working ( ) ;
var v = d . get _values ( ) ;
if ( ! v ) return ;
var node = me . selected _node ( ) ;
v . parent _account = node . data ( 'label' ) ;
2012-07-11 14:39:33 +05:30
v . master _type = '' ;
2012-04-16 18:36:46 +05:30
v . company = me . company ;
2013-07-29 19:30:39 +05:30
return wn . call ( {
2013-01-30 19:16:13 +05:30
args : v ,
method : 'accounts.utils.add_ac' ,
callback : function ( r ) {
2012-04-16 18:36:46 +05:30
$ ( btn ) . done _working ( ) ;
d . hide ( ) ;
2013-01-30 19:16:13 +05:30
node . trigger ( 'reload' ) ;
}
} ) ;
2012-04-16 18:36:46 +05:30
} ) ;
// show
d . onshow = function ( ) {
$ ( fd . group _or _ledger . input ) . change ( ) ;
2012-11-29 18:29:56 +05:30
$ ( fd . account _type . input ) . change ( ) ;
2012-04-16 18:36:46 +05:30
}
2012-12-03 14:11:24 +05:30
$ ( fd . group _or _ledger . input ) . val ( "Ledger" ) . change ( ) ;
2012-04-16 18:36:46 +05:30
d . show ( ) ;
2012-04-13 19:04:55 +05:30
} ,
2012-04-16 18:36:46 +05:30
2012-04-13 19:04:55 +05:30
new _cost _center : function ( ) {
2012-04-16 18:36:46 +05:30
var me = this ;
// the dialog
var d = new wn . ui . Dialog ( {
2013-09-30 16:38:30 -03:00
title : wn . _ ( 'New Cost Center' ) ,
2012-04-16 18:36:46 +05:30
fields : [
2013-09-30 16:38:30 -03:00
{ fieldtype : 'Data' , fieldname : 'cost_center_name' , label : wn . _ ( 'New Cost Center Name' ) , reqd : true } ,
{ fieldtype : 'Select' , fieldname : 'group_or_ledger' , label : wn . _ ( 'Group or Ledger' ) ,
options : 'Group\nLedger' , description : wn . _ ( 'Further accounts can be made under Groups,' ) +
wn . _ ( 'but entries can be made against Ledger' ) } ,
{ fieldtype : 'Button' , fieldname : 'create_new' , label : wn . _ ( 'Create New' ) }
2012-04-16 18:36:46 +05:30
]
2012-07-11 14:39:33 +05:30
} ) ;
2012-04-16 18:36:46 +05:30
// create
$ ( d . fields _dict . create _new . input ) . click ( function ( ) {
var btn = this ;
$ ( btn ) . set _working ( ) ;
var v = d . get _values ( ) ;
if ( ! v ) return ;
var node = me . selected _node ( ) ;
v . parent _cost _center = node . data ( 'label' ) ;
2013-07-09 15:45:38 +05:30
v . company = me . company ;
2012-04-16 18:36:46 +05:30
2013-07-29 19:30:39 +05:30
return wn . call ( {
2013-01-30 19:16:13 +05:30
args : v ,
method : 'accounts.utils.add_cc' ,
callback : function ( r ) {
2012-04-16 18:36:46 +05:30
$ ( btn ) . done _working ( ) ;
d . hide ( ) ;
2013-01-30 19:16:13 +05:30
node . trigger ( 'reload' ) ;
}
} ) ;
2012-04-16 18:36:46 +05:30
} ) ;
d . show ( ) ;
}
2012-05-22 16:54:46 +05:30
} ) ;