# xtw 库存调整
import time
import numpy as np
import requests
import pandas as pd
import arrow
def time_to_utc(localtime):
utctime =arrow.get(localtime).to('UTC')
utc_time_format = utctime.strftime('%Y-%m-%dT%H:%M:%SZ')
return utc_time_format
def time_trans(local):
l = []
for value in local.values:
if pd.isna(value):
k =2
l.append(k)
# 关于 numpy.datetime64 转换为 datetime https://www.codenong.com/49204453/
else:
value =np.datetime64(value)
value =pd.to_datetime(value)
value =value.date()
time =arrow.get(value).to('local')
time_format = time.strftime('%Y-%m-%d')
l.append(time_format)
return l
session_wms = requests.Session()
info =()
# 这个url是登陆入口
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://mercury.xtw-tech.com',
# 'referer': 'https://mercury.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':
'password':
}
# 登录wms
response_wms = session_wms.post(url=login_url, headers=headers, json=data)
# 进行移库
data = pd.read_excel('/Users/xujingfei/Desktop/收货信息/工作簿8.xlsx',engine='openpyxl',sheet_name='Sheet16')
data = data.loc[:,['sp' ,'良品数量' ,'生产日期','库位id','库存id']]
data['date'] = np.where(data['生产日期'].isna(),2,time_trans(data['生产日期']))
# data[data['生产日期']!='2'].assign(date=lambda x:time_trans(x['生产日期']),inplace=False)
#
def wjj(ean_code,id):
li = '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': '471',
'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': "344", 'listOption': {'page': '1', 'pageSize': '100', 'keyword': "",
'sortBy': {'fieldToSort': "quantity", 'direction': "ASCENDING"},
'fieldFilter': [{'fieldToFilter': "no_virtual_location", 'keyword': "1"},
{'fieldToFilter': "only_no_lot", 'keyword': "0"},
{'fieldToFilter': "sp_code", 'keyword': f"{ean_code}"},
{'fieldToFilter': "warehouse_ids", 'keyword': "741"},
{'fieldToFilter': "inventory_scrap_type",
'keyword': "normal"},
{'fieldToFilter': "inventory_lot_status", 'keyword': "valid"},
{'fieldToFilter': "is_show_invalid", 'keyword': "0"},
{'fieldToFilter': "no_zero_inventory", 'keyword': "1"}],
'keyword': ""}}
invent_respon =session_wms.post(url=li,headers=header,json=data)
item = invent_respon.json()['items']
# 获取 生产日期批次
snapshotQuantity = 0
sp_inventory_id = 0
production = ''
for i in item:
try:
production =i['lot']['productionTime']
production = time_to_utc(production)
sp_inventory_id = i['id']
standardProductId = i['standardProductTenantMapping']['standardProduct']['id']
ownerid = i['owner']['id']
if i['location']['id'] in ['261312','278271'] and str(sp_inventory_id) == str(id) :
currentQuantity = i['quantity']
snapshotQuantity = int(currentQuantity )
break
except:
pass
# sp_inventory_id = i['id']
# production =i['lot']['productionTime']
# production = time_to_utc(production)
# standardProductId = i['standardProductTenantMapping']['standardProduct']['id']
# ownerid = i['owner']['id']
# if i['location']['id'] in ['261312','278271'] and str(sp_inventory_id) == str(id):
# currentQuantity = i['quantity']
# snapshotQuantity = int(currentQuantity )
# break
return [snapshotQuantity,sp_inventory_id,production, ownerid ,standardProductId]
def lot(owner_id,productionTime,standardProductId):
lot_url = 'https://titan-api.xtw-tech.com/v1/lot/create'
header = {
'authority': 'titan-api.xtw-tech.com',
'method': 'POST',
'path': '/v1/lot/create',
'scheme': 'https',
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, br',
'accept-languagev': 'zh-CN,zh;q=0.9',
'content-length': '125',
'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',
}
pa = {'ownerId': f"{owner_id}", 'productionTime': f"{productionTime}",
'standardProductId': f"{standardProductId}", 'tenantId': "344",
'warehouseId': "741"}
mm = session_wms.post(url=lot_url, headers=header, json=pa)
lotId = mm.json()['lot']['id']
status = mm.json()['lot']['status']
return [lotId,status]
# 先进行效期批次修改
# 如果日期相同只能进行移库
# 否则可以直接进行效期调整 需要判断效期有效性。
for i in range(len(data.index)):
ean_code = data.iloc[i,:]['sp']
inventory_sp = data.iloc[i,:]['库存id']
locationId = data.iloc[i,:].at['库位id']
quantity = data.iloc[i,:].at['良品数量']
producttime1 = data.iloc[i,:].at['date']
result = wjj(ean_code,id=inventory_sp) # 传入sp、生产日期、库存id [snapshotQuantity,sp_inventory_id,production, ownerid ,standardProductId]
snapshotQuantity = result[0]
inventoryId_sp = result[1]
product = result[2]
ownerid = result[3]
standardProductId = result[4]
if str(product) == str(producttime1):
url = f'https://titan-api.xtw-tech.com/v1/inventory/by_id/{inventoryId_sp}/initialize_location'
header ={
'authority': 'titan-api.xtw-tech.com',
'method': 'POST',
'path': f'/v1/inventory/by_id/{inventoryId_sp}/initialize_location',
'scheme': 'https',
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'content-length': '76',
'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',
}
# 需要完善 locationId 是库位id quantity snapshotQuantity
da = {'locationId': f'{locationId}', 'quantity': f"{quantity}", 'snapshotQuantity': f"{snapshotQuantity}", 'tenantId': "344"}
kk = session_wms.post(url=url,json=da,headers=header)
me = ean_code,kk.json()
try:
if me[1]['code']!="":
print(me[0],snapshotQuantity,me[1]['code'],'报错')
except:
pass
else:
producttime = time_to_utc(producttime1)
LotId = lot(owner_id=ownerid,productionTime=producttime,standardProductId=standardProductId)
status = LotId[1]
url = f'https://titan-api.xtw-tech.com/v1/inventory/by_id/{inventory_sp}/update_initialize_lot'
header ={
'authority': 'titan-api.xtw-tech.com',
'method': 'POST',
'path': f'/v1/inventory/by_id/{inventory_sp}/update_initialize_lot',
'scheme': 'https',
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'content-length': '92',
'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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
}
if status in ['LOT_STATUS_CLOSE_TO_EXPIRY']:
da = {'snapshotQuantity': f"{snapshotQuantity}", 'items': [{'LotId': f"{LotId[0]}", 'quantity': f"{quantity}", 'locationId': '261315'}]}
kk = session_wms.post(url=url,headers=header,json=da)
if kk.status_code!=200:
print(ean_code,producttime1)
elif kk.status_code==200:
print(ean_code,locationId,producttime1)
elif status in ['LOT_STATUS_EXPIRED']:
da = {'snapshotQuantity': f"{snapshotQuantity}", 'items': [{'LotId': f"{LotId[0]}", 'quantity': f"{quantity}", 'locationId': '261316'}]}
kk = session_wms.post(url=url,headers=header,json=da)
if kk.status_code!=200:
print(ean_code,producttime1)
elif kk.status_code==200:
print(ean_code,locationId,producttime1)
else:
da = {'snapshotQuantity': f"{snapshotQuantity}", 'items': [{'LotId': f"{LotId[0]}", 'quantity': f"{quantity}", 'locationId': f"{locationId}"}]}
kk = session_wms.post(url=url,headers=header,json=da)
if kk.status_code!=200:
print(ean_code,producttime1)