mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 16:34:46 +00:00
refactor: replace sql query of matched_payment_requests to query builder
This commit is contained in:
@@ -7,6 +7,8 @@ from functools import reduce
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import ValidationError, _, qb, scrub, throw
|
from frappe import ValidationError, _, qb, scrub, throw
|
||||||
|
from frappe.query_builder import Tuple
|
||||||
|
from frappe.query_builder.functions import Count
|
||||||
from frappe.utils import cint, comma_or, flt, getdate, nowdate
|
from frappe.utils import cint, comma_or, flt, getdate, nowdate
|
||||||
from frappe.utils.data import comma_and, fmt_money, get_link_to_form
|
from frappe.utils.data import comma_and, fmt_money, get_link_to_form
|
||||||
from pypika import Case
|
from pypika import Case
|
||||||
@@ -1822,11 +1824,11 @@ class PaymentEntry(AccountsController):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# FIXME: can be optimize and use query builder
|
|
||||||
def get_matched_payment_requests_of_references(references=None):
|
def get_matched_payment_requests_of_references(references=None):
|
||||||
if not references:
|
if not references:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# to fetch matched rows
|
||||||
refs = [
|
refs = [
|
||||||
(row.reference_doctype, row.reference_name, row.allocated_amount)
|
(row.reference_doctype, row.reference_name, row.allocated_amount)
|
||||||
for row in references
|
for row in references
|
||||||
@@ -1836,26 +1838,27 @@ def get_matched_payment_requests_of_references(references=None):
|
|||||||
if not refs:
|
if not refs:
|
||||||
return
|
return
|
||||||
|
|
||||||
all_matched_prs = frappe.db.sql(
|
PR = frappe.qb.DocType("Payment Request")
|
||||||
"""
|
|
||||||
select name, reference_doctype, reference_name, outstanding_amount
|
# query to group by reference_doctype, reference_name, outstanding_amount
|
||||||
from `tabPayment Request`
|
subquery = (
|
||||||
where (reference_doctype, reference_name, outstanding_amount) in %s
|
frappe.qb.from_(PR)
|
||||||
and status != 'Paid' and docstatus = 1
|
.select(
|
||||||
""",
|
PR.name, PR.reference_doctype, PR.reference_name, PR.outstanding_amount, Count("*").as_("count")
|
||||||
(refs,),
|
)
|
||||||
as_dict=True,
|
.where(Tuple(PR.reference_doctype, PR.reference_name, PR.outstanding_amount).isin(refs))
|
||||||
|
.where(PR.status != "Paid")
|
||||||
|
.where(PR.docstatus == 1)
|
||||||
|
.groupby(PR.reference_doctype, PR.reference_name, PR.outstanding_amount)
|
||||||
)
|
)
|
||||||
|
|
||||||
single_matched_prs = {}
|
# query to fetch matched rows which are single
|
||||||
for pr in all_matched_prs:
|
matched_prs = frappe.qb.from_(subquery).select("*").where(subquery.count == 1).run(as_dict=True)
|
||||||
key = (pr.reference_doctype, pr.reference_name, pr.outstanding_amount)
|
|
||||||
if key in single_matched_prs:
|
|
||||||
single_matched_prs[key].append(pr.name)
|
|
||||||
else:
|
|
||||||
single_matched_prs[key] = [pr.name]
|
|
||||||
|
|
||||||
return {key: names[0] for key, names in single_matched_prs.items() if len(names) == 1}
|
if not matched_prs:
|
||||||
|
return
|
||||||
|
|
||||||
|
return {(pr.reference_doctype, pr.reference_name, pr.outstanding_amount): pr.name for pr in matched_prs}
|
||||||
|
|
||||||
|
|
||||||
def validate_inclusive_tax(tax, doc):
|
def validate_inclusive_tax(tax, doc):
|
||||||
|
|||||||
Reference in New Issue
Block a user