mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 16:34:46 +00:00
refactor: populate opening balances calculation table
(cherry picked from commit 86edacb781)
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
"status",
|
"status",
|
||||||
"p_l_closing_balance",
|
"p_l_closing_balance",
|
||||||
"dates_to_process",
|
"dates_to_process",
|
||||||
|
"opening_balances",
|
||||||
"amended_from"
|
"amended_from"
|
||||||
],
|
],
|
||||||
"fields": [
|
"fields": [
|
||||||
@@ -50,13 +51,20 @@
|
|||||||
"fieldtype": "JSON",
|
"fieldtype": "JSON",
|
||||||
"label": "P&L Closing Balance",
|
"label": "P&L Closing Balance",
|
||||||
"no_copy": 1
|
"no_copy": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname": "opening_balances",
|
||||||
|
"fieldtype": "Table",
|
||||||
|
"label": "Opening Balances",
|
||||||
|
"no_copy": 1,
|
||||||
|
"options": "Process Period Closing Voucher Detail"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"grid_page_length": 50,
|
"grid_page_length": 50,
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"is_submittable": 1,
|
"is_submittable": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2025-10-17 13:04:26.353250",
|
"modified": "2025-10-17 13:10:04.024903",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Accounts",
|
"module": "Accounts",
|
||||||
"name": "Process Period Closing Voucher",
|
"name": "Process Period Closing Voucher",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from datetime import timedelta
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import qb
|
from frappe import qb
|
||||||
from frappe.model.document import Document
|
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 import add_days, flt, get_datetime
|
||||||
from frappe.utils.scheduler import is_scheduler_inactive
|
from frappe.utils.scheduler import is_scheduler_inactive
|
||||||
|
|
||||||
@@ -28,6 +28,7 @@ class ProcessPeriodClosingVoucher(Document):
|
|||||||
|
|
||||||
amended_from: DF.Link | None
|
amended_from: DF.Link | None
|
||||||
dates_to_process: DF.Table[ProcessPeriodClosingVoucherDetail]
|
dates_to_process: DF.Table[ProcessPeriodClosingVoucherDetail]
|
||||||
|
opening_balances: DF.Table[ProcessPeriodClosingVoucherDetail]
|
||||||
p_l_closing_balance: DF.JSON | None
|
p_l_closing_balance: DF.JSON | None
|
||||||
parent_pcv: DF.Link
|
parent_pcv: DF.Link
|
||||||
status: DF.Literal["Queued", "Running", "Completed"]
|
status: DF.Literal["Queued", "Running", "Completed"]
|
||||||
@@ -35,14 +36,20 @@ class ProcessPeriodClosingVoucher(Document):
|
|||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.status = "Queued"
|
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 = []
|
self.dates_to_process = []
|
||||||
pcv = frappe.get_doc("Period Closing Voucher", self.parent_pcv)
|
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 = self.get_dates(get_datetime(pcv.period_start_date), get_datetime(pcv.period_end_date))
|
||||||
dates = [start + timedelta(days=x) for x in range((end - start).days + 1)]
|
|
||||||
for x in dates:
|
for x in dates:
|
||||||
self.append(
|
self.append(
|
||||||
"dates_to_process",
|
"dates_to_process",
|
||||||
@@ -52,6 +59,22 @@ class ProcessPeriodClosingVoucher(Document):
|
|||||||
"dates_to_process", {"processing_date": x, "status": "Queued", "report_type": "Balance Sheet"}
|
"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):
|
def on_submit(self):
|
||||||
start_pcv_processing(self.name)
|
start_pcv_processing(self.name)
|
||||||
|
|
||||||
@@ -66,7 +89,7 @@ def start_pcv_processing(docname: str):
|
|||||||
filters={"parent": docname, "status": "Queued"},
|
filters={"parent": docname, "status": "Queued"},
|
||||||
fields=["processing_date", "report_type"],
|
fields=["processing_date", "report_type"],
|
||||||
order_by="processing_date",
|
order_by="processing_date",
|
||||||
limit=1,
|
limit=4,
|
||||||
):
|
):
|
||||||
if not is_scheduler_inactive():
|
if not is_scheduler_inactive():
|
||||||
for x in dates_to_process:
|
for x in dates_to_process:
|
||||||
|
|||||||
Reference in New Issue
Block a user