Merge pull request #45785 from frappe/mergify/bp/version-14-hotfix/pr-45327

perf: Ignore is_opening column in GL Queries (backport #45327)
This commit is contained in:
ruthra kumar
2025-02-07 17:15:58 +05:30
committed by GitHub
5 changed files with 72 additions and 21 deletions

View File

@@ -73,6 +73,7 @@
"reports_tab", "reports_tab",
"remarks_section", "remarks_section",
"general_ledger_remarks_length", "general_ledger_remarks_length",
"ignore_is_opening_check_for_reporting",
"column_break_lvjk", "column_break_lvjk",
"receivable_payable_remarks_length" "receivable_payable_remarks_length"
], ],
@@ -471,6 +472,13 @@
"fieldtype": "Select", "fieldtype": "Select",
"label": "Posting Date Inheritance for Exchange Gain / Loss", "label": "Posting Date Inheritance for Exchange Gain / Loss",
"options": "Invoice\nPayment\nReconciliation Date" "options": "Invoice\nPayment\nReconciliation Date"
},
{
"default": "0",
"description": "Ignores legacy Is Opening field in GL Entry that allows adding opening balance post the system is in use while generating reports",
"fieldname": "ignore_is_opening_check_for_reporting",
"fieldtype": "Check",
"label": "Ignore Is Opening check for reporting"
} }
], ],
"icon": "icon-cog", "icon": "icon-cog",

View File

@@ -512,12 +512,16 @@ def get_accounting_entries(
.where(gl_entry.company == filters.company) .where(gl_entry.company == filters.company)
) )
ignore_is_opening = frappe.db.get_single_value(
"Accounts Settings", "ignore_is_opening_check_for_reporting"
)
if doctype == "GL Entry": if doctype == "GL Entry":
query = query.select(gl_entry.posting_date, gl_entry.is_opening, gl_entry.fiscal_year) query = query.select(gl_entry.posting_date, gl_entry.is_opening, gl_entry.fiscal_year)
query = query.where(gl_entry.is_cancelled == 0) query = query.where(gl_entry.is_cancelled == 0)
query = query.where(gl_entry.posting_date <= to_date) query = query.where(gl_entry.posting_date <= to_date)
if ignore_opening_entries: if ignore_opening_entries and not ignore_is_opening:
query = query.where(gl_entry.is_opening == "No") query = query.where(gl_entry.is_opening == "No")
else: else:
query = query.select(gl_entry.closing_date.as_("posting_date")) query = query.select(gl_entry.closing_date.as_("posting_date"))

View File

@@ -209,6 +209,10 @@ def get_gl_entries(filters, accounting_dimensions):
def get_conditions(filters): def get_conditions(filters):
conditions = [] conditions = []
ignore_is_opening = frappe.db.get_single_value(
"Accounts Settings", "ignore_is_opening_check_for_reporting"
)
if filters.get("account"): if filters.get("account"):
filters.account = get_accounts_with_children(filters.account) filters.account = get_accounts_with_children(filters.account)
if filters.account: if filters.account:
@@ -268,9 +272,15 @@ def get_conditions(filters):
or filters.get("party") or filters.get("party")
or filters.get("group_by") in ["Group by Account", "Group by Party"] or filters.get("group_by") in ["Group by Account", "Group by Party"]
): ):
if not ignore_is_opening:
conditions.append("(posting_date >=%(from_date)s or is_opening = 'Yes')") conditions.append("(posting_date >=%(from_date)s or is_opening = 'Yes')")
else:
conditions.append("posting_date >=%(from_date)s")
if not ignore_is_opening:
conditions.append("(posting_date <=%(to_date)s or is_opening = 'Yes')") conditions.append("(posting_date <=%(to_date)s or is_opening = 'Yes')")
else:
conditions.append("posting_date <=%(to_date)s")
if filters.get("project"): if filters.get("project"):
conditions.append("project in %(project)s") conditions.append("project in %(project)s")

View File

@@ -89,6 +89,10 @@ def get_data(filters):
) )
company_currency = filters.presentation_currency or erpnext.get_company_currency(filters.company) company_currency = filters.presentation_currency or erpnext.get_company_currency(filters.company)
ignore_is_opening = frappe.db.get_single_value(
"Accounts Settings", "ignore_is_opening_check_for_reporting"
)
if not accounts: if not accounts:
return None return None
@@ -102,7 +106,7 @@ def get_data(filters):
gl_entries_by_account = {} gl_entries_by_account = {}
opening_balances = get_opening_balances(filters) opening_balances = get_opening_balances(filters, ignore_is_opening)
# add filter inside list so that the query in financial_statements.py doesn't break # add filter inside list so that the query in financial_statements.py doesn't break
if filters.project: if filters.project:
@@ -120,7 +124,13 @@ def get_data(filters):
ignore_opening_entries=True, ignore_opening_entries=True,
) )
calculate_values(accounts, gl_entries_by_account, opening_balances, filters.get("show_net_values")) calculate_values(
accounts,
gl_entries_by_account,
opening_balances,
filters.get("show_net_values"),
ignore_is_opening=ignore_is_opening,
)
accumulate_values_into_parents(accounts, accounts_by_name) accumulate_values_into_parents(accounts, accounts_by_name)
data = prepare_data(accounts, filters, parent_children_map, company_currency) data = prepare_data(accounts, filters, parent_children_map, company_currency)
@@ -131,15 +141,15 @@ def get_data(filters):
return data return data
def get_opening_balances(filters): def get_opening_balances(filters, ignore_is_opening):
balance_sheet_opening = get_rootwise_opening_balances(filters, "Balance Sheet") balance_sheet_opening = get_rootwise_opening_balances(filters, "Balance Sheet", ignore_is_opening)
pl_opening = get_rootwise_opening_balances(filters, "Profit and Loss") pl_opening = get_rootwise_opening_balances(filters, "Profit and Loss", ignore_is_opening)
balance_sheet_opening.update(pl_opening) balance_sheet_opening.update(pl_opening)
return balance_sheet_opening return balance_sheet_opening
def get_rootwise_opening_balances(filters, report_type): def get_rootwise_opening_balances(filters, report_type, ignore_is_opening):
gle = [] gle = []
last_period_closing_voucher = "" last_period_closing_voucher = ""
@@ -165,16 +175,24 @@ def get_rootwise_opening_balances(filters, report_type):
report_type, report_type,
accounting_dimensions, accounting_dimensions,
period_closing_voucher=last_period_closing_voucher[0].name, period_closing_voucher=last_period_closing_voucher[0].name,
ignore_is_opening=ignore_is_opening,
) )
# Report getting generate from the mid of a fiscal year # Report getting generate from the mid of a fiscal year
if getdate(last_period_closing_voucher[0].posting_date) < getdate(add_days(filters.from_date, -1)): if getdate(last_period_closing_voucher[0].posting_date) < getdate(add_days(filters.from_date, -1)):
start_date = add_days(last_period_closing_voucher[0].posting_date, 1) start_date = add_days(last_period_closing_voucher[0].posting_date, 1)
gle += get_opening_balance( gle += get_opening_balance(
"GL Entry", filters, report_type, accounting_dimensions, start_date=start_date "GL Entry",
filters,
report_type,
accounting_dimensions,
start_date=start_date,
ignore_is_opening=ignore_is_opening,
) )
else: else:
gle = get_opening_balance("GL Entry", filters, report_type, accounting_dimensions) gle = get_opening_balance(
"GL Entry", filters, report_type, accounting_dimensions, ignore_is_opening=ignore_is_opening
)
opening = frappe._dict() opening = frappe._dict()
for d in gle: for d in gle:
@@ -193,7 +211,13 @@ def get_rootwise_opening_balances(filters, report_type):
def get_opening_balance( def get_opening_balance(
doctype, filters, report_type, accounting_dimensions, period_closing_voucher=None, start_date=None doctype,
filters,
report_type,
accounting_dimensions,
period_closing_voucher=None,
start_date=None,
ignore_is_opening=0,
): ):
closing_balance = frappe.qb.DocType(doctype) closing_balance = frappe.qb.DocType(doctype)
account = frappe.qb.DocType("Account") account = frappe.qb.DocType("Account")
@@ -229,11 +253,16 @@ def get_opening_balance(
(closing_balance.posting_date >= start_date) (closing_balance.posting_date >= start_date)
& (closing_balance.posting_date < filters.from_date) & (closing_balance.posting_date < filters.from_date)
) )
if not ignore_is_opening:
opening_balance = opening_balance.where(closing_balance.is_opening == "No") opening_balance = opening_balance.where(closing_balance.is_opening == "No")
else: else:
if not ignore_is_opening:
opening_balance = opening_balance.where( opening_balance = opening_balance.where(
(closing_balance.posting_date < filters.from_date) | (closing_balance.is_opening == "Yes") (closing_balance.posting_date < filters.from_date) | (closing_balance.is_opening == "Yes")
) )
else:
opening_balance = opening_balance.where(closing_balance.posting_date < filters.from_date)
if doctype == "GL Entry": if doctype == "GL Entry":
opening_balance = opening_balance.where(closing_balance.is_cancelled == 0) opening_balance = opening_balance.where(closing_balance.is_cancelled == 0)
@@ -304,7 +333,7 @@ def get_opening_balance(
return gle return gle
def calculate_values(accounts, gl_entries_by_account, opening_balances, show_net_values): def calculate_values(accounts, gl_entries_by_account, opening_balances, show_net_values, ignore_is_opening=0):
init = { init = {
"opening_debit": 0.0, "opening_debit": 0.0,
"opening_credit": 0.0, "opening_credit": 0.0,
@@ -322,7 +351,7 @@ def calculate_values(accounts, gl_entries_by_account, opening_balances, show_net
d["opening_credit"] = opening_balances.get(d.name, {}).get("opening_credit", 0) d["opening_credit"] = opening_balances.get(d.name, {}).get("opening_credit", 0)
for entry in gl_entries_by_account.get(d.name, []): for entry in gl_entries_by_account.get(d.name, []):
if cstr(entry.is_opening) != "Yes": if cstr(entry.is_opening) != "Yes" or ignore_is_opening:
d["debit"] += flt(entry.debit) d["debit"] += flt(entry.debit)
d["credit"] += flt(entry.credit) d["credit"] += flt(entry.credit)