201 lines
7.1 KiB
Plaintext
201 lines
7.1 KiB
Plaintext
async def get_invoice_data(recharge_id: list):
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
sql = "SELECT *, r.id as id FROM recharge_log r LEFT JOIN invoice_status s ON r.id=s.recharge_id WHERE r.del_flg=0 and r.id in ${recharge_id}$"
|
||
recharge_data = await sor.sqlExe(sql, {"recharge_id": recharge_id})
|
||
return recharge_data
|
||
|
||
|
||
async def get_invoice_info(client_id: str):
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
data = await sor.R("invoice_info", {"client_id": client_id, "delete_flag": 0})
|
||
return data
|
||
|
||
|
||
async def get_sale_id(customerid):
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
data = await sor.R("customer", {"customerid": customerid})
|
||
return data
|
||
|
||
|
||
async def get_approve_phone(sender):
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
# 获取发起人的手机号
|
||
try:
|
||
data = await sor.R("users", {"id": sender})
|
||
except Exception as e:
|
||
return "", "", f"get user_id error:{str(e)}"
|
||
if data and data[0]['mobile']:
|
||
phone = data[0]['mobile']
|
||
name = data[0]['username']
|
||
if len(phone) == 11:
|
||
return phone, name, ""
|
||
else:
|
||
err = f"手机号格式错误,user_id:{sender},phone:{phone},phone_len:{len(phone)}"
|
||
return "", "", err
|
||
else:
|
||
err = f"未找到销售手机号,user_id:{sender}"
|
||
return "", "", err
|
||
|
||
|
||
async def get_users(id):
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
data = await sor.R("users", {"id": id})
|
||
return data
|
||
|
||
|
||
# 获取发起人的手机号
|
||
|
||
|
||
async def apv_invoice(ns={}):
|
||
"""
|
||
客户根据充值id发起开票申请
|
||
:param ns:
|
||
:return:
|
||
"""
|
||
data = {}
|
||
try:
|
||
data['maker'] = ns['maker']
|
||
data['client_id'] = ns['client_id']
|
||
data['orgid'] = ns['orgid']
|
||
data['user_id'] = ns['user_id']
|
||
data['user_name'] = ns['user_name']
|
||
data['recharge_id']: list = ns['recharge_id']
|
||
except Exception as e:
|
||
return {"status": False, "msg": f"get key error, please check key:{str(e)}"}
|
||
|
||
# 获取开票状态
|
||
recharge_data = await get_invoice_data(data['recharge_id'])
|
||
if not recharge_data:
|
||
return {"status": False, "msg": "开票申请失败,该订单已开票, 未获取到信息", "data": data['recharge_id']}
|
||
|
||
# 处理冲账
|
||
new_data = {}
|
||
update_id = []
|
||
for i in recharge_data:
|
||
if i['status'] == 1 or i['status'] == 2:
|
||
return {"status": False, "msg": "开票申请失败,该订单已开票", "data": i['id']}
|
||
else:
|
||
if "REVERSE" in i['action']:
|
||
return {"status": False, "msg": "开票申请失败, 冲账开票不支持", "data": i['id']}
|
||
if i['original_id']:
|
||
update_id.append([i['id'], i["original_id"], i['action']])
|
||
else:
|
||
new_data[i['id']] = i
|
||
for i in update_id:
|
||
new_data[i[1]]['action'] = i[2]
|
||
|
||
# 计算总额
|
||
total_amount = 0
|
||
for i in new_data.values():
|
||
if i['action'] == "RECHARGE":
|
||
total_amount += i['recharge_amt']
|
||
info("计算总额 ok ")
|
||
|
||
# 获取开票配置信息
|
||
invoice_info = await get_invoice_info(data['client_id'])
|
||
if not invoice_info:
|
||
return {"status": False, "msg": "开票申请失败,请通知销售添加开票信息", "data":
|
||
{
|
||
"table": "invoice_info",
|
||
"client_id": data['client_id']
|
||
}}
|
||
else:
|
||
info("获取开票信息 ok ")
|
||
invoice_info = invoice_info[0]
|
||
|
||
# 确定发起人
|
||
if data['maker'] == data['client_id']:
|
||
# 根据客户id,获取所属销售id
|
||
customer_data = await get_sale_id(data['client_id'])
|
||
if customer_data:
|
||
sender = customer_data[0]['salemanid']
|
||
if not sender:
|
||
return {"status": False, "msg": "开票申请失败,客户获取所属销售id失败", data: f"customer_id:{data['client_id']}"}
|
||
user_data = await get_users(sender)
|
||
if user_data:
|
||
data['orgid'] = user_data[0]["orgid"]
|
||
else:
|
||
return {"status": False, "msg": "开票申请失败,客户获取所属销售的orgid失败", data: f"customer_id:{data['client_id']}"}
|
||
|
||
|
||
|
||
else:
|
||
return {"status": False, "msg": "开票申请失败,客户获取销售信息失败", data: f"client_id:{data['client_id']}"}
|
||
else:
|
||
sender = data['maker']
|
||
|
||
info("开始获取销售的手机号")
|
||
sender_phone, sender_name, err = await get_approve_phone(sender=sender)
|
||
if err:
|
||
return {"status": False, "msg": err}
|
||
else:
|
||
info(f"获取发起人的手机号 ok,{sender_name},{sender_phone}")
|
||
|
||
invoice_type_dict = {
|
||
1: "个人",
|
||
2: "公司",
|
||
3: "组织"
|
||
}
|
||
form_component = {
|
||
"title": "",
|
||
"detail": {
|
||
"sender": data['user_name'],
|
||
"invoice_title": invoice_info['invoice_title'],
|
||
"tax_no": invoice_info['tax_no'],
|
||
"tax_rate": invoice_info['tax_rate'],
|
||
"invoice_type": invoice_type_dict.get(invoice_info['invoice_type'], f"未知类型:{invoice_info['invoice_type']}"),
|
||
"total_amount": total_amount,
|
||
"bank_name": invoice_info['bank_name'],
|
||
"bank_account": invoice_info['bank_account'],
|
||
"address": invoice_info['address'],
|
||
"receiver_name": invoice_info['receiver_name'],
|
||
"phone": invoice_info['phone'],
|
||
}
|
||
}
|
||
info("ok")
|
||
# 发起审批
|
||
resp = await issue_approve(sender_phone, data["orgid"], data["client_id"], 7, form_component)
|
||
if resp['status']:
|
||
now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
# 更新开票状态
|
||
db = DBPools()
|
||
# 审批记录入库
|
||
async with db.sqlorContext('kboss') as sor:
|
||
invoice_apv_data = {
|
||
"id": uuid(),
|
||
"apv_id": resp['instanceId'],
|
||
"sender": sender,
|
||
"user_id": data['user_id'],
|
||
"user_name": data['user_name'],
|
||
"customerid": data['client_id'],
|
||
"apv_status": 1,
|
||
"invoice_time": now_time,
|
||
"total_amount": total_amount,
|
||
"recharge_id": ','.join(data['recharge_id']),
|
||
}
|
||
await sor.C("invoice_apv", invoice_apv_data)
|
||
for i in data['recharge_id']:
|
||
invoice_status_data = {
|
||
"id": uuid(),
|
||
"recharge_id": i,
|
||
"status": 1,
|
||
"apv_id": resp['instanceId'],
|
||
"update_time": datetime.datetime.now()
|
||
}
|
||
await sor.C("invoice_status", invoice_status_data)
|
||
return {"status": True, "msg": "success", "apv_id": resp['instanceId']}
|
||
else:
|
||
resp["msg"] = resp['msg']
|
||
return resp
|
||
|
||
return {"status": False, "msg": "sql error"}
|
||
|
||
|
||
ret = await apv_invoice(params_kw)
|
||
return ret
|