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-09-17 13:40:36 +00:00
|
|
|
// 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.
|
2014-06-02 13:17:13 +00:00
|
|
|
//
|
2012-09-17 13:40:36 +00:00
|
|
|
// 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.
|
2014-06-02 13:17:13 +00:00
|
|
|
//
|
2012-09-17 13:40:36 +00:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-02-17 14:23:23 +00:00
|
|
|
frappe.assets.views["Report"]();
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
erpnext.AccountTreeGrid = frappe.views.TreeGridReport.extend({
|
2012-09-17 13:40:36 +00:00
|
|
|
init: function(wrapper, title) {
|
|
|
|
this._super({
|
|
|
|
title: title,
|
|
|
|
page: wrapper,
|
|
|
|
parent: $(wrapper).find('.layout-main'),
|
2014-12-23 12:26:47 +00:00
|
|
|
page: wrapper.page,
|
2012-09-21 14:16:24 +00:00
|
|
|
doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"],
|
2012-09-20 13:33:14 +00:00
|
|
|
tree_grid: {
|
2014-06-02 13:17:13 +00:00
|
|
|
show: true,
|
|
|
|
parent_field: "parent_account",
|
2012-09-20 13:33:14 +00:00
|
|
|
formatter: function(item) {
|
2013-12-23 11:37:57 +00:00
|
|
|
return repl("<a \
|
2014-02-14 10:17:51 +00:00
|
|
|
onclick='frappe.cur_grid_report.show_general_ledger(\"%(value)s\")'>\
|
2013-12-23 11:37:57 +00:00
|
|
|
%(value)s</a>", {
|
2012-09-20 13:33:14 +00:00
|
|
|
value: item.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2012-09-17 13:40:36 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
setup_columns: function() {
|
|
|
|
this.columns = [
|
2014-06-02 13:17:13 +00:00
|
|
|
{id: "name", name: __("Account"), field: "name", width: 300, cssClass: "cell-title",
|
2012-09-20 13:33:14 +00:00
|
|
|
formatter: this.tree_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "opening_dr", name: __("Opening (Dr)"), field: "opening_dr", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "opening_cr", name: __("Opening (Cr)"), field: "opening_cr", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "debit", name: __("Debit"), field: "debit", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "credit", name: __("Credit"), field: "credit", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "closing_dr", name: __("Closing (Dr)"), field: "closing_dr", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-04-14 10:55:30 +00:00
|
|
|
{id: "closing_cr", name: __("Closing (Cr)"), field: "closing_cr", width: 100,
|
2012-09-17 13:40:36 +00:00
|
|
|
formatter: this.currency_formatter}
|
|
|
|
];
|
|
|
|
|
|
|
|
},
|
|
|
|
filters: [
|
2014-06-03 07:32:18 +00:00
|
|
|
{fieldtype: "Select", label: __("Company"), link:"Company", fieldname: "company",
|
|
|
|
default_value: __("Select Company..."),
|
2012-09-19 11:11:19 +00:00
|
|
|
filter: function(val, item, opts, me) {
|
|
|
|
if (item.company == val || val == opts.default_value) {
|
|
|
|
return me.apply_zero_filter(val, item, opts, me);
|
|
|
|
}
|
|
|
|
return false;
|
2012-09-17 13:40:36 +00:00
|
|
|
}},
|
2014-06-03 07:32:18 +00:00
|
|
|
{fieldtype: "Select", label: "Fiscal Year", link:"Fiscal Year", fieldname: "fiscal_year",
|
|
|
|
default_value: __("Select Fiscal Year...")},
|
|
|
|
{fieldtype: "Date", label: __("From Date"), fieldname: "from_date"},
|
2014-04-14 10:55:30 +00:00
|
|
|
{fieldtype: "Label", label: __("To")},
|
2015-01-01 10:55:43 +00:00
|
|
|
{fieldtype: "Date", label: __("To Date"), fieldname: "to_date"}
|
2012-09-17 13:40:36 +00:00
|
|
|
],
|
|
|
|
setup_filters: function() {
|
|
|
|
this._super();
|
|
|
|
var me = this;
|
|
|
|
// default filters
|
|
|
|
this.filter_inputs.fiscal_year.change(function() {
|
|
|
|
var fy = $(this).val();
|
2014-02-14 10:17:51 +00:00
|
|
|
$.each(frappe.report_dump.data["Fiscal Year"], function(i, v) {
|
2012-09-17 13:40:36 +00:00
|
|
|
if (v.name==fy) {
|
|
|
|
me.filter_inputs.from_date.val(dateutil.str_to_user(v.year_start_date));
|
|
|
|
me.filter_inputs.to_date.val(dateutil.str_to_user(v.year_end_date));
|
|
|
|
}
|
|
|
|
});
|
2014-06-03 07:32:18 +00:00
|
|
|
me.refresh();
|
2012-09-17 13:40:36 +00:00
|
|
|
});
|
2012-09-19 11:11:19 +00:00
|
|
|
me.show_zero_check()
|
2013-01-01 08:18:40 +00:00
|
|
|
if(me.ignore_closing_entry) me.ignore_closing_entry();
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
prepare_data: function() {
|
2012-09-19 11:11:19 +00:00
|
|
|
var me = this;
|
2012-11-28 10:36:37 +00:00
|
|
|
if(!this.primary_data) {
|
2012-09-18 13:22:05 +00:00
|
|
|
// make accounts list
|
2012-09-20 13:33:14 +00:00
|
|
|
me.data = [];
|
2012-09-18 13:22:05 +00:00
|
|
|
me.parent_map = {};
|
2012-09-20 13:33:14 +00:00
|
|
|
me.item_by_name = {};
|
2012-09-17 13:40:36 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
$.each(frappe.report_dump.data["Account"], function(i, v) {
|
2012-09-18 13:22:05 +00:00
|
|
|
var d = copy_dict(v);
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
me.data.push(d);
|
|
|
|
me.item_by_name[d.name] = d;
|
2012-09-18 13:22:05 +00:00
|
|
|
if(d.parent_account) {
|
|
|
|
me.parent_map[d.name] = d.parent_account;
|
|
|
|
}
|
2012-11-21 12:32:22 +00:00
|
|
|
});
|
2014-06-02 13:17:13 +00:00
|
|
|
|
2012-11-28 10:36:37 +00:00
|
|
|
me.primary_data = [].concat(me.data);
|
2012-09-18 13:22:05 +00:00
|
|
|
}
|
2014-06-02 13:17:13 +00:00
|
|
|
|
2012-11-28 10:36:37 +00:00
|
|
|
me.data = [].concat(me.primary_data);
|
|
|
|
$.each(me.data, function(i, d) {
|
|
|
|
me.init_account(d);
|
|
|
|
});
|
2014-06-02 13:17:13 +00:00
|
|
|
|
2012-09-18 13:22:05 +00:00
|
|
|
this.set_indent();
|
2012-09-17 13:40:36 +00:00
|
|
|
this.prepare_balances();
|
2014-06-02 13:17:13 +00:00
|
|
|
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
2012-09-21 14:16:24 +00:00
|
|
|
init_account: function(d) {
|
2014-06-02 13:17:13 +00:00
|
|
|
this.reset_item_values(d);
|
2012-09-21 14:16:24 +00:00
|
|
|
},
|
2012-09-20 13:33:14 +00:00
|
|
|
|
2012-09-17 13:40:36 +00:00
|
|
|
prepare_balances: function() {
|
2014-02-14 10:17:51 +00:00
|
|
|
var gl = frappe.report_dump.data['GL Entry'];
|
2012-09-17 13:40:36 +00:00
|
|
|
var me = this;
|
|
|
|
|
|
|
|
this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val());
|
|
|
|
this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val());
|
|
|
|
this.set_fiscal_year();
|
|
|
|
if (!this.fiscal_year) return;
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
$.each(this.data, function(i, v) {
|
2014-06-02 13:17:13 +00:00
|
|
|
v.opening_dr = v.opening_cr = v.debit
|
2014-03-21 05:43:17 +00:00
|
|
|
= v.credit = v.closing_dr = v.closing_cr = 0;
|
2012-09-17 13:40:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$.each(gl, function(i, v) {
|
|
|
|
var posting_date = dateutil.str_to_obj(v.posting_date);
|
2012-09-20 13:33:14 +00:00
|
|
|
var account = me.item_by_name[v.account];
|
2013-08-12 12:02:47 +00:00
|
|
|
me.update_balances(account, posting_date, v);
|
2012-09-17 13:40:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.update_groups();
|
|
|
|
},
|
|
|
|
update_balances: function(account, posting_date, v) {
|
|
|
|
// opening
|
|
|
|
if (posting_date < this.opening_date || v.is_opening === "Yes") {
|
2014-06-02 13:17:13 +00:00
|
|
|
if (account.report_type === "Profit and Loss" &&
|
2012-09-17 13:40:36 +00:00
|
|
|
posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) {
|
2014-06-02 13:17:13 +00:00
|
|
|
// balance of previous fiscal_year should
|
2012-09-17 13:40:36 +00:00
|
|
|
// not be part of opening of pl account balance
|
|
|
|
} else {
|
2014-06-02 13:17:13 +00:00
|
|
|
var opening_bal = flt(account.opening_dr) - flt(account.opening_cr) +
|
|
|
|
flt(v.debit) - flt(v.credit);
|
|
|
|
this.set_debit_or_credit(account, "opening", opening_bal);
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
} else if (this.opening_date <= posting_date && posting_date <= this.closing_date) {
|
|
|
|
// in between
|
2014-03-21 05:43:17 +00:00
|
|
|
account.debit += flt(v.debit);
|
|
|
|
account.credit += flt(v.credit);
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
// closing
|
2014-06-02 13:17:13 +00:00
|
|
|
var closing_bal = flt(account.opening_dr) - flt(account.opening_cr) +
|
|
|
|
flt(account.debit) - flt(account.credit);
|
|
|
|
this.set_debit_or_credit(account, "closing", closing_bal);
|
2014-03-21 05:43:17 +00:00
|
|
|
},
|
|
|
|
set_debit_or_credit: function(account, field, balance) {
|
|
|
|
if(balance > 0) {
|
|
|
|
account[field+"_dr"] = balance;
|
|
|
|
account[field+"_cr"] = 0;
|
2012-09-17 13:40:36 +00:00
|
|
|
} else {
|
2014-03-21 05:43:17 +00:00
|
|
|
account[field+"_cr"] = Math.abs(balance);
|
|
|
|
account[field+"_dr"] = 0;
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
update_groups: function() {
|
|
|
|
// update groups
|
|
|
|
var me= this;
|
2012-09-20 13:33:14 +00:00
|
|
|
$.each(this.data, function(i, account) {
|
2012-09-17 13:40:36 +00:00
|
|
|
// update groups
|
2013-10-27 10:50:51 +00:00
|
|
|
if((account.group_or_ledger == "Ledger") || (account.rgt - account.lft == 1)) {
|
2012-09-18 13:22:05 +00:00
|
|
|
var parent = me.parent_map[account.name];
|
|
|
|
while(parent) {
|
2012-10-11 08:34:27 +00:00
|
|
|
var parent_account = me.item_by_name[parent];
|
2012-09-18 13:22:05 +00:00
|
|
|
$.each(me.columns, function(c, col) {
|
|
|
|
if (col.formatter == me.currency_formatter) {
|
2014-03-21 05:43:17 +00:00
|
|
|
if(col.field=="opening_dr") {
|
2014-06-02 13:17:13 +00:00
|
|
|
var bal = flt(parent_account.opening_dr) -
|
|
|
|
flt(parent_account.opening_cr) +
|
2014-03-21 05:43:17 +00:00
|
|
|
flt(account.opening_dr) - flt(account.opening_cr);
|
|
|
|
me.set_debit_or_credit(parent_account, "opening", bal);
|
|
|
|
} else if(col.field=="closing_dr") {
|
2014-06-02 13:17:13 +00:00
|
|
|
var bal = flt(parent_account.closing_dr) -
|
|
|
|
flt(parent_account.closing_cr) +
|
2014-03-21 05:43:17 +00:00
|
|
|
flt(account.closing_dr) - flt(account.closing_cr);
|
|
|
|
me.set_debit_or_credit(parent_account, "closing", bal);
|
|
|
|
} else if(in_list(["debit", "credit"], col.field)) {
|
2014-06-02 13:17:13 +00:00
|
|
|
parent_account[col.field] = flt(parent_account[col.field]) +
|
2014-03-21 05:43:17 +00:00
|
|
|
flt(account[col.field]);
|
|
|
|
}
|
2012-09-18 13:22:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
parent = me.parent_map[parent];
|
2014-06-02 13:17:13 +00:00
|
|
|
}
|
2012-09-18 13:22:05 +00:00
|
|
|
}
|
2014-06-02 13:17:13 +00:00
|
|
|
});
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set_fiscal_year: function() {
|
|
|
|
if (this.opening_date > this.closing_date) {
|
2014-04-14 10:55:30 +00:00
|
|
|
msgprint(__("Opening Date should be before Closing Date"));
|
2012-09-17 13:40:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fiscal_year = null;
|
|
|
|
var me = this;
|
2014-02-14 10:17:51 +00:00
|
|
|
$.each(frappe.report_dump.data["Fiscal Year"], function(i, v) {
|
2014-06-02 13:17:13 +00:00
|
|
|
if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) &&
|
2012-09-17 13:40:36 +00:00
|
|
|
me.closing_date <= dateutil.str_to_obj(v.year_end_date)) {
|
|
|
|
me.fiscal_year = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.fiscal_year) {
|
2014-04-14 10:55:30 +00:00
|
|
|
msgprint(__("Opening Date and Closing Date should be within same Fiscal Year"));
|
2012-09-17 13:40:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2014-06-02 13:17:13 +00:00
|
|
|
|
2013-12-23 11:37:57 +00:00
|
|
|
show_general_ledger: function(account) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.route_options = {
|
2013-12-23 11:37:57 +00:00
|
|
|
account: account,
|
|
|
|
company: this.company,
|
|
|
|
from_date: this.from_date,
|
|
|
|
to_date: this.to_date
|
|
|
|
};
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.set_route("query-report", "General Ledger");
|
2013-12-23 11:37:57 +00:00
|
|
|
}
|
2014-06-02 13:17:13 +00:00
|
|
|
});
|