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:
parent
f31d07554d
commit
11bd15e580
@ -6,6 +6,7 @@ from collections import defaultdict
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
|
from frappe.query_builder import Criterion
|
||||||
from frappe.utils import flt, getdate
|
from frappe.utils import flt, getdate
|
||||||
|
|
||||||
import erpnext
|
import erpnext
|
||||||
@ -359,6 +360,7 @@ def get_data(
|
|||||||
accounts_by_name,
|
accounts_by_name,
|
||||||
accounts,
|
accounts,
|
||||||
ignore_closing_entries=False,
|
ignore_closing_entries=False,
|
||||||
|
root_type=root_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
calculate_values(accounts_by_name, gl_entries_by_account, companies, filters, fiscal_year)
|
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_by_name,
|
||||||
accounts,
|
accounts,
|
||||||
ignore_closing_entries=False,
|
ignore_closing_entries=False,
|
||||||
|
root_type=None,
|
||||||
):
|
):
|
||||||
"""Returns a dict like { "account": [gl entries], ... }"""
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
||||||
|
|
||||||
@ -610,7 +613,6 @@ def set_gl_entries_by_account(
|
|||||||
"Company", filters.get("company"), ["lft", "rgt"]
|
"Company", filters.get("company"), ["lft", "rgt"]
|
||||||
)
|
)
|
||||||
|
|
||||||
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters)
|
|
||||||
companies = frappe.db.sql(
|
companies = frappe.db.sql(
|
||||||
""" select name, default_currency from `tabCompany`
|
""" select name, default_currency from `tabCompany`
|
||||||
where lft >= %(company_lft)s and rgt <= %(company_rgt)s""",
|
where lft >= %(company_lft)s and rgt <= %(company_rgt)s""",
|
||||||
@ -626,27 +628,42 @@ def set_gl_entries_by_account(
|
|||||||
)
|
)
|
||||||
|
|
||||||
for d in companies:
|
for d in companies:
|
||||||
gl_entries = frappe.db.sql(
|
gle = frappe.qb.DocType("GL Entry")
|
||||||
"""select gl.posting_date, gl.account, gl.debit, gl.credit, gl.is_opening, gl.company,
|
account = frappe.qb.DocType("Account")
|
||||||
gl.fiscal_year, gl.debit_in_account_currency, gl.credit_in_account_currency, gl.account_currency,
|
query = (
|
||||||
acc.account_name, acc.account_number
|
frappe.qb.from_(gle)
|
||||||
from `tabGL Entry` gl, `tabAccount` acc where acc.name = gl.account and gl.company = %(company)s and gl.is_cancelled = 0
|
.inner_join(account)
|
||||||
{additional_conditions} and gl.posting_date <= %(to_date)s and acc.lft >= %(lft)s and acc.rgt <= %(rgt)s
|
.on(account.name == gle.account)
|
||||||
order by gl.account, gl.posting_date""".format(
|
.select(
|
||||||
additional_conditions=additional_conditions
|
gle.posting_date,
|
||||||
),
|
gle.account,
|
||||||
{
|
gle.debit,
|
||||||
"from_date": from_date,
|
gle.credit,
|
||||||
"to_date": to_date,
|
gle.is_opening,
|
||||||
"lft": root_lft,
|
gle.company,
|
||||||
"rgt": root_rgt,
|
gle.fiscal_year,
|
||||||
"company": d.name,
|
gle.debit_in_account_currency,
|
||||||
"finance_book": filters.get("finance_book"),
|
gle.credit_in_account_currency,
|
||||||
"company_fb": frappe.get_cached_value("Company", d.name, "default_finance_book"),
|
gle.account_currency,
|
||||||
},
|
account.account_name,
|
||||||
as_dict=True,
|
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:
|
if filters and filters.get("presentation_currency") != d.default_currency:
|
||||||
currency_info["company"] = d.name
|
currency_info["company"] = d.name
|
||||||
currency_info["company_currency"] = d.default_currency
|
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)
|
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 = []
|
additional_conditions = []
|
||||||
|
|
||||||
if ignore_closing_entries:
|
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:
|
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"):
|
if filters.get("include_default_book_entries"):
|
||||||
additional_conditions.append(
|
additional_conditions.append((gle.finance_book.isin([finance_book, company_fb, "", None])))
|
||||||
"(finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)"
|
|
||||||
)
|
|
||||||
else:
|
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):
|
def add_total_row(out, root_type, balance_must_be, companies, company_currency):
|
||||||
|
@ -188,6 +188,7 @@ def get_data(
|
|||||||
filters,
|
filters,
|
||||||
gl_entries_by_account,
|
gl_entries_by_account,
|
||||||
ignore_closing_entries=ignore_closing_entries,
|
ignore_closing_entries=ignore_closing_entries,
|
||||||
|
root_type=root_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
calculate_values(
|
calculate_values(
|
||||||
@ -417,13 +418,28 @@ def set_gl_entries_by_account(
|
|||||||
gl_entries_by_account,
|
gl_entries_by_account,
|
||||||
ignore_closing_entries=False,
|
ignore_closing_entries=False,
|
||||||
ignore_opening_entries=False,
|
ignore_opening_entries=False,
|
||||||
|
root_type=None,
|
||||||
):
|
):
|
||||||
"""Returns a dict like { "account": [gl entries], ... }"""
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
||||||
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(
|
accounts_list = frappe.db.get_all(
|
||||||
"Account",
|
"Account",
|
||||||
filters={"company": company, "is_group": 0, "lft": (">=", root_lft), "rgt": ("<=", root_rgt)},
|
filters=account_filters,
|
||||||
pluck="name",
|
pluck="name",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user