perf: Ignore is_opening column in GL Queries (#45327)
* perf: Ignore is_opening column in GL Queries
* chore: Remove unwanted changes
* chore: Remove unwanted changes
* chore: Remove unwanted changes
* chore: Remove unwanted changes
* chore: Remove unwanted changes
* chore: Remove unwanted changes
(cherry picked from commit 993f40fa43)
This commit is contained in:
@@ -76,6 +76,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",
|
||||||
"payment_request_settings",
|
"payment_request_settings",
|
||||||
@@ -515,6 +516,13 @@
|
|||||||
"fieldname": "reconciliation_queue_size",
|
"fieldname": "reconciliation_queue_size",
|
||||||
"fieldtype": "Int",
|
"fieldtype": "Int",
|
||||||
"label": "Reconciliation Queue Size"
|
"label": "Reconciliation Queue Size"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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",
|
||||||
@@ -522,7 +530,7 @@
|
|||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2025-01-13 17:38:39.661320",
|
"modified": "2025-01-18 21:24:19.840745",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Accounts",
|
"module": "Accounts",
|
||||||
"name": "Accounts Settings",
|
"name": "Accounts Settings",
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ class AccountsSettings(Document):
|
|||||||
frozen_accounts_modifier: DF.Link | None
|
frozen_accounts_modifier: DF.Link | None
|
||||||
general_ledger_remarks_length: DF.Int
|
general_ledger_remarks_length: DF.Int
|
||||||
ignore_account_closing_balance: DF.Check
|
ignore_account_closing_balance: DF.Check
|
||||||
|
ignore_is_opening_check_for_reporting: DF.Check
|
||||||
make_payment_via_journal_entry: DF.Check
|
make_payment_via_journal_entry: DF.Check
|
||||||
merge_similar_account_heads: DF.Check
|
merge_similar_account_heads: DF.Check
|
||||||
over_billing_allowance: DF.Currency
|
over_billing_allowance: DF.Currency
|
||||||
|
|||||||
@@ -510,12 +510,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"))
|
||||||
|
|||||||
@@ -208,6 +208,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:
|
||||||
@@ -270,9 +274,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"]
|
||||||
):
|
):
|
||||||
conditions.append("(posting_date >=%(from_date)s or is_opening = 'Yes')")
|
if not ignore_is_opening:
|
||||||
|
conditions.append("(posting_date >=%(from_date)s or is_opening = 'Yes')")
|
||||||
|
else:
|
||||||
|
conditions.append("posting_date >=%(from_date)s")
|
||||||
|
|
||||||
conditions.append("(posting_date <=%(to_date)s or is_opening = 'Yes')")
|
if not ignore_is_opening:
|
||||||
|
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")
|
||||||
|
|||||||
@@ -14,14 +14,14 @@
|
|||||||
"owner": "Administrator",
|
"owner": "Administrator",
|
||||||
"ref_doctype": "GL Entry",
|
"ref_doctype": "GL Entry",
|
||||||
"report_name": "Trial Balance",
|
"report_name": "Trial Balance",
|
||||||
"report_type": "Script Report",
|
"report_type": "Script Report",
|
||||||
"roles": [
|
"roles": [
|
||||||
{
|
{
|
||||||
"role": "Accounts User"
|
"role": "Accounts User"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "Accounts Manager"
|
"role": "Accounts Manager"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "Auditor"
|
"role": "Auditor"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -96,7 +100,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:
|
||||||
@@ -114,7 +118,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)
|
||||||
@@ -125,15 +135,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 = ""
|
||||||
@@ -159,16 +169,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].period_end_date) < getdate(add_days(filters.from_date, -1)):
|
if getdate(last_period_closing_voucher[0].period_end_date) < getdate(add_days(filters.from_date, -1)):
|
||||||
start_date = add_days(last_period_closing_voucher[0].period_end_date, 1)
|
start_date = add_days(last_period_closing_voucher[0].period_end_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:
|
||||||
@@ -187,7 +205,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")
|
||||||
@@ -223,11 +247,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)
|
||||||
)
|
)
|
||||||
opening_balance = opening_balance.where(closing_balance.is_opening == "No")
|
|
||||||
|
if not ignore_is_opening:
|
||||||
|
opening_balance = opening_balance.where(closing_balance.is_opening == "No")
|
||||||
else:
|
else:
|
||||||
opening_balance = opening_balance.where(
|
if not ignore_is_opening:
|
||||||
(closing_balance.posting_date < filters.from_date) | (closing_balance.is_opening == "Yes")
|
opening_balance = opening_balance.where(
|
||||||
)
|
(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)
|
||||||
@@ -298,7 +327,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,
|
||||||
@@ -316,7 +345,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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user