2015-03-03 14:55:30 +05:30
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2013-08-05 14:59:54 +05:30
// 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
2015-01-29 18:09:11 +05:30
frappe . pages [ "Accounts Browser" ] . on _page _load = function ( wrapper ) {
2014-02-14 15:47:51 +05:30
frappe . ui . make _app _page ( {
2013-05-23 16:03:49 +05:30
parent : wrapper ,
single _column : true
} )
2014-04-22 18:54:54 +05:30
2015-03-09 15:47:15 +05:30
frappe . breadcrumbs . add ( "Accounts" ) ;
2013-05-23 16:03:49 +05:30
2015-01-19 17:34:33 +05:30
var main = wrapper . page . main ,
2013-05-23 16:03:49 +05:30
chart _area = $ ( "<div>" )
2013-11-02 14:47:11 +05:30
. css ( { "margin-bottom" : "15px" , "min-height" : "200px" } )
2013-05-23 16:03:49 +05:30
. appendTo ( main ) ,
2015-01-01 12:49:18 +05:30
help _area = $ ( '<hr><div style="padding: 0px 15px;">' +
2014-04-14 16:25:30 +05:30
'<h4>' + _ _ ( 'Quick Help' ) + '</h4>' +
2013-09-30 16:38:30 -03:00
'<ol>' +
2014-04-14 16:25:30 +05:30
'<li>' + _ _ ( 'To add child nodes, explore tree and click on the node under which you want to add more nodes.' ) + '</li>' +
2013-09-30 16:38:30 -03:00
'<li>' +
2015-04-23 13:14:17 +05:30
_ _ ( 'Accounting Entries can be made against leaf nodes. Entries against Groups are not allowed.' ) +
2013-09-30 16:38:30 -03:00
'</li>' +
2015-04-23 13:14:17 +05:30
'<li>' + _ _ ( 'Please do NOT create Accounts for Customers and Suppliers. They are created directly from the Customer / Supplier masters.' ) + '</li>' +
2013-09-30 16:38:30 -03:00
'<li>' +
2014-07-29 15:19:41 +05:30
'<b>' + _ _ ( 'To create a Bank Account' ) + '</b>: ' +
2015-04-23 13:14:17 +05:30
_ _ ( 'Go to the appropriate group (usually Application of Funds > Current Assets > Bank Accounts and create a new Account (by clicking on Add Child) of type "Bank"' ) +
2013-09-30 16:38:30 -03:00
'</li>' +
'<li>' +
2014-07-29 15:19:41 +05:30
'<b>' + _ _ ( 'To create a Tax Account' ) + '</b>: ' +
2015-04-23 13:14:17 +05:30
_ _ ( 'Go to the appropriate group (usually Source of Funds > Current Liabilities > Taxes and Duties and create a new Account (by clicking on Add Child) of type "Tax" and do mention the Tax rate.' ) +
2013-09-30 16:38:30 -03:00
'</li>' +
'</ol>' +
2014-04-14 16:25:30 +05:30
'<p>' + _ _ ( 'Please setup your chart of accounts before you start Accounting Entries' ) + '</p></div>' ) . appendTo ( main ) ;
2014-04-22 18:54:54 +05:30
2014-03-11 16:15:05 +05:30
if ( frappe . boot . user . can _create . indexOf ( "Company" ) !== - 1 ) {
2015-01-01 12:49:18 +05:30
wrapper . page . add _menu _item ( _ _ ( 'New Company' ) , function ( ) { newdoc ( 'Company' ) ; } , true ) ;
2012-09-10 14:13:01 +05:30
}
2014-04-22 18:54:54 +05:30
2015-09-02 12:18:37 +05:30
wrapper . page . add _menu _item ( _ _ ( 'Refresh' ) , function ( ) {
2012-04-16 18:36:46 +05:30
wrapper . $company _select . change ( ) ;
2013-11-13 16:55:38 +05:30
} ) ;
2012-04-13 12:01:23 +05:30
2015-01-01 15:59:34 +05:30
wrapper . page . set _primary _action ( _ _ ( 'New' ) , function ( ) {
2015-04-21 10:36:39 +05:30
erpnext . account _chart && erpnext . account _chart . make _new ( ) ;
2015-09-02 12:18:37 +05:30
} , "octicon octicon-plus" ) ;
2015-01-01 15:59:34 +05:30
2012-04-13 19:04:55 +05:30
// company-select
2014-12-23 17:56:47 +05:30
wrapper . $company _select = wrapper . page . add _select ( "Company" , [ ] )
2012-04-13 19:04:55 +05:30
. change ( function ( ) {
2014-02-14 15:47:51 +05:30
var ctype = frappe . get _route ( ) [ 1 ] || 'Account' ;
2014-04-22 18:54:54 +05:30
erpnext . account _chart = new erpnext . AccountsChart ( ctype , $ ( this ) . val ( ) ,
2015-05-25 18:30:53 +05:30
chart _area . get ( 0 ) , wrapper . page ) ;
2012-04-13 19:04:55 +05:30
} )
2014-04-22 18:54:54 +05:30
2012-04-13 19:04:55 +05:30
// load up companies
2014-02-14 15:47:51 +05:30
return frappe . call ( {
2013-12-12 19:12:19 +05:30
method : 'erpnext.accounts.page.accounts_browser.accounts_browser.get_companies' ,
2012-04-13 19:04:55 +05:30
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 10:39:54 +05:30
wrapper . $company _select . val ( frappe . defaults . get _user _default ( "company" ) || r . message [ 0 ] ) . change ( ) ;
2012-04-13 19:04:55 +05:30
}
} ) ;
2011-06-08 14:37:15 +05:30
}
2015-01-29 18:09:11 +05:30
frappe . pages [ "Accounts Browser" ] . on _page _show = function ( wrapper ) {
2012-04-13 19:04:55 +05:30
// set route
2014-02-14 15:47:51 +05:30
var ctype = frappe . get _route ( ) [ 1 ] || 'Account' ;
2012-07-11 13:18:58 +05:30
2015-05-25 18:30:53 +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
}
2012-04-13 19:04:55 +05:30
}
erpnext . AccountsChart = Class . extend ( {
2015-05-25 18:30:53 +05:30
init : function ( ctype , company , wrapper , page ) {
2013-05-23 16:03:49 +05:30
$ ( wrapper ) . empty ( ) ;
2012-04-13 19:04:55 +05:30
var me = this ;
me . ctype = ctype ;
2014-02-14 15:47:51 +05:30
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 ) ;
2015-05-25 18:30:53 +05:30
me . page = page ;
me . set _title ( ) ;
2014-04-22 18:54:54 +05:30
2015-02-20 10:39:39 +05:30
// __("Accounts"), __("Cost Centers")
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
me . company = company ;
2014-02-14 15:47:51 +05:30
this . tree = new frappe . ui . Tree ( {
2014-04-22 18:54:54 +05:30
parent : $ ( wrapper ) ,
2015-02-20 10:39:39 +05:30
label : ctype === "Account" ? "Accounts" : "Cost Centers" ,
2012-05-22 16:54:46 +05:30
args : { ctype : ctype , comp : company } ,
2013-12-12 19:12:19 +05:30
method : 'erpnext.accounts.page.accounts_browser.accounts_browser.get_children' ,
2012-04-13 19:04:55 +05:30
click : function ( link ) {
2012-05-22 16:54:46 +05:30
// bold
2013-12-13 11:51:50 +05:30
$ ( '.bold' ) . removeClass ( 'bold' ) ; // deselect
$ ( link ) . parent ( ) . find ( '.balance-area:first' ) . addClass ( 'bold' ) ; // select
2012-12-06 12:09:52 +05:30
2012-05-22 16:54:46 +05:30
} ,
2014-02-19 12:43:16 +05:30
toolbar : [
{ toggle _btn : true } ,
2014-04-22 18:54:54 +05:30
{
2014-02-19 12:43:16 +05:30
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 ( ) {
2015-04-21 10:36:39 +05:30
me . make _new ( )
2015-09-02 12:18:37 +05:30
} ,
btnClass : "hidden-xs"
2014-02-19 12:43:16 +05:30
} ,
{
condition : function ( node ) {
2014-04-22 18:54:54 +05:30
return ! node . root && me . ctype === 'Account'
2014-03-11 16:15:05 +05:30
&& frappe . boot . user . can _read . indexOf ( "GL Entry" ) !== - 1
2014-02-19 12:43:16 +05:30
} ,
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" ) ;
2015-09-02 12:18:37 +05:30
} ,
btnClass : "hidden-xs"
2014-02-19 12:43:16 +05:30
} ,
{
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 ( ) ;
} ) ;
2015-09-02 12:18:37 +05:30
} ,
btnClass : "hidden-xs"
2014-02-19 12:43:16 +05:30
} ,
{
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 ( ) ;
} ) ;
2015-09-02 12:18:37 +05:30
} ,
btnClass : "hidden-xs"
2014-02-19 12:43:16 +05:30
}
] ,
onrender : function ( node ) {
2015-01-01 12:49:18 +05:30
var dr _or _cr = node . data . balance < 0 ? "Cr" : "Dr" ;
2014-02-19 12:43:16 +05:30
if ( me . ctype == 'Account' && node . data && node . data . balance !== undefined ) {
2015-01-01 12:49:18 +05:30
$ ( '<span class="balance-area pull-right text-muted small">'
2015-09-17 13:57:42 +05:30
+ format _currency ( Math . abs ( node . data . balance ) , node . data . account _currency )
2015-01-01 12:49:18 +05:30
+ " " + dr _or _cr
2014-02-19 12:43:16 +05:30
+ '</span>' ) . insertBefore ( node . $ul ) ;
2012-05-31 13:32:45 +05:30
}
2012-04-13 19:04:55 +05:30
}
} ) ;
2012-04-16 18:36:46 +05:30
} ,
2015-01-07 16:43:27 +05:30
set _title : function ( val ) {
var chart _str = this . ctype == "Account" ? _ _ ( "Chart of Accounts" ) : _ _ ( "Chart of Cost Centers" ) ;
if ( val ) {
2015-05-25 18:30:53 +05:30
this . page . set _title ( chart _str + " - " + cstr ( val ) ) ;
2015-01-07 16:43:27 +05:30
} else {
2015-05-25 18:30:53 +05:30
this . page . set _title ( chart _str ) ;
2015-01-07 16:43:27 +05:30
}
} ,
2015-04-21 10:36:39 +05:30
make _new : function ( ) {
if ( this . ctype == 'Account' ) {
this . new _account ( ) ;
} else {
this . new _cost _center ( ) ;
}
} ,
2012-04-13 19:04:55 +05:30
new _account : function ( ) {
2012-04-16 18:36:46 +05:30
var me = this ;
2014-04-22 18:54:54 +05:30
2015-01-01 15:59:34 +05:30
var node = me . tree . get _selected _node ( ) ;
if ( ! ( node && node . expandable ) ) {
frappe . msgprint ( _ _ ( "Select a group node first." ) ) ;
return ;
}
2012-04-16 18:36:46 +05:30
// the dialog
2014-02-14 15:47:51 +05:30
var d = new frappe . ui . Dialog ( {
2014-04-14 16:25:30 +05:30
title : _ _ ( 'New Account' ) ,
2012-04-16 18:36:46 +05:30
fields : [
2014-04-22 18:54:54 +05:30
{ fieldtype : 'Data' , fieldname : 'account_name' , label : _ _ ( 'New Account Name' ) , reqd : true ,
2015-08-13 10:34:49 +05:30
description : _ _ ( "Name of new Account. Note: Please don't create accounts for Customers and Suppliers" ) } ,
2015-04-23 13:14:17 +05:30
{ fieldtype : 'Check' , fieldname : 'is_group' , label : _ _ ( 'Is Group' ) ,
description : _ _ ( 'Further accounts can be made under Groups, but entries can be made against non-Groups' ) } ,
2014-04-14 16:25:30 +05:30
{ fieldtype : 'Select' , fieldname : 'account_type' , label : _ _ ( 'Account Type' ) ,
2014-04-22 18:54:54 +05:30
options : [ '' , 'Bank' , 'Cash' , 'Warehouse' , 'Receivable' , 'Payable' ,
'Equity' , 'Cost of Goods Sold' , 'Fixed Asset' , 'Expense Account' ,
2015-04-27 18:08:51 +05:30
'Income Account' , 'Tax' , 'Chargeable' , 'Temporary' ] . join ( '\n' ) ,
2014-04-14 16:25:30 +05:30
description : _ _ ( "Optional. This setting will be used to filter in various transactions." ) } ,
2015-03-05 16:38:43 +05:30
{ fieldtype : 'Float' , fieldname : 'tax_rate' , label : _ _ ( 'Tax Rate' ) } ,
2015-08-19 19:21:49 +05:30
{ fieldtype : 'Link' , fieldname : 'warehouse' , label : _ _ ( 'Warehouse' ) , options : "Warehouse" } ,
2015-08-28 19:24:22 +05:30
{ fieldtype : 'Link' , fieldname : 'account_currency' , label : _ _ ( 'Currency' ) , options : "Currency" }
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 ;
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
// account type if ledger
2015-04-23 13:14:17 +05:30
$ ( fd . is _group . input ) . change ( function ( ) {
if ( $ ( this ) . prop ( "checked" ) ) {
2012-04-16 18:36:46 +05:30
$ ( fd . account _type . wrapper ) . toggle ( false ) ;
$ ( fd . tax _rate . wrapper ) . toggle ( false ) ;
2015-03-05 16:38:43 +05:30
$ ( fd . warehouse . wrapper ) . toggle ( false ) ;
2012-04-16 18:36:46 +05:30
} else {
2015-04-23 16:29:15 +05:30
$ ( fd . account _type . wrapper ) . toggle ( true ) ;
2015-03-05 16:38:43 +05:30
fd . account _type . $input . trigger ( "change" ) ;
2012-04-16 18:36:46 +05:30
}
} ) ;
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
// tax rate if tax
$ ( fd . account _type . input ) . change ( function ( ) {
2015-03-05 16:38:43 +05:30
$ ( fd . tax _rate . wrapper ) . toggle ( fd . account _type . get _value ( ) === 'Tax' ) ;
$ ( fd . warehouse . wrapper ) . toggle ( fd . account _type . get _value ( ) === 'Warehouse' ) ;
2012-04-16 18:36:46 +05:30
} )
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
// create
2015-01-01 15:59:34 +05:30
d . set _primary _action ( _ _ ( "Create New" ) , function ( ) {
2012-04-16 18:36:46 +05:30
var btn = this ;
var v = d . get _values ( ) ;
if ( ! v ) return ;
2014-04-22 18:54:54 +05:30
2015-03-05 16:38:43 +05:30
if ( v . account _type === "Warehouse" && ! v . warehouse ) {
msgprint ( _ _ ( "Warehouse is required" ) ) ;
return ;
}
2014-02-19 12:43:16 +05:30
var node = me . tree . get _selected _node ( ) ;
v . parent _account = node . label ;
2012-04-16 18:36:46 +05:30
v . company = me . company ;
2014-04-22 18:54:54 +05:30
2014-02-14 15:47:51 +05:30
return frappe . call ( {
2013-01-30 19:16:13 +05:30
args : v ,
2013-12-12 19:12:19 +05:30
method : 'erpnext.accounts.utils.add_ac' ,
2013-01-30 19:16:13 +05:30
callback : function ( r ) {
2012-04-16 18:36:46 +05:30
d . hide ( ) ;
2014-05-27 15:22:04 +05:30
if ( node . expanded ) {
node . toggle _node ( ) ;
}
2014-04-22 18:54:54 +05:30
node . reload ( ) ;
2013-01-30 19:16:13 +05:30
}
} ) ;
2012-04-16 18:36:46 +05:30
} ) ;
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
// show
2015-01-29 18:09:11 +05:30
d . on _page _show = function ( ) {
2015-04-23 13:14:17 +05:30
$ ( fd . is _group . input ) . change ( ) ;
2012-11-29 18:29:56 +05:30
$ ( fd . account _type . input ) . change ( ) ;
2012-04-16 18:36:46 +05:30
}
2014-04-22 18:54:54 +05:30
2015-04-23 13:14:17 +05:30
$ ( fd . is _group . input ) . prop ( "checked" , false ) . change ( ) ;
2012-04-16 18:36:46 +05:30
d . show ( ) ;
2012-04-13 19:04:55 +05:30
} ,
2014-04-22 18:54:54 +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
2014-02-14 15:47:51 +05:30
var d = new frappe . ui . Dialog ( {
2014-04-14 16:25:30 +05:30
title : _ _ ( 'New Cost Center' ) ,
2012-04-16 18:36:46 +05:30
fields : [
2014-04-14 16:25:30 +05:30
{ fieldtype : 'Data' , fieldname : 'cost_center_name' , label : _ _ ( 'New Cost Center Name' ) , reqd : true } ,
2015-04-23 13:14:17 +05:30
{ fieldtype : 'Check' , fieldname : 'is_group' , label : _ _ ( 'Is Group' ) ,
description : _ _ ( 'Further cost centers can be made under Groups but entries can be made against non-Groups' ) } ,
2014-04-14 16:25:30 +05:30
{ fieldtype : 'Button' , fieldname : 'create_new' , label : _ _ ( 'Create New' ) }
2012-04-16 18:36:46 +05:30
]
2012-07-11 14:39:33 +05:30
} ) ;
2014-04-22 18:54:54 +05:30
2012-04-16 18:36:46 +05:30
// create
$ ( d . fields _dict . create _new . input ) . click ( function ( ) {
var v = d . get _values ( ) ;
if ( ! v ) return ;
2014-04-22 18:54:54 +05:30
2014-02-19 12:43:16 +05:30
var node = me . tree . get _selected _node ( ) ;
2014-04-22 18:54:54 +05:30
2014-02-19 12:43:16 +05:30
v . parent _cost _center = node . label ;
2013-07-09 15:45:38 +05:30
v . company = me . company ;
2014-04-22 18:54:54 +05:30
2014-02-14 15:47:51 +05:30
return frappe . call ( {
2013-01-30 19:16:13 +05:30
args : v ,
2013-12-12 19:12:19 +05:30
method : 'erpnext.accounts.utils.add_cc' ,
2013-01-30 19:16:13 +05:30
callback : function ( r ) {
2012-04-16 18:36:46 +05:30
d . hide ( ) ;
2014-05-27 15:22:04 +05:30
if ( node . expanded ) {
node . toggle _node ( ) ;
}
2014-02-19 12:43:16 +05:30
node . reload ( ) ;
2013-01-30 19:16:13 +05:30
}
} ) ;
2012-04-16 18:36:46 +05:30
} ) ;
d . show ( ) ;
}
2014-04-15 10:08:53 +05:30
} ) ;