mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-20 03:47:11 +00:00
refactor(postgres): port Bank Transaction doctype queries to the query builder
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,8 @@ import frappe
|
|||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.docstatus import DocStatus
|
from frappe.model.docstatus import DocStatus
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from frappe.query_builder import Tuple
|
||||||
|
from frappe.query_builder.functions import Abs, Max, Sum
|
||||||
from frappe.utils import flt, getdate
|
from frappe.utils import flt, getdate
|
||||||
|
|
||||||
|
|
||||||
@@ -478,30 +480,28 @@ def get_clearance_details(transaction, payment_entry, bt_allocations, gl_entries
|
|||||||
|
|
||||||
|
|
||||||
def get_related_bank_gl_entries(docs):
|
def get_related_bank_gl_entries(docs):
|
||||||
# nosemgrep: frappe-semgrep-rules.rules.frappe-using-db-sql
|
|
||||||
if not docs:
|
if not docs:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
result = frappe.db.sql(
|
gle = frappe.qb.DocType("GL Entry")
|
||||||
"""
|
ac = frappe.qb.DocType("Account")
|
||||||
SELECT
|
result = (
|
||||||
gle.voucher_type AS doctype,
|
frappe.qb.from_(gle)
|
||||||
gle.voucher_no AS docname,
|
.left_join(ac)
|
||||||
gle.account AS gl_account,
|
.on(ac.name == gle.account)
|
||||||
SUM(ABS(gle.credit_in_account_currency - gle.debit_in_account_currency)) AS amount
|
.select(
|
||||||
FROM
|
gle.voucher_type.as_("doctype"),
|
||||||
`tabGL Entry` gle
|
gle.voucher_no.as_("docname"),
|
||||||
LEFT JOIN
|
gle.account.as_("gl_account"),
|
||||||
`tabAccount` ac ON ac.name = gle.account
|
Sum(Abs(gle.credit_in_account_currency - gle.debit_in_account_currency)).as_("amount"),
|
||||||
WHERE
|
)
|
||||||
ac.account_type = 'Bank'
|
.where(
|
||||||
AND (gle.voucher_type, gle.voucher_no) IN %(docs)s
|
(ac.account_type == "Bank")
|
||||||
AND gle.is_cancelled = 0
|
& Tuple(gle.voucher_type, gle.voucher_no).isin([Tuple(vt, vn) for vt, vn in docs])
|
||||||
GROUP BY
|
& (gle.is_cancelled == 0)
|
||||||
gle.voucher_type, gle.voucher_no, gle.account
|
)
|
||||||
""",
|
.groupby(gle.voucher_type, gle.voucher_no, gle.account)
|
||||||
{"docs": docs},
|
.run(as_dict=True)
|
||||||
as_dict=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
entries = {}
|
entries = {}
|
||||||
@@ -523,31 +523,32 @@ def get_total_allocated_amount(docs):
|
|||||||
if not docs:
|
if not docs:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# nosemgrep: frappe-semgrep-rules.rules.frappe-using-db-sql
|
# The original window query (ROW_NUMBER/FIRST_VALUE + rownum = 1) just collapses to one
|
||||||
result = frappe.db.sql(
|
# row per (account, payment_document, payment_entry) with the partition's allocation total
|
||||||
"""
|
# and most recent transaction date — i.e. a plain GROUP BY with SUM and MAX.
|
||||||
SELECT total, latest_date, gl_account, payment_document, payment_entry FROM (
|
btp = frappe.qb.DocType("Bank Transaction Payments")
|
||||||
SELECT
|
bt = frappe.qb.DocType("Bank Transaction")
|
||||||
ROW_NUMBER() OVER w AS rownum,
|
ba = frappe.qb.DocType("Bank Account")
|
||||||
SUM(btp.allocated_amount) OVER(PARTITION BY ba.account, btp.payment_document, btp.payment_entry) AS total,
|
|
||||||
FIRST_VALUE(bt.date) OVER w AS latest_date,
|
result = (
|
||||||
ba.account AS gl_account,
|
frappe.qb.from_(btp)
|
||||||
btp.payment_document,
|
.left_join(bt)
|
||||||
btp.payment_entry
|
.on(bt.name == btp.parent)
|
||||||
FROM
|
.left_join(ba)
|
||||||
`tabBank Transaction Payments` btp
|
.on(ba.name == bt.bank_account)
|
||||||
LEFT JOIN `tabBank Transaction` bt ON bt.name=btp.parent
|
.select(
|
||||||
LEFT JOIN `tabBank Account` ba ON ba.name=bt.bank_account
|
Sum(btp.allocated_amount).as_("total"),
|
||||||
WHERE
|
Max(bt.date).as_("latest_date"),
|
||||||
(btp.payment_document, btp.payment_entry) IN %(docs)s
|
ba.account.as_("gl_account"),
|
||||||
AND bt.docstatus = 1
|
btp.payment_document,
|
||||||
WINDOW w AS (PARTITION BY ba.account, btp.payment_document, btp.payment_entry ORDER BY bt.date DESC)
|
btp.payment_entry,
|
||||||
) temp
|
)
|
||||||
WHERE
|
.where(
|
||||||
rownum = 1
|
Tuple(btp.payment_document, btp.payment_entry).isin([Tuple(pd, pe) for pd, pe in docs])
|
||||||
""",
|
& (bt.docstatus == 1)
|
||||||
dict(docs=docs),
|
)
|
||||||
as_dict=True,
|
.groupby(ba.account, btp.payment_document, btp.payment_entry)
|
||||||
|
.run(as_dict=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
payment_allocation_details = {}
|
payment_allocation_details = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user