diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py index 96cb38ba2a..9d6f68d192 100644 --- a/erpnext/accounts/report/trial_balance/trial_balance.py +++ b/erpnext/accounts/report/trial_balance/trial_balance.py @@ -142,16 +142,31 @@ def get_opening_balances(filters): def get_rootwise_opening_balances(filters, report_type): gle = [] - accounting_dimensions = get_accounting_dimensions(as_list=False) - gle = get_opening_balance("Closing Balance", filters, report_type, accounting_dimensions) - last_period_closing_voucher_date = frappe.db.get_value( - "Period Closing Voucher", {"docstatus": 1, "company": filters.company}, "max(posting_date)" + last_period_closing_voucher = frappe.db.get_all( + "Period Closing Voucher", + filters={"docstatus": 1, "company": filters.company, "posting_date": ("<", filters.from_date)}, + fields=["posting_date", "name"], + order_by="posting_date desc", + limit=1, ) - # Check If need to get opening balance from GL Entry - if getdate(last_period_closing_voucher_date) < getdate(add_days(filters.from_date, -1)): - filters.from_date = add_days(last_period_closing_voucher_date, 1) + accounting_dimensions = get_accounting_dimensions(as_list=False) + + if last_period_closing_voucher: + gle = get_opening_balance( + "Closing Balance", + filters, + report_type, + accounting_dimensions, + period_closing_voucher=last_period_closing_voucher[0].name, + ) + if getdate(last_period_closing_voucher[0].posting_date) < getdate( + add_days(filters.from_date, -1) + ): + filters.from_date = add_days(last_period_closing_voucher, 1) + gle = get_opening_balance("GL Entry", filters, report_type, accounting_dimensions) + else: gle = get_opening_balance("GL Entry", filters, report_type, accounting_dimensions) opening = frappe._dict() @@ -161,7 +176,9 @@ def get_rootwise_opening_balances(filters, report_type): return opening -def get_opening_balance(doctype, filters, report_type, accounting_dimensions): +def get_opening_balance( + doctype, filters, report_type, accounting_dimensions, period_closing_voucher=None +): closing_balance = frappe.qb.DocType(doctype) account = frappe.qb.DocType("Account") @@ -185,6 +202,10 @@ def get_opening_balance(doctype, filters, report_type, accounting_dimensions): if doctype == "Closing Balance": opening_balance = opening_balance.where(closing_balance.closing_date < filters.from_date) + if period_closing_voucher: + opening_balance = opening_balance.where( + closing_balance.period_closing_voucher == period_closing_voucher + ) else: opening_balance = opening_balance.where(closing_balance.posting_date < filters.from_date) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 52b1b05f02..ca1ec08bd1 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -328,4 +328,4 @@ erpnext.patches.v14_0.change_autoname_for_tax_withheld_vouchers erpnext.patches.v14_0.set_pick_list_status erpnext.patches.v15_0.update_asset_value_for_manual_depr_entries erpnext.patches.v14_0.create_accounting_dimensions_for_closing_balance -erpnext.patches.v14_0.update_closing_balances +erpnext.patches.v14_0.update_closing_balances #13 diff --git a/erpnext/patches/v14_0/update_closing_balances.py b/erpnext/patches/v14_0/update_closing_balances.py index 6a1844f796..6a9334b150 100644 --- a/erpnext/patches/v14_0/update_closing_balances.py +++ b/erpnext/patches/v14_0/update_closing_balances.py @@ -25,5 +25,5 @@ def execute(): )[1] pcv_doc.make_closing_entries() gl_entries = pcv_doc.get_gl_entries() - make_closing_entries(gl_entries, is_period_closing_voucher_entry=True) + make_closing_entries(gl_entries, is_period_closing_voucher_entry=True, voucher_name=pcv.name) company_wise_order[pcv.company].append(pcv.posting_date)