async def sale_protocol_add(ns={}): """ :param ns: :return: """ ns['id'] = uuid() db = DBPools() async with db.sqlorContext('kboss') as sor: try: # ns_exists_discount = { # 'offer_salemanid': ns.get('offer_salemanid'), # 'offer_orgid': ns.get('offer_orgid'), # 'bid_orgid': ns.get('bid_orgid'), # 'salemode': ns.get('salemode'), # 'sort': ['start_date'] # } # ns['del_flg'] = 0 # same_productid = await sor.R('saleprotocol', ns_exists_discount) # insert_date = datetime.datetime.strptime(ns['start_date'], '%Y-%m-%d').date() # for index, prd in enumerate(same_productid): # start_date = datetime.datetime.strptime(prd['start_date'], '%Y-%m-%d').date() # end_date = datetime.datetime.strptime(prd['end_date'], '%Y-%m-%d').date() # if index == 0 and insert_date < start_date: # ns['end_date'] = start_date # await sor.C('saleprotocol', ns) # break # if index == len(same_productid) - 1 and insert_date > start_date: # prd['end_date'] = ns.get('start_date') # await sor.U('saleprotocol', prd) # ns['end_date'] = '9999-12-31' # await sor.C('saleprotocol', ns) # break # if start_date < insert_date < end_date: # ns['end_date'] = prd.get('end_date') # prd['end_date'] = ns.get('start_date') # await sor.U('saleprotocol', prd) # await sor.C('saleprotocol', ns) # break # if start_date == insert_date: # return { # "status": False, # "msg": "Warning: The current date has already been configured" # } # if not same_productid: # ns['end_date'] = '9999-12-31' # await sor.C('saleprotocol', ns) # 优先开账 # 获取所有供应商 provider_all = await sor.R('organization', {'org_type': 4, 'del_flg': '0'}) for pvider in provider_all: provider_id = pvider['id'] # 查找是否已开账 exist_account_li = await sor.R('account', {'accounting_orgid': ns['bid_orgid'], 'orgid': provider_id}) if not exist_account_li: await openProviderAccounts(sor, ns['bid_orgid'], provider_id) # 如果模式已经添加 就不能重复添加 nss_mode_exist = { 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': ns.get('bid_orgid'), 'salemode': ns.get('salemode'), 'del_flg': '0' } res_mode = await sor.R('saleprotocol', nss_mode_exist) if res_mode: return { 'status': False, 'msg': '已签署过,不能重复添加当前协议' } # 优先判断是否含有回佣 设置过回佣就无法添加其它模式 nss_rebate_exist = { 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': ns.get('bid_orgid'), 'salemode': '1', 'del_flg': '0' } res = await sor.R('saleprotocol', nss_rebate_exist) if res: return { 'status': False, 'msg': '售方买方已经签订了回佣协议,无法再添加当前协议' } # 如果offer是供应商 await sor.C('saleprotocol', ns) return { "status": True, "msg": "saleprotocol add success" } except Exception as e: raise e return { "status": False, "msg": "saleprotocol add failed" } ret = await sale_protocol_add(params_kw) return ret