import { useEffect, useMemo, useRef, useState } from "react"; import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant, signal as raiseEvent } from "../apiContract"; import { BodyPortal, useOverlayLayer } from "../customer-grid/seam"; import { useAppFullscreen } from "../ui/useAppFullscreen"; import "./collection.css"; import "../styles/sdlc72/app-fullscreen.css"; type OpenDocument = { name: string; date: string | null; dueDate: string | null; daysOverdue: number; openBalance: number; }; type CollectionRow = { partner_id: number; Customer: string; Tier: string; Overdue: number; Receivable: number; "Oldest (days)": number; "Open Docs": number; Email: string; Phone: string; Status: string; "Reconcile?": string; _docs: number; documents: OpenDocument[]; sendRefusal: string | null; }; type SendPolicy = { safeMode: boolean; safeRecipients: string[]; sender: { name: string; email: string; replyTo: string }; }; type Worklist = SendPolicy & { rows: CollectionRow[] }; type Refusal = { customer: string; reason: string }; type Statement = { partnerId: number; customer: string; to: string; subject: string; html: string; refusal: string | null; }; type Preview = SendPolicy & { requested: number; statements: Statement[]; refused: Refusal[] }; type SendResult = { created: number; refused: Refusal[]; safeMode: boolean }; type PageState = | { kind: "loading" } | { kind: "ready"; data: Worklist } | { kind: "error" | "restricted"; message: string }; type BatchState = | { kind: "loading"; ids: number[] } | { kind: "ready"; ids: number[]; data: Preview } | { kind: "error"; ids: number[]; message: string }; const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }); const count = new Intl.NumberFormat("en-US"); // C10: "Overdue" is Odoo's res.partner.total_overdue with no grace. A grace number reads "Overdue past grace". const columns = ["Customer", "Tier", "Overdue", "Receivable", "Oldest days", "Open docs", "Email", "Phone", "Status", "Reconcile"]; function plural(value: number, one: string, many: string): string { return `${count.format(value)} ${value === 1 ? one : many}`; } function safeModeNotice(policy: SendPolicy): string { if (!policy.safeMode) return "Statements will be queued to each customer's Odoo email address."; if (!policy.safeRecipients.length) return "Safe mode is on; no recipient addresses can receive mail right now."; return `Safe mode is on; only ${policy.safeRecipients.join(", ")} can receive mail right now.`; } //: The only two refusals above 500 that are written FOR the reader rather than about the server. const SPOKEN_5XX = new Set(["collection_send_unconfirmed", "collection_odoo_unavailable"]); async function responseError(response: Response): Promise { const body = await response.json().catch(() => null) as { error?: { code?: string; message?: string }; detail?: { error?: { code?: string; message?: string } }; } | null; const error = body?.error || body?.detail?.error; // A 4xx message is policy and is shown; a 5xx detail is internal and is not (apiContract's rule). if (response.status >= 500 && !SPOKEN_5XX.has(error?.code || "")) { return "Something went wrong on our side. Try again in a moment."; } return error?.message || "Collection is temporarily unavailable."; } function Refusals({ items }: { items: Refusal[] }) { if (!items.length) return null; return
{plural(items.length, "customer refused", "customers refused")}
; } function TableSkeleton() { return
{columns.map(label => )}{Array.from({ length: 8 }, (_, index) => {Array.from({ length: columns.length + 1 }, (_, cell) => )} )}
; } function CustomerDrawer({ row, onClose }: { row: CollectionRow; onClose: () => void }) { const panelRef = useRef(null); useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false, trapFocus: true }); return
; } function BatchDialog({ batch, sending, onClose, onSend }: { batch: BatchState; sending: boolean; onClose: () => void; onSend: (ids: number[]) => void; }) { const panelRef = useRef(null); const confirmRef = useRef(null); const sendRef = useRef(null); const [confirming, setConfirming] = useState(false); const [statementIndex, setStatementIndex] = useState(0); useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false, trapFocus: true }); useEffect(() => { (confirming ? confirmRef : sendRef).current?.focus(); }, [confirming]); const data = batch.kind === "ready" ? batch.data : null; const statement = data?.statements[statementIndex]; const expected = data ? data.requested - data.refused.length : 0; return

{confirming ? "Confirm batch" : "Batch preview"}

{batch.kind === "loading" &&
} {batch.kind === "error" &&

{batch.message}

} {data && <>

{plural(data.requested, "customer", "customers")} selected. {plural(expected, "statement", "statements")} can be queued; {count.format(data.refused.length)} will be refused.

{confirming ? <>

Queue one statement per eligible customer in Odoo's outbox?

Balances and recipients are read again when you confirm.

: <>

From {data.sender.name} ({data.sender.email}). Replies go to {data.sender.replyTo}.

{statement && <>
To
{statement.to || "No email address"}
Subject
{statement.subject}
{statement.refusal &&

{statement.refusal}

}
{/* Scripts and remote resources are blocked. The frame is read-only and sized to its content, so scrolling and Escape stay in the parent overlay. */}