fix: root type in account map for balance sheet (#36303)

* fix: root type in account map

* fix: fetch gle by root type in consolidated financial statement

* refactor: consolidated financial statement gle query

* fix: filter accounts by root type
This commit is contained in:
Gursheen Kaur Anand 2023-07-31 23:27:16 +05:30 committed by GitHub
parent f31d07554d
commit 11bd15e580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 29 deletions

View File

@ -6,6 +6,7 @@ from collections import defaultdict
import frappe
from frappe import _
from frappe.query_builder import Criterion
from frappe.utils import flt, getdate
import erpnext
@ -359,6 +360,7 @@ def get_data(
accounts_by_name,
accounts,
ignore_closing_entries=False,
root_type=root_type,
)
calculate_values(accounts_by_name, gl_entries_by_account, companies, filters, fiscal_year)
@ -603,6 +605,7 @@ def set_gl_entries_by_account(
accounts_by_name,
accounts,
ignore_closing_entries=False,
root_type=None,
):
"""Returns a dict like { "account": [gl entries], ... }"""
@ -610,7 +613,6 @@ def set_gl_entries_by_account(
"Company", filters.get("company"), ["lft", "rgt"]
)
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters)
companies = frappe.db.sql(
""" select name, default_currency from `tabCompany`
where lft >= %(company_lft)s and rgt <= %(company_rgt)s""",
@ -626,27 +628,42 @@ def set_gl_entries_by_account(
)
for d in companies:
gl_entries = frappe.db.sql(
"""select gl.posting_date, gl.account, gl.debit, gl.credit, gl.is_opening, gl.company,
gl.fiscal_year, gl.debit_in_account_currency, gl.credit_in_account_currency, gl.account_currency,
acc.account_name, acc.account_number
from `tabGL Entry` gl, `tabAccount` acc where acc.name = gl.account and gl.company = %(company)s and gl.is_cancelled = 0
{additional_conditions} and gl.posting_date <= %(to_date)s and acc.lft >= %(lft)s and acc.rgt <= %(rgt)s
order by gl.account, gl.posting_date""".format(
additional_conditions=additional_conditions
),
{
"from_date": from_date,
"to_date": to_date,
"lft": root_lft,
"rgt": root_rgt,
"company": d.name,
"finance_book": filters.get("finance_book"),
"company_fb": frappe.get_cached_value("Company", d.name, "default_finance_book"),
},
as_dict=True,
gle = frappe.qb.DocType("GL Entry")
account = frappe.qb.DocType("Account")
query = (
frappe.qb.from_(gle)
.inner_join(account)
.on(account.name == gle.account)
.select(
gle.posting_date,
gle.account,
gle.debit,
gle.credit,
gle.is_opening,
gle.company,
gle.fiscal_year,
gle.debit_in_account_currency,
gle.credit_in_account_currency,
gle.account_currency,
account.account_name,
account.account_number,
)
.where(
(gle.company == d.name)
& (gle.is_cancelled == 0)
& (gle.posting_date <= to_date)
& (account.lft >= root_lft)
& (account.rgt <= root_rgt)
& (account.root_type <= root_type)
)
.orderby(gle.account, gle.posting_date)
)
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters, d)
if additional_conditions:
query = query.where(Criterion.all(additional_conditions))
gl_entries = query.run(as_dict=True)
if filters and filters.get("presentation_currency") != d.default_currency:
currency_info["company"] = d.name
currency_info["company_currency"] = d.default_currency
@ -716,23 +733,25 @@ def validate_entries(key, entry, accounts_by_name, accounts):
accounts.insert(idx + 1, args)
def get_additional_conditions(from_date, ignore_closing_entries, filters):
def get_additional_conditions(from_date, ignore_closing_entries, filters, d):
gle = frappe.qb.DocType("GL Entry")
additional_conditions = []
if ignore_closing_entries:
additional_conditions.append("gl.voucher_type != 'Period Closing Voucher'")
additional_conditions.append((gle.voucher_type != "Period Closing Voucher"))
if from_date:
additional_conditions.append("gl.posting_date >= %(from_date)s")
additional_conditions.append(gle.posting_date >= from_date)
finance_book = filters.get("finance_book")
company_fb = frappe.get_cached_value("Company", d.name, "default_finance_book")
if filters.get("include_default_book_entries"):
additional_conditions.append(
"(finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)"
)
additional_conditions.append((gle.finance_book.isin([finance_book, company_fb, "", None])))
else:
additional_conditions.append("(finance_book in (%(finance_book)s, '') OR finance_book IS NULL)")
additional_conditions.append((gle.finance_book.isin([finance_book, "", None])))
return " and {}".format(" and ".join(additional_conditions)) if additional_conditions else ""
return additional_conditions
def add_total_row(out, root_type, balance_must_be, companies, company_currency):

View File

@ -188,6 +188,7 @@ def get_data(
filters,
gl_entries_by_account,
ignore_closing_entries=ignore_closing_entries,
root_type=root_type,
)
calculate_values(
@ -417,13 +418,28 @@ def set_gl_entries_by_account(
gl_entries_by_account,
ignore_closing_entries=False,
ignore_opening_entries=False,
root_type=None,
):
"""Returns a dict like { "account": [gl entries], ... }"""
gl_entries = []
account_filters = {
"company": company,
"is_group": 0,
"lft": (">=", root_lft),
"rgt": ("<=", root_rgt),
}
if root_type:
account_filters.update(
{
"root_type": root_type,
}
)
accounts_list = frappe.db.get_all(
"Account",
filters={"company": company, "is_group": 0, "lft": (">=", root_lft), "rgt": ("<=", root_rgt)},
filters=account_filters,
pluck="name",
)