318 lines
9.6 KiB
JavaScript
Raw Normal View History

// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// 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
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({
parent: wrapper,
single_column: true
})
2015-03-09 15:47:15 +05:30
frappe.breadcrumbs.add("Accounts");
2015-01-19 17:34:33 +05:30
var main = wrapper.page.main,
chart_area = $("<div>")
.css({"margin-bottom": "15px", "min-height": "200px"})
.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);
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);
}
wrapper.page.add_menu_item(__('Refresh'), function() {
2012-04-16 18:36:46 +05:30
wrapper.$company_select.change();
});
2012-04-13 12:01:23 +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();
}, "octicon octicon-plus");
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';
erpnext.account_chart = new erpnext.AccountsChart(ctype, $(this).val(),
chart_area.get(0), wrapper.page);
2012-04-13 19:04:55 +05:30
})
2012-04-13 19:04:55 +05:30
// load up companies
2014-02-14 15:47:51 +05:30
return frappe.call({
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
}
});
}
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-04-13 19:04:55 +05:30
if(erpnext.account_chart && erpnext.account_chart.ctype != ctype) {
wrapper.$company_select.change();
}
2012-04-13 19:04:55 +05:30
}
erpnext.AccountsChart = Class.extend({
init: function(ctype, company, wrapper, page) {
$(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);
me.page = page;
me.set_title();
2015-02-20 10:39:39 +05:30
// __("Accounts"), __("Cost Centers")
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({
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},
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
$('.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-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()
},
btnClass: "hidden-xs"
2014-02-19 12:43:16 +05:30
},
{
condition: function(node) {
return !node.root && me.ctype === 'Account'
&& 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");
},
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();
});
},
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();
});
},
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">'
+ 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-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) {
this.page.set_title(chart_str + " - " + cstr(val));
2015-01-07 16:43:27 +05:30
} else {
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;
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: [
{fieldtype:'Data', fieldname:'account_name', label:__('New Account Name'), reqd:true,
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'),
options: ['', 'Bank', 'Cash', 'Warehouse', 'Receivable', 'Payable',
'Equity', 'Cost of Goods Sold', 'Fixed Asset', 'Expense Account',
'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')},
{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;
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
}
});
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
})
2012-04-16 18:36:46 +05:30
// create
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;
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-02-14 15:47:51 +05:30
return frappe.call({
args: v,
method: 'erpnext.accounts.utils.add_ac',
callback: function(r) {
2012-04-16 18:36:46 +05:30
d.hide();
if(node.expanded) {
node.toggle_node();
}
node.reload();
}
});
2012-04-16 18:36:46 +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();
$(fd.account_type.input).change();
2012-04-16 18:36:46 +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
},
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-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-02-19 12:43:16 +05:30
var node = me.tree.get_selected_node();
2014-02-19 12:43:16 +05:30
v.parent_cost_center = node.label;
v.company = me.company;
2014-02-14 15:47:51 +05:30
return frappe.call({
args: v,
method: 'erpnext.accounts.utils.add_cc',
callback: function(r) {
2012-04-16 18:36:46 +05:30
d.hide();
if(node.expanded) {
node.toggle_node();
}
2014-02-19 12:43:16 +05:30
node.reload();
}
});
2012-04-16 18:36:46 +05:30
});
d.show();
}
});