brotherton-erpnext/erpnext/accounts/page/accounts_browser/accounts_browser.js

321 lines
10 KiB
JavaScript
Raw Normal View History

2013-11-20 07:29:58 +00:00
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
// 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-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({
parent: wrapper,
single_column: true
})
wrapper.appframe.add_module_icon("Accounts");
var main = $(wrapper).find(".layout-main"),
chart_area = $("<div>")
.css({"margin-bottom": "15px", "min-height": "200px"})
.appendTo(main),
2013-09-30 19:38:30 +00:00
help_area = $('<div class="well">'+
2014-02-14 10:17:51 +00:00
'<h4>'+frappe._('Quick Help')+'</h4>'+
2013-09-30 19:38:30 +00:00
'<ol>'+
2014-02-14 10:17:51 +00:00
'<li>'+frappe._('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-02-14 10:17:51 +00:00
frappe._('Accounting Entries can be made against leaf nodes, called')+
'<b>' +frappe._('Ledgers')+'</b>.'+ frappe._('Entries against') +
'<b>' +frappe._('Groups') + '</b>'+ frappe._('are not allowed.')+
2013-09-30 19:38:30 +00:00
'</li>'+
2014-02-14 10:17:51 +00:00
'<li>'+frappe._('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-02-14 10:17:51 +00:00
'<b>'+frappe._('To create a Bank Account:')+'</b>'+
frappe._('Go to the appropriate group (usually Application of Funds > Current Assets > Bank Accounts)')+
frappe._('and create a new Account Ledger (by clicking on Add Child) of type "Bank or Cash"')+
2013-09-30 19:38:30 +00:00
'</li>'+
'<li>'+
2014-02-14 10:17:51 +00:00
'<b>'+frappe._('To create a Tax Account:')+'</b>'+
frappe._('Go to the appropriate group (usually Source of Funds > Current Liabilities > Taxes and Duties)')+
frappe._('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-02-14 10:17:51 +00:00
'<p>'+frappe._('Please setup your chart of accounts before you start Accounting Entries')+'</p></div>').appendTo(main);
2014-02-14 10:17:51 +00:00
if (frappe.boot.profile.can_create.indexOf("Company") !== -1) {
wrapper.appframe.add_button(frappe._('New Company'), function() { newdoc('Company'); },
'icon-plus');
}
wrapper.appframe.set_title_right('Refresh', function() {
2012-04-16 13:06:46 +00:00
wrapper.$company_select.change();
});
2012-04-13 06:31:23 +00:00
2012-04-13 13:34:55 +00:00
// company-select
wrapper.$company_select = wrapper.appframe.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';
erpnext.account_chart = new erpnext.AccountsChart(ctype, $(this).val(),
chart_area.get(0));
pscript.set_title(wrapper, ctype, $(this).val());
2012-04-13 13:34:55 +00:00
})
// load up companies
2014-02-14 10:17:51 +00:00
return frappe.call({
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-02-14 10:17:51 +00:00
wrapper.$company_select.val(frappe.defaults.get_default("company") || r[0]).change();
2012-04-13 13:34:55 +00:00
}
});
}
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 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-04-13 13:34:55 +00:00
if(erpnext.account_chart && erpnext.account_chart.ctype != ctype) {
wrapper.$company_select.change();
}
pscript.set_title(wrapper, ctype);
2012-04-13 13:34:55 +00:00
}
erpnext.AccountsChart = Class.extend({
init: function(ctype, company, wrapper) {
$(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);
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({
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},
method: 'erpnext.accounts.page.accounts_browser.accounts_browser.get_children',
2012-04-13 13:34:55 +00:00
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 06:39:52 +00:00
$(me.cur_toolbar).toggle(true);
2012-04-13 13:34:55 +00:00
}
2012-05-22 11:24:46 +00:00
// bold
$('.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
},
onrender: function(treenode) {
if (ctype == 'Account' && treenode.data) {
if(treenode.data.balance) {
treenode.parent.append('<span class="balance-area pull-right">'
+ format_currency(treenode.data.balance, treenode.data.currency)
+ '</span>');
}
}
2012-04-13 13:34:55 +00:00
}
});
2012-05-22 11:24:46 +00:00
this.tree.rootnode.$a.click();
2012-04-13 13:34:55 +00:00
},
make_link_toolbar: function(link) {
var data = $(link).data('node-data');
if(!data) return;
link.toolbar = $('<span class="tree-node-toolbar highlight"></span>').insertAfter(link);
var node_links = [];
2012-04-13 13:34:55 +00:00
// edit
2014-02-14 10:17:51 +00:00
if (frappe.model.can_read(this.ctype) !== -1) {
node_links.push('<a onclick="erpnext.account_chart.open();">'+frappe._('Edit')+'</a>');
2012-04-17 07:10:37 +00:00
}
2014-02-14 10:17:51 +00:00
if (data.expandable && frappe.boot.profile.in_create.indexOf(this.ctype) !== -1) {
node_links.push('<a onclick="erpnext.account_chart.new_node();">'+frappe._('Add Child')+'</a>');
} else if (this.ctype === 'Account' && frappe.boot.profile.can_read.indexOf("GL Entry") !== -1) {
node_links.push('<a onclick="erpnext.account_chart.show_ledger();">'+frappe._('View Ledger')+'</a>');
}
2012-12-06 06:39:52 +00:00
if (this.can_write) {
2014-02-14 10:17:51 +00:00
node_links.push('<a onclick="erpnext.account_chart.rename()">'+frappe._('Rename')+'</a>');
};
2012-12-06 06:39:52 +00:00
if (this.can_delete) {
2014-02-14 10:17:51 +00:00
node_links.push('<a onclick="erpnext.account_chart.delete()">'+frappe._('Delete')+'</a>');
};
link.toolbar.append(node_links.join(" | "));
2012-04-13 13:34:55 +00:00
},
2012-12-06 06:39:52 +00:00
open: function() {
var node = this.selected_node();
2014-02-14 10:17:51 +00:00
frappe.set_route("Form", this.ctype, node.data("label"));
2012-12-06 06:39:52 +00:00
},
2012-04-16 13:06:46 +00:00
show_ledger: function() {
var me = this;
var node = me.selected_node();
2014-02-14 10:17:51 +00:00
frappe.route_options = {
"account": node.data('label'),
"from_date": sys_defaults.year_start_date,
"to_date": sys_defaults.year_end_date,
"company": me.company
};
2014-02-14 10:17:51 +00:00
frappe.set_route("query-report", "General Ledger");
2012-04-16 13:06:46 +00:00
},
rename: function() {
2012-12-06 06:39:52 +00:00
var node = this.selected_node();
2014-02-14 10:17:51 +00:00
frappe.model.rename_doc(this.ctype, node.data('label'), function(new_name) {
node.parents("ul:first").parent().find(".tree-link:first").trigger("reload");
2012-12-06 06:39:52 +00:00
});
},
delete: function() {
2012-12-06 06:39:52 +00:00
var node = this.selected_node();
2014-02-14 10:17:51 +00:00
frappe.model.delete_doc(this.ctype, node.data('label'), function() {
node.parent().remove();
});
},
2012-04-16 13:06:46 +00:00
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 13:34:55 +00:00
new_account: 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({
title:frappe._('New Account'),
2012-04-16 13:06:46 +00:00
fields: [
2014-02-14 10:17:51 +00:00
{fieldtype:'Data', fieldname:'account_name', label:frappe._('New Account Name'), reqd:true,
description: frappe._("Name of new Account. Note: Please don't create accounts for Customers and Suppliers,")+
frappe._("they are created automatically from the Customer and Supplier master")},
{fieldtype:'Select', fieldname:'group_or_ledger', label:frappe._('Group or Ledger'),
options:'Group\nLedger', description: frappe._('Further accounts can be made under Groups,')+
frappe._('but entries can be made against Ledger')},
{fieldtype:'Select', fieldname:'account_type', label:frappe._('Account Type'),
2012-04-16 13:06:46 +00:00
options: ['', 'Fixed Asset Account', 'Bank or Cash', 'Expense Account', 'Tax',
'Income Account', 'Chargeable'].join('\n'),
2014-02-14 10:17:51 +00:00
description: frappe._("Optional. This setting will be used to filter in various transactions.") },
{fieldtype:'Float', fieldname:'tax_rate', label:frappe._('Tax Rate')},
{fieldtype:'Button', fieldname:'create_new', label:frappe._('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;
// 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') {
$(fd.tax_rate.wrapper).toggle(true);
2012-04-16 13:06:46 +00:00
} else {
$(fd.tax_rate.wrapper).toggle(false);
2012-04-16 13:06:46 +00:00
}
})
// 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');
v.master_type = '';
2012-04-16 13:06:46 +00:00
v.company = me.company;
2014-02-14 10:17:51 +00:00
return frappe.call({
args: v,
method: 'erpnext.accounts.utils.add_ac',
callback: function(r) {
2012-04-16 13:06:46 +00:00
$(btn).done_working();
d.hide();
node.trigger('reload');
}
});
2012-04-16 13:06:46 +00:00
});
// show
d.onshow = function() {
$(fd.group_or_ledger.input).change();
$(fd.account_type.input).change();
2012-04-16 13:06:46 +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
},
2012-04-16 13:06:46 +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({
title:frappe._('New Cost Center'),
2012-04-16 13:06:46 +00:00
fields: [
2014-02-14 10:17:51 +00:00
{fieldtype:'Data', fieldname:'cost_center_name', label:frappe._('New Cost Center Name'), reqd:true},
{fieldtype:'Select', fieldname:'group_or_ledger', label:frappe._('Group or Ledger'),
options:'Group\nLedger', description:frappe._('Further accounts can be made under Groups,')+
frappe._('but entries can be made against Ledger')},
{fieldtype:'Button', fieldname:'create_new', label:frappe._('Create New') }
2012-04-16 13:06:46 +00:00
]
});
2012-04-16 13:06:46 +00:00
// 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');
v.company = me.company;
2012-04-16 13:06:46 +00:00
2014-02-14 10:17:51 +00:00
return frappe.call({
args: v,
method: 'erpnext.accounts.utils.add_cc',
callback: function(r) {
2012-04-16 13:06:46 +00:00
$(btn).done_working();
d.hide();
node.trigger('reload');
}
});
2012-04-16 13:06:46 +00:00
});
d.show();
}
2012-05-22 11:24:46 +00:00
});