Skip to content

Commit

Permalink
fix: root type in account map for balance sheet (#36303)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit 11bd15e)

# Conflicts:
#	erpnext/accounts/report/consolidated_financial_statement/consolidated_financial_statement.py

* chore: Resolve conflicts

---------

Co-authored-by: Gursheen Kaur Anand <40693548+GursheenK@users.noreply.github.com>
Co-authored-by: Deepesh Garg <deepeshgarg6@gmail.com>
  • Loading branch information
3 people authored Aug 1, 2023
1 parent 99e7406 commit 5b56296
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import frappe
from frappe import _
from frappe.query_builder import Criterion
from frappe.utils import cint, flt, getdate

import erpnext
Expand Down Expand Up @@ -364,6 +365,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)
Expand Down Expand Up @@ -608,14 +610,14 @@ def set_gl_entries_by_account(
accounts_by_name,
accounts,
ignore_closing_entries=False,
root_type=None,
):
"""Returns a dict like { "account": [gl entries], ... }"""

company_lft, company_rgt = frappe.get_cached_value(
"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""",
Expand All @@ -631,27 +633,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.db.get_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
Expand Down Expand Up @@ -721,23 +738,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("ifnull(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):
Expand Down
18 changes: 17 additions & 1 deletion erpnext/accounts/report/financial_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def get_data(
filters,
gl_entries_by_account,
ignore_closing_entries=ignore_closing_entries,
root_type=root_type,
)

calculate_values(
Expand Down Expand Up @@ -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",
)

Expand Down

0 comments on commit 5b56296

Please sign in to comment.