mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-16 11:13:32 +00:00
fix: use query builder instead of raw SQL in get_loyalty_details
(cherry picked from commit 8696ba2f5d)
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import flt, today
|
from frappe.utils import flt, today
|
||||||
|
|
||||||
|
|
||||||
@@ -18,26 +19,34 @@ def get_loyalty_details(
|
|||||||
if not expiry_date:
|
if not expiry_date:
|
||||||
expiry_date = today()
|
expiry_date = today()
|
||||||
|
|
||||||
condition = ""
|
LoyaltyPointEntry = frappe.qb.DocType("Loyalty Point Entry")
|
||||||
if company:
|
|
||||||
condition = " and company=%s " % frappe.db.escape(company)
|
|
||||||
if not include_expired_entry:
|
|
||||||
condition += " and expiry_date>='%s' " % expiry_date
|
|
||||||
|
|
||||||
loyalty_point_details = frappe.db.sql(
|
query = (
|
||||||
f"""select sum(loyalty_points) as loyalty_points,
|
frappe.qb.from_(LoyaltyPointEntry)
|
||||||
sum(purchase_amount) as total_spent from `tabLoyalty Point Entry`
|
.select(
|
||||||
where customer=%s and loyalty_program=%s and posting_date <= %s
|
Sum(LoyaltyPointEntry.loyalty_points).as_("loyalty_points"),
|
||||||
{condition}
|
Sum(LoyaltyPointEntry.purchase_amount).as_("total_spent"),
|
||||||
group by customer""",
|
)
|
||||||
(customer, loyalty_program, expiry_date),
|
.where(
|
||||||
as_dict=1,
|
(LoyaltyPointEntry.customer == customer)
|
||||||
|
& (LoyaltyPointEntry.loyalty_program == loyalty_program)
|
||||||
|
& (LoyaltyPointEntry.posting_date <= expiry_date)
|
||||||
|
)
|
||||||
|
.groupby(LoyaltyPointEntry.customer)
|
||||||
)
|
)
|
||||||
|
|
||||||
if loyalty_point_details:
|
if company:
|
||||||
return loyalty_point_details[0]
|
query = query.where(LoyaltyPointEntry.company == company)
|
||||||
else:
|
|
||||||
return {"loyalty_points": 0, "total_spent": 0}
|
if not include_expired_entry:
|
||||||
|
query = query.where(LoyaltyPointEntry.expiry_date >= expiry_date)
|
||||||
|
|
||||||
|
loyalty_point_details = query.run(as_dict=True)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"loyalty_points": flt(loyalty_point_details[0].loyalty_points),
|
||||||
|
"total_spent": flt(loyalty_point_details[0].total_spent),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
Reference in New Issue
Block a user