From 6216547027dbcc5bd0cbc55c50569197b886b6d2 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 17 Oct 2025 13:16:56 +0530 Subject: [PATCH] refactor: populate opening balances calculation table (cherry picked from commit 86edacb781107211d99604fc9c32312ba72dd3d4) --- .../process_period_closing_voucher.json | 10 ++++- .../process_period_closing_voucher.py | 37 +++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.json b/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.json index aebdfd1fb75..6ad27c880b1 100644 --- a/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.json +++ b/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.json @@ -9,6 +9,7 @@ "status", "p_l_closing_balance", "dates_to_process", + "opening_balances", "amended_from" ], "fields": [ @@ -50,13 +51,20 @@ "fieldtype": "JSON", "label": "P&L Closing Balance", "no_copy": 1 + }, + { + "fieldname": "opening_balances", + "fieldtype": "Table", + "label": "Opening Balances", + "no_copy": 1, + "options": "Process Period Closing Voucher Detail" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2025-10-17 13:04:26.353250", + "modified": "2025-10-17 13:10:04.024903", "modified_by": "Administrator", "module": "Accounts", "name": "Process Period Closing Voucher", diff --git a/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.py b/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.py index 523a3b23bf5..9939b61dc1e 100644 --- a/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.py +++ b/erpnext/accounts/doctype/process_period_closing_voucher/process_period_closing_voucher.py @@ -6,7 +6,7 @@ from datetime import timedelta import frappe from frappe import qb from frappe.model.document import Document -from frappe.query_builder.functions import Count, Sum +from frappe.query_builder.functions import Count, Max, Min, Sum from frappe.utils import add_days, flt, get_datetime from frappe.utils.scheduler import is_scheduler_inactive @@ -28,6 +28,7 @@ class ProcessPeriodClosingVoucher(Document): amended_from: DF.Link | None dates_to_process: DF.Table[ProcessPeriodClosingVoucherDetail] + opening_balances: DF.Table[ProcessPeriodClosingVoucherDetail] p_l_closing_balance: DF.JSON | None parent_pcv: DF.Link status: DF.Literal["Queued", "Running", "Completed"] @@ -35,14 +36,20 @@ class ProcessPeriodClosingVoucher(Document): def validate(self): self.status = "Queued" - self.populate_processing_table() + self.populate_processing_tables() - def populate_processing_table(self): + def populate_processing_tables(self): + self.generate_pcv_dates() + self.generate_opening_balances_dates() + + def get_dates(self, start, end): + return [start + timedelta(days=x) for x in range((end - start).days + 1)] + + def generate_pcv_dates(self): self.dates_to_process = [] pcv = frappe.get_doc("Period Closing Voucher", self.parent_pcv) - start = get_datetime(pcv.period_start_date) - end = get_datetime(pcv.period_end_date) - dates = [start + timedelta(days=x) for x in range((end - start).days + 1)] + + dates = self.get_dates(get_datetime(pcv.period_start_date), get_datetime(pcv.period_end_date)) for x in dates: self.append( "dates_to_process", @@ -52,6 +59,22 @@ class ProcessPeriodClosingVoucher(Document): "dates_to_process", {"processing_date": x, "status": "Queued", "report_type": "Balance Sheet"} ) + def generate_opening_balances_dates(self): + self.opening_balances = [] + + pcv = frappe.get_doc("Period Closing Voucher", self.parent_pcv) + if pcv.is_first_period_closing_voucher(): + gl = qb.DocType("GL Entry") + min = qb.from_(gl).select(Min(gl.posting_date)).where(gl.company.eq(pcv.company)).run()[0][0] + max = qb.from_(gl).select(Max(gl.posting_date)).where(gl.company.eq(pcv.company)).run()[0][0] + + dates = self.get_dates(get_datetime(min), get_datetime(max)) + for x in dates: + self.append( + "opening_balances", + {"processing_date": x, "status": "Queued", "report_type": "Balance Sheet"}, + ) + def on_submit(self): start_pcv_processing(self.name) @@ -66,7 +89,7 @@ def start_pcv_processing(docname: str): filters={"parent": docname, "status": "Queued"}, fields=["processing_date", "report_type"], order_by="processing_date", - limit=1, + limit=4, ): if not is_scheduler_inactive(): for x in dates_to_process: