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

这个提交包含在:
2026-04-28 21:13:21 +08:00
父节点 82fbd39114
当前提交 b7c018922c
共修改 9 个文件,包含 600 行新增和 14 行删除
+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,
};