2015-03-03 14:55:30 +05:30
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-07-14 19:06:52 +05:30
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 16:44:59 +05:30
|
|
|
|
2014-07-14 19:06:52 +05:30
|
|
|
import frappe
|
2014-07-21 16:13:06 +05:30
|
|
|
from frappe import _
|
2016-11-16 11:18:49 +05:30
|
|
|
from frappe.utils import cint, flt
|
2021-09-02 16:44:59 +05:30
|
|
|
|
2021-05-07 12:16:44 +05:30
|
|
|
from erpnext.accounts.report.financial_statements import (
|
|
|
|
get_columns,
|
|
|
|
get_data,
|
|
|
|
get_filtered_list_for_consolidated_report,
|
2020-04-06 14:07:20 +05:30
|
|
|
get_period_list,
|
2021-05-07 12:16:44 +05:30
|
|
|
)
|
2021-09-02 16:44:59 +05:30
|
|
|
|
2014-07-14 19:06:52 +05:30
|
|
|
|
2014-07-18 18:05:26 +05:30
|
|
|
def execute(filters=None):
|
2017-10-17 12:03:02 +05:30
|
|
|
period_list = get_period_list(
|
|
|
|
filters.from_fiscal_year,
|
|
|
|
filters.to_fiscal_year,
|
2020-04-07 12:16:25 +05:30
|
|
|
filters.period_start_date,
|
|
|
|
filters.period_end_date,
|
|
|
|
filters.filter_based_on,
|
2017-05-04 12:12:29 +05:30
|
|
|
filters.periodicity,
|
|
|
|
company=filters.company,
|
|
|
|
)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2018-08-08 16:37:31 +05:30
|
|
|
currency = filters.presentation_currency or frappe.get_cached_value(
|
|
|
|
"Company", filters.company, "default_currency"
|
|
|
|
)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2017-10-17 12:03:02 +05:30
|
|
|
asset = get_data(
|
|
|
|
filters.company,
|
|
|
|
"Asset",
|
|
|
|
"Debit",
|
|
|
|
period_list,
|
2017-04-25 16:26:33 +05:30
|
|
|
only_current_fiscal_year=False,
|
|
|
|
filters=filters,
|
2017-05-04 12:12:29 +05:30
|
|
|
accumulated_values=filters.accumulated_values,
|
|
|
|
)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2017-10-17 12:03:02 +05:30
|
|
|
liability = get_data(
|
|
|
|
filters.company,
|
|
|
|
"Liability",
|
|
|
|
"Credit",
|
|
|
|
period_list,
|
2017-04-25 16:26:33 +05:30
|
|
|
only_current_fiscal_year=False,
|
|
|
|
filters=filters,
|
2017-05-04 12:12:29 +05:30
|
|
|
accumulated_values=filters.accumulated_values,
|
|
|
|
)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2017-10-17 12:03:02 +05:30
|
|
|
equity = get_data(
|
|
|
|
filters.company,
|
|
|
|
"Equity",
|
|
|
|
"Credit",
|
|
|
|
period_list,
|
2017-04-25 16:26:33 +05:30
|
|
|
only_current_fiscal_year=False,
|
|
|
|
filters=filters,
|
2017-05-04 12:12:29 +05:30
|
|
|
accumulated_values=filters.accumulated_values,
|
|
|
|
)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2016-11-21 18:53:05 +05:30
|
|
|
provisional_profit_loss, total_credit = get_provisional_profit_loss(
|
2018-07-20 15:11:55 +05:30
|
|
|
asset, liability, equity, period_list, filters.company, currency
|
|
|
|
)
|
2016-03-23 18:28:50 +05:30
|
|
|
|
2016-11-21 18:53:05 +05:30
|
|
|
message, opening_balance = check_opening_balance(asset, liability, equity)
|
2014-07-21 16:13:06 +05:30
|
|
|
|
2014-07-14 19:06:52 +05:30
|
|
|
data = []
|
2014-07-21 16:13:06 +05:30
|
|
|
data.extend(asset or [])
|
2016-03-23 18:28:50 +05:30
|
|
|
data.extend(liability or [])
|
2014-07-21 16:13:06 +05:30
|
|
|
data.extend(equity or [])
|
2016-09-12 14:37:58 +08:00
|
|
|
if opening_balance and round(opening_balance, 2) != 0:
|
|
|
|
unclosed = {
|
|
|
|
"account_name": "'" + _("Unclosed Fiscal Years Profit / Loss (Credit)") + "'",
|
2017-03-28 01:49:11 -03:00
|
|
|
"account": "'" + _("Unclosed Fiscal Years Profit / Loss (Credit)") + "'",
|
2016-09-12 14:37:58 +08:00
|
|
|
"warn_if_negative": True,
|
2018-07-20 15:11:55 +05:30
|
|
|
"currency": currency,
|
2016-09-12 14:37:58 +08:00
|
|
|
}
|
|
|
|
for period in period_list:
|
|
|
|
unclosed[period.key] = opening_balance
|
2016-11-21 18:53:05 +05:30
|
|
|
if provisional_profit_loss:
|
|
|
|
provisional_profit_loss[period.key] = provisional_profit_loss[period.key] - opening_balance
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2016-09-12 14:37:58 +08:00
|
|
|
unclosed["total"] = opening_balance
|
|
|
|
data.append(unclosed)
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2014-07-21 16:13:06 +05:30
|
|
|
if provisional_profit_loss:
|
|
|
|
data.append(provisional_profit_loss)
|
2016-10-26 13:05:08 +05:30
|
|
|
if total_credit:
|
2017-10-17 12:03:02 +05:30
|
|
|
data.append(total_credit)
|
2014-07-17 19:12:28 +05:30
|
|
|
|
2017-03-31 02:18:54 -03:00
|
|
|
columns = get_columns(
|
|
|
|
filters.periodicity, period_list, filters.accumulated_values, company=filters.company
|
|
|
|
)
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2017-03-31 02:18:54 -03:00
|
|
|
chart = get_chart_data(filters, columns, asset, liability, equity)
|
2014-07-17 19:12:28 +05:30
|
|
|
|
2020-04-06 14:07:20 +05:30
|
|
|
report_summary = get_report_summary(
|
|
|
|
period_list, asset, liability, equity, provisional_profit_loss, total_credit, currency, filters
|
|
|
|
)
|
|
|
|
|
|
|
|
return columns, data, message, chart, report_summary
|
2014-07-17 19:12:28 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2018-07-20 15:11:55 +05:30
|
|
|
def get_provisional_profit_loss(
|
|
|
|
asset, liability, equity, period_list, company, currency=None, consolidated=False
|
|
|
|
):
|
2016-11-21 18:53:05 +05:30
|
|
|
provisional_profit_loss = {}
|
|
|
|
total_row = {}
|
2014-07-21 16:13:06 +05:30
|
|
|
if asset and (liability or equity):
|
2016-09-12 17:43:14 +05:30
|
|
|
total = total_row_total = 0
|
2018-08-08 16:37:31 +05:30
|
|
|
currency = currency or frappe.get_cached_value("Company", company, "default_currency")
|
2016-09-12 14:37:58 +08:00
|
|
|
total_row = {
|
|
|
|
"account_name": "'" + _("Total (Credit)") + "'",
|
2017-03-28 01:49:11 -03:00
|
|
|
"account": "'" + _("Total (Credit)") + "'",
|
2016-09-12 14:37:58 +08:00
|
|
|
"warn_if_negative": True,
|
|
|
|
"currency": currency,
|
2014-07-21 16:13:06 +05:30
|
|
|
}
|
|
|
|
has_value = False
|
|
|
|
|
|
|
|
for period in period_list:
|
2018-04-18 10:52:07 +05:30
|
|
|
key = period if consolidated else period.key
|
2014-07-21 16:13:06 +05:30
|
|
|
effective_liability = 0.0
|
|
|
|
if liability:
|
2018-04-18 10:52:07 +05:30
|
|
|
effective_liability += flt(liability[-2].get(key))
|
2014-07-21 16:13:06 +05:30
|
|
|
if equity:
|
2018-04-18 10:52:07 +05:30
|
|
|
effective_liability += flt(equity[-2].get(key))
|
2014-07-21 16:13:06 +05:30
|
|
|
|
2018-04-18 10:52:07 +05:30
|
|
|
provisional_profit_loss[key] = flt(asset[-2].get(key)) - effective_liability
|
|
|
|
total_row[key] = effective_liability + provisional_profit_loss[key]
|
2014-07-21 16:13:06 +05:30
|
|
|
|
2018-04-18 10:52:07 +05:30
|
|
|
if provisional_profit_loss[key]:
|
2014-07-21 16:13:06 +05:30
|
|
|
has_value = True
|
2016-03-23 18:28:50 +05:30
|
|
|
|
2018-04-18 10:52:07 +05:30
|
|
|
total += flt(provisional_profit_loss[key])
|
2016-02-02 19:05:30 +05:30
|
|
|
provisional_profit_loss["total"] = total
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2018-04-18 10:52:07 +05:30
|
|
|
total_row_total += flt(total_row[key])
|
2016-09-12 17:43:14 +05:30
|
|
|
total_row["total"] = total_row_total
|
2014-07-17 19:12:28 +05:30
|
|
|
|
2014-07-21 16:13:06 +05:30
|
|
|
if has_value:
|
2016-11-21 18:53:05 +05:30
|
|
|
provisional_profit_loss.update(
|
|
|
|
{
|
|
|
|
"account_name": "'" + _("Provisional Profit / Loss (Credit)") + "'",
|
2017-03-28 01:49:11 -03:00
|
|
|
"account": "'" + _("Provisional Profit / Loss (Credit)") + "'",
|
2016-11-21 18:53:05 +05:30
|
|
|
"warn_if_negative": True,
|
|
|
|
"currency": currency,
|
|
|
|
}
|
|
|
|
)
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2016-11-21 18:53:05 +05:30
|
|
|
return provisional_profit_loss, total_row
|
2016-03-21 12:46:15 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2016-03-21 12:46:15 +05:30
|
|
|
def check_opening_balance(asset, liability, equity):
|
|
|
|
# Check if previous year balance sheet closed
|
2016-05-31 19:11:48 +05:30
|
|
|
opening_balance = 0
|
2016-11-16 11:18:49 +05:30
|
|
|
float_precision = cint(frappe.db.get_default("float_precision")) or 2
|
2016-05-31 19:11:48 +05:30
|
|
|
if asset:
|
2022-02-02 22:35:36 +05:30
|
|
|
opening_balance = flt(asset[-1].get("opening_balance", 0), float_precision)
|
2016-03-21 12:46:15 +05:30
|
|
|
if liability:
|
2022-02-02 22:35:36 +05:30
|
|
|
opening_balance -= flt(liability[-1].get("opening_balance", 0), float_precision)
|
2016-03-21 12:46:15 +05:30
|
|
|
if equity:
|
2022-02-02 22:35:36 +05:30
|
|
|
opening_balance -= flt(equity[-1].get("opening_balance", 0), float_precision)
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2017-05-04 12:12:29 +05:30
|
|
|
opening_balance = flt(opening_balance, float_precision)
|
2016-03-21 12:46:15 +05:30
|
|
|
if opening_balance:
|
2016-09-12 14:37:58 +08:00
|
|
|
return _("Previous Financial Year is not closed"), opening_balance
|
|
|
|
return None, None
|
2022-03-28 18:52:46 +05:30
|
|
|
|
|
|
|
|
2020-04-06 14:07:20 +05:30
|
|
|
def get_report_summary(
|
|
|
|
period_list,
|
|
|
|
asset,
|
|
|
|
liability,
|
|
|
|
equity,
|
|
|
|
provisional_profit_loss,
|
|
|
|
total_credit,
|
|
|
|
currency,
|
|
|
|
filters,
|
|
|
|
consolidated=False,
|
|
|
|
):
|
|
|
|
|
|
|
|
net_asset, net_liability, net_equity, net_provisional_profit_loss = 0.0, 0.0, 0.0, 0.0
|
|
|
|
|
|
|
|
if filters.get("accumulated_values"):
|
|
|
|
period_list = [period_list[-1]]
|
|
|
|
|
2021-05-07 12:16:44 +05:30
|
|
|
# from consolidated financial statement
|
|
|
|
if filters.get("accumulated_in_group_company"):
|
2021-05-13 14:59:28 +05:30
|
|
|
period_list = get_filtered_list_for_consolidated_report(filters, period_list)
|
2021-05-07 12:16:44 +05:30
|
|
|
|
2020-04-06 14:07:20 +05:30
|
|
|
for period in period_list:
|
|
|
|
key = period if consolidated else period.key
|
|
|
|
if asset:
|
|
|
|
net_asset += asset[-2].get(key)
|
|
|
|
if liability:
|
|
|
|
net_liability += liability[-2].get(key)
|
|
|
|
if equity:
|
|
|
|
net_equity += equity[-2].get(key)
|
|
|
|
if provisional_profit_loss:
|
|
|
|
net_provisional_profit_loss += provisional_profit_loss.get(key)
|
|
|
|
|
|
|
|
return [
|
2022-04-02 16:56:59 +05:30
|
|
|
{"value": net_asset, "label": _("Total Asset"), "datatype": "Currency", "currency": currency},
|
2020-04-06 14:07:20 +05:30
|
|
|
{
|
|
|
|
"value": net_liability,
|
2022-04-02 16:56:59 +05:30
|
|
|
"label": _("Total Liability"),
|
2020-04-06 14:07:20 +05:30
|
|
|
"datatype": "Currency",
|
|
|
|
"currency": currency,
|
|
|
|
},
|
2022-04-02 16:56:59 +05:30
|
|
|
{"value": net_equity, "label": _("Total Equity"), "datatype": "Currency", "currency": currency},
|
2020-04-06 14:07:20 +05:30
|
|
|
{
|
|
|
|
"value": net_provisional_profit_loss,
|
2022-04-02 16:56:59 +05:30
|
|
|
"label": _("Provisional Profit / Loss (Credit)"),
|
2020-04-06 14:07:20 +05:30
|
|
|
"indicator": "Green" if net_provisional_profit_loss > 0 else "Red",
|
|
|
|
"datatype": "Currency",
|
|
|
|
"currency": currency,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2017-03-31 02:18:54 -03:00
|
|
|
def get_chart_data(filters, columns, asset, liability, equity):
|
2017-10-17 12:03:02 +05:30
|
|
|
labels = [d.get("label") for d in columns[2:]]
|
|
|
|
|
2016-05-27 12:21:28 +05:30
|
|
|
asset_data, liability_data, equity_data = [], [], []
|
2017-10-17 12:03:02 +05:30
|
|
|
|
2016-05-20 11:44:08 +05:30
|
|
|
for p in columns[2:]:
|
2016-05-27 12:21:28 +05:30
|
|
|
if asset:
|
|
|
|
asset_data.append(asset[-2].get(p.get("fieldname")))
|
|
|
|
if liability:
|
|
|
|
liability_data.append(liability[-2].get(p.get("fieldname")))
|
|
|
|
if equity:
|
|
|
|
equity_data.append(equity[-2].get(p.get("fieldname")))
|
2017-10-17 12:03:02 +05:30
|
|
|
|
|
|
|
datasets = []
|
2016-05-27 12:21:28 +05:30
|
|
|
if asset_data:
|
2019-08-23 16:32:58 +05:30
|
|
|
datasets.append({"name": _("Assets"), "values": asset_data})
|
2016-05-27 12:21:28 +05:30
|
|
|
if liability_data:
|
2019-08-23 16:32:58 +05:30
|
|
|
datasets.append({"name": _("Liabilities"), "values": liability_data})
|
2016-05-27 12:21:28 +05:30
|
|
|
if equity_data:
|
2019-08-23 16:32:58 +05:30
|
|
|
datasets.append({"name": _("Equity"), "values": equity_data})
|
2016-05-20 11:44:08 +05:30
|
|
|
|
2017-10-17 12:03:02 +05:30
|
|
|
chart = {"data": {"labels": labels, "datasets": datasets}}
|
2017-03-31 02:18:54 -03:00
|
|
|
|
|
|
|
if not filters.accumulated_values:
|
2017-10-17 12:03:02 +05:30
|
|
|
chart["type"] = "bar"
|
|
|
|
else:
|
|
|
|
chart["type"] = "line"
|
2017-03-31 02:18:54 -03:00
|
|
|
|
|
|
|
return chart
|