2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-07-18 12:35:26 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2018-02-23 11:52:07 +00:00
|
|
|
import functools
|
2020-07-15 06:47:43 +00:00
|
|
|
import math
|
2021-09-02 11:14:59 +00:00
|
|
|
import re
|
2018-02-23 11:52:07 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
import frappe
|
2018-02-12 09:34:50 +00:00
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.utils import add_days, add_months, cint, cstr, flt, formatdate, get_first_day, getdate
|
|
|
|
|
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|
|
|
get_accounting_dimensions,
|
|
|
|
get_dimension_with_children,
|
|
|
|
)
|
|
|
|
from erpnext.accounts.report.utils import convert_to_presentation_currency, get_currency
|
|
|
|
from erpnext.accounts.utils import get_fiscal_year
|
|
|
|
|
2017-05-04 06:42:29 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_period_list(
|
|
|
|
from_fiscal_year,
|
|
|
|
to_fiscal_year,
|
|
|
|
period_start_date,
|
|
|
|
period_end_date,
|
|
|
|
filter_based_on,
|
|
|
|
periodicity,
|
|
|
|
accumulated_values=False,
|
|
|
|
company=None,
|
|
|
|
reset_period_on_fy_change=True,
|
|
|
|
ignore_fiscal_year=False,
|
|
|
|
):
|
2016-02-02 13:35:30 +00:00
|
|
|
"""Get a list of dict {"from_date": from_date, "to_date": to_date, "key": key, "label": label}
|
2022-03-28 13:22:46 +00:00
|
|
|
Periodicity can be (Yearly, Quarterly, Monthly)"""
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if filter_based_on == "Fiscal Year":
|
2020-04-07 06:46:25 +00:00
|
|
|
fiscal_year = get_fiscal_year_data(from_fiscal_year, to_fiscal_year)
|
|
|
|
validate_fiscal_year(fiscal_year, from_fiscal_year, to_fiscal_year)
|
|
|
|
year_start_date = getdate(fiscal_year.year_start_date)
|
|
|
|
year_end_date = getdate(fiscal_year.year_end_date)
|
|
|
|
else:
|
|
|
|
validate_dates(period_start_date, period_end_date)
|
|
|
|
year_start_date = getdate(period_start_date)
|
|
|
|
year_end_date = getdate(period_end_date)
|
2016-08-21 11:44:12 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
months_to_add = {"Yearly": 12, "Half-Yearly": 6, "Quarterly": 3, "Monthly": 1}[periodicity]
|
2016-08-19 06:18:58 +00:00
|
|
|
|
|
|
|
period_list = []
|
|
|
|
|
|
|
|
start_date = year_start_date
|
2016-08-21 11:44:12 +00:00
|
|
|
months = get_months(year_start_date, year_end_date)
|
2016-08-19 06:18:58 +00:00
|
|
|
|
2020-08-18 10:37:24 +00:00
|
|
|
for i in range(cint(math.ceil(months / months_to_add))):
|
2022-03-28 13:22:46 +00:00
|
|
|
period = frappe._dict({"from_date": start_date})
|
2016-08-19 06:18:58 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if i == 0 and filter_based_on == "Date Range":
|
2021-03-23 15:41:01 +00:00
|
|
|
to_date = add_months(get_first_day(start_date), months_to_add)
|
|
|
|
else:
|
|
|
|
to_date = add_months(start_date, months_to_add)
|
|
|
|
|
2016-08-19 06:18:58 +00:00
|
|
|
start_date = to_date
|
2016-09-14 07:20:36 +00:00
|
|
|
|
2020-06-13 13:46:37 +00:00
|
|
|
# Subtract one day from to_date, as it may be first day in next fiscal year or month
|
|
|
|
to_date = add_days(to_date, -1)
|
2016-08-19 06:18:58 +00:00
|
|
|
|
|
|
|
if to_date <= year_end_date:
|
|
|
|
# the normal case
|
|
|
|
period.to_date = to_date
|
|
|
|
else:
|
|
|
|
# if a fiscal year ends before a 12 month period
|
|
|
|
period.to_date = year_end_date
|
|
|
|
|
2020-05-12 05:48:32 +00:00
|
|
|
if not ignore_fiscal_year:
|
|
|
|
period.to_date_fiscal_year = get_fiscal_year(period.to_date, company=company)[0]
|
|
|
|
period.from_date_fiscal_year_start_date = get_fiscal_year(period.from_date, company=company)[1]
|
2016-08-19 06:18:58 +00:00
|
|
|
|
|
|
|
period_list.append(period)
|
|
|
|
|
|
|
|
if period.to_date == year_end_date:
|
|
|
|
break
|
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
# common processing
|
|
|
|
for opts in period_list:
|
2014-07-21 10:43:06 +00:00
|
|
|
key = opts["to_date"].strftime("%b_%Y").lower()
|
2017-03-31 05:18:54 +00:00
|
|
|
if periodicity == "Monthly" and not accumulated_values:
|
2016-02-01 01:43:45 +00:00
|
|
|
label = formatdate(opts["to_date"], "MMM YYYY")
|
|
|
|
else:
|
2017-03-31 05:18:54 +00:00
|
|
|
if not accumulated_values:
|
|
|
|
label = get_label(periodicity, opts["from_date"], opts["to_date"])
|
|
|
|
else:
|
2017-05-04 06:42:29 +00:00
|
|
|
if reset_period_on_fy_change:
|
|
|
|
label = get_label(periodicity, opts.from_date_fiscal_year_start_date, opts["to_date"])
|
|
|
|
else:
|
|
|
|
label = get_label(periodicity, period_list[0].from_date, opts["to_date"])
|
2017-03-31 05:18:54 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
opts.update(
|
|
|
|
{
|
|
|
|
"key": key.replace(" ", "_").replace("-", "_"),
|
|
|
|
"label": label,
|
|
|
|
"year_start_date": year_start_date,
|
|
|
|
"year_end_date": year_end_date,
|
|
|
|
}
|
|
|
|
)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
return period_list
|
2016-08-21 11:44:12 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-09-14 07:20:36 +00:00
|
|
|
def get_fiscal_year_data(from_fiscal_year, to_fiscal_year):
|
2022-03-28 13:22:46 +00:00
|
|
|
fiscal_year = frappe.db.sql(
|
|
|
|
"""select min(year_start_date) as year_start_date,
|
2017-06-30 11:15:11 +00:00
|
|
|
max(year_end_date) as year_end_date from `tabFiscal Year` where
|
2016-09-14 07:20:36 +00:00
|
|
|
name between %(from_fiscal_year)s and %(to_fiscal_year)s""",
|
2022-03-28 13:22:46 +00:00
|
|
|
{"from_fiscal_year": from_fiscal_year, "to_fiscal_year": to_fiscal_year},
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2016-09-14 07:20:36 +00:00
|
|
|
|
|
|
|
return fiscal_year[0] if fiscal_year else {}
|
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-09-14 07:20:36 +00:00
|
|
|
def validate_fiscal_year(fiscal_year, from_fiscal_year, to_fiscal_year):
|
2022-03-28 13:22:46 +00:00
|
|
|
if not fiscal_year.get("year_start_date") or not fiscal_year.get("year_end_date"):
|
2020-04-07 06:46:25 +00:00
|
|
|
frappe.throw(_("Start Year and End Year are mandatory"))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if getdate(fiscal_year.get("year_end_date")) < getdate(fiscal_year.get("year_start_date")):
|
2016-08-21 11:44:12 +00:00
|
|
|
frappe.throw(_("End Year cannot be before Start Year"))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-07 06:46:25 +00:00
|
|
|
def validate_dates(from_date, to_date):
|
|
|
|
if not from_date or not to_date:
|
2021-05-07 06:46:44 +00:00
|
|
|
frappe.throw(_("From Date and To Date are mandatory"))
|
2020-04-07 06:46:25 +00:00
|
|
|
|
|
|
|
if to_date < from_date:
|
2021-05-07 06:46:44 +00:00
|
|
|
frappe.throw(_("To Date cannot be less than From Date"))
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-08-19 06:18:58 +00:00
|
|
|
def get_months(start_date, end_date):
|
|
|
|
diff = (12 * end_date.year + end_date.month) - (12 * start_date.year + start_date.month)
|
|
|
|
return diff + 1
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_label(periodicity, from_date, to_date):
|
2018-02-12 09:34:50 +00:00
|
|
|
if periodicity == "Yearly":
|
2016-02-02 13:35:30 +00:00
|
|
|
if formatdate(from_date, "YYYY") == formatdate(to_date, "YYYY"):
|
|
|
|
label = formatdate(from_date, "YYYY")
|
|
|
|
else:
|
|
|
|
label = formatdate(from_date, "YYYY") + "-" + formatdate(to_date, "YYYY")
|
2016-02-01 01:43:45 +00:00
|
|
|
else:
|
2016-02-02 13:35:30 +00:00
|
|
|
label = formatdate(from_date, "MMM YY") + "-" + formatdate(to_date, "MMM YY")
|
2016-02-01 01:43:45 +00:00
|
|
|
|
|
|
|
return label
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
def get_data(
|
2022-03-28 13:22:46 +00:00
|
|
|
company,
|
|
|
|
root_type,
|
|
|
|
balance_must_be,
|
|
|
|
period_list,
|
|
|
|
filters=None,
|
|
|
|
accumulated_values=1,
|
|
|
|
only_current_fiscal_year=True,
|
|
|
|
ignore_closing_entries=False,
|
|
|
|
ignore_accumulated_values_for_fy=False,
|
|
|
|
total=True,
|
|
|
|
):
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
accounts = get_accounts(company, root_type)
|
|
|
|
if not accounts:
|
|
|
|
return None
|
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
company_currency = get_appropriate_currency(company, filters)
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-11-30 12:23:08 +00:00
|
|
|
gl_entries_by_account = {}
|
2022-03-28 13:22:46 +00:00
|
|
|
for root in frappe.db.sql(
|
|
|
|
"""select lft, rgt from tabAccount
|
|
|
|
where root_type=%s and ifnull(parent_account, '') = ''""",
|
|
|
|
root_type,
|
|
|
|
as_dict=1,
|
|
|
|
):
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
set_gl_entries_by_account(
|
|
|
|
company,
|
2016-02-02 13:35:30 +00:00
|
|
|
period_list[0]["year_start_date"] if only_current_fiscal_year else None,
|
2017-06-30 11:15:11 +00:00
|
|
|
period_list[-1]["to_date"],
|
2022-03-28 13:22:46 +00:00
|
|
|
root.lft,
|
|
|
|
root.rgt,
|
|
|
|
filters,
|
|
|
|
gl_entries_by_account,
|
|
|
|
ignore_closing_entries=ignore_closing_entries,
|
2018-02-12 09:34:50 +00:00
|
|
|
)
|
2016-02-02 13:35:30 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
calculate_values(
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts_by_name,
|
|
|
|
gl_entries_by_account,
|
|
|
|
period_list,
|
|
|
|
accumulated_values,
|
|
|
|
ignore_accumulated_values_for_fy,
|
|
|
|
)
|
2020-04-06 08:37:20 +00:00
|
|
|
accumulate_values_into_parents(accounts, accounts_by_name, period_list)
|
2016-01-12 06:26:31 +00:00
|
|
|
out = prepare_data(accounts, balance_must_be, period_list, company_currency)
|
2016-03-04 07:02:33 +00:00
|
|
|
out = filter_out_zero_value_rows(out, parent_children_map)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2019-02-11 07:42:44 +00:00
|
|
|
if out and total:
|
2016-05-20 06:14:08 +00:00
|
|
|
add_total_row(out, root_type, balance_must_be, period_list, company_currency)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
2017-09-13 09:57:30 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
def get_appropriate_currency(company, filters=None):
|
|
|
|
if filters and filters.get("presentation_currency"):
|
|
|
|
return filters["presentation_currency"]
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_cached_value("Company", company, "default_currency")
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def calculate_values(
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts_by_name,
|
|
|
|
gl_entries_by_account,
|
|
|
|
period_list,
|
|
|
|
accumulated_values,
|
|
|
|
ignore_accumulated_values_for_fy,
|
|
|
|
):
|
2021-11-04 14:18:32 +00:00
|
|
|
for entries in gl_entries_by_account.values():
|
2015-06-14 12:19:29 +00:00
|
|
|
for entry in entries:
|
|
|
|
d = accounts_by_name.get(entry.account)
|
2017-09-13 09:57:30 +00:00
|
|
|
if not d:
|
|
|
|
frappe.msgprint(
|
2022-03-28 13:22:46 +00:00
|
|
|
_("Could not retrieve information for {0}.").format(entry.account),
|
|
|
|
title="Error",
|
|
|
|
raise_exception=1,
|
2017-09-13 09:57:30 +00:00
|
|
|
)
|
2015-06-14 12:19:29 +00:00
|
|
|
for period in period_list:
|
|
|
|
# check if posting date is within the period
|
2016-08-19 06:18:58 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if entry.posting_date <= period.to_date:
|
2022-03-28 13:22:46 +00:00
|
|
|
if (accumulated_values or entry.posting_date >= period.from_date) and (
|
|
|
|
not ignore_accumulated_values_for_fy or entry.fiscal_year == period.to_date_fiscal_year
|
|
|
|
):
|
2016-02-02 13:35:30 +00:00
|
|
|
d[period.key] = d.get(period.key, 0.0) + flt(entry.debit) - flt(entry.credit)
|
2016-08-19 06:18:58 +00:00
|
|
|
|
2016-03-21 07:16:15 +00:00
|
|
|
if entry.posting_date < period_list[0].year_start_date:
|
|
|
|
d["opening_balance"] = d.get("opening_balance", 0.0) + flt(entry.debit) - flt(entry.credit)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2020-04-06 08:37:20 +00:00
|
|
|
def accumulate_values_into_parents(accounts, accounts_by_name, period_list):
|
2014-07-18 12:35:26 +00:00
|
|
|
"""accumulate children's values in parent accounts"""
|
|
|
|
for d in reversed(accounts):
|
|
|
|
if d.parent_account:
|
|
|
|
for period in period_list:
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts_by_name[d.parent_account][period.key] = accounts_by_name[d.parent_account].get(
|
|
|
|
period.key, 0.0
|
|
|
|
) + d.get(period.key, 0.0)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts_by_name[d.parent_account]["opening_balance"] = accounts_by_name[d.parent_account].get(
|
|
|
|
"opening_balance", 0.0
|
|
|
|
) + d.get("opening_balance", 0.0)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-01-12 06:26:31 +00:00
|
|
|
def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
2016-03-04 07:02:33 +00:00
|
|
|
data = []
|
2014-07-21 10:43:06 +00:00
|
|
|
year_start_date = period_list[0]["year_start_date"].strftime("%Y-%m-%d")
|
|
|
|
year_end_date = period_list[-1]["year_end_date"].strftime("%Y-%m-%d")
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
for d in accounts:
|
|
|
|
# add to output
|
|
|
|
has_value = False
|
2016-02-02 13:35:30 +00:00
|
|
|
total = 0
|
2022-03-28 13:22:46 +00:00
|
|
|
row = frappe._dict(
|
|
|
|
{
|
|
|
|
"account": _(d.name),
|
|
|
|
"parent_account": _(d.parent_account) if d.parent_account else "",
|
|
|
|
"indent": flt(d.indent),
|
|
|
|
"year_start_date": year_start_date,
|
|
|
|
"year_end_date": year_end_date,
|
|
|
|
"currency": company_currency,
|
|
|
|
"include_in_gross": d.include_in_gross,
|
|
|
|
"account_type": d.account_type,
|
|
|
|
"is_group": d.is_group,
|
|
|
|
"opening_balance": d.get("opening_balance", 0.0) * (1 if balance_must_be == "Debit" else -1),
|
|
|
|
"account_name": (
|
|
|
|
"%s - %s" % (_(d.account_number), _(d.account_name))
|
|
|
|
if d.account_number
|
|
|
|
else _(d.account_name)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2014-07-18 12:35:26 +00:00
|
|
|
for period in period_list:
|
2018-02-12 09:34:50 +00:00
|
|
|
if d.get(period.key) and balance_must_be == "Credit":
|
2014-07-18 12:35:26 +00:00
|
|
|
# change sign based on Debit or Credit, since calculation is done using (debit - credit)
|
2016-03-21 07:16:15 +00:00
|
|
|
d[period.key] *= -1
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
row[period.key] = flt(d.get(period.key, 0.0), 3)
|
|
|
|
|
2014-07-18 12:50:44 +00:00
|
|
|
if abs(row[period.key]) >= 0.005:
|
|
|
|
# ignore zero values
|
|
|
|
has_value = True
|
2016-02-02 13:35:30 +00:00
|
|
|
total += flt(row[period.key])
|
2014-07-18 12:50:44 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
row["has_value"] = has_value
|
|
|
|
row["total"] = total
|
|
|
|
data.append(row)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
return data
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-03-10 06:31:01 +00:00
|
|
|
def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False):
|
2016-03-04 07:02:33 +00:00
|
|
|
data_with_value = []
|
|
|
|
for d in data:
|
2016-03-10 06:31:01 +00:00
|
|
|
if show_zero_values or d.get("has_value"):
|
2016-03-04 07:02:33 +00:00
|
|
|
data_with_value.append(d)
|
|
|
|
else:
|
2016-03-10 06:31:01 +00:00
|
|
|
# show group with zero balance, if there are balances against child
|
|
|
|
children = [child.name for child in parent_children_map.get(d.get("account")) or []]
|
|
|
|
if children:
|
|
|
|
for row in data:
|
|
|
|
if row.get("account") in children and row.get("has_value"):
|
|
|
|
data_with_value.append(d)
|
|
|
|
break
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
return data_with_value
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2016-05-20 06:14:08 +00:00
|
|
|
def add_total_row(out, root_type, balance_must_be, period_list, company_currency):
|
2015-11-30 12:23:08 +00:00
|
|
|
total_row = {
|
2019-12-12 07:36:17 +00:00
|
|
|
"account_name": _("Total {0} ({1})").format(_(root_type), _(balance_must_be)),
|
|
|
|
"account": _("Total {0} ({1})").format(_(root_type), _(balance_must_be)),
|
2022-02-02 17:05:36 +00:00
|
|
|
"currency": company_currency,
|
2022-03-28 13:22:46 +00:00
|
|
|
"opening_balance": 0.0,
|
2014-07-18 12:35:26 +00:00
|
|
|
}
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-11-30 12:23:08 +00:00
|
|
|
for row in out:
|
|
|
|
if not row.get("parent_account"):
|
|
|
|
for period in period_list:
|
|
|
|
total_row.setdefault(period.key, 0.0)
|
|
|
|
total_row[period.key] += row.get(period.key, 0.0)
|
2019-02-21 11:48:28 +00:00
|
|
|
row[period.key] = row.get(period.key, 0.0)
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
total_row.setdefault("total", 0.0)
|
|
|
|
total_row["total"] += flt(row["total"])
|
2022-02-02 17:05:36 +00:00
|
|
|
total_row["opening_balance"] += row["opening_balance"]
|
2016-02-02 13:35:30 +00:00
|
|
|
row["total"] = ""
|
2017-06-30 11:15:11 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
if "total" in total_row:
|
2016-03-04 07:02:33 +00:00
|
|
|
out.append(total_row)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
# blank row after Total
|
|
|
|
out.append({})
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
def get_accounts(company, root_type):
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""
|
2019-02-11 07:42:44 +00:00
|
|
|
select name, account_number, parent_account, lft, rgt, root_type, report_type, account_name, include_in_gross, account_type, is_group, lft, rgt
|
2018-07-31 07:06:16 +00:00
|
|
|
from `tabAccount`
|
2022-03-28 13:22:46 +00:00
|
|
|
where company=%s and root_type=%s order by lft""",
|
|
|
|
(company, root_type),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2020-10-24 16:30:32 +00:00
|
|
|
def filter_accounts(accounts, depth=20):
|
2014-07-18 12:35:26 +00:00
|
|
|
parent_children_map = {}
|
|
|
|
accounts_by_name = {}
|
|
|
|
for d in accounts:
|
|
|
|
accounts_by_name[d.name] = d
|
|
|
|
parent_children_map.setdefault(d.parent_account or None, []).append(d)
|
|
|
|
|
|
|
|
filtered_accounts = []
|
2015-04-29 06:55:18 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
def add_to_list(parent, level):
|
|
|
|
if level < depth:
|
2015-04-29 07:37:36 +00:00
|
|
|
children = parent_children_map.get(parent) or []
|
2022-03-28 13:22:46 +00:00
|
|
|
sort_accounts(children, is_root=True if parent == None else False)
|
2015-04-29 06:55:18 +00:00
|
|
|
|
2015-04-29 07:37:36 +00:00
|
|
|
for child in children:
|
2014-07-18 12:35:26 +00:00
|
|
|
child.indent = level
|
|
|
|
filtered_accounts.append(child)
|
|
|
|
add_to_list(child.name, level + 1)
|
|
|
|
|
|
|
|
add_to_list(None, 0)
|
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
return filtered_accounts, accounts_by_name, parent_children_map
|
2015-05-20 11:51:53 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2018-04-02 04:44:32 +00:00
|
|
|
def sort_accounts(accounts, is_root=False, key="name"):
|
2015-04-29 07:37:36 +00:00
|
|
|
"""Sort root types as Asset, Liability, Equity, Income, Expense"""
|
|
|
|
|
2018-04-02 04:44:32 +00:00
|
|
|
def compare_accounts(a, b):
|
2022-03-28 13:22:46 +00:00
|
|
|
if re.split(r"\W+", a[key])[0].isdigit():
|
2019-07-03 05:04:31 +00:00
|
|
|
# if chart of accounts is numbered, then sort by number
|
2021-11-04 13:19:06 +00:00
|
|
|
return int(a[key] > b[key]) - int(a[key] < b[key])
|
2019-07-03 05:04:31 +00:00
|
|
|
elif is_root:
|
2018-04-02 04:44:32 +00:00
|
|
|
if a.report_type != b.report_type and a.report_type == "Balance Sheet":
|
|
|
|
return -1
|
|
|
|
if a.root_type != b.root_type and a.root_type == "Asset":
|
|
|
|
return -1
|
|
|
|
if a.root_type == "Liability" and b.root_type == "Equity":
|
|
|
|
return -1
|
|
|
|
if a.root_type == "Income" and b.root_type == "Expense":
|
|
|
|
return -1
|
|
|
|
else:
|
2019-05-02 03:52:48 +00:00
|
|
|
# sort by key (number) or name
|
2021-11-04 13:19:06 +00:00
|
|
|
return int(a[key] > b[key]) - int(a[key] < b[key])
|
2015-04-29 07:37:36 +00:00
|
|
|
return 1
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
accounts.sort(key=functools.cmp_to_key(compare_accounts))
|
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
def set_gl_entries_by_account(
|
2022-03-28 13:22:46 +00:00
|
|
|
company,
|
|
|
|
from_date,
|
|
|
|
to_date,
|
|
|
|
root_lft,
|
|
|
|
root_rgt,
|
|
|
|
filters,
|
|
|
|
gl_entries_by_account,
|
|
|
|
ignore_closing_entries=False,
|
|
|
|
):
|
2014-07-18 12:35:26 +00:00
|
|
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
2023-03-10 07:32:01 +00:00
|
|
|
gl_entries = []
|
2020-02-03 10:19:24 +00:00
|
|
|
|
2023-03-19 14:16:01 +00:00
|
|
|
accounts_list = frappe.db.get_all(
|
|
|
|
"Account",
|
|
|
|
filters={"company": company, "is_group": 0, "lft": (">=", root_lft), "rgt": ("<=", root_rgt)},
|
|
|
|
pluck="name",
|
|
|
|
)
|
2020-02-05 06:42:47 +00:00
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
if accounts_list:
|
|
|
|
# For balance sheet
|
|
|
|
if not from_date:
|
|
|
|
from_date = filters["period_start_date"]
|
|
|
|
last_period_closing_voucher = frappe.db.get_all(
|
|
|
|
"Period Closing Voucher",
|
|
|
|
filters={"docstatus": 1, "company": filters.company, "posting_date": ("<", from_date)},
|
|
|
|
fields=["posting_date", "name"],
|
|
|
|
order_by="posting_date desc",
|
|
|
|
limit=1,
|
|
|
|
)
|
|
|
|
if last_period_closing_voucher:
|
|
|
|
gl_entries += get_accounting_entries(
|
|
|
|
"Account Closing Balance",
|
|
|
|
from_date,
|
|
|
|
to_date,
|
|
|
|
accounts_list,
|
|
|
|
filters,
|
|
|
|
ignore_closing_entries,
|
|
|
|
last_period_closing_voucher[0].name,
|
|
|
|
)
|
|
|
|
from_date = add_days(last_period_closing_voucher[0].posting_date, 1)
|
|
|
|
|
|
|
|
gl_entries += get_accounting_entries(
|
|
|
|
"GL Entry", from_date, to_date, accounts_list, filters, ignore_closing_entries
|
2022-01-31 11:54:50 +00:00
|
|
|
)
|
2020-02-05 06:42:47 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if filters and filters.get("presentation_currency"):
|
|
|
|
convert_to_presentation_currency(gl_entries, get_currency(filters), filters.get("company"))
|
2020-02-05 06:42:47 +00:00
|
|
|
|
|
|
|
for entry in gl_entries:
|
|
|
|
gl_entries_by_account.setdefault(entry.account, []).append(entry)
|
|
|
|
|
|
|
|
return gl_entries_by_account
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
def get_accounting_entries(
|
|
|
|
doctype,
|
|
|
|
from_date,
|
|
|
|
to_date,
|
|
|
|
accounts,
|
|
|
|
filters,
|
|
|
|
ignore_closing_entries,
|
|
|
|
period_closing_voucher=None,
|
|
|
|
):
|
|
|
|
gl_entry = frappe.qb.DocType(doctype)
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(gl_entry)
|
|
|
|
.select(
|
|
|
|
gl_entry.account,
|
|
|
|
gl_entry.debit,
|
|
|
|
gl_entry.credit,
|
|
|
|
gl_entry.debit_in_account_currency,
|
|
|
|
gl_entry.credit_in_account_currency,
|
|
|
|
gl_entry.account_currency,
|
|
|
|
)
|
|
|
|
.where(gl_entry.company == filters.company)
|
|
|
|
)
|
2016-08-24 13:42:14 +00:00
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
if doctype == "GL Entry":
|
2023-03-19 07:16:42 +00:00
|
|
|
query = query.select(gl_entry.posting_date, gl_entry.is_opening, gl_entry.fiscal_year)
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(gl_entry.is_cancelled == 0)
|
|
|
|
query = query.where(gl_entry.posting_date <= to_date)
|
|
|
|
else:
|
|
|
|
query = query.select(gl_entry.closing_date.as_("posting_date"))
|
|
|
|
query = query.where(gl_entry.period_closing_voucher == period_closing_voucher)
|
|
|
|
|
|
|
|
query = apply_additional_conditions(doctype, query, from_date, ignore_closing_entries, filters)
|
2023-03-19 14:16:01 +00:00
|
|
|
query = query.where(gl_entry.account.isin(accounts))
|
2023-03-10 07:32:01 +00:00
|
|
|
|
|
|
|
entries = query.run(as_dict=True)
|
|
|
|
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
|
|
def apply_additional_conditions(doctype, query, from_date, ignore_closing_entries, filters):
|
|
|
|
gl_entry = frappe.qb.DocType(doctype)
|
2020-03-17 05:23:24 +00:00
|
|
|
accounting_dimensions = get_accounting_dimensions(as_list=False)
|
2019-05-12 13:04:23 +00:00
|
|
|
|
2016-08-24 13:42:14 +00:00
|
|
|
if ignore_closing_entries:
|
2023-03-10 07:32:01 +00:00
|
|
|
if doctype == "GL Entry":
|
|
|
|
query = query.where(gl_entry.voucher_type != "Period Closing Voucher")
|
|
|
|
else:
|
|
|
|
query = query.where(gl_entry.is_period_closing_voucher_entry == 0)
|
2016-08-24 13:42:14 +00:00
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
if from_date and doctype == "GL Entry":
|
|
|
|
query = query.where(gl_entry.posting_date >= from_date)
|
2016-08-24 13:42:14 +00:00
|
|
|
|
|
|
|
if filters:
|
2016-11-24 11:02:22 +00:00
|
|
|
if filters.get("project"):
|
2018-08-27 06:38:53 +00:00
|
|
|
if not isinstance(filters.get("project"), list):
|
2019-06-19 06:45:37 +00:00
|
|
|
filters.project = frappe.parse_json(filters.get("project"))
|
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(gl_entry.project.isin(filters.project))
|
2018-08-27 06:38:53 +00:00
|
|
|
|
2016-11-24 11:02:22 +00:00
|
|
|
if filters.get("cost_center"):
|
2018-09-07 07:47:11 +00:00
|
|
|
filters.cost_center = get_cost_centers_with_children(filters.cost_center)
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(gl_entry.cost_center.isin(filters.cost_center))
|
2018-05-18 03:40:07 +00:00
|
|
|
|
2020-01-31 03:38:29 +00:00
|
|
|
if filters.get("include_default_book_entries"):
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(
|
|
|
|
(gl_entry.finance_book.isin([cstr(filters.finance_book), cstr(filters.company_fb), ""]))
|
|
|
|
| (gl_entry.finance_book.isnull())
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-01-30 10:03:51 +00:00
|
|
|
else:
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(
|
|
|
|
(gl_entry.finance_book.isin([cstr(filters.company_fb), ""])) | (gl_entry.finance_book.isnull())
|
|
|
|
)
|
2020-03-17 05:23:24 +00:00
|
|
|
|
2019-05-12 13:04:23 +00:00
|
|
|
if accounting_dimensions:
|
|
|
|
for dimension in accounting_dimensions:
|
2020-03-17 05:23:24 +00:00
|
|
|
if filters.get(dimension.fieldname):
|
2022-03-28 13:22:46 +00:00
|
|
|
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)
|
|
|
|
)
|
2019-05-12 13:04:23 +00:00
|
|
|
|
2023-03-10 07:32:01 +00:00
|
|
|
query = query.where(gl_entry[dimension.fieldname].isin(filters[dimension.fieldname]))
|
|
|
|
|
|
|
|
return query
|
2016-08-24 13:42:14 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-09-07 07:47:11 +00:00
|
|
|
def get_cost_centers_with_children(cost_centers):
|
|
|
|
if not isinstance(cost_centers, list):
|
2022-03-28 13:22:46 +00:00
|
|
|
cost_centers = [d.strip() for d in cost_centers.strip().split(",") if d]
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2018-09-07 07:47:11 +00:00
|
|
|
all_cost_centers = []
|
|
|
|
for d in cost_centers:
|
2019-08-19 04:54:44 +00:00
|
|
|
if frappe.db.exists("Cost Center", d):
|
|
|
|
lft, rgt = frappe.db.get_value("Cost Center", d, ["lft", "rgt"])
|
|
|
|
children = frappe.get_all("Cost Center", filters={"lft": [">=", lft], "rgt": ["<=", rgt]})
|
|
|
|
all_cost_centers += [c.name for c in children]
|
|
|
|
else:
|
2020-01-29 09:36:18 +00:00
|
|
|
frappe.throw(_("Cost Center: {0} does not exist").format(d))
|
2018-08-27 06:38:53 +00:00
|
|
|
|
2018-09-07 07:47:11 +00:00
|
|
|
return list(set(all_cost_centers))
|
2016-11-24 11:02:22 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_columns(periodicity, period_list, accumulated_values=1, company=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
columns = [
|
|
|
|
{
|
|
|
|
"fieldname": "account",
|
|
|
|
"label": _("Account"),
|
2016-02-02 13:35:30 +00:00
|
|
|
"fieldtype": "Link",
|
2022-03-28 13:22:46 +00:00
|
|
|
"options": "Account",
|
|
|
|
"width": 300,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
if company:
|
|
|
|
columns.append(
|
|
|
|
{
|
|
|
|
"fieldname": "currency",
|
|
|
|
"label": _("Currency"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Currency",
|
|
|
|
"hidden": 1,
|
|
|
|
}
|
|
|
|
)
|
2014-07-18 12:35:26 +00:00
|
|
|
for period in period_list:
|
2022-03-28 13:22:46 +00:00
|
|
|
columns.append(
|
|
|
|
{
|
|
|
|
"fieldname": period.key,
|
|
|
|
"label": period.label,
|
2016-02-01 01:43:45 +00:00
|
|
|
"fieldtype": "Currency",
|
2022-03-28 13:22:46 +00:00
|
|
|
"options": "currency",
|
|
|
|
"width": 150,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if periodicity != "Yearly":
|
|
|
|
if not accumulated_values:
|
|
|
|
columns.append(
|
|
|
|
{"fieldname": "total", "label": _("Total"), "fieldtype": "Currency", "width": 150}
|
|
|
|
)
|
2016-02-01 01:43:45 +00:00
|
|
|
|
2021-05-07 06:46:44 +00:00
|
|
|
return columns
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-07 06:46:44 +00:00
|
|
|
def get_filtered_list_for_consolidated_report(filters, period_list):
|
|
|
|
filtered_summary_list = []
|
|
|
|
for period in period_list:
|
2022-03-28 13:22:46 +00:00
|
|
|
if period == filters.get("company"):
|
2021-05-07 06:46:44 +00:00
|
|
|
filtered_summary_list.append(period)
|
|
|
|
|
|
|
|
return filtered_summary_list
|