R111/A3-A6 强化发票查重和状态治理
这个提交包含在:
@@ -25,6 +25,7 @@ function buildPrisma(claimedInvoices) {
|
||||
create: vi.fn(async () => ({ id: "ocr-result-1" })),
|
||||
},
|
||||
invoicePool: {
|
||||
findMany: vi.fn(async () => []),
|
||||
findFirst: vi.fn(async () => null),
|
||||
update: vi.fn(async () => ({ id: "invoice-1" })),
|
||||
},
|
||||
|
||||
@@ -24,9 +24,28 @@ function buildQueryRawHandler(state) {
|
||||
});
|
||||
}
|
||||
|
||||
function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
function buildPrisma({
|
||||
invoice,
|
||||
travelOrder,
|
||||
reimbursement = null,
|
||||
reimbursementItems = [{ id: "item-1", amount: "500.00" }],
|
||||
linkedItem = { id: "item-1" },
|
||||
expenseType = { id: "expense-type-1" },
|
||||
}) {
|
||||
const state = {
|
||||
invoice: { ...invoice },
|
||||
invoice: {
|
||||
owner_employee_id: "actor-1",
|
||||
uploaded_by_employee_id: "actor-1",
|
||||
file_sha256: "sha256-1",
|
||||
invoice_code: null,
|
||||
invoice_number: null,
|
||||
invoice_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
seller_name: "测试商户",
|
||||
original_name: "invoice.pdf",
|
||||
archived_reason: null,
|
||||
is_duplicate_override: false,
|
||||
...invoice,
|
||||
},
|
||||
travelOrder: travelOrder ? { ...travelOrder } : null,
|
||||
reimbursement,
|
||||
};
|
||||
@@ -34,6 +53,7 @@ function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
const prisma = {
|
||||
invoicePool: {
|
||||
findUnique: vi.fn().mockImplementation(async () => state.invoice),
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
update: vi.fn().mockImplementation(async ({ data, select }) => {
|
||||
state.invoice = { ...state.invoice, ...data };
|
||||
if (!select) {
|
||||
@@ -46,7 +66,16 @@ function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
}),
|
||||
},
|
||||
reimbursementItem: {
|
||||
findFirst: vi.fn().mockResolvedValue({ id: "item-1" }),
|
||||
findFirst: vi.fn().mockResolvedValue(linkedItem),
|
||||
findMany: vi.fn().mockResolvedValue(reimbursementItems),
|
||||
create: vi.fn().mockResolvedValue({ id: "item-auto-1" }),
|
||||
aggregate: vi.fn().mockResolvedValue({ _sum: { amount: state.invoice.total_amount ?? "0.00" } }),
|
||||
},
|
||||
reimbursementForm: {
|
||||
update: vi.fn().mockResolvedValue({ id: reimbursement?.id ?? "reim-1" }),
|
||||
},
|
||||
expenseType: {
|
||||
findFirst: vi.fn().mockResolvedValue(expenseType),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({ id: "audit-1" }),
|
||||
@@ -104,6 +133,101 @@ async function buildApp({ prisma, permissionCodes = ["travel.payment.manage"] })
|
||||
}
|
||||
|
||||
describe("invoicePool travel-order bind routes", () => {
|
||||
it("adds invoice to reimbursement and auto-creates an item when no detail matches", async () => {
|
||||
const reimbursementId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const { prisma, state } = buildPrisma({
|
||||
invoice: {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
status: "available",
|
||||
linked_reimbursement_id: null,
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
total_amount: "1683.00",
|
||||
},
|
||||
travelOrder: null,
|
||||
reimbursement: {
|
||||
id: reimbursementId,
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
status: "draft",
|
||||
reimbursement_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
},
|
||||
reimbursementItems: [],
|
||||
});
|
||||
const app = await buildApp({ prisma, permissionCodes: ["invoice.upload"] });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/11111111-1111-4111-8111-111111111111/bind",
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: { reimbursementId },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data.linkedReimbursementId).toBe(reimbursementId);
|
||||
expect(response.json().data.linkedItemId).toBe("item-auto-1");
|
||||
expect(state.invoice.linked_item_id).toBe("item-auto-1");
|
||||
expect(prisma.reimbursementItem.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
reimbursement_id: reimbursementId,
|
||||
expense_type_id: "expense-type-1",
|
||||
amount: "1683.00",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(prisma.reimbursementForm.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: reimbursementId },
|
||||
data: expect.objectContaining({
|
||||
total_amount: "1683.00",
|
||||
updated_by: "actor-1",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects reimbursement binding when linked item does not belong to the form", async () => {
|
||||
const reimbursementId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const { prisma } = buildPrisma({
|
||||
invoice: {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
status: "available",
|
||||
linked_reimbursement_id: null,
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
total_amount: "1683.00",
|
||||
},
|
||||
travelOrder: null,
|
||||
reimbursement: {
|
||||
id: reimbursementId,
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
status: "draft",
|
||||
reimbursement_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
},
|
||||
linkedItem: null,
|
||||
});
|
||||
const app = await buildApp({ prisma, permissionCodes: ["invoice.upload"] });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/11111111-1111-4111-8111-111111111111/bind",
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: {
|
||||
reimbursementId,
|
||||
linkedItemId: "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.json().error.details).toEqual({ linkedItemId: "item_not_in_reimbursement" });
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("binds invoice to confirmed travel order", async () => {
|
||||
const { prisma, state } = buildPrisma({
|
||||
invoice: {
|
||||
|
||||
@@ -36,17 +36,29 @@ function buildMultipartRequest({ filename, contentType, content, fields = [] })
|
||||
};
|
||||
}
|
||||
|
||||
async function buildApp({ ocrProvider = "off" } = {}) {
|
||||
async function buildApp({
|
||||
ocrProvider = "off",
|
||||
invoiceConflicts = [],
|
||||
statusRows = [],
|
||||
permissionCodes = ["invoice.upload"],
|
||||
} = {}) {
|
||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "hyb-oa-r71-invoice-upload-"));
|
||||
const invoicePoolCreate = vi.fn(async ({ data }) => ({
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
status: data.status,
|
||||
file_sha256: data.file_sha256,
|
||||
}));
|
||||
const invoicePoolFindMany = vi.fn(async (args) => {
|
||||
if (args?.where?.id?.in) {
|
||||
return statusRows;
|
||||
}
|
||||
return invoiceConflicts;
|
||||
});
|
||||
const auditCreate = vi.fn().mockResolvedValue({ id: "audit-1" });
|
||||
|
||||
const prisma = {
|
||||
invoicePool: {
|
||||
findMany: invoicePoolFindMany,
|
||||
create: invoicePoolCreate,
|
||||
},
|
||||
auditLog: {
|
||||
@@ -71,7 +83,7 @@ async function buildApp({ ocrProvider = "off" } = {}) {
|
||||
request.employee = {
|
||||
id: "actor-1",
|
||||
roleCodes: ["employee"],
|
||||
permissionCodes: ["invoice.upload"],
|
||||
permissionCodes,
|
||||
};
|
||||
});
|
||||
app.decorate("hasPermission", (actor, permissionCode) => {
|
||||
@@ -207,4 +219,179 @@ describe("invoicePool upload route", () => {
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("reuses same-owner existing invoice instead of creating a duplicate file record", async () => {
|
||||
const existingInvoiceId = "22222222-2222-4222-8222-222222222222";
|
||||
const { app, prisma, tempDir } = await buildApp({
|
||||
invoiceConflicts: [
|
||||
{
|
||||
id: existingInvoiceId,
|
||||
owner_employee_id: "actor-1",
|
||||
uploaded_by_employee_id: "actor-1",
|
||||
status: "available",
|
||||
archived_reason: null,
|
||||
linked_reimbursement_id: null,
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
invoice_code: null,
|
||||
invoice_number: null,
|
||||
file_sha256: "unused-in-test",
|
||||
linked_reimbursement: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
try {
|
||||
const requestPayload = buildMultipartRequest({
|
||||
filename: "same.pdf",
|
||||
contentType: "application/pdf",
|
||||
content: "%PDF-1.4 same invoice payload",
|
||||
fields: [["ocr_type", "vat"]],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/upload",
|
||||
headers: {
|
||||
authorization: "Bearer test",
|
||||
...requestPayload.headers,
|
||||
},
|
||||
payload: requestPayload.payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual(
|
||||
expect.objectContaining({
|
||||
invoicePoolId: existingInvoiceId,
|
||||
status: "available",
|
||||
uploadAction: "reuse_existing",
|
||||
unavailableReason: "duplicate_available_in_company",
|
||||
}),
|
||||
);
|
||||
expect(prisma.invoicePool.create).not.toHaveBeenCalled();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
await app.close();
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("blocks upload when the same company invoice has already been used", async () => {
|
||||
const { app, prisma, tempDir } = await buildApp({
|
||||
invoiceConflicts: [
|
||||
{
|
||||
id: "33333333-3333-4333-8333-333333333333",
|
||||
owner_employee_id: "actor-1",
|
||||
uploaded_by_employee_id: "actor-1",
|
||||
status: "bound",
|
||||
archived_reason: null,
|
||||
linked_reimbursement_id: "44444444-4444-4444-8444-444444444444",
|
||||
linked_item_id: "55555555-5555-4555-8555-555555555555",
|
||||
travel_order_id: null,
|
||||
invoice_code: null,
|
||||
invoice_number: null,
|
||||
file_sha256: "unused-in-test",
|
||||
linked_reimbursement: {
|
||||
form_number: "BX20260508001",
|
||||
status: "reviewing",
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
try {
|
||||
const requestPayload = buildMultipartRequest({
|
||||
filename: "used.pdf",
|
||||
contentType: "application/pdf",
|
||||
content: "%PDF-1.4 used invoice payload",
|
||||
fields: [["ocr_type", "vat"]],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/upload",
|
||||
headers: {
|
||||
authorization: "Bearer test",
|
||||
...requestPayload.headers,
|
||||
},
|
||||
payload: requestPayload.payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual(
|
||||
expect.objectContaining({
|
||||
invoicePoolId: null,
|
||||
status: "bound",
|
||||
uploadAction: "blocked",
|
||||
unavailableReason: "used_by_company",
|
||||
unavailableMessage: "该发票已用于报销,不能重复添加",
|
||||
}),
|
||||
);
|
||||
expect(prisma.invoicePool.create).not.toHaveBeenCalled();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
await app.close();
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("returns readable invoice statuses for upload polling", async () => {
|
||||
const invoiceId = "66666666-6666-4666-8666-666666666666";
|
||||
const { app, tempDir } = await buildApp({
|
||||
permissionCodes: ["invoice.view_own"],
|
||||
statusRows: [
|
||||
{
|
||||
id: invoiceId,
|
||||
status: "archived",
|
||||
archived_reason: "used_complete",
|
||||
owner_employee_id: "actor-1",
|
||||
uploaded_by_employee_id: "actor-1",
|
||||
linked_reimbursement_id: "77777777-7777-4777-8777-777777777777",
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
invoice_code: "044001900111",
|
||||
invoice_number: "12345678",
|
||||
invoice_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
seller_name: "北京测试有限公司",
|
||||
total_amount: "1683.00",
|
||||
original_name: "invoice.pdf",
|
||||
ocr_attempts: 1,
|
||||
ocr_last_error: null,
|
||||
ocr_completed_at: new Date("2026-05-08T01:00:00.000Z"),
|
||||
updated_at: new Date("2026-05-08T01:01:00.000Z"),
|
||||
linked_reimbursement: {
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/invoice-pool/status?ids=${invoiceId}`,
|
||||
headers: { authorization: "Bearer test" },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual({
|
||||
items: [
|
||||
expect.objectContaining({
|
||||
id: invoiceId,
|
||||
status: "archived",
|
||||
reason: "used_by_company",
|
||||
message: "该发票已用于报销,不能重复添加",
|
||||
totalAmount: "1683.00",
|
||||
}),
|
||||
],
|
||||
missingIds: [],
|
||||
});
|
||||
} finally {
|
||||
await app.close();
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,11 +62,20 @@ function buildPrisma({ processedTaskCount = 0 } = {}) {
|
||||
},
|
||||
invoice: {
|
||||
id: INVOICE_ID,
|
||||
owner_employee_id: OWNER_ID,
|
||||
uploaded_by_employee_id: OWNER_ID,
|
||||
status: "bound",
|
||||
linked_reimbursement_id: REIMBURSEMENT_ID,
|
||||
linked_item_id: ITEM_ID,
|
||||
travel_order_id: null,
|
||||
archived_reason: null,
|
||||
file_sha256: "sha256-invoice",
|
||||
invoice_code: "044001",
|
||||
invoice_number: "12345678",
|
||||
invoice_date: new Date("2026-05-01T00:00:00.000Z"),
|
||||
seller_name: "测试销售方",
|
||||
total_amount: "1683.00",
|
||||
is_duplicate_override: false,
|
||||
},
|
||||
processedTaskCount,
|
||||
};
|
||||
@@ -111,6 +120,7 @@ function buildPrisma({ processedTaskCount = 0 } = {}) {
|
||||
create: vi.fn(async () => ({ id: "history-1" })),
|
||||
},
|
||||
invoicePool: {
|
||||
findMany: vi.fn(async () => []),
|
||||
findUnique: vi.fn(async ({ where, select }) => {
|
||||
if (!state.invoice || where.id !== state.invoice.id) {
|
||||
return null;
|
||||
|
||||
在新工单中引用
屏蔽一个用户