salescrm/b/provider_settle/get_settle_bill_list.dspy
2025-10-27 15:50:44 +08:00

99 lines
4.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 通过机构ID拿到流水
async def get_bill_data(ns):
response = {"status": False}
try:
start_date = ns["start_date"]
end_date = ns["end_date"]
provider_id = ns["provider_id"]
orgid = ns["orgid"]
except KeyError as e:
response["msg"] = f"参数错误,缺少:{e}"
return response
db = DBPools()
# 通过供应商account表的机构id 拿到 acc_detail表数据
async with db.sqlorContext('kboss') as sor:
sql = "SELECT a.subjectid,ad.acc_date,o.orgname,p.`name`,p.description,b.quantity,og.list_price,al.amount,og.discount,og.amount,p.ptype,ad.summary FROM acc_detail ad LEFT JOIN account a ON a.id=ad.accountid LEFT JOIN accounting_log al ON al.id=ad.acclogid LEFT JOIN bill b ON b.id=al.billid LEFT JOIN order_goods og ON og.id=b.ordergoodsid LEFT JOIN product p ON p.id=b.productid LEFT JOIN organization o ON o.id=b.customerid WHERE ad.summary IN ('BUY','BUY_REVERSE') AND ( ad.acc_date >= ${start_date}$ AND ad.acc_date < ${end_date}$ ) AND a.accounting_orgid=${orgid}$ AND a.orgid=${provider_id}$ AND a.subjectid IN ('subject010','subject011','subject009')"
if start_date == end_date:
sql = sql.replace(" ad.acc_date >= ${start_date}$ AND ad.acc_date < ${end_date}$", "ad.acc_date = ${end_date}$")
data = await sor.sqlExe(sql, ns)
if not data:
response["status"] = True
response["msg"] = "该供应商目前暂无流水"
# 更新总价到provider_settle_data.money
sql = "UPDATE provider_settle_data SET money=${money}$,money_0=0,money_1=0,money_2=0,detail_status=1 WHERE orgid=${orgid}$ and provider_id=${provider_id}$ and bill_start_date=${bill_start_date}$ and bill_end_date=${bill_end_date}$"
await sor.sqlExe(sql, {"money": 0, "orgid": orgid, "provider_id": provider_id, "bill_start_date": start_date, "bill_end_date": end_date})
return response
provider_data = await sor.R('provider', {'orgid': provider_id, "del_flg": 0})
if not provider_data:
response["status"] = True
response["msg"] = "未找到该供应商"
return response
else:
provider_data = provider_data[0]
# 转换类型
if provider_data['discount_mode'] == '1':
response["provider_settle_type"] = "折扣"
elif provider_data['rebate_mode'] == '1':
response["provider_settle_type"] = "返佣"
elif provider_data['floorprice_mode'] == '1':
response["provider_settle_type"] = "底价"
else:
response["provider_settle_type"] = "未知知结算方式"
# 计算总额
async with db.sqlorContext('kboss') as sor:
total_amount = 0
data_dict = {}
for i, d in enumerate(data):
amount = 0
subject_id = d['subjectid']
if subject_id not in data_dict:
data_dict[subject_id] = {
"data": [],
"amount": 0,
}
data_dict[subject_id]["data"].append(d)
if d["summary"] == "BUY":
amount += round(d['amount'] * 100)
elif d["summary"] == "BUY_REVERSE":
amount -= round(d['amount'] * 100)
data_dict[subject_id]["amount"] += amount
total_amount += amount
# subject009 待结转折扣销售收入
# subject010 待结转代付费销售收入
# subject011 待结转底价销售收入
money_2 = data_dict.get("subject011", {}).get('amount', 0)
money_1 = data_dict.get("subject010", {}).get('amount', 0)
money_0 = data_dict.get("subject009", {}).get('amount', 0)
info("更新总价到provider_settle_data.money")
sql = "UPDATE provider_settle_data SET money=${money}$,money_0=${money_0}$,money_1=${money_1}$,money_2=${money_2}$,detail_status=1 WHERE orgid=${orgid}$ and provider_id=${provider_id}$ and bill_start_date=${bill_start_date}$ and bill_end_date=${bill_end_date}$"
await sor.sqlExe(sql,
{"money": total_amount,
"orgid": orgid,
"provider_id": provider_id,
"bill_start_date": start_date,
"bill_end_date": end_date,
"money_0": money_0,
"money_1": money_1,
"money_2": money_2}
)
info("更新总价到provider_settle_data.money完成")
response["total_amount"] = total_amount
response["status"] = True
response["data"] = data_dict
if data_dict:
response["subject_type"] = list(data_dict.keys())[0]
else:
response["subject_type"] = ""
return response
ret = await get_bill_data(params_kw)
return ret