2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-07-22 13:32:11 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2017-05-10 14:10:36 +00:00
|
|
|
import frappe, erpnext
|
2014-07-22 13:32:11 +00:00
|
|
|
from frappe import _
|
2016-03-10 06:31:01 +00:00
|
|
|
from frappe.utils import flt, getdate, formatdate, cstr
|
|
|
|
from erpnext.accounts.report.financial_statements \
|
|
|
|
import filter_accounts, set_gl_entries_by_account, filter_out_zero_value_rows
|
2020-03-17 05:23:24 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions, get_dimension_with_children
|
2014-07-22 13:32:11 +00:00
|
|
|
|
|
|
|
value_fields = ("opening_debit", "opening_credit", "debit", "credit", "closing_debit", "closing_credit")
|
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
def execute(filters=None):
|
2014-07-22 13:32:11 +00:00
|
|
|
validate_filters(filters)
|
|
|
|
data = get_data(filters)
|
|
|
|
columns = get_columns()
|
|
|
|
return columns, data
|
|
|
|
|
|
|
|
def validate_filters(filters):
|
2016-01-11 06:29:08 +00:00
|
|
|
if not filters.fiscal_year:
|
2016-02-15 06:33:04 +00:00
|
|
|
frappe.throw(_("Fiscal Year {0} is required").format(filters.fiscal_year))
|
|
|
|
|
|
|
|
fiscal_year = frappe.db.get_value("Fiscal Year", filters.fiscal_year, ["year_start_date", "year_end_date"], as_dict=True)
|
|
|
|
if not fiscal_year:
|
|
|
|
frappe.throw(_("Fiscal Year {0} does not exist").format(filters.fiscal_year))
|
|
|
|
else:
|
|
|
|
filters.year_start_date = getdate(fiscal_year.year_start_date)
|
|
|
|
filters.year_end_date = getdate(fiscal_year.year_end_date)
|
2014-07-22 13:32:11 +00:00
|
|
|
|
|
|
|
if not filters.from_date:
|
|
|
|
filters.from_date = filters.year_start_date
|
|
|
|
|
|
|
|
if not filters.to_date:
|
|
|
|
filters.to_date = filters.year_end_date
|
|
|
|
|
|
|
|
filters.from_date = getdate(filters.from_date)
|
|
|
|
filters.to_date = getdate(filters.to_date)
|
|
|
|
|
|
|
|
if filters.from_date > filters.to_date:
|
|
|
|
frappe.throw(_("From Date cannot be greater than To Date"))
|
|
|
|
|
|
|
|
if (filters.from_date < filters.year_start_date) or (filters.from_date > filters.year_end_date):
|
|
|
|
frappe.msgprint(_("From Date should be within the Fiscal Year. Assuming From Date = {0}")\
|
|
|
|
.format(formatdate(filters.year_start_date)))
|
|
|
|
|
|
|
|
filters.from_date = filters.year_start_date
|
|
|
|
|
|
|
|
if (filters.to_date < filters.year_start_date) or (filters.to_date > filters.year_end_date):
|
|
|
|
frappe.msgprint(_("To Date should be within the Fiscal Year. Assuming To Date = {0}")\
|
|
|
|
.format(formatdate(filters.year_end_date)))
|
|
|
|
filters.to_date = filters.year_end_date
|
|
|
|
|
|
|
|
def get_data(filters):
|
2018-08-20 12:28:28 +00:00
|
|
|
|
|
|
|
accounts = frappe.db.sql("""select name, account_number, parent_account, account_name, root_type, report_type, lft, rgt
|
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
from `tabAccount` where company=%s order by lft""", filters.company, as_dict=True)
|
2020-09-29 20:02:42 +00:00
|
|
|
company_currency = filters.presentation_currency or erpnext.get_company_currency(filters.company)
|
2014-07-22 13:32:11 +00:00
|
|
|
|
|
|
|
if not accounts:
|
|
|
|
return None
|
|
|
|
|
2016-03-10 06:31:01 +00:00
|
|
|
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
|
2014-07-22 13:32:11 +00:00
|
|
|
|
|
|
|
min_lft, max_rgt = frappe.db.sql("""select min(lft), max(rgt) from `tabAccount`
|
|
|
|
where company=%s""", (filters.company,))[0]
|
|
|
|
|
2015-12-01 12:47:18 +00:00
|
|
|
gl_entries_by_account = {}
|
|
|
|
|
2019-01-15 10:22:52 +00:00
|
|
|
opening_balances = get_opening_balances(filters)
|
2020-05-15 07:28:48 +00:00
|
|
|
|
|
|
|
#add filter inside list so that the query in financial_statements.py doesn't break
|
2020-05-20 16:45:12 +00:00
|
|
|
if filters.project:
|
|
|
|
filters.project = [filters.project]
|
2020-05-15 07:28:48 +00:00
|
|
|
|
2015-12-01 12:47:18 +00:00
|
|
|
set_gl_entries_by_account(filters.company, filters.from_date,
|
2016-08-24 13:42:14 +00:00
|
|
|
filters.to_date, min_lft, max_rgt, filters, gl_entries_by_account, ignore_closing_entries=not flt(filters.with_period_closing_entry))
|
2014-07-22 13:32:11 +00:00
|
|
|
|
2017-05-10 14:10:36 +00:00
|
|
|
total_row = calculate_values(accounts, gl_entries_by_account, opening_balances, filters, company_currency)
|
2014-07-22 13:32:11 +00:00
|
|
|
accumulate_values_into_parents(accounts, accounts_by_name)
|
|
|
|
|
2017-05-10 14:10:36 +00:00
|
|
|
data = prepare_data(accounts, filters, total_row, parent_children_map, company_currency)
|
2019-11-11 05:29:20 +00:00
|
|
|
data = filter_out_zero_value_rows(data, parent_children_map, show_zero_values=filters.get("show_zero_values"))
|
2018-08-20 12:28:28 +00:00
|
|
|
|
2014-07-22 13:32:11 +00:00
|
|
|
return data
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
def get_opening_balances(filters):
|
|
|
|
balance_sheet_opening = get_rootwise_opening_balances(filters, "Balance Sheet")
|
|
|
|
pl_opening = get_rootwise_opening_balances(filters, "Profit and Loss")
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
balance_sheet_opening.update(pl_opening)
|
|
|
|
return balance_sheet_opening
|
2015-12-01 12:47:18 +00:00
|
|
|
|
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
def get_rootwise_opening_balances(filters, report_type):
|
2016-09-12 06:37:58 +00:00
|
|
|
additional_conditions = ""
|
2016-10-17 11:27:52 +00:00
|
|
|
if not filters.show_unclosed_fy_pl_balances:
|
|
|
|
additional_conditions = " and posting_date >= %(year_start_date)s" \
|
|
|
|
if report_type == "Profit and Loss" else ""
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if not flt(filters.with_period_closing_entry):
|
|
|
|
additional_conditions += " and ifnull(voucher_type, '')!='Period Closing Voucher'"
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2019-01-15 10:22:52 +00:00
|
|
|
if filters.cost_center:
|
|
|
|
lft, rgt = frappe.db.get_value('Cost Center', filters.cost_center, ['lft', 'rgt'])
|
|
|
|
additional_conditions += """ and cost_center in (select name from `tabCost Center`
|
|
|
|
where lft >= %s and rgt <= %s)""" % (lft, rgt)
|
|
|
|
|
2020-05-15 07:28:48 +00:00
|
|
|
if filters.project:
|
|
|
|
additional_conditions += " and project = %(project)s"
|
|
|
|
|
2019-01-15 10:22:52 +00:00
|
|
|
if filters.finance_book:
|
2020-01-27 09:48:51 +00:00
|
|
|
fb_conditions = " AND finance_book = %(finance_book)s"
|
2019-01-17 12:23:58 +00:00
|
|
|
if filters.include_default_book_entries:
|
2020-01-27 09:48:51 +00:00
|
|
|
fb_conditions = " AND (finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)"
|
2019-01-15 10:22:52 +00:00
|
|
|
|
|
|
|
additional_conditions += fb_conditions
|
|
|
|
|
2020-03-17 05:23:24 +00:00
|
|
|
accounting_dimensions = get_accounting_dimensions(as_list=False)
|
2019-07-03 12:38:41 +00:00
|
|
|
|
|
|
|
query_filters = {
|
|
|
|
"company": filters.company,
|
|
|
|
"from_date": filters.from_date,
|
|
|
|
"report_type": report_type,
|
|
|
|
"year_start_date": filters.year_start_date,
|
2020-05-15 07:28:48 +00:00
|
|
|
"project": filters.project,
|
2019-07-03 12:38:41 +00:00
|
|
|
"finance_book": filters.finance_book,
|
|
|
|
"company_fb": frappe.db.get_value("Company", filters.company, 'default_finance_book')
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounting_dimensions:
|
|
|
|
for dimension in accounting_dimensions:
|
2020-03-17 05:23:24 +00:00
|
|
|
if filters.get(dimension.fieldname):
|
|
|
|
if frappe.get_cached_value('DocType', dimension.document_type, 'is_tree'):
|
|
|
|
filters[dimension.fieldname] = get_dimension_with_children(dimension.document_type,
|
|
|
|
filters.get(dimension.fieldname))
|
2020-04-07 09:47:06 +00:00
|
|
|
additional_conditions += "and {0} in %({0})s".format(dimension.fieldname)
|
|
|
|
else:
|
|
|
|
additional_conditions += "and {0} in (%({0})s)".format(dimension.fieldname)
|
2019-07-03 12:38:41 +00:00
|
|
|
|
2019-07-29 09:27:33 +00:00
|
|
|
query_filters.update({
|
2020-03-17 05:23:24 +00:00
|
|
|
dimension.fieldname: filters.get(dimension.fieldname)
|
2019-07-29 09:27:33 +00:00
|
|
|
})
|
2019-07-03 12:38:41 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
gle = frappe.db.sql("""
|
2015-12-01 12:47:18 +00:00
|
|
|
select
|
|
|
|
account, sum(debit) as opening_debit, sum(credit) as opening_credit
|
2015-06-14 12:19:29 +00:00
|
|
|
from `tabGL Entry`
|
2015-12-01 12:47:18 +00:00
|
|
|
where
|
2015-06-14 12:19:29 +00:00
|
|
|
company=%(company)s
|
|
|
|
{additional_conditions}
|
2015-06-17 10:05:06 +00:00
|
|
|
and (posting_date < %(from_date)s or ifnull(is_opening, 'No') = 'Yes')
|
2015-06-14 12:19:29 +00:00
|
|
|
and account in (select name from `tabAccount` where report_type=%(report_type)s)
|
2020-07-21 07:17:16 +00:00
|
|
|
and is_cancelled = 0
|
2019-07-03 12:38:41 +00:00
|
|
|
group by account""".format(additional_conditions=additional_conditions), query_filters , as_dict=True)
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
opening = frappe._dict()
|
|
|
|
for d in gle:
|
|
|
|
opening.setdefault(d.account, d)
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
return opening
|
|
|
|
|
2017-05-10 14:10:36 +00:00
|
|
|
def calculate_values(accounts, gl_entries_by_account, opening_balances, filters, company_currency):
|
2014-07-22 13:32:11 +00:00
|
|
|
init = {
|
|
|
|
"opening_debit": 0.0,
|
|
|
|
"opening_credit": 0.0,
|
|
|
|
"debit": 0.0,
|
|
|
|
"credit": 0.0,
|
|
|
|
"closing_debit": 0.0,
|
|
|
|
"closing_credit": 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
total_row = {
|
2017-03-28 04:49:11 +00:00
|
|
|
"account": "'" + _("Total") + "'",
|
|
|
|
"account_name": "'" + _("Total") + "'",
|
2014-07-22 13:32:11 +00:00
|
|
|
"warn_if_negative": True,
|
2017-12-06 07:44:28 +00:00
|
|
|
"opening_debit": 0.0,
|
|
|
|
"opening_credit": 0.0,
|
2014-07-22 13:32:11 +00:00
|
|
|
"debit": 0.0,
|
2017-03-28 04:49:11 +00:00
|
|
|
"credit": 0.0,
|
2017-12-06 07:44:28 +00:00
|
|
|
"closing_debit": 0.0,
|
|
|
|
"closing_credit": 0.0,
|
2017-03-28 04:49:11 +00:00
|
|
|
"parent_account": None,
|
|
|
|
"indent": 0,
|
2017-05-10 14:10:36 +00:00
|
|
|
"has_value": True,
|
|
|
|
"currency": company_currency
|
2014-07-22 13:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for d in accounts:
|
|
|
|
d.update(init.copy())
|
|
|
|
|
2015-06-17 09:39:33 +00:00
|
|
|
# add opening
|
|
|
|
d["opening_debit"] = opening_balances.get(d.name, {}).get("opening_debit", 0)
|
|
|
|
d["opening_credit"] = opening_balances.get(d.name, {}).get("opening_credit", 0)
|
|
|
|
|
2014-07-22 13:32:11 +00:00
|
|
|
for entry in gl_entries_by_account.get(d.name, []):
|
2015-06-18 05:02:42 +00:00
|
|
|
if cstr(entry.is_opening) != "Yes":
|
2015-06-17 09:39:33 +00:00
|
|
|
d["debit"] += flt(entry.debit)
|
|
|
|
d["credit"] += flt(entry.credit)
|
2014-07-22 13:32:11 +00:00
|
|
|
|
2018-08-20 12:28:28 +00:00
|
|
|
d["closing_debit"] = d["opening_debit"] + d["debit"]
|
|
|
|
d["closing_credit"] = d["opening_credit"] + d["credit"]
|
|
|
|
|
2019-11-11 05:29:20 +00:00
|
|
|
prepare_opening_closing(d)
|
2019-06-14 06:27:14 +00:00
|
|
|
|
2019-11-11 05:29:20 +00:00
|
|
|
for field in value_fields:
|
|
|
|
total_row[field] += d[field]
|
2014-07-22 13:32:11 +00:00
|
|
|
|
|
|
|
return total_row
|
|
|
|
|
|
|
|
def accumulate_values_into_parents(accounts, accounts_by_name):
|
|
|
|
for d in reversed(accounts):
|
|
|
|
if d.parent_account:
|
|
|
|
for key in value_fields:
|
|
|
|
accounts_by_name[d.parent_account][key] += d[key]
|
|
|
|
|
2017-05-10 14:10:36 +00:00
|
|
|
def prepare_data(accounts, filters, total_row, parent_children_map, company_currency):
|
2014-07-22 13:32:11 +00:00
|
|
|
data = []
|
2018-07-27 05:43:00 +00:00
|
|
|
|
2015-04-29 13:09:33 +00:00
|
|
|
for d in accounts:
|
2019-11-11 05:29:20 +00:00
|
|
|
# Prepare opening closing for group account
|
|
|
|
if parent_children_map.get(d.account):
|
|
|
|
prepare_opening_closing(d)
|
|
|
|
|
2014-07-22 13:32:11 +00:00
|
|
|
has_value = False
|
|
|
|
row = {
|
|
|
|
"account": d.name,
|
|
|
|
"parent_account": d.parent_account,
|
|
|
|
"indent": d.indent,
|
|
|
|
"from_date": filters.from_date,
|
2016-04-25 11:48:52 +00:00
|
|
|
"to_date": filters.to_date,
|
2018-07-30 05:08:51 +00:00
|
|
|
"currency": company_currency,
|
|
|
|
"account_name": ('{} - {}'.format(d.account_number, d.account_name)
|
|
|
|
if d.account_number else d.account_name)
|
2014-07-22 13:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key in value_fields:
|
2016-03-10 06:31:01 +00:00
|
|
|
row[key] = flt(d.get(key, 0.0), 3)
|
2018-08-20 12:28:28 +00:00
|
|
|
|
2016-03-10 06:31:01 +00:00
|
|
|
if abs(row[key]) >= 0.005:
|
|
|
|
# ignore zero values
|
2014-07-22 13:32:11 +00:00
|
|
|
has_value = True
|
|
|
|
|
2016-03-10 06:31:01 +00:00
|
|
|
row["has_value"] = has_value
|
|
|
|
data.append(row)
|
2018-08-20 12:28:28 +00:00
|
|
|
|
2014-07-22 13:32:11 +00:00
|
|
|
data.extend([{},total_row])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_columns():
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"fieldname": "account",
|
|
|
|
"label": _("Account"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Account",
|
|
|
|
"width": 300
|
|
|
|
},
|
2018-11-21 10:34:05 +00:00
|
|
|
{
|
|
|
|
"fieldname": "currency",
|
|
|
|
"label": _("Currency"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Currency",
|
|
|
|
"hidden": 1
|
|
|
|
},
|
2014-07-22 13:32:11 +00:00
|
|
|
{
|
|
|
|
"fieldname": "opening_debit",
|
|
|
|
"label": _("Opening (Dr)"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "opening_credit",
|
|
|
|
"label": _("Opening (Cr)"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "debit",
|
|
|
|
"label": _("Debit"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "credit",
|
|
|
|
"label": _("Credit"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "closing_debit",
|
|
|
|
"label": _("Closing (Dr)"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fieldname": "closing_credit",
|
|
|
|
"label": _("Closing (Cr)"),
|
|
|
|
"fieldtype": "Currency",
|
2016-04-25 11:48:52 +00:00
|
|
|
"options": "currency",
|
2014-07-22 13:32:11 +00:00
|
|
|
"width": 120
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-11-11 05:29:20 +00:00
|
|
|
def prepare_opening_closing(row):
|
|
|
|
dr_or_cr = "debit" if row["root_type"] in ["Asset", "Equity", "Expense"] else "credit"
|
|
|
|
reverse_dr_or_cr = "credit" if dr_or_cr == "debit" else "debit"
|
|
|
|
|
|
|
|
for col_type in ["opening", "closing"]:
|
|
|
|
valid_col = col_type + "_" + dr_or_cr
|
|
|
|
reverse_col = col_type + "_" + reverse_dr_or_cr
|
|
|
|
row[valid_col] -= row[reverse_col]
|
|
|
|
if row[valid_col] < 0:
|
|
|
|
row[reverse_col] = abs(row[valid_col])
|
|
|
|
row[valid_col] = 0.0
|
|
|
|
else:
|
|
|
|
row[reverse_col] = 0.0
|