R73-A2 收敛 VAT OCR 归一化与质量门槛

这个提交包含在:
2026-04-28 21:13:21 +08:00
父节点 82fbd39114
当前提交 b7c018922c
共修改 9 个文件,包含 600 行新增和 14 行删除
+46 -1
查看文件
@@ -1,6 +1,6 @@
const cron = require("node-cron");
const { runOcrForInvoice } = require("../services/ocr");
const { buildMissingRequiredOcrError, runOcrForInvoice } = require("../services/ocr");
const { readFileBuffer } = require("../services/storage");
const OCR_DUPLICATE_ERROR = "duplicate_invoice_code_number";
@@ -201,6 +201,30 @@ async function markInvoiceAsAvailable(prisma, invoiceId, normalized, ocrResultId
});
}
async function markInvoiceAsQualityFailed(prisma, invoiceId, normalized, ocrResultId, errorMessage) {
const now = new Date();
await prisma.invoicePool.update({
where: { id: invoiceId },
data: {
status: "ocr_failed",
ocr_result_id: ocrResultId,
invoice_code: normalized.invoiceCode ?? null,
invoice_number: normalized.invoiceNumber ?? null,
invoice_date: normalized.invoiceDate ? new Date(toDateOnly(normalized.invoiceDate)) : null,
amount: toMoney(normalized.amount),
tax_amount: toMoney(normalized.taxAmount),
total_amount: toMoney(normalized.totalAmount),
seller_name: normalized.sellerName ?? null,
buyer_name: normalized.buyerName ?? null,
ocr_last_error: errorMessage,
ocr_completed_at: now,
ocr_attempts: { increment: 1 },
ocr_locked_at: null,
ocr_lock_owner: null,
},
});
}
async function markInvoiceAsFailed(prisma, claimedInvoice, errorMessage, config) {
const now = new Date();
const nextAttempts = Number(claimedInvoice.ocr_attempts || 0) + 1;
@@ -234,6 +258,26 @@ async function processClaimedInvoice(prisma, config, claimedInvoice, workerId, l
imageBase64,
);
const qualityError = buildMissingRequiredOcrError(claimedInvoice.ocr_type, normalized);
if (qualityError) {
await markInvoiceAsQualityFailed(
prisma,
claimedInvoice.id,
normalized,
ocrResultId,
qualityError,
);
logger.warn(
{
invoiceId: claimedInvoice.id,
ocrType: claimedInvoice.ocr_type,
qualityError,
},
"OCR invoice marked as failed for missing required fields",
);
return { outcome: "failed" };
}
const duplicate = await findDuplicateInvoice(
prisma,
claimedInvoice.id,
@@ -363,6 +407,7 @@ function startOcrJob(fastify) {
module.exports = {
OCR_DUPLICATE_ERROR,
claimPendingInvoices,
markInvoiceAsQualityFailed,
runOcrOnce,
startOcrJob,
};
+30 -4
查看文件
@@ -9,7 +9,7 @@ const { compactDateString } = require("../utils/date");
const { sha256Hex } = require("../utils/crypto");
const { toMoneyString } = require("../utils/workflow");
const { findActiveDelegationForDelegate } = require("../utils/delegation");
const { isOcrEnabledForType } = require("../services/ocr");
const { getMissingRequiredOcrFields, isOcrEnabledForType } = require("../services/ocr");
const { registerInvoiceVerificationRoutes } = require("./invoicePool/verification");
const { registerInvoicePoolReadRoutes } = require("./invoicePool/read");
const { normalizeInvoiceResponse } = require("./invoicePool/shared/mappers");
@@ -46,7 +46,7 @@ const invoiceEditBodySchema = z
invoiceNumber: z.string().trim().min(1).max(64).nullable().optional(),
invoiceDate: z
.string()
.regex(/^\\d{4}-\\d{2}-\\d{2}$/)
.regex(/^\d{4}-\d{2}-\d{2}$/)
.nullable()
.optional(),
sellerName: z.string().trim().min(1).max(256).nullable().optional(),
@@ -163,7 +163,7 @@ function normalizeMoneyInput(value) {
return null;
}
if (!/^\\d+(?:\\.\\d{1,2})?$/.test(input)) {
if (!/^\d+(?:\.\d{1,2})?$/.test(input)) {
return undefined;
}
@@ -186,7 +186,7 @@ function parseDateOnlyString(value) {
return null;
}
if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(trimmed)) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
return undefined;
}
@@ -576,6 +576,32 @@ async function invoicePoolRoutes(fastify) {
if (input.status) {
if (input.status === "available" && current.status === "ocr_failed") {
const nextSnapshot = {
invoiceCode: "invoiceCode" in input ? input.invoiceCode : current.invoice_code,
invoiceNumber: "invoiceNumber" in input ? input.invoiceNumber : current.invoice_number,
invoiceDate:
"invoiceDate" in input
? input.invoiceDate
: current.invoice_date?.toISOString?.()?.slice(0, 10) ?? null,
totalAmount:
"totalAmount" in input
? normalizeMoneyInput(input.totalAmount)
: current.total_amount?.toString?.() ?? null,
sellerName: "sellerName" in input ? input.sellerName : current.seller_name,
};
const missingFields = getMissingRequiredOcrFields(current.ocr_type, nextSnapshot);
if (missingFields.length) {
throw new AppError({
code: "VALID_001",
message: "参数校验失败",
statusCode: 409,
details: {
ocr: "missing_required_fields",
missingFields,
},
});
}
updateData.status = "available";
updateData.ocr_last_error = null;
updateData.ocr_completed_at = new Date();
+27
查看文件
@@ -1,6 +1,10 @@
const { runTencentVatOcr } = require("./tencent");
const { runMockOcr } = require("./mock");
const REQUIRED_OCR_FIELDS_BY_TYPE = {
vat: ["invoiceCode", "invoiceNumber", "invoiceDate", "totalAmount", "sellerName"],
};
function isOcrEnabledForType(config, ocrType) {
if (config.ocrProvider === "mock") {
return ocrType === "vat";
@@ -9,6 +13,27 @@ function isOcrEnabledForType(config, ocrType) {
return config.ocrProvider === "tencent" && ocrType === "vat";
}
function getMissingRequiredOcrFields(ocrType, normalized) {
const requiredFields = REQUIRED_OCR_FIELDS_BY_TYPE[ocrType] ?? [];
if (!requiredFields.length) {
return [];
}
return requiredFields.filter((field) => {
const value = normalized?.[field];
return value == null || value === "";
});
}
function buildMissingRequiredOcrError(ocrType, normalized) {
const missingFields = getMissingRequiredOcrFields(ocrType, normalized);
if (!missingFields.length) {
return null;
}
return `missing_required_fields:${missingFields.join(",")}`;
}
async function runOcrForInvoice({ config, ocrType, imageBase64 }) {
if (config.ocrProvider === "mock") {
return runMockOcr({ ocrType, imageBase64 });
@@ -26,6 +51,8 @@ async function runOcrForInvoice({ config, ocrType, imageBase64 }) {
}
module.exports = {
buildMissingRequiredOcrError,
getMissingRequiredOcrFields,
isOcrEnabledForType,
runOcrForInvoice,
};
+21 -1
查看文件
@@ -18,6 +18,18 @@ function buildInvoiceKey(markerText) {
return `hash:${hash.slice(0, 20)}`;
}
function parseMissingFields(markerText) {
const match = markerText.match(/MOCK_OCR_MISSING:([A-Za-z0-9_,-]+)/);
if (!match?.[1]) {
return [];
}
return match[1]
.split(",")
.map((field) => field.trim())
.filter(Boolean);
}
function toAmount(seedHex, fallback) {
const value = Number.parseInt(seedHex.slice(0, 8), 16);
if (!Number.isFinite(value)) {
@@ -38,7 +50,7 @@ async function runMockOcr({ ocrType, imageBase64 }) {
const invoiceCode = `MOCK${hash.slice(0, 8).toUpperCase()}`;
const invoiceNumber = hash.slice(8, 16).toUpperCase();
return {
const result = {
invoiceCode,
invoiceNumber,
invoiceDate: "2026-02-08",
@@ -53,6 +65,14 @@ async function runMockOcr({ ocrType, imageBase64 }) {
marker: markerText.slice(0, 128),
},
};
for (const field of parseMissingFields(markerText)) {
if (Object.prototype.hasOwnProperty.call(result, field)) {
result[field] = null;
}
}
return result;
}
module.exports = {
+42 -6
查看文件
@@ -46,12 +46,41 @@ function toNumberString(value) {
return num.toFixed(2);
}
function addNumberStrings(...values) {
const normalized = values.map((value) => toNumberString(value));
if (normalized.some((value) => value == null)) {
return null;
}
const total = normalized.reduce((sum, value) => sum + Number(value), 0);
return total.toFixed(2);
}
function subtractNumberStrings(minuend, subtrahend) {
const normalizedMinuend = toNumberString(minuend);
const normalizedSubtrahend = toNumberString(subtrahend);
if (normalizedMinuend == null || normalizedSubtrahend == null) {
return null;
}
return (Number(normalizedMinuend) - Number(normalizedSubtrahend)).toFixed(2);
}
function normalizeIdentifier(value) {
if (typeof value !== "string") {
return null;
}
const normalized = value.replace(/\s+/g, "").trim();
return normalized || null;
}
function normalizeString(value) {
if (typeof value !== "string") {
return null;
}
const normalized = value.trim();
const normalized = value.replace(/\s+/g, " ").trim();
return normalized || null;
}
@@ -76,13 +105,19 @@ function createTencentOcrClient(config) {
}
function normalizeVatInvoiceResult(result) {
const amount = toNumberString(result?.AmountWithoutTax);
const taxAmount = toNumberString(result?.TaxAmount);
const totalAmountFromSource = toNumberString(result?.TotalAmount ?? result?.AmountInFiguers);
const derivedAmount = amount ?? subtractNumberStrings(totalAmountFromSource, taxAmount);
const derivedTotalAmount = totalAmountFromSource ?? addNumberStrings(derivedAmount, taxAmount);
return {
invoiceCode: normalizeString(result?.InvoiceCode),
invoiceNumber: normalizeString(result?.InvoiceNum),
invoiceCode: normalizeIdentifier(result?.InvoiceCode),
invoiceNumber: normalizeIdentifier(result?.InvoiceNum),
invoiceDate: normalizeDate(result?.InvoiceDate),
amount: toNumberString(result?.AmountWithoutTax),
taxAmount: toNumberString(result?.TaxAmount),
totalAmount: toNumberString(result?.TotalAmount ?? result?.AmountInFiguers),
amount: derivedAmount,
taxAmount,
totalAmount: derivedTotalAmount,
sellerName: normalizeString(result?.SellerName),
buyerName: normalizeString(result?.BuyerName),
raw: result ?? {},
@@ -99,5 +134,6 @@ async function runTencentVatOcr(config, imageBase64) {
}
module.exports = {
normalizeVatInvoiceResult,
runTencentVatOcr,
};