async def apv_callback_provider_settle(ns={}): """ 供应商结算入账审批回调 :param ns: :return: """ info(f"apv_callback_provider_settle_data::{ns}") response = {"status": False} apv_callback_data = {} now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: apv_callback_data['apv_id'] = ns['apv_id'] apv_callback_data['status'] = ns['status'] except Exception as e: response["msg"] = "args is error,please check:{}".format(e) return response status_type = { "start": 1, "agree": 2, "refuse": 3, "terminate": 4 } provider_settle_type_dict = { '折扣': '0', '返佣': '1', '低价': '2' } db = DBPools() async with db.sqlorContext('kboss') as sor: # 更新状态记录 apv_callback_data['update_time'] = datetime.datetime.now() apv_callback_data['status'] = status_type.get(apv_callback_data['status'], -1) sql = "update provider_settle_data set status = ${status}$,update_time = ${update_time}$ where apv_id = ${apv_id}$" d = await sor.sqlExe(sql, apv_callback_data) if not d: response["msg"] = "更新失败" return response if apv_callback_data['status'] != 2: response['msg'] = "审批未通过,不入账" return response # 拿到记账数据 provider_data = await sor.R('provider_settle_data', {'apv_id': apv_callback_data['apv_id'], "del_flg": 0}) if not provider_data: response["msg"] = "未找到该供应商结算数据" return response for i in provider_data: args = [] if i['settle_status'] == 1: info("已经全部成功记账,不再重复记账") continue try: i['money'] = round(i['money'] / 100, 2) i['money_2'] = round(i['money_2'] / 100, 2) i['money_1'] = round(i['money_1'] / 100, 2) i['money_0'] = round(i['money_0'] / 100, 2) except Exception as e: info("总额计算失败:{}".format(e)) sql_data = {'settle_status': 2, 'id': i["id"], "update_time": now_time} await sor.U('provider_settle_data', sql_data) continue if i['settle_status'] == 0: if i['money_2'] != 0: args.append([i['money_2'], '2']) if i['money_1'] != 0: args.append([i['money_1'], '1']) if i['money_0'] != 0: args.append([i['money_0'], '0']) elif i['settle_status'] == 2: if i['failure_id']: failure_id = i['failure_id'].split(',') for f in failure_id: args.append([i['money_{}'.format(f)], f]) else: info("失败记账状态异常failure_id:{}".format(i['failure_id'])) continue else: info("记账状态异常settle_status:{}".format(i['settle_status'])) continue if not args: sql_data = {'settle_status': 1, 'id': i["id"], "update_time": now_time} await sor.U('provider_settle_data', sql_data) continue orgid = i['orgid'] provider_id = i['provider_id'] settle_mode = i['settle_mode'] failure_id = [] for a in args: # 记账 try: settle_log = { 'accounting_orgid': orgid, 'providerid': provider_id, # 供应商id 'settle_date': await get_business_date(sor=None), 'settle_mode': settle_mode, # 供应商 销售结算方式 'sale_mode': a[1], # 0:折扣, 1:返佣 2:低价 'settle_amt': a[0], # 金额 'business_op': 'SETTLE' } ai = SettleAccounting(settle_log) rets = await ai.accounting(sor) if isinstance(rets, bool): info(f"记账成功:{i['id']},sale_mode:{a[1]}") except Exception as e: info(f"记账失败,供应商id:{i['id']},sale_mode:{a[1]},error:{e}") failure_id.append(a[1]) if failure_id: sql_data = {'settle_status': 2, 'failure_id': ','.join(failure_id), 'id': i["id"], "update_time": now_time} else: sql_data = {'settle_status': 1, 'failure_id': None, 'id': i["id"], "update_time": now_time} await sor.U('provider_settle_data', sql_data) return {"status": True, "msg": "success"} response["msg"] = "sql error,please check" return response ret = await apv_callback_provider_settle(params_kw) return ret