到货单自动审核部分

import time
import requests
from apscheduler.schedulers.blocking import BlockingScheduler
from warnings import filterwarnings
from pytz_deprecation_shim import PytzUsageWarning
filterwarnings('ignore', category=PytzUsageWarning)
from dingtalkchatbot.chatbot import DingtalkChatbot
import re

a = ()

webhook = 'https://oapi.dingtalk.com/robot/send?access_token=b66aaa7dd354abf4e427af958bb38edc791eed318fd9c3132e3021606e477d55'
tel = {'廊坊北京中心仓': ['18533783296'], '上海中心仓': ['13916513262'], '成都中心仓': ['19830476773'], '深圳中心仓': ['13164735481']}
id_audit = {}
bot = DingtalkChatbot(webhook)

K = 'https://docs.qq.com/sheet/DY0lFYVhRSHN0TkVO?tab=BB08J2'

def robot(fromWarehouse, warehouse, total, to, remark_asn, url, i_defective):
bot.send_markdown(title=f'收货仓:{warehouse} {to}',
text=f"### 发货仓:{fromWarehouse} \n {warehouse} 异常金额:{total}元 异常SP数:{i_defective} \n> ### {to} \n> 到货异常信息反馈--腾讯文档 \n> \n> 前置仓验收备注:{remark_asn} \n>",
at_mobiles=tel[f'{fromWarehouse}'])

创建session对象,第一次登陆成功后将自动存储cookie

session = requests.Session()

这个url是登陆入口

login_url = "https://bi.xtw-tech.com/auth/login?redirect=%2F"
headers = {
'authority': 'bi.xtw-tech.com',
'method': 'GET',
'path': '/auth/login?redirect=%2F',
'scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "Windows",
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
}
response = session.get(url=login_url, headers=headers)

url = 'https://bi.xtw-tech.com/api/session'

请求头文件Request Headers

headers = {
'authority': 'bi.xtw-tech.com',
'method': 'POST',
'path': '/api/session',
'scheme': 'https',
'accept': 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'content-length': '75',
'content-type': 'application/json',
# 'cookie': '_ga=GA1.2.997697532.1637155627; _gid=GA1.2.1584931448.1637155627; _gat=1',
'origin': 'https://bi.xtw-tech.com',
'referer': 'https://bi.xtw-tech.com/auth/login?redirect=%2F',
'sec-ch-ua-platform': "macos",
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
}

登陆信息

data = {

}

response = session.post(url=url, headers=headers, json=data)

def test():
ur = 'https://bi.xtw-tech.com/question/315'
headers = {
'authority': 'bi.xtw-tech.com',
'method': 'GET',
'path': '/api/card/315',
'scheme': 'https',
'accept': 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'content-type': 'application/json',
# 'cookie': '_ga=GA1.2.997697532.1637155627; _gid=GA1.2.1584931448.1637155627; metabase.SESSION=61b2b4a0-157b-4901-bc69-f040ed70052d; _gat=1',
'referer': 'https://bi.xtw-tech.com/question/315',
'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "Windows",
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
}

response = session.get(url=ur, headers=headers)

Url_current = 'https://bi.xtw-tech.com/api/user/current'
headers = {
    'authority': 'bi.xtw-tech.com',
    'method': 'GET',
    'path': '/api/user/current',
    'scheme': 'https',
    'accept': 'application/json',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-type': 'application/json',
    'referer': 'https://bi.xtw-tech.com/auth/login?redirect=%2F',
    'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
}
response = session.get(url=Url_current, headers=headers)

url_data = 'https://bi.xtw-tech.com/api/database'
headers = {
    'authority': 'bi.xtw-tech.com',
    'method': 'GET',
    'path': '/api/database',
    'scheme': 'https',
    'accept': 'application/json',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-type': 'application/json',
    'if-modified-since': 'Sat, 7 May 2022 10:24:23 GMT',
    'referer': 'https://bi.xtw-tech.com/question/491',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': "macOS",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36',
}
response = session.get(url=url_data, headers=headers)

url = 'https://bi.xtw-tech.com/api/card/315/query'
headers = {
    'authority': 'bi.xtw-tech.com',
    'method': 'POST',
    'path': '/api/card/315/query',
    'scheme': 'https',
    'accept': 'application/json',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-length': '38',
    'content-type': 'application/json',
    # 'cookie': '_ga=GA1.2.997697532.1637155627; _gid=GA1.2.1584931448.1637155627; metabase.SESSION=61b2b4a0-157b-4901-bc69-f040ed70052d; _gat=1',
    'origin': 'https://bi.xtw-tech.com',
    'referer': 'https://bi.xtw-tech.com/question/315',
    'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
}
data = {
    'ignore_cache': False,  # xujingfei
    'parameters': [{'type': "category", 'target': ["variable", ["template-tag", "username"]], 'value': "xujingfei"}]
}
response = session.post(url=url, json=data, headers=headers)

session_wms = requests.Session()
# 这个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 = {

}

response_wms = session_wms.post(url=login_url, headers=headers, json=data)
item = response.json()['data']['rows']

for i in item:
    # for i in range(1):
    url = i[5]
    # url = 'https://mercury.xtw-tech.com/asn/653280?tenant_id=15'
    # 加入到货异常异常详细信息的逻辑
    # 登陆信天翁wms系统
    asn_id = re.findall(r'asn/(.+?)\?', url)[0]
    tenant_id = re.findall(r'tenant_id=(.+)\d*', url)[0]
    id_audit[str(asn_id)] = str(tenant_id)

    # 对于需要审核的asn——url进行发起服务器请求。
    asn_url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}?tenant_id={tenant_id}'
    headers = {
        'authority': 'titan-api.xtw-tech.com',
        'method': 'GET',
        'path': f'/v1/asn/by_id/{asn_id}?tenant_id={tenant_id}',
        'scheme': 'https',
        'accept': 'application/json, text/plain, */*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        '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/',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': "macOS",
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
    }
    i_wait =0
    response_asn = session_wms.get(headers=headers, url=asn_url)
    respon = response_asn.json()['asnWithDetails']
    remark = respon['inspectionReportList'][0]
    remark_asn = remark['remark']
    fromWarehouse = respon['asn']['fromWarehouse']['name']
    warehouse = respon['asn']['warehouse']['name']
    to = response_asn.json()['asnWithDetails']['asn']['relatedSerialNumber']
    i_defective = 0
    total = 0
    sp_total = 0

    for i in respon['inventories']:
        sp = i['standardProductTenantMapping']['standardProduct']['code']
        sp_price = i['unitPriceBeforeDiscount']
        name = i['standardProductTenantMapping']['standardProduct']['name']
        receivedDefectiveQuantity = 0
        moreQuantity = 0
        waitingQuantity = 0
        if int(i['expectedQuantity'])-int(i['actualQuantity'])<0:
            moreQuantity = -int(i['expectedQuantity'])+int(i['actualQuantity'])
            moreQuantity_total = float(sp_price) * moreQuantity
        else:
            moreQuantity =0
            moreQuantity_total = 0
        receivedDefectiveQuantity_total = 0
        wait_total = 0
        id = i['id']

        for k in respon['inventoryPutawayQuantity']:
            if k['inventoryRelationId'] == id:
                receivedDefectiveQuantity = k['scrapQuantity']
                receivedDefectiveQuantity_total = float(sp_price) * int(receivedDefectiveQuantity)
                waitingQuantity= int(k['expectedQuantity']) -int(k['normalQuantity'])-int(receivedDefectiveQuantity)
                if waitingQuantity <=0:
                    pass
                else:
                    waitingQuantity = waitingQuantity
                    wait_total = float(sp_price) * waitingQuantity
            else:
                continue

        if int(receivedDefectiveQuantity) > 0 or int(waitingQuantity) > 0 or moreQuantity > 0:
            if int(waitingQuantity)>=1:
                i_wait =i_wait+1
            i_defective += 1
            sp_total = receivedDefectiveQuantity_total + moreQuantity_total + wait_total
            total =total+sp_total
            webhook_tencent = 'https://api.ssp.cloud.tencent.com/engine/webhook/31/1562303392210083842'
            dic = {'发货仓': f'{fromWarehouse}', '调拨单号': f'{to}', '收货仓': f'{warehouse}', '货值': f'{sp_total}',
                   '品名': f'{name}',
                   'SP': f'{sp}', '少货': f'{waitingQuantity}', '破损': f'{receivedDefectiveQuantity}',
                   '多货': f'{moreQuantity}'}
            requests.post(url=webhook_tencent, json=dic)
            time.sleep(2)

    total = round(total, 2)
    robot(fromWarehouse, warehouse, total, to, remark_asn, url, i_defective)



    # 加入 少货sp大于6个的时候自动创建补单



    # 自动审核到货单
    try:
        for k, v in id_audit.items():
            url_pass = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{k}/inspection/report/audit'
            header = {
                'authority': 'titan-api.xtw-tech.com',
                'method': 'POST',
                'path': f'/v1/asn/by_id/{k}/inspection/report/audit',
                'scheme': 'https',
                'accept': 'application/json, text/plain, */*',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.9',
                'content-length': '124',
                'content-type': 'application/json',
                'grpc-metadata-application-code': 'TITAN',
                'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
                'origin': 'https://mercury.xtw-tech.com',
                'referer': 'https://mercury.xtw-tech.com/',
                'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
                '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/95.0.4638.69 Safari/537.36",
            }
            data = {
                'asnId': f"{k}",
                'auditResult': "AUDIT_ADVANCE_SHIPPING_NOTICE_INSPECTION_REPORT_RESULT_PASS",
                'remark': "ok 审核通过",
                'tenantId': f"{v}",
            }
            response = session_wms.post(url=url_pass, headers=header, json=data)
    except:
        url_sevsr1 = f'https://sctapi.ftqq.com/SCT44097TjePCj6jaQ7q4d2ahjT6iADXt.send?title=异常{asn_url}&channel=9'
        requests.post(url_sevsr1)

    time.sleep(10)
    if i_wait >= 6:
        url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/putaway/make_up'
        header = {
            'authority': 'titan-api.xtw-tech.com',
            'method': 'POST',
            'path': f'/v1/asn/by_id/640994/{asn_id}/make_up',
            'scheme': 'https',
            'accept': 'application/json, text/plain, */*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'content-length': '18',
            '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': tenant_id}
        session_wms.post(url=url, headers=header, json=data)

scheduler = BlockingScheduler()
scheduler.add_job(test, 'interval', seconds=300, max_instances=3)
scheduler.start()

posted @ 2022-11-03 11:52  烦恼1234  阅读(46)  评论(0编辑  收藏  举报