R73-A2 收敛 VAT OCR 归一化与质量门槛
这个提交包含在:
+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,
|
||||
};
|
||||
|
||||
在新工单中引用
屏蔽一个用户