feat: add get_min_supplier_discount and get_min_distributor_discount

- sor_get_min_product_discount now accepts exact_match param
- exact_match=True: no wildcard/NULL fallback (supplier/distributor)
- exact_match=False (default): includes '*' and NULL fallback (customer)
- Both new functions registered in load_discount()
This commit is contained in:
yumoqing 2026-07-06 17:15:32 +08:00
parent 8b054f01d1
commit de1e873d91

View File

@ -276,18 +276,33 @@ where discountid = ${discountid}$
return 1.0
async def sor_get_min_product_discount(sor, product_id, resellerid, customerid):
"""Find the minimum discount for a specific product from active discount records."""
async def sor_get_min_product_discount(sor, product_id, resellerid, customerid, exact_match=False):
"""Find the minimum discount for a specific product from active discount records.
Args:
sor: sqlor context
product_id: product.id
resellerid: discount.resellerid
customerid: discount.customerid (user/supplier/distributor org ID)
exact_match: if True, only match exact customerid (no NULL/'*' fallback).
Use True for suppliers/distributors, False (default) for customers.
"""
env = ServerEnv()
biz_date = await env.get_business_date(sor)
sql = """SELECT dd.discount
if exact_match:
customer_cond = "d.customerid = ${customerid}$"
else:
customer_cond = "(d.customerid = ${customerid}$ OR d.customerid IS NULL OR d.customerid = '*')"
sql = f"""SELECT dd.discount
FROM discount_detail dd
JOIN discount d ON dd.discountid = d.id
WHERE d.resellerid = ${resellerid}$
AND (d.customerid = ${customerid}$ OR d.customerid IS NULL OR d.customerid = '*')
AND d.enabled_date <= ${biz_date}$
AND d.expired_date > ${biz_date}$
AND dd.productid = ${product_id}$"""
WHERE d.resellerid = ${{resellerid}}$
AND {customer_cond}
AND d.enabled_date <= ${{biz_date}}$
AND d.expired_date > ${{biz_date}}$
AND dd.productid = ${{product_id}}$"""
ns = {
'resellerid': resellerid,
'customerid': customerid,
@ -314,6 +329,36 @@ async def get_min_product_discount(product_id, resellerid, customerid):
return 1.0
async def get_min_supplier_discount(product_id, resellerid, supplierid):
"""Get the minimum discount for a supplier on a specific product.
No wildcard/NULL fallback exact supplier match only.
"""
env = ServerEnv()
dbname = env.get_module_dbname('discount')
config = getConfig()
db = DBPools()
db.databases = config.databases
async with db.sqlorContext(dbname) as sor:
return await sor_get_min_product_discount(sor, product_id, resellerid, supplierid, exact_match=True)
return 1.0
async def get_min_distributor_discount(product_id, resellerid, distributorid):
"""Get the minimum discount for a distributor on a specific product.
No wildcard/NULL fallback exact distributor match only.
"""
env = ServerEnv()
dbname = env.get_module_dbname('discount')
config = getConfig()
db = DBPools()
db.databases = config.databases
async with db.sqlorContext(dbname) as sor:
return await sor_get_min_product_discount(sor, product_id, resellerid, distributorid, exact_match=True)
return 1.0
async def get_customer_discount(resellerid, customerid):
"""Legacy: get default discount for a customer (all products)."""
env = ServerEnv()
@ -810,6 +855,8 @@ def load_discount():
env.sor_get_product_discount = sor_get_product_discount
env.sor_get_min_product_discount = sor_get_min_product_discount
env.get_min_product_discount = get_min_product_discount
env.get_min_supplier_discount = get_min_supplier_discount
env.get_min_distributor_discount = get_min_distributor_discount
env.discount_qrcode = discount_qrcode
env.set_promote_discount = set_promote_discount
env.get_discount_details = get_discount_details