fix: batch fix dspy violations — remove imports/print/ServerEnv, use return json.dumps, proper widgettype format
This commit is contained in:
parent
e79627c55f
commit
e766111aa1
@ -1,222 +1,191 @@
|
|||||||
import json
|
|
||||||
from appPublic.uniqueID import getID
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
async def main(request, params_kw):
|
user_id = await get_user()
|
||||||
"""
|
user_orgid = await get_userorgid()
|
||||||
产品销售时调用此API计算供销记账金额。
|
dbname = get_module_dbname('supplychain')
|
||||||
|
|
||||||
输入参数:
|
# Parse input
|
||||||
productid: 产品ID
|
data = params_kw.get("data", "{}")
|
||||||
prodtypeid: 产品分类ID (可选)
|
if isinstance(data, str):
|
||||||
quantity: 销售数量
|
data = json.loads(data)
|
||||||
unit_price: 销售单价
|
|
||||||
sub_distributor_id: 二级分销商ID (可选, 如果是直接销售则为空)
|
productid = data.get("productid")
|
||||||
sale_date: 销售日期 (可选, 默认今天)
|
prodtypeid = data.get("prodtypeid")
|
||||||
source_type: 来源类型 (1=手动, 2=API调用)
|
quantity = float(data.get("quantity", 0))
|
||||||
source_id: 来源记录ID (可选)
|
unit_price = float(data.get("unit_price", 0))
|
||||||
|
sub_distributor_id = data.get("sub_distributor_id")
|
||||||
计算逻辑:
|
sale_date = data.get("sale_date", curDateString())
|
||||||
1. 查找有效的供销合同及对应产品折扣
|
source_type = data.get("source_type", "2")
|
||||||
2. 查找有效的分销协议及对应产品折扣 (如果有二级分销商)
|
source_id = data.get("source_id")
|
||||||
3. 计算: 进货金额 = 单价 * 数量 * 进货折扣
|
remark = data.get("remark", "")
|
||||||
4. 计算: 分销金额 = 单价 * 数量 * 分销折扣
|
|
||||||
5. 计算: 利润金额 = 分销金额 - 进货金额
|
if not productid or quantity <= 0 or unit_price <= 0:
|
||||||
6. 创建记账记录
|
return json.dumps({"status": "error", "message": "缺少必要参数: productid, quantity, unit_price"})
|
||||||
|
|
||||||
返回: 记账记录数据
|
db = DBPools()
|
||||||
"""
|
total_amount = quantity * unit_price
|
||||||
user_id = await get_user()
|
|
||||||
user_orgid = await get_userorgid()
|
async with db.sqlorContext(dbname) as sor:
|
||||||
dbname = get_module_dbname('supplychain')
|
# Step 1: Find active supply contract with product discount
|
||||||
|
supply_contract_id = None
|
||||||
# Parse input
|
supply_contract_item_id = None
|
||||||
data = params_kw.get("data", "{}")
|
supplier_id = None
|
||||||
if isinstance(data, str):
|
supply_discount = 1.0
|
||||||
data = json.loads(data)
|
supply_amount = total_amount
|
||||||
|
|
||||||
productid = data.get("productid")
|
if prodtypeid:
|
||||||
prodtypeid = data.get("prodtypeid")
|
sql_sci = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price, sc.supplier_id
|
||||||
quantity = float(data.get("quantity", 0))
|
FROM supply_contract_items sci
|
||||||
unit_price = float(data.get("unit_price", 0))
|
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
||||||
sub_distributor_id = data.get("sub_distributor_id")
|
WHERE sci.resellerid = ${resellerid}$
|
||||||
sale_date = data.get("sale_date", datetime.now().strftime("%Y-%m-%d"))
|
AND sc.status = '1'
|
||||||
source_type = data.get("source_type", "2")
|
AND sc.start_date <= ${sale_date}$
|
||||||
source_id = data.get("source_id")
|
AND (sc.end_date IS NULL OR sc.end_date >= ${sale_date}$)
|
||||||
remark = data.get("remark", "")
|
AND sci.productid = ${productid}$
|
||||||
|
ORDER BY sci.created_at DESC LIMIT 1"""
|
||||||
if not productid or quantity <= 0 or unit_price <= 0:
|
ns_sci = {"resellerid": user_orgid, "sale_date": sale_date, "productid": productid}
|
||||||
return json.dumps({"status": "error", "message": "缺少必要参数: productid, quantity, unit_price"})
|
sci_recs = await sor.sqlExe(sql_sci, ns_sci)
|
||||||
|
|
||||||
config = getConfig(".")
|
if not prodtypeid or not sci_recs:
|
||||||
DBPools(config.databases)
|
sql_sci = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price, sc.supplier_id
|
||||||
total_amount = quantity * unit_price
|
FROM supply_contract_items sci
|
||||||
|
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
||||||
async with db.sqlorContext(dbname) as sor:
|
WHERE sci.resellerid = ${resellerid}$
|
||||||
# Step 1: Find active supply contract with product discount
|
AND sc.status = '1'
|
||||||
# Priority: exact product > product type > default contract discount
|
AND sc.start_date <= ${sale_date}$
|
||||||
supply_contract_id = None
|
AND (sc.end_date IS NULL OR sc.end_date >= ${sale_date}$)
|
||||||
supply_contract_item_id = None
|
AND sci.prodtypeid = ${prodtypeid}$
|
||||||
supplier_id = None
|
ORDER BY sci.created_at DESC LIMIT 1"""
|
||||||
supply_discount = 1.0
|
ns_sci = {"resellerid": user_orgid, "sale_date": sale_date, "prodtypeid": prodtypeid}
|
||||||
supply_amount = total_amount
|
sci_recs = await sor.sqlExe(sql_sci, ns_sci)
|
||||||
|
|
||||||
# Find supply contract items matching this product
|
if sci_recs:
|
||||||
if prodtypeid:
|
supply_contract_item_id = sci_recs[0].id
|
||||||
sql_sci = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price, sc.supplier_id
|
supply_contract_id = sci_recs[0].contract_id
|
||||||
FROM supply_contract_items sci
|
supplier_id = sci_recs[0].supplier_id
|
||||||
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
supply_discount = float(sci_recs[0].discount) if sci_recs[0].discount else 1.0
|
||||||
WHERE sci.resellerid = ${resellerid}$
|
if sci_recs[0].settlement_price:
|
||||||
AND sc.status = '1'
|
supply_amount = float(sci_recs[0].settlement_price) * quantity
|
||||||
AND sc.start_date <= ${sale_date}$
|
else:
|
||||||
AND (sc.end_date IS NULL OR sc.end_date >= ${sale_date}$)
|
supply_amount = total_amount * supply_discount
|
||||||
AND sci.productid = ${productid}$
|
else:
|
||||||
ORDER BY sci.created_at DESC LIMIT 1"""
|
sql_sc = """SELECT id, supplier_id, default_discount FROM supply_contracts
|
||||||
ns_sci = {"resellerid": user_orgid, "sale_date": sale_date, "productid": productid}
|
WHERE resellerid = ${resellerid}$
|
||||||
sci_recs = await sor.sqlExe(sql_sci, ns_sci)
|
AND status = '1'
|
||||||
|
AND start_date <= ${sale_date}$
|
||||||
if not prodtypeid or not sci_recs:
|
AND (end_date IS NULL OR end_date >= ${sale_date}$)
|
||||||
sql_sci = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price, sc.supplier_id
|
ORDER BY created_at DESC LIMIT 1"""
|
||||||
FROM supply_contract_items sci
|
sc_recs = await sor.sqlExe(sql_sc, {"resellerid": user_orgid, "sale_date": sale_date})
|
||||||
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
if sc_recs:
|
||||||
WHERE sci.resellerid = ${resellerid}$
|
supply_contract_id = sc_recs[0].id
|
||||||
AND sc.status = '1'
|
supplier_id = sc_recs[0].supplier_id
|
||||||
AND sc.start_date <= ${sale_date}$
|
supply_discount = float(sc_recs[0].default_discount) if sc_recs[0].default_discount else 1.0
|
||||||
AND (sc.end_date IS NULL OR sc.end_date >= ${sale_date}$)
|
supply_amount = total_amount * supply_discount
|
||||||
AND sci.prodtypeid = ${prodtypeid}$
|
|
||||||
ORDER BY sci.created_at DESC LIMIT 1"""
|
# Step 2: Find active distribution agreement with product discount
|
||||||
ns_sci = {"resellerid": user_orgid, "sale_date": sale_date, "prodtypeid": prodtypeid}
|
distribution_agreement_id = None
|
||||||
sci_recs = await sor.sqlExe(sql_sci, ns_sci)
|
distribution_agreement_item_id = None
|
||||||
|
dist_discount = 1.0
|
||||||
if sci_recs:
|
dist_amount = total_amount
|
||||||
supply_contract_item_id = sci_recs[0].id
|
|
||||||
supply_contract_id = sci_recs[0].contract_id
|
if sub_distributor_id:
|
||||||
supplier_id = sci_recs[0].supplier_id
|
sql_dai = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price
|
||||||
supply_discount = float(sci_recs[0].discount) if sci_recs[0].discount else 1.0
|
FROM distribution_agreement_items dai
|
||||||
if sci_recs[0].settlement_price:
|
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
||||||
supply_amount = float(sci_recs[0].settlement_price) * quantity
|
WHERE dai.resellerid = ${resellerid}$
|
||||||
else:
|
AND da.sub_distributor_id = ${sub_distributor_id}$
|
||||||
supply_amount = total_amount * supply_discount
|
AND da.status = '1'
|
||||||
else:
|
AND da.start_date <= ${sale_date}$
|
||||||
# Fallback: find any active supply contract with default discount
|
AND (da.end_date IS NULL OR da.end_date >= ${sale_date}$)
|
||||||
sql_sc = """SELECT id, supplier_id, default_discount FROM supply_contracts
|
AND dai.productid = ${productid}$
|
||||||
WHERE resellerid = ${resellerid}$
|
ORDER BY dai.created_at DESC LIMIT 1"""
|
||||||
AND status = '1'
|
ns_dai = {"resellerid": user_orgid, "sub_distributor_id": sub_distributor_id,
|
||||||
AND start_date <= ${sale_date}$
|
"sale_date": sale_date, "productid": productid}
|
||||||
AND (end_date IS NULL OR end_date >= ${sale_date}$)
|
dai_recs = await sor.sqlExe(sql_dai, ns_dai)
|
||||||
ORDER BY created_at DESC LIMIT 1"""
|
|
||||||
sc_recs = await sor.sqlExe(sql_sc, {"resellerid": user_orgid, "sale_date": sale_date})
|
if not dai_recs and prodtypeid:
|
||||||
if sc_recs:
|
sql_dai = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price
|
||||||
supply_contract_id = sc_recs[0].id
|
FROM distribution_agreement_items dai
|
||||||
supplier_id = sc_recs[0].supplier_id
|
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
||||||
supply_discount = float(sc_recs[0].default_discount) if sc_recs[0].default_discount else 1.0
|
WHERE dai.resellerid = ${resellerid}$
|
||||||
supply_amount = total_amount * supply_discount
|
AND da.sub_distributor_id = ${sub_distributor_id}$
|
||||||
|
AND da.status = '1'
|
||||||
# Step 2: Find active distribution agreement with product discount (if sub_distributor)
|
AND da.start_date <= ${sale_date}$
|
||||||
distribution_agreement_id = None
|
AND (da.end_date IS NULL OR da.end_date >= ${sale_date}$)
|
||||||
distribution_agreement_item_id = None
|
AND dai.prodtypeid = ${prodtypeid}$
|
||||||
dist_discount = 1.0
|
ORDER BY dai.created_at DESC LIMIT 1"""
|
||||||
dist_amount = total_amount
|
dai_recs = await sor.sqlExe(sql_dai, {"resellerid": user_orgid,
|
||||||
|
"sub_distributor_id": sub_distributor_id,
|
||||||
if sub_distributor_id:
|
"sale_date": sale_date,
|
||||||
# Find distribution agreement items matching this product
|
"prodtypeid": prodtypeid})
|
||||||
sql_dai = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price
|
|
||||||
FROM distribution_agreement_items dai
|
if dai_recs:
|
||||||
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
distribution_agreement_item_id = dai_recs[0].id
|
||||||
WHERE dai.resellerid = ${resellerid}$
|
distribution_agreement_id = dai_recs[0].agreement_id
|
||||||
AND da.sub_distributor_id = ${sub_distributor_id}$
|
dist_discount = float(dai_recs[0].discount) if dai_recs[0].discount else 1.0
|
||||||
AND da.status = '1'
|
if dai_recs[0].settlement_price:
|
||||||
AND da.start_date <= ${sale_date}$
|
dist_amount = float(dai_recs[0].settlement_price) * quantity
|
||||||
AND (da.end_date IS NULL OR da.end_date >= ${sale_date}$)
|
else:
|
||||||
AND dai.productid = ${productid}$
|
dist_amount = total_amount * dist_discount
|
||||||
ORDER BY dai.created_at DESC LIMIT 1"""
|
else:
|
||||||
ns_dai = {"resellerid": user_orgid, "sub_distributor_id": sub_distributor_id,
|
sql_da = """SELECT id, default_discount FROM distribution_agreements
|
||||||
"sale_date": sale_date, "productid": productid}
|
WHERE resellerid = ${resellerid}$
|
||||||
dai_recs = await sor.sqlExe(sql_dai, ns_dai)
|
AND sub_distributor_id = ${sub_distributor_id}$
|
||||||
|
AND status = '1'
|
||||||
if not dai_recs and prodtypeid:
|
AND start_date <= ${sale_date}$
|
||||||
sql_dai = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price
|
AND (end_date IS NULL OR end_date >= ${sale_date}$)
|
||||||
FROM distribution_agreement_items dai
|
ORDER BY created_at DESC LIMIT 1"""
|
||||||
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
da_recs = await sor.sqlExe(sql_da, {"resellerid": user_orgid,
|
||||||
WHERE dai.resellerid = ${resellerid}$
|
"sub_distributor_id": sub_distributor_id,
|
||||||
AND da.sub_distributor_id = ${sub_distributor_id}$
|
"sale_date": sale_date})
|
||||||
AND da.status = '1'
|
if da_recs:
|
||||||
AND da.start_date <= ${sale_date}$
|
distribution_agreement_id = da_recs[0].id
|
||||||
AND (da.end_date IS NULL OR da.end_date >= ${sale_date}$)
|
dist_discount = float(da_recs[0].default_discount) if da_recs[0].default_discount else 1.0
|
||||||
AND dai.prodtypeid = ${prodtypeid}$
|
dist_amount = total_amount * dist_discount
|
||||||
ORDER BY dai.created_at DESC LIMIT 1"""
|
|
||||||
ns_dai["productid"] = None
|
# Step 3: Calculate profit
|
||||||
dai_recs = await sor.sqlExe(sql_dai, ns_dai)
|
profit_amount = dist_amount - supply_amount
|
||||||
|
|
||||||
if dai_recs:
|
# Step 4: Create accounting record
|
||||||
distribution_agreement_item_id = dai_recs[0].id
|
accounting_id = getID()
|
||||||
distribution_agreement_id = dai_recs[0].agreement_id
|
record = {
|
||||||
dist_discount = float(dai_recs[0].discount) if dai_recs[0].discount else 1.0
|
"id": accounting_id,
|
||||||
if dai_recs[0].settlement_price:
|
"resellerid": user_orgid,
|
||||||
dist_amount = float(dai_recs[0].settlement_price) * quantity
|
"supply_contract_id": supply_contract_id,
|
||||||
else:
|
"supply_contract_item_id": supply_contract_item_id,
|
||||||
dist_amount = total_amount * dist_discount
|
"distribution_agreement_id": distribution_agreement_id,
|
||||||
else:
|
"distribution_agreement_item_id": distribution_agreement_item_id,
|
||||||
# Fallback: find active distribution agreement with default discount
|
"sub_distributor_id": sub_distributor_id,
|
||||||
sql_da = """SELECT id, default_discount FROM distribution_agreements
|
"supplier_id": supplier_id,
|
||||||
WHERE resellerid = ${resellerid}$
|
"prodtypeid": prodtypeid,
|
||||||
AND sub_distributor_id = ${sub_distributor_id}$
|
"productid": productid,
|
||||||
AND status = '1'
|
"quantity": quantity,
|
||||||
AND start_date <= ${sale_date}$
|
"unit_price": unit_price,
|
||||||
AND (end_date IS NULL OR end_date >= ${sale_date}$)
|
"supply_discount": supply_discount,
|
||||||
ORDER BY created_at DESC LIMIT 1"""
|
"supply_amount": supply_amount,
|
||||||
da_recs = await sor.sqlExe(sql_da, {"resellerid": user_orgid,
|
"dist_discount": dist_discount,
|
||||||
"sub_distributor_id": sub_distributor_id,
|
"dist_amount": dist_amount,
|
||||||
"sale_date": sale_date})
|
"profit_amount": profit_amount,
|
||||||
if da_recs:
|
"sale_date": sale_date,
|
||||||
distribution_agreement_id = da_recs[0].id
|
"source_type": source_type,
|
||||||
dist_discount = float(da_recs[0].default_discount) if da_recs[0].default_discount else 1.0
|
"source_id": source_id,
|
||||||
dist_amount = total_amount * dist_discount
|
"remark": remark,
|
||||||
|
"created_by": user_id,
|
||||||
# Step 3: Calculate profit
|
"created_at": timestampstr()
|
||||||
profit_amount = dist_amount - supply_amount
|
}
|
||||||
|
|
||||||
# Step 4: Create accounting record
|
await sor.C("supplychain_accounting", record)
|
||||||
accounting_id = getID()
|
|
||||||
record = {
|
result = {
|
||||||
"id": accounting_id,
|
"status": "ok",
|
||||||
"resellerid": user_orgid,
|
"data": record,
|
||||||
"supply_contract_id": supply_contract_id,
|
"summary": {
|
||||||
"supply_contract_item_id": supply_contract_item_id,
|
"total_amount": total_amount,
|
||||||
"distribution_agreement_id": distribution_agreement_id,
|
"supply_amount": supply_amount,
|
||||||
"distribution_agreement_item_id": distribution_agreement_item_id,
|
"dist_amount": dist_amount,
|
||||||
"sub_distributor_id": sub_distributor_id,
|
"profit_amount": profit_amount,
|
||||||
"supplier_id": supplier_id,
|
"supply_discount": supply_discount,
|
||||||
"prodtypeid": prodtypeid,
|
"dist_discount": dist_discount
|
||||||
"productid": productid,
|
}
|
||||||
"quantity": quantity,
|
}
|
||||||
"unit_price": unit_price,
|
|
||||||
"supply_discount": supply_discount,
|
return json.dumps(result)
|
||||||
"supply_amount": supply_amount,
|
|
||||||
"dist_discount": dist_discount,
|
|
||||||
"dist_amount": dist_amount,
|
|
||||||
"profit_amount": profit_amount,
|
|
||||||
"sale_date": sale_date,
|
|
||||||
"source_type": source_type,
|
|
||||||
"source_id": source_id,
|
|
||||||
"remark": remark,
|
|
||||||
"created_by": user_id,
|
|
||||||
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
}
|
|
||||||
|
|
||||||
await sor.C("supplychain_accounting", record)
|
|
||||||
|
|
||||||
result = {
|
|
||||||
"status": "ok",
|
|
||||||
"data": record,
|
|
||||||
"summary": {
|
|
||||||
"total_amount": total_amount,
|
|
||||||
"supply_amount": supply_amount,
|
|
||||||
"dist_amount": dist_amount,
|
|
||||||
"profit_amount": profit_amount,
|
|
||||||
"supply_discount": supply_discount,
|
|
||||||
"dist_discount": dist_discount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.dumps(result)
|
|
||||||
|
|||||||
@ -1,74 +1,59 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
async def main(request, params_kw):
|
user_orgid = await get_userorgid()
|
||||||
"""
|
dbname = get_module_dbname('supplychain')
|
||||||
查询某二级分销商在某产品上的分销协议折扣。
|
|
||||||
|
sub_distributor_id = params_kw.get("sub_distributor_id")
|
||||||
参数: sub_distributor_id, productid, prodtypeid(可选)
|
productid = params_kw.get("productid")
|
||||||
|
prodtypeid = params_kw.get("prodtypeid")
|
||||||
折扣查找优先级:
|
|
||||||
1. 精确匹配 productid
|
if not sub_distributor_id or not productid:
|
||||||
2. 匹配 prodtypeid
|
return json.dumps({"status": "error", "message": "缺少sub_distributor_id或productid参数"})
|
||||||
3. 使用协议默认折扣
|
|
||||||
"""
|
db = DBPools()
|
||||||
user_orgid = await get_userorgid()
|
async with db.sqlorContext(dbname) as sor:
|
||||||
dbname = get_module_dbname('supplychain')
|
sql = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price,
|
||||||
|
da.agreement_code, da.agreement_name
|
||||||
sub_distributor_id = params_kw.get("sub_distributor_id")
|
FROM distribution_agreement_items dai
|
||||||
productid = params_kw.get("productid")
|
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
||||||
prodtypeid = params_kw.get("prodtypeid")
|
WHERE dai.resellerid = ${resellerid}$
|
||||||
|
AND da.sub_distributor_id = ${sub_distributor_id}$
|
||||||
if not sub_distributor_id or not productid:
|
AND da.status = '1'
|
||||||
return json.dumps({"status": "error", "message": "缺少sub_distributor_id或productid参数"})
|
AND da.start_date <= CURDATE()
|
||||||
|
AND (da.end_date IS NULL OR da.end_date >= CURDATE())
|
||||||
config = getConfig(".")
|
AND dai.productid = ${productid}$
|
||||||
DBPools(config.databases)
|
ORDER BY da.start_date DESC"""
|
||||||
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
||||||
async with db.sqlorContext(dbname) as sor:
|
"sub_distributor_id": sub_distributor_id,
|
||||||
# Try exact product match
|
"productid": productid})
|
||||||
sql = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price,
|
|
||||||
da.agreement_code, da.agreement_name
|
if not recs and prodtypeid:
|
||||||
FROM distribution_agreement_items dai
|
sql = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price,
|
||||||
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
da.agreement_code, da.agreement_name
|
||||||
WHERE dai.resellerid = ${resellerid}$
|
FROM distribution_agreement_items dai
|
||||||
AND da.sub_distributor_id = ${sub_distributor_id}$
|
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
||||||
AND da.status = '1'
|
WHERE dai.resellerid = ${resellerid}$
|
||||||
AND da.start_date <= CURDATE()
|
AND da.sub_distributor_id = ${sub_distributor_id}$
|
||||||
AND (da.end_date IS NULL OR da.end_date >= CURDATE())
|
AND da.status = '1'
|
||||||
AND dai.productid = ${productid}$
|
AND da.start_date <= CURDATE()
|
||||||
ORDER BY da.start_date DESC"""
|
AND (da.end_date IS NULL OR da.end_date >= CURDATE())
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
AND dai.prodtypeid = ${prodtypeid}$
|
||||||
"sub_distributor_id": sub_distributor_id,
|
ORDER BY da.start_date DESC"""
|
||||||
"productid": productid})
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
||||||
|
"sub_distributor_id": sub_distributor_id,
|
||||||
if not recs and prodtypeid:
|
"prodtypeid": prodtypeid})
|
||||||
sql = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price,
|
|
||||||
da.agreement_code, da.agreement_name
|
if not recs:
|
||||||
FROM distribution_agreement_items dai
|
sql = """SELECT id as agreement_id, agreement_code, agreement_name,
|
||||||
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
default_discount as discount, NULL as settlement_price
|
||||||
WHERE dai.resellerid = ${resellerid}$
|
FROM distribution_agreements
|
||||||
AND da.sub_distributor_id = ${sub_distributor_id}$
|
WHERE resellerid = ${resellerid}$
|
||||||
AND da.status = '1'
|
AND sub_distributor_id = ${sub_distributor_id}$
|
||||||
AND da.start_date <= CURDATE()
|
AND status = '1'
|
||||||
AND (da.end_date IS NULL OR da.end_date >= CURDATE())
|
AND start_date <= CURDATE()
|
||||||
AND dai.prodtypeid = ${prodtypeid}$
|
AND (end_date IS NULL OR end_date >= CURDATE())
|
||||||
ORDER BY da.start_date DESC"""
|
ORDER BY start_date DESC"""
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
||||||
"sub_distributor_id": sub_distributor_id,
|
"sub_distributor_id": sub_distributor_id})
|
||||||
"prodtypeid": prodtypeid})
|
|
||||||
|
result = [dict(r) for r in recs] if recs else []
|
||||||
if not recs:
|
return json.dumps({"status": "ok", "data": result})
|
||||||
sql = """SELECT id as agreement_id, agreement_code, agreement_name,
|
|
||||||
default_discount as discount, NULL as settlement_price
|
|
||||||
FROM distribution_agreements
|
|
||||||
WHERE resellerid = ${resellerid}$
|
|
||||||
AND sub_distributor_id = ${sub_distributor_id}$
|
|
||||||
AND status = '1'
|
|
||||||
AND start_date <= CURDATE()
|
|
||||||
AND (end_date IS NULL OR end_date >= CURDATE())
|
|
||||||
ORDER BY start_date DESC"""
|
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid,
|
|
||||||
"sub_distributor_id": sub_distributor_id})
|
|
||||||
|
|
||||||
result = [dict(r) for r in recs] if recs else []
|
|
||||||
return json.dumps({"status": "ok", "data": result})
|
|
||||||
|
|||||||
@ -1,70 +1,54 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
async def main(request, params_kw):
|
user_orgid = await get_userorgid()
|
||||||
"""
|
dbname = get_module_dbname('supplychain')
|
||||||
查询某产品在有效供销合同下的折扣信息。
|
|
||||||
|
productid = params_kw.get("productid")
|
||||||
参数: productid, prodtypeid(可选)
|
prodtypeid = params_kw.get("prodtypeid")
|
||||||
|
|
||||||
折扣查找优先级:
|
if not productid:
|
||||||
1. 精确匹配 productid
|
return json.dumps({"status": "error", "message": "缺少productid参数"})
|
||||||
2. 匹配 prodtypeid
|
|
||||||
3. 使用合同默认折扣
|
db = DBPools()
|
||||||
"""
|
async with db.sqlorContext(dbname) as sor:
|
||||||
user_orgid = await get_userorgid()
|
sql = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price,
|
||||||
dbname = get_module_dbname('supplychain')
|
sc.contract_code, sc.contract_name, sc.supplier_id,
|
||||||
|
s.supplier_name
|
||||||
productid = params_kw.get("productid")
|
FROM supply_contract_items sci
|
||||||
prodtypeid = params_kw.get("prodtypeid")
|
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
||||||
|
LEFT JOIN suppliers s ON sc.supplier_id = s.id
|
||||||
if not productid:
|
WHERE sci.resellerid = ${resellerid}$
|
||||||
return json.dumps({"status": "error", "message": "缺少productid参数"})
|
AND sc.status = '1'
|
||||||
|
AND sc.start_date <= CURDATE()
|
||||||
config = getConfig(".")
|
AND (sc.end_date IS NULL OR sc.end_date >= CURDATE())
|
||||||
DBPools(config.databases)
|
AND sci.productid = ${productid}$
|
||||||
|
ORDER BY sc.start_date DESC"""
|
||||||
async with db.sqlorContext(dbname) as sor:
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid, "productid": productid})
|
||||||
# Try exact product match
|
|
||||||
sql = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price,
|
if not recs and prodtypeid:
|
||||||
sc.contract_code, sc.contract_name, sc.supplier_id,
|
sql = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price,
|
||||||
s.supplier_name
|
sc.contract_code, sc.contract_name, sc.supplier_id,
|
||||||
FROM supply_contract_items sci
|
s.supplier_name
|
||||||
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
FROM supply_contract_items sci
|
||||||
LEFT JOIN suppliers s ON sc.supplier_id = s.id
|
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
||||||
WHERE sci.resellerid = ${resellerid}$
|
LEFT JOIN suppliers s ON sc.supplier_id = s.id
|
||||||
AND sc.status = '1'
|
WHERE sci.resellerid = ${resellerid}$
|
||||||
AND sc.start_date <= CURDATE()
|
AND sc.status = '1'
|
||||||
AND (sc.end_date IS NULL OR sc.end_date >= CURDATE())
|
AND sc.start_date <= CURDATE()
|
||||||
AND sci.productid = ${productid}$
|
AND (sc.end_date IS NULL OR sc.end_date >= CURDATE())
|
||||||
ORDER BY sc.start_date DESC"""
|
AND sci.prodtypeid = ${prodtypeid}$
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid, "productid": productid})
|
ORDER BY sc.start_date DESC"""
|
||||||
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid, "prodtypeid": prodtypeid})
|
||||||
if not recs and prodtypeid:
|
|
||||||
sql = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price,
|
if not recs:
|
||||||
sc.contract_code, sc.contract_name, sc.supplier_id,
|
sql = """SELECT id as contract_id, contract_code, contract_name,
|
||||||
s.supplier_name
|
supplier_id, default_discount as discount, NULL as settlement_price
|
||||||
FROM supply_contract_items sci
|
FROM supply_contracts
|
||||||
JOIN supply_contracts sc ON sci.contract_id = sc.id
|
WHERE resellerid = ${resellerid}$
|
||||||
LEFT JOIN suppliers s ON sc.supplier_id = s.id
|
AND status = '1'
|
||||||
WHERE sci.resellerid = ${resellerid}$
|
AND start_date <= CURDATE()
|
||||||
AND sc.status = '1'
|
AND (end_date IS NULL OR end_date >= CURDATE())
|
||||||
AND sc.start_date <= CURDATE()
|
ORDER BY start_date DESC"""
|
||||||
AND (sc.end_date IS NULL OR sc.end_date >= CURDATE())
|
recs = await sor.sqlExe(sql, {"resellerid": user_orgid})
|
||||||
AND sci.prodtypeid = ${prodtypeid}$
|
|
||||||
ORDER BY sc.start_date DESC"""
|
result = [dict(r) for r in recs] if recs else []
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid, "prodtypeid": prodtypeid})
|
return json.dumps({"status": "ok", "data": result})
|
||||||
|
|
||||||
if not recs:
|
|
||||||
# Fallback to contract default
|
|
||||||
sql = """SELECT id as contract_id, contract_code, contract_name,
|
|
||||||
supplier_id, default_discount as discount, NULL as settlement_price
|
|
||||||
FROM supply_contracts
|
|
||||||
WHERE resellerid = ${resellerid}$
|
|
||||||
AND status = '1'
|
|
||||||
AND start_date <= CURDATE()
|
|
||||||
AND (end_date IS NULL OR end_date >= CURDATE())
|
|
||||||
ORDER BY start_date DESC"""
|
|
||||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid})
|
|
||||||
|
|
||||||
result = [dict(r) for r in recs] if recs else []
|
|
||||||
return json.dumps({"status": "ok", "data": result})
|
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import json
|
|
||||||
from ahserver.serverenv import ServerEnv
|
ns = params_kw.copy()
|
||||||
env = ServerEnv()
|
|
||||||
delete_func = getattr(env, 'delete_sales_ledger', None)
|
data = ns.copy()
|
||||||
if delete_func is None:
|
data.pop('page', None)
|
||||||
print(json.dumps({"status": "error", "message": "delete_sales_ledger function not found"}))
|
data.pop('rows', None)
|
||||||
else:
|
data.pop('data_filter', None)
|
||||||
result = await delete_func(request, params_kw)
|
|
||||||
print(result)
|
if not data.get('id'):
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.D('sales_ledger', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Delete Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import json
|
|
||||||
from ahserver.serverenv import ServerEnv
|
ns = params_kw.copy()
|
||||||
env = ServerEnv()
|
|
||||||
update_func = getattr(env, 'update_sales_ledger', None)
|
data = ns.copy()
|
||||||
if update_func is None:
|
data.pop('page', None)
|
||||||
print(json.dumps({"status": "error", "message": "update_sales_ledger function not found"}))
|
data.pop('rows', None)
|
||||||
else:
|
data.pop('data_filter', None)
|
||||||
result = await update_func(request, params_kw)
|
|
||||||
print(result)
|
if not data.get('id'):
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.U('sales_ledger', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Update Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import json
|
|
||||||
from ahserver.serverenv import ServerEnv
|
ns = params_kw.copy()
|
||||||
env = ServerEnv()
|
|
||||||
delete_func = getattr(env, 'delete_sub_resellers', None)
|
data = ns.copy()
|
||||||
if delete_func is None:
|
data.pop('page', None)
|
||||||
print(json.dumps({"status": "error", "message": "delete_sub_resellers function not found"}))
|
data.pop('rows', None)
|
||||||
else:
|
data.pop('data_filter', None)
|
||||||
result = await delete_func(request, params_kw)
|
|
||||||
print(result)
|
if not data.get('id'):
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.D('sub_resellers', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Delete Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import json
|
|
||||||
from ahserver.serverenv import ServerEnv
|
ns = params_kw.copy()
|
||||||
env = ServerEnv()
|
|
||||||
update_func = getattr(env, 'update_sub_resellers', None)
|
data = ns.copy()
|
||||||
if update_func is None:
|
data.pop('page', None)
|
||||||
print(json.dumps({"status": "error", "message": "update_sub_resellers function not found"}))
|
data.pop('rows', None)
|
||||||
else:
|
data.pop('data_filter', None)
|
||||||
result = await update_func(request, params_kw)
|
|
||||||
print(result)
|
if not data.get('id'):
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.U('sub_resellers', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Update Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,9 +1,18 @@
|
|||||||
import json
|
ns = params_kw.copy()
|
||||||
from ahserver.serverenv import ServerEnv
|
|
||||||
env = ServerEnv()
|
data = ns.copy()
|
||||||
delete_func = getattr(env, 'delete_suppliers', None)
|
data.pop('page', None)
|
||||||
if delete_func is None:
|
data.pop('rows', None)
|
||||||
print(json.dumps({"status": "error", "message": "delete_suppliers function not found"}))
|
data.pop('data_filter', None)
|
||||||
else:
|
|
||||||
result = await delete_func(request, params_kw)
|
if not data.get('id'):
|
||||||
print(result)
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.D('suppliers', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Delete Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Delete Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import json
|
|
||||||
from ahserver.serverenv import ServerEnv
|
ns = params_kw.copy()
|
||||||
env = ServerEnv()
|
|
||||||
update_func = getattr(env, 'update_suppliers', None)
|
data = ns.copy()
|
||||||
if update_func is None:
|
data.pop('page', None)
|
||||||
print(json.dumps({"status": "error", "message": "update_suppliers function not found"}))
|
data.pop('rows', None)
|
||||||
else:
|
data.pop('data_filter', None)
|
||||||
result = await update_func(request, params_kw)
|
|
||||||
print(result)
|
if not data.get('id'):
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':'缺少id'}}, ensure_ascii=False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db = DBPools()
|
||||||
|
dbname = get_module_dbname('supplychain')
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.U('suppliers', data)
|
||||||
|
return json.dumps({'widgettype':'Message','options':{'title':'Update Success','message':'ok'}}, ensure_ascii=False)
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'widgettype':'Error','options':{'title':'Update Error','message':str(e)}}, ensure_ascii=False)
|
||||||
|
|||||||
@ -1,19 +1,15 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
async def main(request, params_kw):
|
dbname = get_module_dbname('supplychain')
|
||||||
"""Delete a supplychain_accounting record."""
|
|
||||||
dbname = get_module_dbname('supplychain')
|
data = params_kw.get("data", "{}")
|
||||||
|
if isinstance(data, str):
|
||||||
data = params_kw.get("data", "{}")
|
data = json.loads(data)
|
||||||
if isinstance(data, str):
|
|
||||||
data = json.loads(data)
|
record_id = data.get("id")
|
||||||
|
if not record_id:
|
||||||
record_id = data.get("id")
|
return json.dumps({"status": "error", "message": "Missing record id"})
|
||||||
if not record_id:
|
|
||||||
return json.dumps({"status": "error", "message": "Missing record id"})
|
db = DBPools()
|
||||||
|
async with db.sqlorContext(dbname) as sor:
|
||||||
config = getConfig(".")
|
await sor.D("supplychain_accounting", {"id": record_id})
|
||||||
DBPools(config.databases)
|
return json.dumps({"status": "ok", "message": "Deleted successfully"})
|
||||||
async with db.sqlorContext(dbname) as sor:
|
|
||||||
await sor.D("supplychain_accounting", {"id": record_id})
|
|
||||||
return json.dumps({"status": "ok", "message": "Deleted successfully"})
|
|
||||||
|
|||||||
@ -1,27 +1,21 @@
|
|||||||
import json
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
async def main(request, params_kw):
|
user_id = await get_user()
|
||||||
"""Update a supplychain_accounting record."""
|
dbname = get_module_dbname('supplychain')
|
||||||
user_id = await get_user()
|
|
||||||
dbname = get_module_dbname('supplychain')
|
data = params_kw.get("data", "{}")
|
||||||
|
if isinstance(data, str):
|
||||||
data = params_kw.get("data", "{}")
|
data = json.loads(data)
|
||||||
if isinstance(data, str):
|
|
||||||
data = json.loads(data)
|
record_id = data.get("id")
|
||||||
|
if not record_id:
|
||||||
record_id = data.get("id")
|
return json.dumps({"status": "error", "message": "Missing record id"})
|
||||||
if not record_id:
|
|
||||||
return json.dumps({"status": "error", "message": "Missing record id"})
|
data["updated_at"] = timestampstr()
|
||||||
|
|
||||||
data["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
for key in ["id", "resellerid", "created_by", "created_at"]:
|
||||||
|
data.pop(key, None)
|
||||||
# Remove fields that should not be updated
|
|
||||||
for key in ["id", "resellerid", "created_by", "created_at"]:
|
db = DBPools()
|
||||||
data.pop(key, None)
|
async with db.sqlorContext(dbname) as sor:
|
||||||
|
await sor.U("supplychain_accounting", data)
|
||||||
config = getConfig(".")
|
return json.dumps({"status": "ok", "data": data})
|
||||||
DBPools(config.databases)
|
|
||||||
async with db.sqlorContext(dbname) as sor:
|
|
||||||
await sor.U("supplychain_accounting", data)
|
|
||||||
return json.dumps({"status": "ok", "data": data})
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user