fix(product_accounting): 折扣对齐正确模型,消除双重折扣+换正确模块

- 客户折扣: calculate_product_cost 的 amount 已含客户折扣,product_accounting 不再重复乘
- 供应商成本: 用 cost_result['cost'](含 supplychain 供应商折扣,基于原价),不用 discount 模块的 get_min_supplier_discount
- 分销折扣: 用 supplychain calculate_sale_amounts 的 distribution_amount,不用 get_min_distributor_discount
- 供应商成本基于原价 original_amount,不再基于含客户折扣的售价
This commit is contained in:
ymq 2026-08-26 17:11:28 +08:00
parent 7852583739
commit 1cf2f62d74

View File

@ -1543,6 +1543,8 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
if not cost_result or not cost_result.get('success'):
raise Exception(f'calculate_product_cost failed: {cost_result}')
raw_cost = cost_result.get('amount', 0)
original_amount = cost_result.get('original_amount', 0) or raw_cost
supplier_cost = cost_result.get('cost', 0) or raw_cost
# 4b. Get product currency
prod_currency = 'CNY'
@ -1586,10 +1588,9 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
except Exception:
chain = [ownerid]
# 7. Get customer discount (seller gives to customer)
cust_discount = await env.get_min_product_discount(
product_id, seller_orgid, userorgid)
customer_amount = cost_in_user * cust_discount
# 7. 客户售价calculate_product_cost 的 amount 已含客户折扣discount 模块),
# 这里不再重复乘,避免双重折扣。
customer_amount = cost_in_user
# 8. Build accounting items for each link in chain
# Chain: product_owner → ... → seller
@ -1598,13 +1599,8 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
orderid = getID()
# Determine supplier discount (for last link to provider)
last_link_discount = 1.0
try:
last_link_discount = await env.get_min_supplier_discount(
product_id, ownerid, providerid) # supplier discount from product owner
except Exception:
pass
# 供应商成本calculate_product_cost 的 cost 已含 supplychain 供应商折扣(基于原价)。
# 不用 discount 模块的 get_min_supplier_discount供应商折扣归 supplychain 管)。
accounting_items = []
@ -1619,8 +1615,8 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
productid=product_id, transamt=customer_amount,
currency=user_currency, base_amount=round(customer_amount, 2),
variable={"交易金额": customer_amount, "交易币种": user_currency, "交易手续费": 0}))
# PAY*: owner → supplier, amount = raw_cost * supplier_discount
supplier_amount = raw_cost * last_link_discount
# PAY*: owner → supplier, amount = supplier_cost含 supplychain 供应商折扣)
supplier_amount = supplier_cost
accounting_items.append(DictObject(
action='PAY*', customerid=ownerid, resellerid=ownerid,
providerid=providerid, biz_date=biz_date, timestamp=timestamp,
@ -1643,14 +1639,25 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
variable={"交易金额": customer_amount, "交易币种": user_currency, "交易手续费": 0}))
else:
# Middle link: PAY*
# 分销折扣从 supplychain 查distribution_agreement_items
# 不用 discount 模块的 get_min_distributor_discount。
next_orgid = chain[i + 1]
up_discount = 1.0
link_amount = raw_cost
try:
up_discount = await env.get_min_distributor_discount(
product_id, next_orgid, orgid)
sc = await env.calculate_sale_amounts(None, {
'resellerid': next_orgid,
'sub_reseller_id': orgid,
'productid': product_id,
'prodtypeid': '',
'quantity': 1,
'unit_price': original_amount,
})
if sc:
scd = json.loads(sc) if isinstance(sc, str) else sc
scd = scd.get('data', {}) if isinstance(scd, dict) else {}
link_amount = float(scd.get('distribution_amount', raw_cost) or raw_cost)
except Exception:
pass
link_amount = raw_cost * up_discount
accounting_items.append(DictObject(
action='PAY*', customerid=orgid, resellerid=next_orgid,
providerid=providerid, biz_date=biz_date, timestamp=timestamp,
@ -1658,7 +1665,7 @@ WHERE resource_ref_id = ${llm_id}$ AND product_type = 'llm_model' LIMIT 1"""
currency=prod_currency, base_amount=round(link_amount, 2),
variable={"分销结算": link_amount, "结算币种": prod_currency}))
# Final PAY*: top of chain → supplier
supplier_amount = raw_cost * last_link_discount
supplier_amount = supplier_cost
accounting_items.append(DictObject(
action='PAY*', customerid=chain[0], resellerid=chain[0],
providerid=providerid, biz_date=biz_date, timestamp=timestamp,