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,67 +1,37 @@
|
||||
import json
|
||||
from appPublic.uniqueID import getID
|
||||
from datetime import datetime
|
||||
|
||||
async def main(request, params_kw):
|
||||
"""
|
||||
产品销售时调用此API计算供销记账金额。
|
||||
user_id = await get_user()
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
输入参数:
|
||||
productid: 产品ID
|
||||
prodtypeid: 产品分类ID (可选)
|
||||
quantity: 销售数量
|
||||
unit_price: 销售单价
|
||||
sub_distributor_id: 二级分销商ID (可选, 如果是直接销售则为空)
|
||||
sale_date: 销售日期 (可选, 默认今天)
|
||||
source_type: 来源类型 (1=手动, 2=API调用)
|
||||
source_id: 来源记录ID (可选)
|
||||
|
||||
计算逻辑:
|
||||
1. 查找有效的供销合同及对应产品折扣
|
||||
2. 查找有效的分销协议及对应产品折扣 (如果有二级分销商)
|
||||
3. 计算: 进货金额 = 单价 * 数量 * 进货折扣
|
||||
4. 计算: 分销金额 = 单价 * 数量 * 分销折扣
|
||||
5. 计算: 利润金额 = 分销金额 - 进货金额
|
||||
6. 创建记账记录
|
||||
|
||||
返回: 记账记录数据
|
||||
"""
|
||||
user_id = await get_user()
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
# Parse input
|
||||
data = params_kw.get("data", "{}")
|
||||
if isinstance(data, str):
|
||||
# Parse input
|
||||
data = params_kw.get("data", "{}")
|
||||
if isinstance(data, str):
|
||||
data = json.loads(data)
|
||||
|
||||
productid = data.get("productid")
|
||||
prodtypeid = data.get("prodtypeid")
|
||||
quantity = float(data.get("quantity", 0))
|
||||
unit_price = float(data.get("unit_price", 0))
|
||||
sub_distributor_id = data.get("sub_distributor_id")
|
||||
sale_date = data.get("sale_date", datetime.now().strftime("%Y-%m-%d"))
|
||||
source_type = data.get("source_type", "2")
|
||||
source_id = data.get("source_id")
|
||||
remark = data.get("remark", "")
|
||||
productid = data.get("productid")
|
||||
prodtypeid = data.get("prodtypeid")
|
||||
quantity = float(data.get("quantity", 0))
|
||||
unit_price = float(data.get("unit_price", 0))
|
||||
sub_distributor_id = data.get("sub_distributor_id")
|
||||
sale_date = data.get("sale_date", curDateString())
|
||||
source_type = data.get("source_type", "2")
|
||||
source_id = data.get("source_id")
|
||||
remark = data.get("remark", "")
|
||||
|
||||
if not productid or quantity <= 0 or unit_price <= 0:
|
||||
if not productid or quantity <= 0 or unit_price <= 0:
|
||||
return json.dumps({"status": "error", "message": "缺少必要参数: productid, quantity, unit_price"})
|
||||
|
||||
config = getConfig(".")
|
||||
DBPools(config.databases)
|
||||
total_amount = quantity * unit_price
|
||||
db = DBPools()
|
||||
total_amount = quantity * unit_price
|
||||
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
# Step 1: Find active supply contract with product discount
|
||||
# Priority: exact product > product type > default contract discount
|
||||
supply_contract_id = None
|
||||
supply_contract_item_id = None
|
||||
supplier_id = None
|
||||
supply_discount = 1.0
|
||||
supply_amount = total_amount
|
||||
|
||||
# Find supply contract items matching this product
|
||||
if prodtypeid:
|
||||
sql_sci = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price, sc.supplier_id
|
||||
FROM supply_contract_items sci
|
||||
@ -98,7 +68,6 @@ async def main(request, params_kw):
|
||||
else:
|
||||
supply_amount = total_amount * supply_discount
|
||||
else:
|
||||
# Fallback: find any active supply contract with default discount
|
||||
sql_sc = """SELECT id, supplier_id, default_discount FROM supply_contracts
|
||||
WHERE resellerid = ${resellerid}$
|
||||
AND status = '1'
|
||||
@ -112,14 +81,13 @@ async def main(request, params_kw):
|
||||
supply_discount = float(sc_recs[0].default_discount) if sc_recs[0].default_discount else 1.0
|
||||
supply_amount = total_amount * supply_discount
|
||||
|
||||
# Step 2: Find active distribution agreement with product discount (if sub_distributor)
|
||||
# Step 2: Find active distribution agreement with product discount
|
||||
distribution_agreement_id = None
|
||||
distribution_agreement_item_id = None
|
||||
dist_discount = 1.0
|
||||
dist_amount = total_amount
|
||||
|
||||
if sub_distributor_id:
|
||||
# Find distribution agreement items matching this product
|
||||
sql_dai = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price
|
||||
FROM distribution_agreement_items dai
|
||||
JOIN distribution_agreements da ON dai.agreement_id = da.id
|
||||
@ -145,8 +113,10 @@ async def main(request, params_kw):
|
||||
AND (da.end_date IS NULL OR da.end_date >= ${sale_date}$)
|
||||
AND dai.prodtypeid = ${prodtypeid}$
|
||||
ORDER BY dai.created_at DESC LIMIT 1"""
|
||||
ns_dai["productid"] = None
|
||||
dai_recs = await sor.sqlExe(sql_dai, ns_dai)
|
||||
dai_recs = await sor.sqlExe(sql_dai, {"resellerid": user_orgid,
|
||||
"sub_distributor_id": sub_distributor_id,
|
||||
"sale_date": sale_date,
|
||||
"prodtypeid": prodtypeid})
|
||||
|
||||
if dai_recs:
|
||||
distribution_agreement_item_id = dai_recs[0].id
|
||||
@ -157,7 +127,6 @@ async def main(request, params_kw):
|
||||
else:
|
||||
dist_amount = total_amount * dist_discount
|
||||
else:
|
||||
# Fallback: find active distribution agreement with default discount
|
||||
sql_da = """SELECT id, default_discount FROM distribution_agreements
|
||||
WHERE resellerid = ${resellerid}$
|
||||
AND sub_distributor_id = ${sub_distributor_id}$
|
||||
@ -201,7 +170,7 @@ async def main(request, params_kw):
|
||||
"source_id": source_id,
|
||||
"remark": remark,
|
||||
"created_by": user_id,
|
||||
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
"created_at": timestampstr()
|
||||
}
|
||||
|
||||
await sor.C("supplychain_accounting", record)
|
||||
|
||||
@ -1,31 +1,16 @@
|
||||
import json
|
||||
|
||||
async def main(request, params_kw):
|
||||
"""
|
||||
查询某二级分销商在某产品上的分销协议折扣。
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
参数: sub_distributor_id, productid, prodtypeid(可选)
|
||||
sub_distributor_id = params_kw.get("sub_distributor_id")
|
||||
productid = params_kw.get("productid")
|
||||
prodtypeid = params_kw.get("prodtypeid")
|
||||
|
||||
折扣查找优先级:
|
||||
1. 精确匹配 productid
|
||||
2. 匹配 prodtypeid
|
||||
3. 使用协议默认折扣
|
||||
"""
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
sub_distributor_id = params_kw.get("sub_distributor_id")
|
||||
productid = params_kw.get("productid")
|
||||
prodtypeid = params_kw.get("prodtypeid")
|
||||
|
||||
if not sub_distributor_id or not productid:
|
||||
if not sub_distributor_id or not productid:
|
||||
return json.dumps({"status": "error", "message": "缺少sub_distributor_id或productid参数"})
|
||||
|
||||
config = getConfig(".")
|
||||
DBPools(config.databases)
|
||||
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
# Try exact product match
|
||||
db = DBPools()
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
sql = """SELECT dai.id, dai.agreement_id, dai.discount, dai.settlement_price,
|
||||
da.agreement_code, da.agreement_name
|
||||
FROM distribution_agreement_items dai
|
||||
|
||||
@ -1,30 +1,15 @@
|
||||
import json
|
||||
|
||||
async def main(request, params_kw):
|
||||
"""
|
||||
查询某产品在有效供销合同下的折扣信息。
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
参数: productid, prodtypeid(可选)
|
||||
productid = params_kw.get("productid")
|
||||
prodtypeid = params_kw.get("prodtypeid")
|
||||
|
||||
折扣查找优先级:
|
||||
1. 精确匹配 productid
|
||||
2. 匹配 prodtypeid
|
||||
3. 使用合同默认折扣
|
||||
"""
|
||||
user_orgid = await get_userorgid()
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
productid = params_kw.get("productid")
|
||||
prodtypeid = params_kw.get("prodtypeid")
|
||||
|
||||
if not productid:
|
||||
if not productid:
|
||||
return json.dumps({"status": "error", "message": "缺少productid参数"})
|
||||
|
||||
config = getConfig(".")
|
||||
DBPools(config.databases)
|
||||
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
# Try exact product match
|
||||
db = DBPools()
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
sql = """SELECT sci.id, sci.contract_id, sci.discount, sci.settlement_price,
|
||||
sc.contract_code, sc.contract_name, sc.supplier_id,
|
||||
s.supplier_name
|
||||
@ -55,7 +40,6 @@ async def main(request, params_kw):
|
||||
recs = await sor.sqlExe(sql, {"resellerid": user_orgid, "prodtypeid": prodtypeid})
|
||||
|
||||
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
|
||||
|
||||
@ -1,9 +1,19 @@
|
||||
import json
|
||||
from ahserver.serverenv import ServerEnv
|
||||
env = ServerEnv()
|
||||
delete_func = getattr(env, 'delete_sales_ledger', None)
|
||||
if delete_func is None:
|
||||
print(json.dumps({"status": "error", "message": "delete_sales_ledger function not found"}))
|
||||
else:
|
||||
result = await delete_func(request, params_kw)
|
||||
print(result)
|
||||
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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
|
||||
env = ServerEnv()
|
||||
update_func = getattr(env, 'update_sales_ledger', None)
|
||||
if update_func is None:
|
||||
print(json.dumps({"status": "error", "message": "update_sales_ledger function not found"}))
|
||||
else:
|
||||
result = await update_func(request, params_kw)
|
||||
print(result)
|
||||
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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
|
||||
env = ServerEnv()
|
||||
delete_func = getattr(env, 'delete_sub_resellers', None)
|
||||
if delete_func is None:
|
||||
print(json.dumps({"status": "error", "message": "delete_sub_resellers function not found"}))
|
||||
else:
|
||||
result = await delete_func(request, params_kw)
|
||||
print(result)
|
||||
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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
|
||||
env = ServerEnv()
|
||||
update_func = getattr(env, 'update_sub_resellers', None)
|
||||
if update_func is None:
|
||||
print(json.dumps({"status": "error", "message": "update_sub_resellers function not found"}))
|
||||
else:
|
||||
result = await update_func(request, params_kw)
|
||||
print(result)
|
||||
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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
|
||||
from ahserver.serverenv import ServerEnv
|
||||
env = ServerEnv()
|
||||
delete_func = getattr(env, 'delete_suppliers', None)
|
||||
if delete_func is None:
|
||||
print(json.dumps({"status": "error", "message": "delete_suppliers function not found"}))
|
||||
else:
|
||||
result = await delete_func(request, params_kw)
|
||||
print(result)
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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('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
|
||||
env = ServerEnv()
|
||||
update_func = getattr(env, 'update_suppliers', None)
|
||||
if update_func is None:
|
||||
print(json.dumps({"status": "error", "message": "update_suppliers function not found"}))
|
||||
else:
|
||||
result = await update_func(request, params_kw)
|
||||
print(result)
|
||||
|
||||
ns = params_kw.copy()
|
||||
|
||||
data = ns.copy()
|
||||
data.pop('page', None)
|
||||
data.pop('rows', None)
|
||||
data.pop('data_filter', None)
|
||||
|
||||
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):
|
||||
"""Delete a supplychain_accounting record."""
|
||||
dbname = get_module_dbname('supplychain')
|
||||
dbname = get_module_dbname('supplychain')
|
||||
|
||||
data = params_kw.get("data", "{}")
|
||||
if isinstance(data, str):
|
||||
data = params_kw.get("data", "{}")
|
||||
if isinstance(data, str):
|
||||
data = json.loads(data)
|
||||
|
||||
record_id = data.get("id")
|
||||
if not record_id:
|
||||
record_id = data.get("id")
|
||||
if not record_id:
|
||||
return json.dumps({"status": "error", "message": "Missing record id"})
|
||||
|
||||
config = getConfig(".")
|
||||
DBPools(config.databases)
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
db = DBPools()
|
||||
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):
|
||||
"""Update a supplychain_accounting record."""
|
||||
user_id = await get_user()
|
||||
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", "{}")
|
||||
if isinstance(data, str):
|
||||
data = json.loads(data)
|
||||
|
||||
record_id = data.get("id")
|
||||
if not record_id:
|
||||
record_id = data.get("id")
|
||||
if not record_id:
|
||||
return json.dumps({"status": "error", "message": "Missing record id"})
|
||||
|
||||
data["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
data["updated_at"] = timestampstr()
|
||||
|
||||
# Remove fields that should not be updated
|
||||
for key in ["id", "resellerid", "created_by", "created_at"]:
|
||||
for key in ["id", "resellerid", "created_by", "created_at"]:
|
||||
data.pop(key, None)
|
||||
|
||||
config = getConfig(".")
|
||||
DBPools(config.databases)
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
db = DBPools()
|
||||
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