// 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 .
// tree of chart of accounts / cost centers
// multiple companies
// add node
// edit node
// see ledger
pscript['onload_Accounts Browser'] = function(wrapper){
wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'));
wrapper.appframe.add_button('New Company', function() { newdoc('Company'); }, 'icon-plus');
// company-select
wrapper.$company_select = $(' ')
.change(function() {
var ctype = wn.get_route()[1] || 'Account';
erpnext.account_chart = new erpnext.AccountsChart(ctype, $(this).val(), wrapper);
})
.appendTo(wrapper.appframe.$w.find('.appframe-toolbar'));
// default company
if(sys_defaults.company) {
$('')
.html(sys_defaults.company)
.attr('value', sys_defaults.company)
.appendTo(wrapper.$company_select);
wrapper.$company_select
.val(sys_defaults.company).change();
}
// load up companies
wn.call({
method:'accounts.page.accounts_browser.accounts_browser.get_companies',
callback: function(r) {
wrapper.$company_select.empty();
$.each(r.message, function(i, v) {
$(' ').html(v).attr('value', v).appendTo(wrapper.$company_select);
});
wrapper.$company_select.val(sys_defaults.company || r[0]);
}
});
}
pscript['onshow_Accounts Browser'] = function(wrapper){
//wn.require('lib/js/wn/ui/tree.js');
// set route
var ctype = wn.get_route()[1] || 'Account';
wrapper.appframe.$titlebar.find('.appframe-title').html('Chart of '+ctype+'s');
if(erpnext.account_chart && erpnext.account_chart.ctype != ctype) {
wrapper.$company_select.change();
}
}
erpnext.AccountsChart = Class.extend({
init: function(ctype, company, wrapper) {
$(wrapper).find('.tree-area').empty();
var me = this;
me.ctype = ctype;
this.tree = new wn.ui.Tree({
parent: $(wrapper).find('.tree-area'),
label: company,
args: {ctype: ctype},
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;
$(me.cur_toolbar).toggle(true);
}
}
});
this.tree.rootnode.$a.click();
},
make_link_toolbar: function(link) {
var data = $(link).data('node-data');
if(!data) return;
link.toolbar = $(' ').insertAfter(link);
// edit
$('Edit ').appendTo(link.toolbar);
if(data.expandable) {
link.toolbar.append(' | Add Child ');
} else {
link.toolbar.append(' | View Ledger ');
}
},
new_account: function() {
var d = new Dialog(300,400,'Create A New Account');
d.make_body([
['HTML','Heading'],
['Data','New Account Name'],
['Select','Group or Ledger','Specify whether the new account is a Ledger or Group'],
['Select','Account Type','[Optional] Specify the type of this account'],
['Data','Tax Rate','Specify the default tax rate'],
['Select','Master Type','Specify the master type of this account'],
['Button','Create']
]);
add_sel_options(d.widgets['Group or Ledger'], ['Group', 'Ledger'],'Group');
add_sel_options(d.widgets['Account Type'], ['', 'Fixed Asset Account','Bank or Cash','Expense Account','Tax','Income Account','Chargeable'], '');
add_sel_options(d.widgets['Master Type'], ['NA', 'Supplier','Customer','Employee'],'NA');
// hide / show account type
d.widgets['Group or Ledger'].onchange = function() {
if(sel_val(this)=='Ledger')$ds(d.rows['Account Type']);
else $dh(d.rows['Account Type']);
}
// hide / show tax rate
d.widgets['Account Type'].onchange = function() {
if(sel_val(this)=='Tax' || sel_val(this)=='Chargeable')$ds(d.rows['Tax Rate']);
else $dh(d.rows['Tax Rate']);
}
d.onshow = function() {
$dh(this.rows['Account Type']);
$dh(this.rows['Tax Rate']);
this.widgets['Group or Ledger'].selectedIndex = 0;
this.widgets['Account Type'].selectedIndex = 0;
this.widgets['Master Type'].selectedIndex = 0;
d.widgets['New Account Name'].value = '';
d.widgets['Tax Rate'].value = '';
}
d.widgets['Create'].onclick = function() {
if(!d.widgets['New Account Name'].value) {
msgprint('Please enter New Account Name'); return;
}
if(!sel_val(d.widgets['Master Type'])) {
msgprint('Please enter master type of this new account'); return;
}
args = {
'account_name' : d.widgets['New Account Name'].value,
'parent_account' : pscript.cur_node.rec.name,
'group_or_ledger' : sel_val(d.widgets['Group or Ledger']),
'company' : sel_val(pscript.ab_company_sel),
'account_type' : sel_val(d.widgets['Account Type']),
'tax_rate' : d.widgets['Tax Rate'].value,
'master_type': sel_val(d.widgets['Master Type'])
}
$c_obj('GL Control', 'add_ac', docstring(args), function(r,rt) { d.hide(); pscript.group_area.ref_btn.onclick(); });
}
pscript.new_acc_dialog = d;
},
new_cost_center: function(){
pscript.cc_dialog = new Dialog(300,400,'Create A New Cost Center');
pscript.cc_dialog.make_body([
['HTML','Heading'],
['Data','New Cost Center Name'],
['Select','Group or Ledger','Specify whether the new cost center is a Ledger or Group'],
['Button','Create']
]);
add_sel_options(pscript.cc_dialog.widgets['Group or Ledger'], ['Group','Ledger'], 'Group');
pscript.new_cost_center_dialog = pscript.cc_dialog;
}
});
/*
pscript.make_chart = function(b, wrapper) {
pscript.ctype = b;
$(wrapper).find('.tree-area').empty()
$(wrapper).find('.layout-sidesection').empty()
//================== table body======================================
var ac_main_grid = make_table($i('ab_body'),1,2,'100%',['60%','40%'],{border:"0px", padding:"4px",tableLayout: "fixed", borderCollapse: "collapse"});
$y($td(ac_main_grid,0,0),{border: "1px solid #dddddd", padding: "8px"});
pscript.account_tree = $a($td(ac_main_grid,0,0),'div', '',{minHeight:'400px'});
$y($td(ac_main_grid,0,1),{border: "1px solid #DDD"});
pscript.la = $a($td(ac_main_grid,0,1),'div');
pscript.acc_period_bal = $a($td(ac_main_grid,0,1),'div');
//=====================footer area ==================================
if (pscript.ctype == 'Account') {
var footer = $a($i('ab_body'),'div','',{backgroundColor: "#FFD", padding: "8px", color: "#444", fontSize: "12px", marginTop: "14px"});
var help1 = $a(footer,'span');
help1.innerHTML = "Note: To create accounts for Customers and Suppliers, please create Customer and Supplier "
+ " Masters. This will ensure that the accounts are linked to your Selling and Buying Processes. The Account Heads for Customer and Supplier will automatically be created."
}
// header and toolbar
// ------------------
wrapper.appframe.$titlebar.find('.appframe-title').html('Chart of '+pscript.ctype+'s');
// select company
// --------------
var tab = make_table(select_area, 1, 2, null, [], {verticalAlign:'middle', padding: '2px'});
$td(tab,0,0).innerHTML = 'Select Company'.bold();
var sel = $a($td(tab,0,1),'select','',{width:'160px'});
// set tree
var set_tree = function() {
if(pscript.ac_tree) {
pscript.ac_tree.body.innerHTML = '';
}
pscript.make_ac_tree();
var cn = sel_val(sel);
var n = pscript.ac_tree.addNode(null, cn, null,pscript.ac_tree.std_onclick, pscript.ac_tree.std_onexp);
n.rec = {};
n.rec.name = 'Root Node';
n.rec.account_name = cn;
n.rec.cost_center_name = cn;
pscript.set_ac_head('',n.rec);
$ds(pscript.ac_head_area);
}
// select company
add_sel_options(sel, ['Loading...']);
var callback = function(r,rt) {
empty_select(sel);
add_sel_options(sel,r.message.cl,sys_defaults.company);
set_tree();
sel.onchange = function() { set_tree(); }
}
$c_obj('GL Control', 'get_companies', '', callback);
pscript.ab_company_sel = sel;
pscript.make_ac_head();
pscript.make_group_area();
pscript.make_ledger_area();
pscript.make_new_acc_dialog();
pscript.make_new_cost_center_dialog();
}
pscript.make_ac_tree = function() {
//var type= sel_val($i('ctype'))
var type= pscript.ctype;
var tree = new Tree(pscript.account_tree, '90%');
pscript.ac_tree = tree;
// on click
tree.std_onclick = function(node) {
pscript.cur_node = node;
// show ledger
pscript.set_ac_head(node.parent_account, node.rec,type);
}
// on expand
tree.std_onexp = function(node) {
if(node.expanded_once)return;
$ds(node.loading_div);
//set_ac_head
var callback = function(r,rt) {
$dh(node.loading_div);
var n = tree.allnodes[r.message.parent_acc_name];
var cl = r.message.cl;
if(type=='Account'){
for(var i=0;i