refactor: convert sql to query builder on Payments query
This commit is contained in:
@@ -7,6 +7,8 @@ import json
|
||||
import frappe
|
||||
from frappe import _, bold, qb, throw
|
||||
from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
|
||||
from frappe.query_builder import Criterion
|
||||
from frappe.query_builder.custom import ConstantColumn
|
||||
from frappe.query_builder.functions import Abs, Sum
|
||||
from frappe.utils import (
|
||||
add_days,
|
||||
@@ -2541,6 +2543,9 @@ def get_advance_payment_entries(
|
||||
condition=None,
|
||||
payment_name=None,
|
||||
):
|
||||
pe = qb.DocType("Payment Entry")
|
||||
per = qb.DocType("Payment Entry Reference")
|
||||
|
||||
party_account_field = "paid_from" if party_type == "Customer" else "paid_to"
|
||||
currency_field = (
|
||||
"paid_from_account_currency" if party_type == "Customer" else "paid_to_account_currency"
|
||||
@@ -2551,76 +2556,74 @@ def get_advance_payment_entries(
|
||||
)
|
||||
|
||||
payment_entries_against_order, unallocated_payment_entries = [], []
|
||||
limit_cond = "limit %s" % limit if limit else ""
|
||||
|
||||
if payment_name:
|
||||
condition.append(pe.name.like(f"%%{payment_name}%%"))
|
||||
|
||||
if order_list or against_all_orders:
|
||||
if order_list:
|
||||
reference_condition = " and t2.reference_name in ({0})".format(
|
||||
", ".join(["%s"] * len(order_list))
|
||||
condition.append(per.refernce_name.isin(order_list))
|
||||
payment_entries_query = (
|
||||
qb.from_(pe)
|
||||
.inner_join(per)
|
||||
.on(pe.name == per.parent)
|
||||
.select(
|
||||
ConstantColumn("Payment Entry").as_("reference_type"),
|
||||
pe.name,
|
||||
pe.remarks,
|
||||
per.allocated_amount.as_("amount"),
|
||||
per.name.as_("reference_row"),
|
||||
per.reference_name.as_("against_order"),
|
||||
pe.posting_date,
|
||||
pe[currency_field].as_("currency"),
|
||||
pe[exchange_rate_field].as_("exchange_rate"),
|
||||
)
|
||||
else:
|
||||
reference_condition = ""
|
||||
order_list = []
|
||||
|
||||
payment_name_filter = ""
|
||||
if payment_name:
|
||||
payment_name_filter = " and t1.name like '%%{0}%%'".format(payment_name)
|
||||
|
||||
if not condition:
|
||||
condition = ""
|
||||
|
||||
payment_entries_against_order = frappe.db.sql(
|
||||
"""
|
||||
select
|
||||
'Payment Entry' as reference_type, t1.name as reference_name,
|
||||
t1.remarks, t2.allocated_amount as amount, t2.name as reference_row,
|
||||
t2.reference_name as against_order, t1.posting_date,
|
||||
t1.{0} as currency, t1.{5} as exchange_rate
|
||||
from `tabPayment Entry` t1, `tabPayment Entry Reference` t2
|
||||
where
|
||||
t1.name = t2.parent and t1.{1} = %s and t1.payment_type = %s
|
||||
and t1.party_type = %s and t1.party = %s and t1.docstatus = 1
|
||||
and t2.reference_doctype = %s {2} {3} {6}
|
||||
order by t1.posting_date {4}
|
||||
""".format(
|
||||
currency_field,
|
||||
party_account_field,
|
||||
reference_condition,
|
||||
condition,
|
||||
limit_cond,
|
||||
exchange_rate_field,
|
||||
payment_name_filter,
|
||||
),
|
||||
[party_account, payment_type, party_type, party, order_doctype] + order_list,
|
||||
as_dict=1,
|
||||
.where(
|
||||
(pe[party_account_field] == party_account)
|
||||
& (pe.payment_type == payment_type)
|
||||
& (pe.party_type == party_type)
|
||||
& (pe.party == party)
|
||||
& (pe.docstatus == 1)
|
||||
& (per.reference_doctype == order_doctype)
|
||||
)
|
||||
.where(Criterion.all(condition))
|
||||
.orderby(pe.posting_date)
|
||||
)
|
||||
|
||||
if limit:
|
||||
payment_entries_query = payment_entries_query.limit(limit)
|
||||
|
||||
payment_entries_against_order = payment_entries_query.run(as_dict=1)
|
||||
|
||||
if include_unallocated:
|
||||
payment_name_filter = ""
|
||||
if payment_name:
|
||||
payment_name_filter = " and name like '%%{0}%%'".format(payment_name)
|
||||
|
||||
unallocated_payment_entries = frappe.db.sql(
|
||||
"""
|
||||
select 'Payment Entry' as reference_type, name as reference_name, posting_date,
|
||||
remarks, unallocated_amount as amount, {2} as exchange_rate, {3} as currency
|
||||
from `tabPayment Entry`
|
||||
where
|
||||
{0} = %s and party_type = %s and party = %s and payment_type = %s
|
||||
and docstatus = 1 and unallocated_amount > 0 {condition} {4}
|
||||
order by posting_date {1}
|
||||
""".format(
|
||||
party_account_field,
|
||||
limit_cond,
|
||||
exchange_rate_field,
|
||||
currency_field,
|
||||
payment_name_filter,
|
||||
condition=condition or "",
|
||||
),
|
||||
(party_account, party_type, party, payment_type),
|
||||
as_dict=1,
|
||||
unallocated_payment_query = (
|
||||
qb.from_(pe)
|
||||
.select(
|
||||
ConstantColumn("Payment Entry").as_("reference_type"),
|
||||
pe.name.as_("reference_name"),
|
||||
pe.posting_date,
|
||||
pe.remarks,
|
||||
pe.unallocated_amount.as_("amount"),
|
||||
pe[exchange_rate_field].as_("exchange_rate"),
|
||||
pe[currency_field].as_("currency"),
|
||||
)
|
||||
.where(
|
||||
(pe[party_account_field] == party_account)
|
||||
& (pe.party_type == party_type)
|
||||
& (pe.party == party)
|
||||
& (pe.payment_type == payment_type)
|
||||
& (pe.docstatus == 1)
|
||||
& (pe.unallocated_amount.gt(0))
|
||||
)
|
||||
.where(Criterion.all(condition))
|
||||
.orderby(pe.posting_date)
|
||||
)
|
||||
|
||||
if limit:
|
||||
unallocated_payment_query = unallocated_payment_query.limit(limit)
|
||||
|
||||
unallocated_payment_entries = unallocated_payment_query.run(as_dict=1)
|
||||
|
||||
return list(payment_entries_against_order) + list(unallocated_payment_entries)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user