forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

payments.ts44 lines, 2.1 KiBHistoryRawDownload
1import type { StatementEntry } from "@/api/payments";
2import type { MessageKey } from "@/i18n/messages";
3
4export type StatementStatus = {
5 key: MessageKey;
6 // Badge color classes (used with Badge variant="outline") and a matching dot.
7 class: string;
8 dot: string;
9};
10
11// A charge's status, mirrored from the backend rollup fields, in the finance
12// operator's vocabulary (paid / partial / overdue / pending / cancelled), with
13// receivables-aging colors: green current/paid, amber partial, red overdue.
14export function statementStatus(entry: StatementEntry): StatementStatus {
15 if (entry.reversed) return { key: "payments.statusCancelled", class: "border-border bg-muted/40 text-muted-foreground", dot: "bg-muted-foreground/50" };
16 if (entry.outstanding_minor <= 0) return { key: "payments.paid", class: "border-success/50 bg-success/10 text-success-text", dot: "bg-success" };
17 if (entry.overdue) return { key: "payments.overdue", class: "border-destructive/50 bg-destructive/10 text-destructive-text", dot: "bg-destructive" };
18 if (entry.credited_minor > 0) return { key: "payments.statusPartial", class: "border-warning/50 bg-warning/10 text-warning-text", dot: "bg-warning" };
19 return { key: "payments.statusPending", class: "border-border bg-background/50 text-muted-foreground", dot: "bg-muted-foreground/40" };
20}
21
22// Sort rank for a receivables list: overdue first, then unpaid by due date,
23// then settled, then cancelled last.
24export function statementSortRank(entry: StatementEntry): number {
25 if (entry.reversed) return 3;
26 if (entry.outstanding_minor <= 0) return 2;
27 if (entry.overdue) return 0;
28 return 1;
29}
30
31export function sortStatementEntries(entries: StatementEntry[]): StatementEntry[] {
32 return [...entries].sort(
33 (a, b) => statementSortRank(a) - statementSortRank(b) || (a.due_at ?? Infinity) - (b.due_at ?? Infinity),
34 );
35}
36
37// Common collection methods. Values are stored as free text on the ledger line,
38// so the localized label is what a receipt shows.
39export const PAYMENT_METHOD_KEYS: MessageKey[] = [
40 "payments.methodCash",
41 "payments.methodTransfer",
42 "payments.methodCard",
43 "payments.methodCheck",
44];