Инвойсы: дубль по вендору и номеру (пересылка и повторная отправка), корректировка суммы подсвечивается; processes.md
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -211,17 +211,26 @@ async function resolveAll(pool: Pool, r: RecognizedInvoice): Promise<Resolved> {
|
||||
return { vendorQboId: v.id, projectCustomerId: p.id, contractId, problems };
|
||||
}
|
||||
|
||||
async function isDuplicate(pool: Pool, id: string, vendorQboId: string | null, r: RecognizedInvoice): Promise<boolean> {
|
||||
if (!r.invoice_number || r.amount == null || !r.vendor_name) return false;
|
||||
/**
|
||||
* Дубль инвойса (тот же вендор и номер): same - сумма та же (повторная отправка или пересылка внутри компании), второй задачи не создаём;
|
||||
* amount_differs - номер тот же, сумма другая (корректировка счёта): создаём, но подсвечиваем. Смотрим и историю разбора.
|
||||
*/
|
||||
async function findDuplicate(pool: Pool, id: string, vendorQboId: string | null, r: RecognizedInvoice): Promise<"same" | "amount_differs" | null> {
|
||||
if (!r.invoice_number || !r.vendor_name) return null;
|
||||
const vendorName = normalizeVendorName(r.vendor_name);
|
||||
for (const t of ["acc_invoices", "acc_invoices_review"]) {
|
||||
const { rows } = await pool.query<{ vendor_name: string | null; vendor_qbo_id: string | null }>(
|
||||
`select vendor_name, vendor_qbo_id from ${t} where id <> $1 and invoice_number = $2 and amount = $3`,
|
||||
[id, r.invoice_number, r.amount],
|
||||
let result: "same" | "amount_differs" | null = null;
|
||||
for (const t of ["acc_invoices", "acc_invoices_review", "acc_invoices_review_history"]) {
|
||||
const { rows } = await pool.query<{ vendor_name: string | null; vendor_qbo_id: string | null; amount: string | null }>(
|
||||
`select vendor_name, vendor_qbo_id, amount from ${t} where id <> $1 and invoice_number = $2`,
|
||||
[id, r.invoice_number],
|
||||
);
|
||||
if (rows.some((x) => (vendorQboId && x.vendor_qbo_id === vendorQboId) || normalizeVendorName(x.vendor_name) === vendorName)) return true;
|
||||
for (const x of rows) {
|
||||
if (!((vendorQboId && x.vendor_qbo_id === vendorQboId) || normalizeVendorName(x.vendor_name) === vendorName)) continue;
|
||||
if (r.amount != null && x.amount != null && Number(x.amount) === Number(r.amount)) return "same";
|
||||
result = "amount_differs";
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return result;
|
||||
}
|
||||
|
||||
const safeName = (s: string): string => s.replace(/[\\/:*?"<>|\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim().slice(0, 100) || "_";
|
||||
@@ -284,7 +293,7 @@ async function recognize(deps: InvoiceIngestDeps, msg: MailMessage, att: MailAtt
|
||||
}
|
||||
|
||||
/** Результат по одному вложению: created - новая запись; existing - запись уже есть; none - не инвойс. */
|
||||
async function processAttachment(deps: InvoiceIngestDeps, msg: MailMessage, att: MailAttachment, index: number): Promise<"created" | "existing" | "certificate" | "none"> {
|
||||
async function processAttachment(deps: InvoiceIngestDeps, msg: MailMessage, att: MailAttachment, index: number): Promise<"created" | "existing" | "duplicate" | "certificate" | "none"> {
|
||||
const { pool } = deps;
|
||||
const id = invoiceReviewId(msg.id, index, att.filename);
|
||||
const ex = await pool.query(
|
||||
@@ -313,7 +322,12 @@ async function processAttachment(deps: InvoiceIngestDeps, msg: MailMessage, att:
|
||||
|
||||
const resolved = await resolveAll(pool, rec);
|
||||
const problems = [...resolved.problems];
|
||||
if (await isDuplicate(pool, id, resolved.vendorQboId, rec)) problems.push("подозрение на дубль (тот же вендор, номер и сумма уже есть)");
|
||||
const dup = await findDuplicate(pool, id, resolved.vendorQboId, rec);
|
||||
if (dup === "same") {
|
||||
deps.log.info({ messageId: msg.id, vendor: rec.vendor_name, invoice: rec.invoice_number }, "дубль инвойса (вендор, номер и сумма совпали): вторая задача не создаётся");
|
||||
return "duplicate";
|
||||
}
|
||||
if (dup === "amount_differs") problems.push("тот же вендор и номер инвойса уже есть, но сумма другая (корректировка счёта?)");
|
||||
const tbd: string[] = [];
|
||||
if (!rec.vendor_name) tbd.push("vendor_name");
|
||||
if (!rec.invoice_number) tbd.push("invoice_number");
|
||||
|
||||
Reference in New Issue
Block a user