R111/A3-A6 强化发票查重和状态治理
这个提交包含在:
@@ -6,6 +6,7 @@ const { buildAuditLogData } = require("../../utils/audit");
|
||||
const { signPayload, verifyToken } = require("../../utils/signedUrl");
|
||||
const { openReadStream, StorageNotFoundError } = require("../../services/storage");
|
||||
const { toMoneyString } = require("../../utils/workflow");
|
||||
const { mapInvoiceUnavailableReason } = require("../../services/invoiceGovernance");
|
||||
const {
|
||||
mapInvoiceSummary,
|
||||
normalizeInvoiceResponse,
|
||||
@@ -26,6 +27,9 @@ const bindableInvoiceQuerySchema = z.object({
|
||||
search: z.string().trim().max(100).optional(),
|
||||
limit: z.coerce.number().int().min(1).max(50).optional(),
|
||||
});
|
||||
const invoiceStatusQuerySchema = z.object({
|
||||
ids: z.string().trim().min(1).max(2000),
|
||||
});
|
||||
|
||||
function sanitizeFilename(fileName) {
|
||||
if (!fileName) return "file";
|
||||
@@ -69,6 +73,83 @@ function parseBindableInvoiceQuery(query) {
|
||||
};
|
||||
}
|
||||
|
||||
function parseInvoiceStatusQuery(query) {
|
||||
const parsed = invoiceStatusQuerySchema.safeParse(query ?? {});
|
||||
if (!parsed.success) {
|
||||
throw new AppError({
|
||||
code: "VALID_001",
|
||||
message: "参数校验失败",
|
||||
statusCode: 400,
|
||||
details: parsed.error.flatten(),
|
||||
});
|
||||
}
|
||||
|
||||
const ids = Array.from(
|
||||
new Set(
|
||||
parsed.data.ids
|
||||
.split(",")
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean),
|
||||
),
|
||||
);
|
||||
|
||||
if (!ids.length || ids.length > 50 || ids.some((id) => !z.string().uuid().safeParse(id).success)) {
|
||||
throw new AppError({
|
||||
code: "VALID_001",
|
||||
message: "参数校验失败",
|
||||
statusCode: 400,
|
||||
details: { ids: "invalid_uuid_list" },
|
||||
});
|
||||
}
|
||||
|
||||
return { ids };
|
||||
}
|
||||
|
||||
function resolveInvoiceStatusReason(invoice) {
|
||||
if (invoice.status === "pending_ocr") {
|
||||
return {
|
||||
reason: "pending_ocr",
|
||||
message: "发票正在识别,请稍后刷新",
|
||||
};
|
||||
}
|
||||
|
||||
if (invoice.status === "ocr_failed") {
|
||||
const rawError = String(invoice.ocr_last_error ?? "");
|
||||
if (rawError === "duplicate_invoice_code_number" || rawError.includes("duplicate_invoice")) {
|
||||
return {
|
||||
reason: "duplicate_invoice_code_number",
|
||||
message: mapInvoiceUnavailableReason("used_by_company") ?? "发票重复,不能添加",
|
||||
};
|
||||
}
|
||||
return {
|
||||
reason: "ocr_failed",
|
||||
message: rawError || "发票识别失败,请重新识别或手动处理",
|
||||
};
|
||||
}
|
||||
|
||||
if (invoice.status === "bound") {
|
||||
return {
|
||||
reason: "already_added",
|
||||
message: "发票已添加到报销单",
|
||||
};
|
||||
}
|
||||
|
||||
if (invoice.status === "archived") {
|
||||
return {
|
||||
reason: invoice.archived_reason === "used_complete" ? "used_by_company" : "archived",
|
||||
message:
|
||||
invoice.archived_reason === "used_complete"
|
||||
? "该发票已用于报销,不能重复添加"
|
||||
: "发票已归档,不可添加",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
reason: "available",
|
||||
message: "发票可添加",
|
||||
};
|
||||
}
|
||||
|
||||
function ensureInvoiceReadableByUser(request, invoice) {
|
||||
const roleCodes = request.employee.roleCodes;
|
||||
const permissionCodes = request.employee.permissionCodes ?? [];
|
||||
@@ -126,6 +207,73 @@ async function hasPendingTaskAccess(fastify, request, linkedReimbursementId) {
|
||||
}
|
||||
|
||||
function registerInvoicePoolReadRoutes(fastify) {
|
||||
fastify.get(
|
||||
"/invoice-pool/status",
|
||||
{
|
||||
schema: { tags: ["Invoice Pool"], summary: "Get Invoice Status Batch" },
|
||||
preHandler: fastify.requirePermission("invoice.view_own"),
|
||||
},
|
||||
async (request, reply) => {
|
||||
const query = parseInvoiceStatusQuery(request.query);
|
||||
const rows = await fastify.prisma.invoicePool.findMany({
|
||||
where: { id: { in: query.ids } },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
archived_reason: true,
|
||||
owner_employee_id: true,
|
||||
uploaded_by_employee_id: true,
|
||||
linked_reimbursement_id: true,
|
||||
linked_item_id: true,
|
||||
travel_order_id: true,
|
||||
invoice_code: true,
|
||||
invoice_number: true,
|
||||
invoice_date: true,
|
||||
seller_name: true,
|
||||
total_amount: true,
|
||||
original_name: true,
|
||||
ocr_attempts: true,
|
||||
ocr_last_error: true,
|
||||
ocr_completed_at: true,
|
||||
updated_at: true,
|
||||
linked_reimbursement: {
|
||||
select: {
|
||||
owner_id: true,
|
||||
submitter_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const readableRows = rows.filter((invoice) => ensureInvoiceReadableByUser(request, invoice));
|
||||
return sendData(reply, request, {
|
||||
items: readableRows.map((invoice) => {
|
||||
const statusReason = resolveInvoiceStatusReason(invoice);
|
||||
return {
|
||||
id: invoice.id,
|
||||
status: invoice.status,
|
||||
reason: statusReason.reason,
|
||||
message: statusReason.message,
|
||||
invoiceCode: invoice.invoice_code ?? null,
|
||||
invoiceNumber: invoice.invoice_number ?? null,
|
||||
invoiceDate: invoice.invoice_date?.toISOString?.()?.slice(0, 10) ?? null,
|
||||
sellerName: invoice.seller_name ?? null,
|
||||
totalAmount: toMoneyString(invoice.total_amount),
|
||||
originalName: invoice.original_name ?? null,
|
||||
linkedReimbursementId: invoice.linked_reimbursement_id ?? null,
|
||||
linkedItemId: invoice.linked_item_id ?? null,
|
||||
linkedTravelOrderId: invoice.travel_order_id ?? null,
|
||||
ocrAttempts: invoice.ocr_attempts ?? 0,
|
||||
ocrLastError: invoice.ocr_last_error ?? null,
|
||||
ocrCompletedAt: invoice.ocr_completed_at?.toISOString?.() ?? null,
|
||||
updatedAt: invoice.updated_at?.toISOString?.() ?? null,
|
||||
};
|
||||
}),
|
||||
missingIds: query.ids.filter((id) => !readableRows.some((invoice) => invoice.id === id)),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/invoice-pool/bindable",
|
||||
{
|
||||
|
||||
在新工单中引用
屏蔽一个用户