抖音 校驗庫存 轉單版
阿新 • • 發佈:2022-12-03
import requests as req from openpyxl import Workbook import re session_wms = req.Session() login_url = "https://apollo-api.xtw-tech.com/v1/auth/token" # 請求標頭檔案Request Headers headers = { # 'accept': 'application/json, text/plain, */*', # 'accept-encoding': 'gzip, deflate, br', # 'accept-language': 'zh-CN,zh;q=0.9', # 'content-length': '46', # 'content-type': 'application/json', 'grpc-metadata-application-code': 'VENUS', # 'grpc-metadata-user-auth-token': 'null', 'origin': 'https://venus.xtw-tech.com', 'referer': 'https://venus.xtw-tech.com', # 'sec-fetch-mode': 'cors', # 'sec-fetch-site': 'same-site', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36' } # 登陸wms data = { 'username': "xujingfei", 'password': "Xjf2567483" } # 登入wms response_wms = session_wms.post(url=login_url, headers=headers, json=data) wb = Workbook() ws = wb.active # title = ['發貨倉', '訂單型別', '發貨單號', '發貨單號狀態', 'SP', '品名', '貨主id','貨主','sp_map_id','庫存型別', '數量', '客戶名', '下單地址', '訂單號', '訂單渠道','需求','良品臨期','良品過期','良品正常效期'] title = ['發貨倉', '訂單型別', '發貨單號', '發貨單號狀態', 'SP', '品名', '貨主id','貨主','sp_map_id','庫存型別', '數量', '客戶名', '下單地址', '訂單號', '訂單渠道','需求'] ws.append(title) solist = 'https://titan-api.xtw-tech.com/v1/shipment_order/list' header = { 'authority': 'titan-api.xtw-tech.com', 'method': 'POST', 'path': '/v1/shipment_order/list', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'content-length': '295', 'content-type': 'application/json', 'grpc-metadata-application-code': 'MERCURY', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://mercury.xtw-tech.com', 'referer': 'https://mercury.xtw-tech.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } tenantIds = ['1'] warehouse_ids = ['137'] for tenantId, warehouse_id in zip(tenantIds, warehouse_ids): # for tenantId in tenantIds: for page in range(1): data = {'tenantId': f"{tenantId}", 'justShowMine': False, 'listOption': {'keyword': "", 'page': f'{page + 1}', 'pageSize': '100', 'fieldFilter': [{'fieldToFilter': "show_crossowner", 'keyword': "false"}, {'fieldToFilter': "status_list", 'keyword': "201"}, {'fieldToFilter': "serial_number", 'keyword': "SO-20221201-1859585877"}, {'fieldToFilter': "type", 'keyword': "1"}, {'fieldToFilter': "warehouse_ids", 'keyword': f"{warehouse_id}"}, {'fieldToFilter': "created_time_start", 'keyword': "2022-10-01T00:00:00+08:00"}, {'fieldToFilter': "created_time_end", 'keyword': "2030-11-30T23:59:59+08:00"}]} } response = session_wms.post(headers=header, url=solist, json=data) item = response.json()['items'] logo ={'LOCATION_TYPE_EXPIRED':'良品過期','LOCATION_TYPE_CLOSE_TO_EXPIRY':'良品臨期','LOCATION_TYPE_NORMAL':'良品正常效期'} for i in item: name = i['consigneeInfo']['name'] poiAddress = i['consigneeInfo']['poiAddress'] relatedSerialNumber = i['relatedSerialNumber'] ordertype = relatedSerialNumber.split('-')[3] relatedSerial = relatedSerialNumber.split('-')[2] status = i['status'] type = i['type'] serialNumber = i['serialNumber'] warehouse = i['warehouse']['name'] so_url = f'https://titan-api.xtw-tech.com/v1/shipment_order/by_serial_number/{serialNumber}?tenantId={tenantId}' header = { 'authority': 'titan-api.xtw-tech.com', 'method': 'GET', 'path': f'/v1/shipment_order/by_serial_number/{serialNumber}?tenantId={tenantId}', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'grpc-metadata-application-code': 'MERCURY', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://mercury.xtw-tech.com', 'referer': 'https://mercury.xtw-tech.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } deatil = session_wms.get(url=so_url, headers=header) req = deatil.json() shipmentOrderWithDetail = req['shipmentOrderWithDetail']['inventories'] sp_num = 0 for sp in shipmentOrderWithDetail: # ['發貨倉','訂單號','發貨單號','發貨單號狀態','SP','品名','效期狀態','數量','收貨人','收貨地址'] sieveName = sp['sieveName'] # 有效期 owner_id = sp['owner']['id'] owner_name = sp['owner']['name'] code = sp['standardProductTenantMapping']['standardProduct']['code'] sp_name = sp['standardProductTenantMapping']['standardProduct']['name'] sp_id = sp['standardProductTenantMapping']['id'] expectedQuantity = sp['expectedQuantity'] # 庫存校驗 if ordertype =='11': k = {} sieve = re.match('良品臨期|良品過期|良品正常效期',sieveName) sieve =sieve.group() k[f'{code+owner_id+owner_name+sieve}'] = expectedQuantity # info = (warehouse, type, serialNumber, status, code, sp_name,owner_id,owner_name, sp_id,sieve, int(expectedQuantity), name, # poiAddress, relatedSerialNumber, ordertype, k[f'{code+owner_id+owner_name+sieve}'] ) # ws.append(info) # 進入庫存校驗 inventory = 'https://titan-api.xtw-tech.com/v1/inventory/list' header ={ 'authority': 'titan-api.xtw-tech.com', 'method': 'POST', 'path': '/v1/inventory/list', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'content-length': '519', 'content-type': 'application/json', 'grpc-metadata-application-code': 'MERCURY', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://mercury.xtw-tech.com', 'referer': 'https://mercury.xtw-tech.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } data ={'tenantId': "1", 'listOption': {'page': '1', 'pageSize': '100', 'keyword': "",'fieldFilter':[{'fieldToFilter': "no_virtual_location", 'keyword': "1"}, {'fieldToFilter': "only_no_lot", 'keyword': "0"},{'fieldToFilter': "sp_code", 'keyword': f"{code}"}, {'fieldToFilter': "inventory_scrap_type", 'keyword': "normal"},{'fieldToFilter': "is_show_invalid", 'keyword': "1"}, {'fieldToFilter': "no_zero_inventory", 'keyword': "1"}, {'fieldToFilter': "warehouse_ids", 'keyword': "137"}],'sortBy': {'fieldToSort': "created_at", 'direction': "DESCENDING"}}} req_inventory = session_wms.post(json=data,url=inventory,headers=header) items = req_inventory.json()['items'] # sp_quantity ={'良品臨期':0,'良品過期':0,'良品正常效期':0} sp_quantity ={} for i in items: owner_id_1 = i['owner']['id'] owner_name_1 = i['owner']['name'] quantity = i['quantity'] lotEnabled = i['lotEnabled'] if lotEnabled: locationType= i['location']['locationType'] # sp_quantity['sieveName'] =locationType else: locationType ='LOCATION_TYPE_NORMAL' # sp 貨主 數量 庫存型別 # sp_quantity['sp']=code # sp_quantity['owner_id'] =owner_id_1 # sp_quantity['owner_name'] =owner_name_1 # sp_quantity['quantity'] =quantity # sp_quantity['sieveName'] =logo[locationType] # print(str(code+owner_name_1+logo[locationType]),sp_quantity.keys()) if str(code+owner_name_1+logo[locationType]) in sp_quantity.keys(): sp_quantity[f'{code+owner_name_1+logo[locationType]}'] +=int(quantity) else: sp_quantity[f'{code+owner_name_1+logo[locationType]}']=int(quantity) # 這裡 應該判斷相同庫存型別: # 需要考慮一張訂單有多個sp需要校驗庫存 if str(code+owner_name+sieve) in sp_quantity.keys(): if int(sp_quantity[f'{code+owner_name_1+logo[locationType]}']) >=int(k[f'{code+owner_id+owner_name+sieve}']): sp_num =0 print('該sp庫存足夠') else: sp_num+=1 print('部分庫存可供貨,可以考慮轉單') info = (warehouse, type, serialNumber, status, code, sp_name,owner_id,owner_name, sp_id,sieve, int(expectedQuantity), name, poiAddress, relatedSerialNumber, ordertype, k[f'{code+owner_id+owner_name+sieve}'] ,int(sp_quantity[f'{code+owner_name_1+logo[locationType]}']) ) ws.append(info) else: sp_num+=1 print("對應0庫存,可以考慮轉單") if sp_num>=1: # 進入venus菜單系統 order_list = 'https://matrix-api.xtw-tech.com/v1/matrix/shop/query_conditions/list' header ={ 'authority': 'matrix-api.xtw-tech.com', 'method': 'GET', 'path': '/v1/matrix/shop/query_conditions/list', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'grpc-metadata-application-code': 'VENUS', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://venus.xtw-tech.com', 'referer': 'https://venus.xtw-tech.com/', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': "Windows", 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-site', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } id =relatedSerial order =f'https://fz.baiou-tech.com:8080/manager/orders?platform_order_id={id}&page=1&page_size=10' header={ 'authority': 'fz.baiou-tech.com:8080', 'method': 'GET', 'path': f'/manager/orders?platform_order_id={id}&page=1&page_size=10', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'grpc-metadata-application-code': 'VENUS', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://venus.xtw-tech.com', 'referer': 'https://venus.xtw-tech.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } response = session_wms.get(url=order,headers=header) order_id = response.json()['data']['data'][0]['id'] warehouse =response.json()['data']['data'][0]['warehouse_name'] # 發起轉單 transfer = 'https://fz.baiou-tech.com:8080/manager/order/transfer_order' data = {'order_id': f'{order_id}', 'to_warehouse_id': '727'} header={ 'authority': 'fz.baiou-tech.com:8080', 'method': 'POST', 'path': '/manager/order/transfer_order', 'scheme': 'https', 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'content-length': '42', 'content-type': 'application/json', 'grpc-metadata-application-code': 'VENUS', 'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}', 'origin': 'https://venus.xtw-tech.com', 'referer': 'https://venus.xtw-tech.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', } trans = session_wms.post(url=transfer,headers=header,json=data) if trans.status_code == '200': print('轉單成功') else: pass else: pass wb.save('/Users/xujingfei/Desktop/xtw.xlsx') # -----------------------------------------------------------------------------------------------------------------