2025-08-17 13:24:36 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
# This file is auto-generated, don't edit it. Thanks.
|
|
|
|
|
|
import os
|
|
|
|
|
|
import statistics
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
from alibabacloud_credentials.models import Config
|
|
|
|
|
|
from alibabacloud_ocr_api20210707.client import Client as ocr_api20210707Client, Client
|
|
|
|
|
|
from alibabacloud_credentials.client import Client as CredentialClient
|
|
|
|
|
|
from alibabacloud_tea_openapi import models as open_api_models
|
|
|
|
|
|
from alibabacloud_ocr_api20210707 import models as ocr_api_20210707_models
|
|
|
|
|
|
from alibabacloud_tea_util import models as util_models
|
|
|
|
|
|
from alibabacloud_tea_console.client import Client as ConsoleClient
|
|
|
|
|
|
from alibabacloud_tea_util.client import Client as UtilClient
|
|
|
|
|
|
from alibabacloud_darabonba_stream.client import Client as StreamClient
|
|
|
|
|
|
from flask import json
|
|
|
|
|
|
|
|
|
|
|
|
from alibabacloud_sample.dataservice import DataService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Service:
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
# 发票类型字典
|
|
|
|
|
|
typeDict = {
|
|
|
|
|
|
"增值税专用发票": 1,
|
|
|
|
|
|
"增值税普通发票": 4,
|
|
|
|
|
|
"增值税普通发票(电子)": 10,
|
|
|
|
|
|
"增值税普通发票(卷式)": 11,
|
|
|
|
|
|
"增值税普通发票(通行费)": 14,
|
|
|
|
|
|
"增值税电子专用发票": 20,
|
|
|
|
|
|
"数电发票(增值税专用发票)": 31,
|
|
|
|
|
|
"数电发票(增值税普通发票)": 32,
|
|
|
|
|
|
"数电发票(纸质专用发票)": 85,
|
|
|
|
|
|
"数电发票(纸质普通发票)": 86,
|
|
|
|
|
|
}
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def create_client() -> ocr_api20210707Client:
|
|
|
|
|
|
"""
|
|
|
|
|
|
使用凭据初始化账号Client
|
|
|
|
|
|
@return: Client
|
|
|
|
|
|
@throws Exception
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378659.html。
|
|
|
|
|
|
|
|
|
|
|
|
# credential = CredentialClient()
|
|
|
|
|
|
# config = open_api_models.Config(
|
|
|
|
|
|
# credential=credential
|
|
|
|
|
|
# )
|
|
|
|
|
|
config = Config(
|
|
|
|
|
|
type='access_key',
|
|
|
|
|
|
access_key_id='LTAI5tHvCWb5uHZ9Q7iUJTLp',
|
|
|
|
|
|
access_key_secret='zKAL7WqvnJ0lN0GHsvJptAGRiuCOfM'
|
|
|
|
|
|
)
|
|
|
|
|
|
config.endpoint = f'ocr-api.cn-hangzhou.aliyuncs.com'
|
|
|
|
|
|
cred = CredentialClient(config)
|
|
|
|
|
|
cred.get_credential()
|
|
|
|
|
|
nconfig = open_api_models.Config(
|
|
|
|
|
|
credential=cred
|
|
|
|
|
|
)
|
|
|
|
|
|
# Endpoint 请参考 https://api.aliyun.com/product/ocr-api
|
|
|
|
|
|
|
|
|
|
|
|
nconfig.endpoint = f'ocr-api.cn-hangzhou.aliyuncs.com'
|
|
|
|
|
|
return ocr_api20210707Client(nconfig)
|
|
|
|
|
|
# 发票验证
|
|
|
|
|
|
@staticmethod
|
2025-08-18 17:45:36 +08:00
|
|
|
|
def verify(data: dict, file_path: str,reverify: bool = False):
|
2025-08-17 13:24:36 +08:00
|
|
|
|
dataservice = DataService()
|
2025-08-18 17:45:36 +08:00
|
|
|
|
# 复验
|
|
|
|
|
|
if not reverify:
|
|
|
|
|
|
data1 = dataservice.get_verify_log(file_path=file_path)
|
2025-08-19 13:52:25 +08:00
|
|
|
|
print(data1)
|
|
|
|
|
|
if data1 is not None and int(data1.get('inspectionAmount')) > 0:
|
2025-08-18 17:45:36 +08:00
|
|
|
|
return data1
|
2025-08-17 13:24:36 +08:00
|
|
|
|
client = Service.create_client()
|
|
|
|
|
|
type = Service.typeDict.get(data.get('invoiceType'))
|
|
|
|
|
|
sum = data.get('totalAmount')
|
|
|
|
|
|
if type == 1 or type == 20:
|
|
|
|
|
|
sum = data.get('totalAmountWithoutTax')
|
|
|
|
|
|
if data.get('checkCode') is not None:
|
|
|
|
|
|
verify_code = data.get('checkCode')[-6:]
|
|
|
|
|
|
else:
|
|
|
|
|
|
verify_code = None
|
|
|
|
|
|
date = data.get('invoiceDate').replace('年', "").replace('月', "").replace('日', "")
|
|
|
|
|
|
verify_vatinvoice_request = ocr_api_20210707_models.VerifyVATInvoiceRequest(
|
|
|
|
|
|
invoice_no= data.get('invoiceNumber'),
|
|
|
|
|
|
invoice_code= data.get('invoiceCode'),
|
|
|
|
|
|
invoice_date= date,
|
|
|
|
|
|
invoice_sum= sum,
|
|
|
|
|
|
invoice_kind= type,
|
|
|
|
|
|
verify_code= verify_code,
|
|
|
|
|
|
)
|
|
|
|
|
|
runtime = util_models.RuntimeOptions()
|
|
|
|
|
|
try:
|
|
|
|
|
|
resp = client.verify_vatinvoice_with_options(verify_vatinvoice_request, runtime)
|
|
|
|
|
|
ConsoleClient.log(UtilClient.to_jsonstring(resp))
|
2025-08-19 13:52:25 +08:00
|
|
|
|
respjson = json.loads(UtilClient.to_jsonstring(resp.body.data))
|
|
|
|
|
|
code = respjson.get('code')
|
|
|
|
|
|
if code != '001':
|
|
|
|
|
|
return {'cyjgxx': respjson.get('msg'), 'inspectionAmount': -1,'status': 'fail'}
|
|
|
|
|
|
resdata = respjson.get('data')
|
2025-08-18 17:45:36 +08:00
|
|
|
|
if resdata is None:
|
|
|
|
|
|
resdata = {'cyjgxx': "未查询到发票信息", 'inspectionAmount': -1,'status': 'fail'}
|
|
|
|
|
|
else:
|
|
|
|
|
|
resdata.update({'status': 'success'})
|
2025-08-19 22:42:38 +08:00
|
|
|
|
resdata['status'] = 'success'
|
2025-08-18 17:45:36 +08:00
|
|
|
|
dataservice.insert_verify_log(resdata.get('inspectionAmount'), resdata.get('cyjgxx'), file_path)
|
2025-08-19 13:52:25 +08:00
|
|
|
|
dataservice.update_invoice_status(data.get('id'), resdata.get('status'), resdata.get('inspectionAmount'))
|
2025-08-18 17:45:36 +08:00
|
|
|
|
return resdata
|
2025-08-17 13:24:36 +08:00
|
|
|
|
except Exception as error:
|
|
|
|
|
|
# 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
|
|
|
# 错误 message
|
2025-08-19 22:42:38 +08:00
|
|
|
|
print(error)
|
2025-08-17 13:24:36 +08:00
|
|
|
|
# 诊断地址
|
|
|
|
|
|
print(error.data.get("Recommend"))
|
|
|
|
|
|
UtilClient.assert_as_string(error.message)
|
|
|
|
|
|
# 发票识别
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def recognize(file_path: str):
|
|
|
|
|
|
client = Service.create_client()
|
|
|
|
|
|
dataservice = DataService()
|
|
|
|
|
|
body_stream = StreamClient.read_from_file_path(file_path)
|
|
|
|
|
|
recognize_mixed_invoices_request = ocr_api_20210707_models.RecognizeMixedInvoicesRequest(
|
|
|
|
|
|
body=body_stream
|
|
|
|
|
|
)
|
|
|
|
|
|
runtime = util_models.RuntimeOptions()
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 复制代码运行请自行打印 API 的返回值
|
|
|
|
|
|
res = client.recognize_mixed_invoices_with_options(recognize_mixed_invoices_request, runtime)
|
|
|
|
|
|
print(UtilClient.to_jsonstring(res.body.data))
|
|
|
|
|
|
data = json.loads(UtilClient.to_jsonstring(res.body.data)).get('subMsgs')[0].get('result').get('data')
|
2025-08-18 17:45:36 +08:00
|
|
|
|
data_id = dataservice.insert_invoice_log(data, file_path)
|
|
|
|
|
|
data.update({'id': data_id})
|
2025-08-17 13:24:36 +08:00
|
|
|
|
return data
|
|
|
|
|
|
except Exception as error:
|
|
|
|
|
|
# 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
|
|
|
# 错误 message
|
2025-08-20 15:11:35 +08:00
|
|
|
|
print(error)
|
2025-08-17 13:24:36 +08:00
|
|
|
|
# 诊断地址
|
2025-08-20 15:11:35 +08:00
|
|
|
|
UtilClient.assert_as_string(error)
|
2025-08-18 17:45:36 +08:00
|
|
|
|
# 发票验证记录
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def get_verify_log_list(file_path):
|
|
|
|
|
|
dataservice = DataService()
|
|
|
|
|
|
return dataservice.get_verify_log_list(file_path)
|