refactor(stock): convert backdated-entry SLE check to qb (PG fix)

The authorized-user backdated-entry guard used MariaDB-only
`MAX(timestamp(posting_date, posting_time))`, invalid on Postgres. Convert to
`Max(posting_datetime)` (the precomputed column) via frappe.qb. Same result on
MariaDB; now valid under Postgres.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-20 09:39:33 +05:30
parent aa73606ed2
commit 46c1b49be1

View File

@@ -8,7 +8,7 @@ import frappe
from frappe import _
from frappe.core.doctype.role.role import get_users
from frappe.model.document import Document
from frappe.query_builder.functions import Sum
from frappe.query_builder.functions import Max, Sum
from frappe.utils import add_days, cint, flt, formatdate, get_datetime, getdate
from erpnext.accounts.utils import get_fiscal_year
@@ -311,13 +311,17 @@ class StockLedgerEntry(Document):
if authorized_role:
authorized_users = get_users(authorized_role)
if authorized_users and frappe.session.user not in authorized_users:
last_transaction_time = frappe.db.sql(
"""
select MAX(timestamp(posting_date, posting_time)) as posting_time
from `tabStock Ledger Entry`
where docstatus = 1 and is_cancelled = 0 and item_code = %s
and warehouse = %s""",
(self.item_code, self.warehouse),
sle = frappe.qb.DocType("Stock Ledger Entry")
last_transaction_time = (
frappe.qb.from_(sle)
.select(Max(sle.posting_datetime))
.where(
(sle.docstatus == 1)
& (sle.is_cancelled == 0)
& (sle.item_code == self.item_code)
& (sle.warehouse == self.warehouse)
)
.run()
)[0][0]
cur_doc_posting_datetime = "{} {}".format(