发票信息查询

  初步构想通过调用 国家税务总局增值税发票查验平台https://inv-veri.chinatax.gov.cn/接口查验发票信息,但验证部分相对复杂,于是退而求其次找到一个可实现相同功能且无需验证的接口http://inv-veri.com

参考资料:国家税务总局全国增值税发票查验平台 以及 国税局发票查验API接口文档说明

需要注意:单张发票每日上限查询5次,为方便调试使用jupyter进行编译

本次已实现的内容为:利用python获取接口数据进行初步整理,但由于时间仓促且手头没有发票测试,代码倍显简陋

import requests
import json

fpdm = '011001900311'  # 发票代码
fphm = '26540678' # 发票号码
date = '20190708' # 发票时间
code = '316342' # 校验码(后六位) or 开具金额

url = "http://inv-veri.com/check?fpdm={0}&fphm={1}&date={2}&code={3}&channel=yd".format(fpdm,fphm,date,code)

res = requests.get(url)
# 得到json数据
content = json.loads(res.content.decode())

if content['code'] == 0:
    print(content['message'])
    data = content['data']
else:
    print("查验失败!{0}".format(content['message']))

# 之所以这样写是因为不确定其他发票的部分键中会有_dzfp的后缀
if 'dzfp' in list(data.keys())[0]:
    print('--增值税电子普通发票--')
    for i in data.keys():
        if 'fpdm' in i:
            print('发票代码:{0}'.format(data[i]))
        elif 'fphm' in i:
            print('发票号码:{0}'.format(data[i]))
        elif 'jym' in i:
            print('校验码:{0},(后六位:{1})'.format(data[i],data[i][-6:]))
        elif 'fpcc' in i:
            print('发票名称:{0}'.format(data[i]))
        elif 'cycs' in i:
            print('在线查询次数:{0}'.format(data[i]))
        elif 'kprq' in i:
            print('【开票日期:{0}'.format(data[i]))
        elif 'xfmc' in i:
            print('【销方名称:{0}】'.format(data[i]))
        elif 'xfsbh' in i:
            print('销方??号:{0}'.format(data[i]))
        elif 'xfdzdh' in i:
            print('销方地址电话:{0}'.format(data[i]))
        elif 'xfyhzh' in i:
            print('销方银行账号:{0}'.format(data[i]))
        elif 'gfmc' in i:
            print('【购方名称:{0}】'.format(data[i]))
        elif 'gfsbh' in i:
            print('购方??号:{0}'.format(data[i]))
        elif 'gfdzdh' in i:
            print('购方地址电话:{0}'.format(data[i]))
        elif 'gfyhzh' in i:
            print('购方银行账号:{0}'.format(data[i]))
        elif 'je' in i:
            print('【金额:{0}】'.format(data[i]))
        elif 'se' in i:
            print('【税额:{0}】'.format(data[i]))
        elif 'jshj' in i:
            print('【价税合计:{0}】'.format(data[i]))
        elif 'bz' == i:
            print('备注:{0}'.format(data[i]))
        elif 'bz' in i and len(data[i])>1:
            print('【相关信息:{0}】'.format(data[i]))
        elif 'sbbh' in i:
            print('??编号:{0}'.format(data[i]))
            
        if 'zfbz' == i:
            if data[i] == '0':
                print('【发票状态:未作废】')
            elif data[i] == '1':
                print('【发票状态:已作废】')
            elif data[i] == '3':
                print('【发票状态:红冲】')
            elif data[i] == '8':
                print('【发票状态:全额红冲】')
            else:
                print("【作废标志:{0}】".format(i))
        if 'pdfDownNow' == i:
            if data[i] == False:
                print('该发票没有版式文件')
            else:
                print(data[i])

print('--购置信息--')
try:
    summarys = data['summarys'] 
    summary0 = data['summary'][0]
#     summary1 = data['summary'][1]
    print("商品编码:{0}".format(summary0['flbm']))
    amount = int(float(summary0['amount'])) # 数量:整数
    if amount > 1: # 一张发票含多个物品时将这里补为循环
        print("信息错误,目前只支持单个数据")
        pass
    print("【数量:{0}{1}】".format(amount,summary0['unit']))
    print("【名称;{0}】".format(summary0['name']))
    print("【附加信息:{0}】".format(summary0['spec']))
    print("【价格总和:{0}】_(价格单位:{1})".format(summary0['priceSum'],summary0['priceUnit']))
    print("【税率:{0}】".format(summary0['taxRate']))
    if len(re.findall('\d+',summary0['taxSum'])) != 0:
        print("【总计税额:{0}】".format(summary0['taxSum']))
    print("【减免金额:{0}】".format(summary1['priceSum']))
except:
    pass

 结果示例:

 

 代码本身没什么技术含量,绝对新手适用

 

 

posted on 2022-10-16 20:46  HOr7z  阅读(381)  评论(0编辑  收藏  举报