accounting_automation, этап 2 (часть A): роль cfo по сумме, флажки fraud, проверка и оплата инвойсов
- approval.ts: роль подтверждающего при создании задачи категоризации (порог approvalThresholdUsd, первая оплата вендору) - fraud.ts: красные флажки по картам (первая оплата, рост суммы от медианы), задание fraud-flags - invoice-check.ts: заключение по инвойсу и задача invoice_approval; задание invoice-check - invoice-paid.ts: markPaid и задание invoice-paid (paid_on, paid_amount контракта) - MCP: acc_invoice_to_approve, acc_invoice_decide; миграция 041; документы proposed Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
41
packages/accounting_automation/src/approval.ts
Normal file
41
packages/accounting_automation/src/approval.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { Pool } from "khb_common";
|
||||
import { queryVendorHistory, suggestionAmount, vendorOf, type Suggestion } from "./fraud.js";
|
||||
|
||||
/**
|
||||
* Роль подтверждающего задачу категоризации (stage2.md п. 3; processes.md «Категоризация банковских операций», шаг 5):
|
||||
* cfo (Анастасия) - при сумме операции от порога или первой оплате новому вендору; иначе accountant (Юля).
|
||||
* Задачу по-прежнему может закрыть и пользователь с ролью accountant+cfo; в acc_manual_review.assignee_role пишется основная роль.
|
||||
* Сумма и вендор берутся из suggestion (amount, vendor_qbo_id, vendor_name) строки acc_categorization_review.
|
||||
*/
|
||||
|
||||
export type AssigneeRole = "accountant" | "cfo";
|
||||
|
||||
export function decideAssigneeRole(i: { amount: number | null; threshold: number; firstPayment: boolean }): { role: AssigneeRole; reason: string | null } {
|
||||
if (i.amount !== null && Math.abs(i.amount) >= i.threshold) return { role: "cfo", reason: `сумма ${Math.abs(i.amount).toFixed(2)} >= порога ${i.threshold}` };
|
||||
if (i.firstPayment) return { role: "cfo", reason: "первая оплата новому вендору" };
|
||||
return { role: "accountant", reason: null };
|
||||
}
|
||||
|
||||
/** Определяет роль и обновляет задачу в acc_manual_review; задачу, уже переданную вручную (роль не accountant), не трогает. */
|
||||
export async function applyCategorizationRole(
|
||||
pool: Pool,
|
||||
feedTxnId: string,
|
||||
opts: { threshold: number; realmId?: string | null },
|
||||
): Promise<{ role: AssigneeRole; reason: string | null }> {
|
||||
const row = (await pool.query("select suggestion from acc_categorization_review where feed_txn_id = $1", [feedTxnId])).rows[0] as { suggestion: Suggestion | null } | undefined;
|
||||
const s = row?.suggestion ?? null;
|
||||
const v = vendorOf(s);
|
||||
let firstPayment = false;
|
||||
if (v.qboId || v.name) {
|
||||
const h = await queryVendorHistory(pool, { realmId: opts.realmId, vendorQboId: v.qboId, vendorName: v.name, feedTxnId });
|
||||
firstPayment = h.priorCount === 0;
|
||||
}
|
||||
const decision = decideAssigneeRole({ amount: suggestionAmount(s), threshold: opts.threshold, firstPayment });
|
||||
if (decision.role === "cfo") {
|
||||
await pool.query(
|
||||
"update acc_manual_review set assignee_role = 'cfo' where ref_table = 'acc_categorization_review' and ref_id = $1 and assignee_role = 'accountant'",
|
||||
[feedTxnId],
|
||||
);
|
||||
}
|
||||
return decision;
|
||||
}
|
||||
Reference in New Issue
Block a user