生成电子发票重复报销检测规则
人工核对效率低,系统无统一规则,相同发票被不同员工多次报销。
有人这样说「这月又发现两张一模一样的发票被不同同事报了,真头疼。」
能替你选的都选好了。不想动,直接点下面「交给 AI 办」,点一下就开始做;想改,就点一下要的那个。
已替你选好想改就点一下
只有你知道的不填就先空着,成品里留【待填】
还想怎么改可以不选
开工前请确认
高级选项一般不用动 · 这件事通常 1 分钟左右做好
示例成品全文按上面这组示例选项真跑出来的
直接按这套做。
1. 规则说人话
同一张发票是否重复,不看单号,不看报销人,只看这三个字段:
invoice_code:发票代码,12位数字invoice_number:发票号码,8位数字check_code:校验码,20位数字或字母
三个字段完全一致,就判定为重复发票。
处理方式:
- 重复:整单驳回,写入
duplicate_alerts表,状态为“疑似重复” - 不重复:写入
invoice_records占位,状态pending - 审批通过:
pending改成approved - 审批驳回/撤回:释放
pending占位,避免员工以后不能重新提交
数据库加唯一约束,防止两个请求同时提交同一张发票时都通过。
2. JSON Schema 供 API 接入
2.1 请求体 JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InvoiceDuplicateCheckRequest",
"type": "object",
"required": ["reimbursement_id", "applicant_id", "invoices"],
"properties": {
"reimbursement_id": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"applicant_id": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"invoices": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["invoice_code", "invoice_number", "check_code"],
"properties": {
"invoice_code": {
"type": "string",
"pattern": "^[0-9]{12}$"
},
"invoice_number": {
"type": "string",
"pattern": "^[0-9]{8}$"
},
"check_code": {
"type": "string",
"pattern": "^[A-Za-z0-9]{20}$"
}
}
}
}
}
}说明:如果校验码确认只允许数字,把 check_code 的 pattern 改成 ^[0-9]{20}$。
2.2 响应体 JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InvoiceDuplicateCheckResponse",
"type": "object",
"required": ["allow_submit", "action", "duplicates"],
"properties": {
"allow_submit": {
"type": "boolean"
},
"action": {
"type": "string",
"enum": ["pass", "reject"]
},
"message": {
"type": "string"
},
"duplicates": {
"type": "array",
"items": {
"type": "object",
"required": ["invoice_code", "invoice_number", "check_code", "tag"],
"properties": {
"invoice_code": {
"type": "string"
},
"invoice_number": {
"type": "string"
},
"check_code": {
"type": "string"
},
"tag": {
"type": "string",
"enum": ["疑似重复"]
},
"existing_reimbursement_id": {
"type": "string"
},
"existing_applicant_id": {
"type": "string"
}
}
}
}
}
}3. Flask 直接可用代码
依赖:
pip install flask flask-sqlalchemy代码:
import re
from datetime import datetime
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///invoice_dup.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
def normalize(s):
return (s or "").replace(" ", "").replace("\u3000", "").strip()
def is_valid_code(code):
return bool(re.fullmatch(r"\d{12}", code))
def is_valid_number(num):
return bool(re.fullmatch(r"\d{8}", num))
def is_valid_check_code(check):
# 如果校验码确认纯数字,把 [A-Za-z0-9] 改成 [0-9]
return bool(re.fullmatch(r"[A-Za-z0-9]{20}", check))
class InvoiceRecord(db.Model):
__tablename__ = "invoice_records"
id = db.Column(db.Integer, primary_key=True)
invoice_code = db.Column(db.String(12), nullable