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