From 9ec30319e4232f7dffd221003cfa39e7395fe978 Mon Sep 17 00:00:00 2001 From: "ili.ad" <108145573+ili-ad@users.noreply.github.com> Date: Tue, 17 Feb 2026 00:02:53 -0500 Subject: [PATCH] fix(postgres): validate against period closing using MAX(period_end_date) (#51554) * fix(postgres): validate against period closing using MAX(period_end_date) * refactor: remove non-existent field --------- Co-authored-by: Matt Howard Co-authored-by: ruthra kumar --- erpnext/accounts/general_ledger.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 60b3efd3cbc..ab86dcfd15c 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -804,12 +804,19 @@ def validate_against_pcv(is_opening, posting_date, company): title=_("Invalid Opening Entry"), ) - last_pcv_date = frappe.db.get_value( - "Period Closing Voucher", {"docstatus": 1, "company": company}, "max(period_end_date)" - ) + # Local import so you don't have to touch file-level imports + from frappe.query_builder.functions import Max + + pcv = frappe.qb.DocType("Period Closing Voucher") + + last_pcv_date = ( + frappe.qb.from_(pcv) + .select(Max(pcv.period_end_date)) + .where((pcv.docstatus == 1) & (pcv.company == company)) + ).run(pluck=True)[0] if last_pcv_date and getdate(posting_date) <= getdate(last_pcv_date): - message = _("Books have been closed till the period ending on {0}").format(formatdate(last_pcv_date)) + message = _("Books have been closed till the period ending on {0}.").format(formatdate(last_pcv_date)) message += "
" message += _("You cannot create/amend any accounting entries till this date.") frappe.throw(message, title=_("Period Closed"))