乐企平台介绍
乐企平台作为国家税务总局推出的唯一数电票标准化的API接口重要平台,旨在通过税务系统与企业自有信息系统的直连涉税服务,开票员登录企业自有系统(ERP、OMS、发票管理系统等)无需通过第三方平台即可实现国税直连开票,真正实现了自动化,从而加速企业的税务数字化转型,实现了业财税融合。
接口说明
- 查询本地未使用数电票号码段,判断是否低于设定的阈值(例如:低于10%,或者每天凌晨判断是否低于下一个工作日的最大开票量等),则需要调用税务局端接口重新获取数电票批量预赋码信息。
- 数电票号码前2位代表使用年份,禁止跨年度使用。例如:“24”开头的发票号码为2022年度的发票号码,只能在2024年使用,开票日期必须为2024年。企业在2024年开具的数电票,可以在跨年后继续上传,跨年后企业需要重新下载新一年度的数电票号码开具数电票。
直接上代码
#!/usr/bin/env python
# encoding: utf-8
# 获取预赋码写入ERP数据库import uuid
import cjc_cfg as cfg
from cjc_lq_request_util import LQRequestUtil
from cjc_oracle_client import OracleDBClient# 定义数据库连接(使用默认连接)
db = OracleDBClient()
db.connect()# 构建工具类实例
requester = LQRequestUtil(base_url=cfg.url_front,headers=cfg.hjheaders,key=cfg.hjlqkey
)# 查询数据库中剩余发票号是否在阈值以下
selectsql0 = """
select count(*) as SYFPS from cjc_lq_yfm_dtl
where usestatus = 0
"""
print(selectsql0)
result = db.execute_query(selectsql0)# 阈值自定义设置, 测试用10张发票以内时下载预赋码
if result[0]["SYFPS"] < 10:# 用UUID生成业务流水号后缀batch_id = str(uuid.uuid4()).replace('-', '')print(batch_id)insertsql0 = f"""insert into cjc_lq_yfm_doc(pre_code_batch_id) values('{batch_id}')"""print(insertsql0)db.execute_non_query(insertsql0) # 定义请求数据request_data = {"nsrsbh": cfg.hjxfxx["taxno"],"lysl": 10,"ywlsh": cfg.hjheaders["jrdwptbh"] + cfg.hjheaders["jrdwptbh"] + batch_id , }# 调用接口try:result = requester.post(cfg.url_after["yfm"], request_data, verify_ssl=False)if result.get("Data") is not None:if result["Data"]["returncode"] == '00':print("解密结果:", result)start_invoice_no = int(result["Data"]["fpqshm"])end_invoice_no = result["Data"]["fpzzhm"]quantity = result["Data"]["lysl"]#写入数据库表头updatesql0 = f""" update cjc_lq_yfm_doc set get_date = sysdate,start_invoice_no = '{start_invoice_no}',end_invoice_no = '{end_invoice_no}',quantity = {quantity}where pre_code_batch_id = '{batch_id}' """print(updatesql0)db.execute_non_query(updatesql0) # 循环每个发票号写入明细表for i in range(quantity):insertsql1 = f"""insert into cjc_lq_yfm_dtl(invoice_no,batch_id,usestatus) values('{start_invoice_no}','{batch_id}',0)"""print(insertsql1)db.execute_non_query(insertsql1) start_invoice_no += 1 except Exception as e:print("请求失败:", e)
#关闭数据库连接
db.disconnect()
接口返回示例
成功返回
{
"Response":{
"RequestId": "16位随机数字和字母组合",
"Data":{
"returncode": "00",
"returnmsg": "成功",
"fpqshm": "发票起始号码",
"fpzzhm": "发票终止号码",
"lysl": "领用数量"
}
}
}
失败返回
{
"Response":{
"RequestId": "16位随机数字和字母组合",
"Error":{
"Code": "01”,
"Message": "错误说明"
}
}
}