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