ownledger 1.2.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +73 -1
- package/CHANGELOG.md +64 -0
- package/README.md +13 -0
- package/assets/apple-touch-icon.png +0 -0
- package/assets/icon-192.png +0 -0
- package/assets/icon-512.png +0 -0
- package/assets/icon-maskable-512.png +0 -0
- package/assets/index.html +15 -18
- package/assets/manifest.webmanifest +1 -1
- package/assets/static/Account-sw3qGLbB.js +1 -0
- package/assets/static/Accounts-DWyzX3qB.js +1 -0
- package/assets/static/Attention-DxV-4qdY.js +1 -0
- package/assets/static/ByCategory-B4P3ikmE.js +1 -0
- package/assets/static/CardDetail-CayBXBm-.js +1 -0
- package/assets/static/Cards-CVuOP8Bt.js +1 -0
- package/assets/static/Cards-XUBdWxyo.js +1 -0
- package/assets/static/CardsValue-EoW2jG_0.js +1 -0
- package/assets/static/Cashback-BibVYHf_.js +1 -0
- package/assets/static/CategoryIcon-HfjYhH1l.js +1 -0
- package/assets/static/CategoryPicker-BkmvzX2I.js +1 -0
- package/assets/static/ChartWidget-u09-whUU.js +1 -0
- package/assets/static/Combobox-xXeyjGgK.js +1 -0
- package/assets/static/Deposits-CLAda4dj.js +1 -0
- package/assets/static/DrillLink-Bvfam9FA.js +1 -0
- package/assets/static/Field-tKFmKDEF.js +1 -0
- package/assets/static/HomeEditor-4vxj5LMD.js +1 -0
- package/assets/static/Import-DwaaFpRJ.js +2 -0
- package/assets/static/IncomeSpending-RVDSxAK4.js +1 -0
- package/assets/static/KV-DMA1AlP-.js +1 -0
- package/assets/static/Left-C_4gTGSX.js +1 -0
- package/assets/static/Money-PfFNNDOw.js +1 -0
- package/assets/static/NetWorth-BSoseczC.js +1 -0
- package/assets/static/Pace-B8p-9174.js +1 -0
- package/assets/static/Panel-BnfWrw9R.js +1 -0
- package/assets/static/PinDialog-BqAU9Vh4.js +1 -0
- package/assets/static/Plans-2KuQH2_I.js +1 -0
- package/assets/static/Recent-Ccmb34bq.js +1 -0
- package/assets/static/Reports-Dd-dhclr.js +1 -0
- package/assets/static/Settings-BR8WBlen.js +1 -0
- package/assets/static/Transactions-BAuQ-6Eb.js +1 -0
- package/assets/static/TxRow-EKIIMQKa.js +1 -0
- package/assets/static/Unusual-CwFp8aA5.js +1 -0
- package/assets/static/Upcoming-B28roT9r.js +1 -0
- package/assets/static/WhichCard-B3kLc842.js +1 -0
- package/assets/static/arrow-up-D3gxUKLo.js +1 -0
- package/assets/static/bits-Dr2E0ObC.js +1 -0
- package/assets/static/button-rfwMVz0i.js +1 -0
- package/assets/static/cards-Bb5zjzRG.js +1 -0
- package/assets/static/charts-BqP7HKwP.js +65 -0
- package/assets/static/chevron-down-Co1w7ApZ.js +1 -0
- package/assets/static/chevron-right-Dqh3vs9Y.js +1 -0
- package/assets/static/createLucideIcon-flJrJK2p.js +1 -0
- package/assets/static/dialog-BuRqO4sc.js +1 -0
- package/assets/static/dist-B4_eGtNj.js +1 -0
- package/assets/static/dist-B7PR2b0C.js +1 -0
- package/assets/static/dist-BDn7ydjn.js +1 -0
- package/assets/static/dist-Dl-m8cc2.js +1 -0
- package/assets/static/drill-t1zr57xi.js +1 -0
- package/assets/static/es2015-DJh4h8NX.js +41 -0
- package/assets/static/fold-CGWYt9Iu.js +1 -0
- package/assets/static/index-eR1GkdC_.js +14 -0
- package/assets/static/index-iYMxgw-7.css +1 -0
- package/assets/static/queries-DZIMMe4i.js +1 -0
- package/assets/static/react-dom-wK_7mEjs.js +1 -0
- package/assets/static/select-eaSi9K-d.js +1 -0
- package/assets/static/shared-Box7on3B.js +1 -0
- package/assets/static/triangle-exclamation-point-CuL0XSJl.js +1 -0
- package/assets/static/useMutation-_Lw8xXWa.js +1 -0
- package/dist/cli/cards.d.ts +2 -0
- package/dist/cli/cards.js +86 -0
- package/dist/cli/check.js +7 -1
- package/dist/cli/dev.js +22 -2
- package/dist/cli/import.js +12 -2
- package/dist/cli/index.js +6 -0
- package/dist/cli/statements.d.ts +1 -0
- package/dist/cli/statements.js +231 -0
- package/dist/engine/actions.d.ts +5 -0
- package/dist/engine/actions.js +9 -5
- package/dist/engine/attention.d.ts +15 -0
- package/dist/engine/attention.js +63 -0
- package/dist/engine/check.js +8 -1
- package/dist/engine/dashboard.d.ts +72 -0
- package/dist/engine/dashboard.js +180 -0
- package/dist/engine/features.d.ts +20 -0
- package/dist/engine/features.js +37 -0
- package/dist/engine/index.d.ts +5 -0
- package/dist/engine/index.js +5 -0
- package/dist/engine/insights.d.ts +164 -0
- package/dist/engine/insights.js +534 -0
- package/dist/engine/load.js +6 -0
- package/dist/engine/registry.d.ts +2 -0
- package/dist/engine/registry.js +15 -33
- package/dist/engine/reports.d.ts +21 -2
- package/dist/engine/reports.js +51 -3
- package/dist/engine/schedules.d.ts +7 -84
- package/dist/engine/schedules.js +8 -207
- package/dist/engine/types.d.ts +2 -0
- package/dist/engine/write.d.ts +9 -0
- package/dist/engine/write.js +26 -5
- package/dist/import/mapping.js +6 -0
- package/dist/import/run.d.ts +7 -0
- package/dist/import/run.js +28 -1
- package/dist/modules/api.d.ts +22 -0
- package/dist/modules/api.js +16 -0
- package/dist/modules/cards/api.d.ts +3 -0
- package/dist/modules/cards/api.js +105 -0
- package/dist/modules/cards/engine.d.ts +42 -0
- package/dist/modules/cards/engine.js +102 -0
- package/dist/modules/cards/files.d.ts +11 -0
- package/dist/modules/cards/files.js +17 -0
- package/dist/modules/cards/index.d.ts +26 -0
- package/dist/modules/cards/index.js +304 -0
- package/dist/modules/cards/statements.d.ts +128 -0
- package/dist/modules/cards/statements.js +426 -0
- package/dist/modules/cards/terms.d.ts +105 -0
- package/dist/modules/cards/terms.js +194 -0
- package/dist/modules/cards/tracker.d.ts +36 -0
- package/dist/modules/cards/tracker.js +232 -0
- package/dist/modules/cards/types.d.ts +2 -0
- package/dist/modules/cards/types.js +16 -0
- package/dist/modules/deposits/engine.d.ts +14 -0
- package/dist/modules/deposits/engine.js +32 -0
- package/dist/modules/deposits/index.d.ts +2 -0
- package/dist/modules/deposits/index.js +24 -0
- package/dist/modules/deposits/types.d.ts +2 -0
- package/dist/modules/deposits/types.js +15 -0
- package/dist/modules/index.d.ts +5 -0
- package/dist/modules/index.js +10 -0
- package/dist/modules/insurance/index.d.ts +3 -0
- package/dist/modules/insurance/index.js +8 -0
- package/dist/modules/insurance/types.d.ts +2 -0
- package/dist/modules/insurance/types.js +3 -0
- package/dist/modules/investments/index.d.ts +2 -0
- package/dist/modules/investments/index.js +24 -0
- package/dist/modules/investments/types.d.ts +2 -0
- package/dist/modules/investments/types.js +7 -0
- package/dist/modules/loans/engine.d.ts +30 -0
- package/dist/modules/loans/engine.js +77 -0
- package/dist/modules/loans/index.d.ts +2 -0
- package/dist/modules/loans/index.js +27 -0
- package/dist/modules/loans/types.d.ts +2 -0
- package/dist/modules/loans/types.js +5 -0
- package/dist/modules/types.d.ts +39 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/util.d.ts +8 -0
- package/dist/modules/util.js +8 -0
- package/dist/node/scaffold.js +2 -2
- package/dist/node/statements.d.ts +64 -0
- package/dist/node/statements.js +218 -0
- package/dist/worker/api.d.ts +11 -1
- package/dist/worker/api.js +169 -13
- package/dist/worker/bestcard.d.ts +31 -9
- package/dist/worker/bestcard.js +78 -50
- package/dist/worker/github.d.ts +3 -1
- package/dist/worker/github.js +13 -2
- package/dist/worker/mcp.js +209 -30
- package/dist/worker/store.d.ts +2 -0
- package/dist/worker/store.js +7 -2
- package/dist/worker/tar.d.ts +2 -1
- package/dist/worker/tar.js +4 -1
- package/dist/worker/version.d.ts +1 -1
- package/dist/worker/version.js +1 -1
- package/package.json +28 -4
- package/presets/misa.mapping.yaml +7 -0
- package/assets/app.css +0 -61
- package/assets/app.js +0 -369
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { addDays, todayIn } from "../../engine/dates.js";
|
|
2
|
+
import { planStatus } from "../loans/engine.js";
|
|
3
|
+
import { cardList } from "./index.js";
|
|
4
|
+
import { bestCard, cardValues, cashback, feeWaiver, interestFreeUntil, lineKind, statements } from "./statements.js";
|
|
5
|
+
import { cardsData, recordFor, termsOf } from "./terms.js";
|
|
6
|
+
import { flattenCategories } from "../../engine/categories.js";
|
|
7
|
+
import { statementPdf } from "./files.js";
|
|
8
|
+
/** GET /api/m/cards/statement-pdf?account=<id>&close=<YYYY-MM-DD>: the bank's PDF for one closed statement, streamed. */
|
|
9
|
+
async function statementPdfResponse(c) {
|
|
10
|
+
const L = c.snap.ledger;
|
|
11
|
+
const id = c.q.get("account") ?? "";
|
|
12
|
+
const close = c.q.get("close") ?? "";
|
|
13
|
+
if (!id || !/^\d{4}-\d{2}-\d{2}$/.test(close))
|
|
14
|
+
return { body: { error: "account and close (YYYY-MM-DD)" }, status: 400 };
|
|
15
|
+
const a = L.accounts.find((x) => x.id === id && x.type === "credit_card");
|
|
16
|
+
if (!a)
|
|
17
|
+
return { body: { error: `no card ${id}` }, status: 404 };
|
|
18
|
+
// the path is built from the ledger's own card and close date, never taken from the request
|
|
19
|
+
const st = statements(L, a.id, c.asOf).find((s) => !s.open && s.close === close);
|
|
20
|
+
const path = st ? statementPdf(c.snap.files, a.id, st.close) : null;
|
|
21
|
+
if (!path)
|
|
22
|
+
return { body: { error: "no statement PDF" }, status: 404 };
|
|
23
|
+
const blob = await c.src.blob(c.snap.sha, path);
|
|
24
|
+
if (!blob?.body)
|
|
25
|
+
return { body: { error: "no statement PDF" }, status: 404 };
|
|
26
|
+
return {
|
|
27
|
+
response: new Response(blob.body, {
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/pdf",
|
|
30
|
+
"Content-Disposition": `inline; filename="${a.id}-${close.slice(0, 7)}.pdf"`,
|
|
31
|
+
"Cache-Control": "private, no-store",
|
|
32
|
+
"X-Content-Type-Options": "nosniff",
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** /api/m/cards/… */
|
|
38
|
+
export function cardsApi(path, c) {
|
|
39
|
+
if (path === "statement-pdf")
|
|
40
|
+
return statementPdfResponse(c);
|
|
41
|
+
const L = c.snap.ledger;
|
|
42
|
+
const asOf = c.asOf;
|
|
43
|
+
if (path === "") {
|
|
44
|
+
const list = cardList(L, asOf, { includeClosed: c.q.get("closed") === "1" });
|
|
45
|
+
const week = addDays(asOf, 7);
|
|
46
|
+
const monthEnd = `${asOf.slice(0, 7)}-31`;
|
|
47
|
+
return {
|
|
48
|
+
body: {
|
|
49
|
+
asOf,
|
|
50
|
+
cards: list,
|
|
51
|
+
totals: {
|
|
52
|
+
owed: list.reduce((s, x) => s + Math.max(0, x.owed), 0),
|
|
53
|
+
dueWeek: list.filter((x) => x.toPay > 0 && x.due && x.due <= week).reduce((s, x) => s + x.toPay, 0),
|
|
54
|
+
dueMonth: list.filter((x) => x.toPay > 0 && x.due && x.due <= monthEnd).reduce((s, x) => s + x.toPay, 0),
|
|
55
|
+
limit: list.reduce((s, x) => s + (x.limit ?? 0), 0),
|
|
56
|
+
},
|
|
57
|
+
records: cardsData(L).records,
|
|
58
|
+
values: cardValues(L, asOf),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (path === "best") {
|
|
63
|
+
const category = c.q.get("category");
|
|
64
|
+
if (!category)
|
|
65
|
+
return { body: { error: "category" }, status: 400 };
|
|
66
|
+
return { body: bestCard(L, { category, amount: c.q.get("amount") ? Number(c.q.get("amount")) : undefined, date: c.q.get("on") ?? asOf }) };
|
|
67
|
+
}
|
|
68
|
+
if (path === "values")
|
|
69
|
+
return { body: cardValues(L, asOf) };
|
|
70
|
+
const [id, sub] = decodeURIComponent(path).split("/");
|
|
71
|
+
const a = L.accounts.find((x) => x.id === id);
|
|
72
|
+
if (!a)
|
|
73
|
+
return { body: { error: `no account ${id}` }, status: 404 };
|
|
74
|
+
const rec = recordFor(L, a.id);
|
|
75
|
+
if (sub === "statement") {
|
|
76
|
+
const close = c.q.get("close");
|
|
77
|
+
const st = statements(L, a.id, asOf).find((s) => s.close === close);
|
|
78
|
+
if (!st)
|
|
79
|
+
return { body: { error: "no such statement" }, status: 404 };
|
|
80
|
+
const { map } = flattenCategories(L.categories);
|
|
81
|
+
const txs = L.transactions
|
|
82
|
+
.filter((t) => !t.void && t.date >= st.from && t.date <= st.close && t.lines.some((ln) => ln.account === a.id))
|
|
83
|
+
.map((t) => ({ ...t, kind: lineKind(t, a.id, map)?.kind ?? null }))
|
|
84
|
+
.reverse();
|
|
85
|
+
return { body: { statement: { ...st, pdf: !st.open && !!statementPdf(c.snap.files, a.id, st.close) }, transactions: txs } };
|
|
86
|
+
}
|
|
87
|
+
const summary = cardList(L, asOf, { includeClosed: true }).find((x) => x.account === a.id) ?? null;
|
|
88
|
+
const terms = termsOf(a, rec);
|
|
89
|
+
return {
|
|
90
|
+
body: {
|
|
91
|
+
asOf,
|
|
92
|
+
account: a,
|
|
93
|
+
record: rec ?? null,
|
|
94
|
+
terms,
|
|
95
|
+
summary,
|
|
96
|
+
statements: statements(L, a.id, asOf).reverse().map((s) => ({ ...s, pdf: !s.open && !!statementPdf(c.snap.files, a.id, s.close) })),
|
|
97
|
+
cashback: cashback(L, a.id, asOf).reverse().slice(0, 13),
|
|
98
|
+
feeWaiver: feeWaiver(L, a.id, asOf),
|
|
99
|
+
instalments: planStatus(L, asOf).filter((p) => p.kind === "instalment" && p.account === a.id),
|
|
100
|
+
value: cardValues(L, asOf).find((v) => v.account === a.id) ?? null,
|
|
101
|
+
payByToday: interestFreeUntil(terms, asOf),
|
|
102
|
+
today: todayIn(),
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ISODate, Ledger } from "../../engine/types.js";
|
|
2
|
+
export interface CardStatus {
|
|
3
|
+
account: string;
|
|
4
|
+
name: string;
|
|
5
|
+
owed: number;
|
|
6
|
+
limit: number | null;
|
|
7
|
+
available: number | null;
|
|
8
|
+
statementDate: ISODate | null;
|
|
9
|
+
statementBalance: number | null;
|
|
10
|
+
paidSinceStatement: number;
|
|
11
|
+
toPay: number | null;
|
|
12
|
+
dueDate: ISODate | null;
|
|
13
|
+
daysToDue: number | null;
|
|
14
|
+
/** details.bank and details.product as the owner wrote them (product: the card's real product name), or null */
|
|
15
|
+
bank: string | null;
|
|
16
|
+
product: string | null;
|
|
17
|
+
period: CardPeriod;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What happened on a card in the current statement period (day after the last statement → asOf), or in the
|
|
21
|
+
* calendar month when the card has no statement_day. Only transactions where the card is the only account count
|
|
22
|
+
* towards spending; transfers and card payments never do.
|
|
23
|
+
*/
|
|
24
|
+
export interface CardPeriod {
|
|
25
|
+
from: ISODate;
|
|
26
|
+
to: ISODate;
|
|
27
|
+
basis: "statement" | "month";
|
|
28
|
+
/** expense lines paid with the card (before refunds) */
|
|
29
|
+
spent: number;
|
|
30
|
+
/** expense lines refunded to the card */
|
|
31
|
+
refunds: number;
|
|
32
|
+
/** spent - refunds, per expense category path, largest first */
|
|
33
|
+
byCategory: {
|
|
34
|
+
category: string;
|
|
35
|
+
amount: number;
|
|
36
|
+
}[];
|
|
37
|
+
/** income under the `cashback` category (and its children) credited to the card */
|
|
38
|
+
cashback: number;
|
|
39
|
+
/** transactions that touch the card together with other accounts and categories: not split, left out of the figures above */
|
|
40
|
+
mixed: number;
|
|
41
|
+
}
|
|
42
|
+
export declare function cards(l: Ledger, asOf: ISODate): CardStatus[];
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { holdingsAt } from "../../engine/balances.js";
|
|
2
|
+
import { flattenCategories } from "../../engine/categories.js";
|
|
3
|
+
import { addDays, addMonths, dayIn, daysBetween, monthStart, parts } from "../../engine/dates.js";
|
|
4
|
+
import { day, isOpen as open, num } from "../util.js";
|
|
5
|
+
function cardPeriod(l, card, from, to, basis, kinds) {
|
|
6
|
+
let spent = 0;
|
|
7
|
+
let refunds = 0;
|
|
8
|
+
let cashback = 0;
|
|
9
|
+
let mixed = 0;
|
|
10
|
+
const by = new Map();
|
|
11
|
+
for (const t of l.transactions) {
|
|
12
|
+
if (t.void || t.date < from || t.date > to)
|
|
13
|
+
continue;
|
|
14
|
+
if (!t.lines.some((ln) => ln.account === card))
|
|
15
|
+
continue;
|
|
16
|
+
const cats = t.lines.filter((ln) => ln.category !== undefined);
|
|
17
|
+
if (!cats.length)
|
|
18
|
+
continue; // a transfer or card payment
|
|
19
|
+
if (t.lines.some((ln) => ln.account !== undefined && ln.account !== card)) {
|
|
20
|
+
mixed++;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
for (const ln of cats) {
|
|
24
|
+
const c = ln.category;
|
|
25
|
+
if (kinds.get(c) === "expense") {
|
|
26
|
+
if (ln.amount > 0)
|
|
27
|
+
spent += ln.amount;
|
|
28
|
+
else
|
|
29
|
+
refunds -= ln.amount;
|
|
30
|
+
by.set(c, (by.get(c) ?? 0) + ln.amount);
|
|
31
|
+
}
|
|
32
|
+
else if (kinds.get(c) === "income" && (c === "cashback" || c.startsWith("cashback/"))) {
|
|
33
|
+
cashback -= ln.amount;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const byCategory = [...by].filter(([, a]) => a !== 0).map(([category, amount]) => ({ category, amount })).sort((a, b) => b.amount - a.amount);
|
|
38
|
+
return { from, to, basis, spent, refunds, byCategory, cashback, mixed };
|
|
39
|
+
}
|
|
40
|
+
export function cards(l, asOf) {
|
|
41
|
+
const now = holdingsAt(l, asOf);
|
|
42
|
+
const kinds = new Map([...flattenCategories(l.categories).map].map(([p, c]) => [p, c.kind]));
|
|
43
|
+
const str = (v) => (typeof v === "string" && v.trim() ? v.trim() : null);
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const a of l.accounts) {
|
|
46
|
+
if (a.type !== "credit_card" || !open(a, asOf))
|
|
47
|
+
continue;
|
|
48
|
+
const d = a.details ?? {};
|
|
49
|
+
const sd = day(d.statement_day);
|
|
50
|
+
const dd = day(d.due_day);
|
|
51
|
+
const limit = num(d.limit) ?? null;
|
|
52
|
+
const owed = -(now.get(a.id)?.amount ?? 0);
|
|
53
|
+
let statementDate = null;
|
|
54
|
+
let statementBalance = null;
|
|
55
|
+
let paid = 0;
|
|
56
|
+
let dueDate = null;
|
|
57
|
+
if (sd) {
|
|
58
|
+
const [y, m] = parts(asOf);
|
|
59
|
+
statementDate = dayIn(y, m, sd);
|
|
60
|
+
if (statementDate > asOf) {
|
|
61
|
+
const prev = addMonths(`${asOf.slice(0, 7)}-01`, -1);
|
|
62
|
+
statementDate = dayIn(...parts(prev).slice(0, 2), sd);
|
|
63
|
+
}
|
|
64
|
+
statementBalance = -(holdingsAt(l, statementDate).get(a.id)?.amount ?? 0);
|
|
65
|
+
for (const t of l.transactions) {
|
|
66
|
+
if (t.void || t.date <= statementDate || t.date > asOf)
|
|
67
|
+
continue;
|
|
68
|
+
for (const ln of t.lines)
|
|
69
|
+
if (ln.account === a.id && ln.amount > 0)
|
|
70
|
+
paid += ln.amount;
|
|
71
|
+
}
|
|
72
|
+
if (dd) {
|
|
73
|
+
const [sy, sm] = parts(statementDate);
|
|
74
|
+
dueDate = dayIn(sy, sm, dd);
|
|
75
|
+
if (dueDate <= statementDate) {
|
|
76
|
+
const nx = addMonths(`${statementDate.slice(0, 7)}-01`, 1);
|
|
77
|
+
dueDate = dayIn(...parts(nx).slice(0, 2), dd);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const toPay = statementBalance === null ? null : Math.max(0, statementBalance - paid);
|
|
82
|
+
out.push({
|
|
83
|
+
account: a.id,
|
|
84
|
+
name: a.name,
|
|
85
|
+
owed,
|
|
86
|
+
limit,
|
|
87
|
+
available: limit === null ? null : limit - owed,
|
|
88
|
+
statementDate,
|
|
89
|
+
statementBalance,
|
|
90
|
+
paidSinceStatement: paid,
|
|
91
|
+
toPay,
|
|
92
|
+
dueDate,
|
|
93
|
+
daysToDue: dueDate ? daysBetween(asOf, dueDate) : null,
|
|
94
|
+
bank: str(d.bank),
|
|
95
|
+
product: str(d.product),
|
|
96
|
+
period: statementDate
|
|
97
|
+
? cardPeriod(l, a.id, addDays(statementDate, 1), asOf, "statement", kinds)
|
|
98
|
+
: cardPeriod(l, a.id, monthStart(asOf), asOf, "month", kinds),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FileMap } from "../../engine/load.js";
|
|
2
|
+
/**
|
|
3
|
+
* Bank statement PDFs (plan 004): imports/statements/<account-id>/<YYYY-MM>.pdf, YYYY-MM = the month of the close date.
|
|
4
|
+
* The Worker only knows their paths (from the tarball, contents not read) and streams one to the signed-in dashboard;
|
|
5
|
+
* it never parses them. Reading them is the CLI's job, on the user's computer.
|
|
6
|
+
*/
|
|
7
|
+
export declare const STATEMENTS_DIR = "imports/statements";
|
|
8
|
+
export declare const STATEMENT_PDF_RE: RegExp;
|
|
9
|
+
export declare function statementPdfPath(account: string, close: string): string;
|
|
10
|
+
/** The statement PDF filed for this card's statement closing on `close`, or null. */
|
|
11
|
+
export declare function statementPdf(files: FileMap, account: string, close: string): string | null;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bank statement PDFs (plan 004): imports/statements/<account-id>/<YYYY-MM>.pdf, YYYY-MM = the month of the close date.
|
|
3
|
+
* The Worker only knows their paths (from the tarball, contents not read) and streams one to the signed-in dashboard;
|
|
4
|
+
* it never parses them. Reading them is the CLI's job, on the user's computer.
|
|
5
|
+
*/
|
|
6
|
+
export const STATEMENTS_DIR = "imports/statements";
|
|
7
|
+
export const STATEMENT_PDF_RE = /^imports\/statements\/[^/]+\/\d{4}-(0[1-9]|1[0-2])\.pdf$/;
|
|
8
|
+
export function statementPdfPath(account, close) {
|
|
9
|
+
return `${STATEMENTS_DIR}/${account}/${close.slice(0, 7)}.pdf`;
|
|
10
|
+
}
|
|
11
|
+
/** The statement PDF filed for this card's statement closing on `close`, or null. */
|
|
12
|
+
export function statementPdf(files, account, close) {
|
|
13
|
+
if (!account || account.includes("/") || account.startsWith("."))
|
|
14
|
+
return null;
|
|
15
|
+
const p = statementPdfPath(account, close);
|
|
16
|
+
return STATEMENT_PDF_RE.test(p) && files[p] !== undefined ? p : null;
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ISODate, Ledger } from "../../engine/types.js";
|
|
2
|
+
import type { ModuleDef } from "../types.js";
|
|
3
|
+
import { type CashbackPeriod, type Statement } from "./statements.js";
|
|
4
|
+
export interface CardSummary {
|
|
5
|
+
account: string;
|
|
6
|
+
name: string;
|
|
7
|
+
bank: string | null;
|
|
8
|
+
last4: string | null;
|
|
9
|
+
status: string;
|
|
10
|
+
owed: number;
|
|
11
|
+
limit: number | null;
|
|
12
|
+
/** At least this limit: the most ever owed (when no limit is set). */
|
|
13
|
+
limitAtLeast: number | null;
|
|
14
|
+
used: number | null;
|
|
15
|
+
last: Statement | null;
|
|
16
|
+
current: Statement | null;
|
|
17
|
+
toPay: number;
|
|
18
|
+
due: ISODate | null;
|
|
19
|
+
daysToDue: number | null;
|
|
20
|
+
cashback: CashbackPeriod | null;
|
|
21
|
+
missing: string[];
|
|
22
|
+
}
|
|
23
|
+
export declare function cardList(l: Ledger, asOf: ISODate, o?: {
|
|
24
|
+
includeClosed?: boolean;
|
|
25
|
+
}): CardSummary[];
|
|
26
|
+
export declare const cardsModule: ModuleDef;
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { parse as parseYaml } from "yaml";
|
|
2
|
+
import { holdingsAt } from "../../engine/balances.js";
|
|
3
|
+
import { addDays, addMonths, daysBetween, isDate } from "../../engine/dates.js";
|
|
4
|
+
import { isOpen } from "../util.js";
|
|
5
|
+
import { cashback, cardValues, feeWaiver, isAnnualFee, statements } from "./statements.js";
|
|
6
|
+
import { CARDS_FILE, STATEMENTS_FILE, cardsData, recordFor, termsOf } from "./terms.js";
|
|
7
|
+
import { CARD_TYPES } from "./types.js";
|
|
8
|
+
const KINDS = new Set(["credit", "debit", "atm"]);
|
|
9
|
+
const STATUSES = new Set(["active", "locked", "inactive", "cancelled"]);
|
|
10
|
+
const NETWORKS = new Set(["visa", "mastercard", "amex", "jcb", "napas", "unionpay"]);
|
|
11
|
+
function loadCards(files, issues) {
|
|
12
|
+
const read = (path) => {
|
|
13
|
+
const text = files[path];
|
|
14
|
+
if (text === undefined)
|
|
15
|
+
return [];
|
|
16
|
+
try {
|
|
17
|
+
const v = parseYaml(text);
|
|
18
|
+
if (v == null)
|
|
19
|
+
return [];
|
|
20
|
+
if (!Array.isArray(v)) {
|
|
21
|
+
issues.push({ level: "error", file: path, message: "expected a list" });
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
return v;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
issues.push({ level: "error", file: path, message: `YAML parse error: ${e.message.split("\n")[0]}` });
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return { records: read(CARDS_FILE), statements: read(STATEMENTS_FILE) };
|
|
32
|
+
}
|
|
33
|
+
function checkCards(l) {
|
|
34
|
+
const out = [];
|
|
35
|
+
const err = (file, where, message) => out.push({ level: "error", file, where, message });
|
|
36
|
+
const warn = (file, where, message) => out.push({ level: "warning", file, where, message });
|
|
37
|
+
const { records, statements: bank } = cardsData(l);
|
|
38
|
+
const accounts = new Map(l.accounts.map((a) => [a.id, a]));
|
|
39
|
+
const ids = new Set();
|
|
40
|
+
records.forEach((r, i) => {
|
|
41
|
+
const w = r && typeof r.id === "string" ? r.id : `#${i + 1}`;
|
|
42
|
+
if (!r || typeof r.id !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(r.id))
|
|
43
|
+
return err(CARDS_FILE, w, "id must be letters, digits, . _ -");
|
|
44
|
+
if (ids.has(r.id))
|
|
45
|
+
err(CARDS_FILE, w, "duplicate card id");
|
|
46
|
+
ids.add(r.id);
|
|
47
|
+
if (!KINDS.has(r.kind))
|
|
48
|
+
err(CARDS_FILE, w, "kind must be credit, debit or atm");
|
|
49
|
+
if (r.status !== undefined && !STATUSES.has(r.status))
|
|
50
|
+
err(CARDS_FILE, w, "status must be active, locked, inactive or cancelled");
|
|
51
|
+
if (r.network !== undefined && !NETWORKS.has(String(r.network).toLowerCase()))
|
|
52
|
+
warn(CARDS_FILE, w, `unknown network "${r.network}"`);
|
|
53
|
+
if (r.last4 !== undefined && !/^\d{4}$/.test(String(r.last4)))
|
|
54
|
+
err(CARDS_FILE, w, 'last4 must be exactly 4 digits, quoted ("3584"); never store a full card number');
|
|
55
|
+
for (const k of ["number", "card_number", "pan", "cvv"])
|
|
56
|
+
if (r[k] !== undefined)
|
|
57
|
+
err(CARDS_FILE, w, `${k}: never store card numbers or codes here (only last4)`);
|
|
58
|
+
for (const k of ["issued", "activated", "cancelled", "locked"])
|
|
59
|
+
if (r[k] !== undefined && !isDate(r[k]))
|
|
60
|
+
err(CARDS_FILE, w, `${k} is not a date (yyyy-mm-dd)`);
|
|
61
|
+
for (const k of ["annual_fee", "close_fee", "benefits"])
|
|
62
|
+
if (r[k] !== undefined && (!Number.isInteger(r[k]) || r[k] < 0))
|
|
63
|
+
err(CARDS_FILE, w, `${k} must be a whole amount ≥ 0`);
|
|
64
|
+
if (r.cancel_plan?.by !== undefined && !/^\d{4}-\d{2}(-\d{2})?$/.test(String(r.cancel_plan.by)))
|
|
65
|
+
err(CARDS_FILE, w, "cancel_plan.by: yyyy-mm or yyyy-mm-dd");
|
|
66
|
+
if (r.account !== undefined) {
|
|
67
|
+
const a = accounts.get(r.account);
|
|
68
|
+
if (!a)
|
|
69
|
+
err(CARDS_FILE, w, `unknown account "${r.account}"`);
|
|
70
|
+
else if (r.kind === "credit" && a.type !== "credit_card")
|
|
71
|
+
err(CARDS_FILE, w, `a credit card's account must be a credit_card (${a.id} is ${a.type})`);
|
|
72
|
+
else if (r.status === "cancelled" && !a.closed)
|
|
73
|
+
warn(CARDS_FILE, w, `cancelled, but account ${a.id} is still open (close it with closed: <date>)`);
|
|
74
|
+
}
|
|
75
|
+
else if (r.kind === "credit")
|
|
76
|
+
warn(CARDS_FILE, w, "a credit card should name its account");
|
|
77
|
+
});
|
|
78
|
+
bank.forEach((s, i) => {
|
|
79
|
+
const w = `#${i + 1}`;
|
|
80
|
+
if (!s || !accounts.has(s.account))
|
|
81
|
+
err(STATEMENTS_FILE, w, `unknown account "${s?.account}"`);
|
|
82
|
+
if (!s || !isDate(s.statement_date))
|
|
83
|
+
err(STATEMENTS_FILE, w, "statement_date is not a date");
|
|
84
|
+
if (!s || !Number.isInteger(s.amount))
|
|
85
|
+
err(STATEMENTS_FILE, w, "amount must be a whole amount");
|
|
86
|
+
});
|
|
87
|
+
// the new detail fields
|
|
88
|
+
for (const a of l.accounts) {
|
|
89
|
+
if (a.type !== "credit_card" && !records.some((r) => r.account === a.id && r.kind === "debit"))
|
|
90
|
+
continue;
|
|
91
|
+
const d = { ...(records.find((r) => r.account === a.id && r.kind !== "credit") ?? {}), ...(a.details ?? {}) };
|
|
92
|
+
const cb = d.cashback;
|
|
93
|
+
if (cb !== undefined) {
|
|
94
|
+
if (!cb || typeof cb !== "object")
|
|
95
|
+
warn("data/accounts.yaml", a.id, "details.cashback should be a map");
|
|
96
|
+
else {
|
|
97
|
+
if (cb.base !== undefined && !(typeof cb.base === "number" && cb.base >= 0 && cb.base <= 100))
|
|
98
|
+
warn("data/accounts.yaml", a.id, "cashback.base: a % from 0 to 100");
|
|
99
|
+
for (const [j, r] of (Array.isArray(cb.rules) ? cb.rules : []).entries()) {
|
|
100
|
+
const rule = r;
|
|
101
|
+
const rate = rule?.rate;
|
|
102
|
+
if (rate !== null && rate !== undefined && !(typeof rate === "number" && rate >= 0 && rate <= 100))
|
|
103
|
+
warn("data/accounts.yaml", `${a.id} cashback.rules[${j}]`, "rate: a % from 0 to 100");
|
|
104
|
+
const cats = Array.isArray(rule?.categories) ? rule.categories : [];
|
|
105
|
+
for (const c of cats)
|
|
106
|
+
if (!categoryExists(l, c))
|
|
107
|
+
warn("data/accounts.yaml", `${a.id} cashback.rules[${j}]`, `unknown category "${c}"`);
|
|
108
|
+
}
|
|
109
|
+
for (const k of ["cap", "min_spend", "spend_max"]) {
|
|
110
|
+
const v = cb[k];
|
|
111
|
+
if (v === undefined)
|
|
112
|
+
continue;
|
|
113
|
+
const amt = typeof v === "number" ? v : v?.amount;
|
|
114
|
+
if (!(Number.isInteger(amt) && amt > 0))
|
|
115
|
+
warn("data/accounts.yaml", a.id, `cashback.${k}: a positive whole amount`);
|
|
116
|
+
}
|
|
117
|
+
if (cb.paid_on !== undefined && !/^(next_statement|days_after:\d+)$/.test(String(cb.paid_on)))
|
|
118
|
+
warn("data/accounts.yaml", a.id, "cashback.paid_on: next_statement or days_after:<n>");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const fw = d.fee_waiver;
|
|
122
|
+
if (fw !== undefined && !(fw && typeof fw === "object" && Number.isInteger(fw.spend) && fw.spend > 0))
|
|
123
|
+
warn("data/accounts.yaml", a.id, "fee_waiver: { spend: <amount>, within: year }");
|
|
124
|
+
if (d.fee_month !== undefined && !(Number.isInteger(d.fee_month) && d.fee_month >= 1 && d.fee_month <= 12))
|
|
125
|
+
warn("data/accounts.yaml", a.id, "fee_month: 1 to 12");
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function categoryExists(l, path) {
|
|
130
|
+
const walk = (nodes, parts) => {
|
|
131
|
+
const n = (nodes ?? []).find((x) => x.id === parts[0]);
|
|
132
|
+
if (!n)
|
|
133
|
+
return false;
|
|
134
|
+
return parts.length === 1 || walk(n.children, parts.slice(1));
|
|
135
|
+
};
|
|
136
|
+
const parts = path.split("/");
|
|
137
|
+
return walk(l.categories.expense, parts) || walk(l.categories.income, parts);
|
|
138
|
+
}
|
|
139
|
+
export function cardList(l, asOf, o = {}) {
|
|
140
|
+
const hs = holdingsAt(l, asOf);
|
|
141
|
+
const out = [];
|
|
142
|
+
for (const a of l.accounts) {
|
|
143
|
+
if (a.type !== "credit_card" || (!o.includeClosed && !isOpen(a, asOf)))
|
|
144
|
+
continue;
|
|
145
|
+
const rec = recordFor(l, a.id);
|
|
146
|
+
const t = termsOf(a, rec);
|
|
147
|
+
const st = statements(l, a.id, asOf);
|
|
148
|
+
const last = [...st].reverse().find((s) => !s.open) ?? null;
|
|
149
|
+
const current = st.find((s) => s.open) ?? null;
|
|
150
|
+
const cb = cashback(l, a.id, asOf).find((p) => p.open) ?? null;
|
|
151
|
+
const owed = -(hs.get(a.id)?.amount ?? 0);
|
|
152
|
+
let maxOwed = 0;
|
|
153
|
+
if (t.limit === null) {
|
|
154
|
+
let b = 0;
|
|
155
|
+
for (const x of l.transactions) {
|
|
156
|
+
if (x.void || x.date > asOf)
|
|
157
|
+
continue;
|
|
158
|
+
for (const ln of x.lines)
|
|
159
|
+
if (ln.account === a.id)
|
|
160
|
+
b += ln.amount;
|
|
161
|
+
maxOwed = Math.max(maxOwed, -b);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const missing = [];
|
|
165
|
+
if (!t.statementDay)
|
|
166
|
+
missing.push("statement_day");
|
|
167
|
+
if (!t.dueDay)
|
|
168
|
+
missing.push("due_day");
|
|
169
|
+
if (t.limit === null)
|
|
170
|
+
missing.push("limit");
|
|
171
|
+
if (t.cashback?.rules.some((r) => r.rate === null))
|
|
172
|
+
missing.push("cashback_rates");
|
|
173
|
+
out.push({
|
|
174
|
+
account: a.id,
|
|
175
|
+
name: a.name,
|
|
176
|
+
bank: t.bank,
|
|
177
|
+
last4: t.last4,
|
|
178
|
+
status: rec?.status ?? (isOpen(a, asOf) ? "active" : "cancelled"),
|
|
179
|
+
owed,
|
|
180
|
+
limit: t.limit,
|
|
181
|
+
limitAtLeast: t.limit === null && maxOwed > 0 ? maxOwed : null,
|
|
182
|
+
used: t.limit ? owed / t.limit : null,
|
|
183
|
+
last,
|
|
184
|
+
current,
|
|
185
|
+
toPay: last?.toPay ?? 0,
|
|
186
|
+
due: last?.due ?? null,
|
|
187
|
+
daysToDue: last?.due ? daysBetween(asOf, last.due) : null,
|
|
188
|
+
cashback: cb,
|
|
189
|
+
missing,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const key = (c) => (c.toPay > 0 && c.due ? c.due : "9999-99-99");
|
|
193
|
+
return out.sort((x, y) => (key(x) < key(y) ? -1 : key(x) > key(y) ? 1 : y.owed - x.owed));
|
|
194
|
+
}
|
|
195
|
+
/* ------------------------------------------------------------------ Needs you */
|
|
196
|
+
function cardAttention(l, asOf) {
|
|
197
|
+
const out = [];
|
|
198
|
+
const link = (id) => `#/cards/${encodeURIComponent(id)}`;
|
|
199
|
+
for (const c of cardList(l, asOf)) {
|
|
200
|
+
const s = c.last;
|
|
201
|
+
if (s && s.due && s.toPay > 0) {
|
|
202
|
+
const days = daysBetween(asOf, s.due);
|
|
203
|
+
if (s.status === "due" && days <= 7) {
|
|
204
|
+
out.push({ kind: "card_due", module: "cards", severity: days <= 3 ? "red" : "amber", date: s.due, title: `Card ${c.name}: pay ${s.toPay} by ${s.due}`, amount: s.toPay, account: c.account, name: c.name, days, link: link(c.account) });
|
|
205
|
+
}
|
|
206
|
+
else if (s.status === "partly" || s.status === "unpaid") {
|
|
207
|
+
out.push({ kind: "card_overdue", module: "cards", severity: "red", date: s.due, title: `Card ${c.name}: statement of ${s.close} not fully paid (due ${s.due}, ${s.toPay} left)`, amount: s.toPay, account: c.account, name: c.name, days, link: link(c.account) });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (s && s.status === "late" && daysBetween(s.due, asOf) <= 30) {
|
|
211
|
+
out.push({ kind: "card_paid_late", module: "cards", severity: "info", date: s.due, title: `Card ${c.name}: the statement of ${s.close} was paid after its due date`, account: c.account, name: c.name, params: { close: s.close }, link: link(c.account) });
|
|
212
|
+
}
|
|
213
|
+
if (s && s.bankDiff !== null && Math.abs(s.bankDiff) > 1000) {
|
|
214
|
+
out.push({ kind: "card_bank_diff", module: "cards", severity: "amber", date: s.close, title: `Card ${c.name}: computed ${(s.bank ?? 0) + s.bankDiff} vs the bank's ${s.bank} for ${s.close}`, amount: s.bankDiff, account: c.account, name: c.name, params: { bank: s.bank, close: s.close }, link: link(c.account), ask: true });
|
|
215
|
+
}
|
|
216
|
+
if (c.used !== null && c.used >= 0.8) {
|
|
217
|
+
out.push({ kind: "card_near_limit", module: "cards", severity: "amber", date: asOf, title: `Card ${c.name} is at ${Math.round(c.used * 100)}% of its limit`, amount: c.owed, account: c.account, name: c.name, params: { pct: Math.round(c.used * 100), limit: c.limit }, link: link(c.account) });
|
|
218
|
+
}
|
|
219
|
+
// cashback: expected for a closed period but not received by when the bank pays it
|
|
220
|
+
for (const p of cashback(l, c.account, asOf)) {
|
|
221
|
+
if (p.open || p.expected <= 0 || p.received >= p.expected || p.payBy >= asOf || daysBetween(p.payBy, asOf) > 45)
|
|
222
|
+
continue;
|
|
223
|
+
out.push({ kind: "cashback_missing", module: "cards", severity: "amber", date: p.payBy, title: `Card ${c.name}: expected cashback ${p.expected} for ${p.from}…${p.close}, received ${p.received}`, amount: p.expected - p.received, account: c.account, name: c.name, params: { from: p.from, close: p.close, expected: p.expected, received: p.received }, link: link(c.account), ask: true });
|
|
224
|
+
}
|
|
225
|
+
const cb = c.cashback;
|
|
226
|
+
if (cb && cb.cap !== null && cb.capLeft !== null && cb.capLeft <= cb.cap * 0.1 && cb.expected > 0) {
|
|
227
|
+
out.push({ kind: "cashback_cap", module: "cards", severity: "info", date: asOf, title: `Card ${c.name}: cashback cap almost used (${cb.capLeft} left)`, amount: cb.capLeft, account: c.account, name: c.name, link: link(c.account) });
|
|
228
|
+
}
|
|
229
|
+
if (cb && cb.minSpend !== null && !cb.minMet && c.current) {
|
|
230
|
+
const end = c.current ? nextCloseOf(l, c.account, asOf) : null;
|
|
231
|
+
const left = end ? daysBetween(asOf, end) : null;
|
|
232
|
+
if (left !== null && left <= 10)
|
|
233
|
+
out.push({ kind: "cashback_min", module: "cards", severity: "info", date: end, title: `Card ${c.name}: spend ${cb.needForMin} more in ${left} days to get cashback`, amount: cb.needForMin, account: c.account, name: c.name, days: left, link: link(c.account) });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// fee waivers and annual fees
|
|
237
|
+
for (const a of l.accounts) {
|
|
238
|
+
if (a.type !== "credit_card" || !isOpen(a, asOf))
|
|
239
|
+
continue;
|
|
240
|
+
const fw = feeWaiver(l, a.id, asOf);
|
|
241
|
+
if (fw && !fw.met && !fw.onTrack && fw.daysLeft <= 30) {
|
|
242
|
+
out.push({ kind: "fee_waiver_risk", module: "cards", severity: fw.daysLeft <= 7 ? "red" : "amber", date: fw.deadline, title: `Card ${a.name}: spend ${fw.needed} more by ${fw.deadline} to waive the annual fee`, amount: fw.needed, account: a.id, name: a.name, days: fw.daysLeft, link: link(a.id) });
|
|
243
|
+
}
|
|
244
|
+
for (const t of l.transactions) {
|
|
245
|
+
if (t.void || t.date > asOf || daysBetween(t.date, asOf) > 30 || !isAnnualFee(t) || !t.lines.some((ln) => ln.account === a.id))
|
|
246
|
+
continue;
|
|
247
|
+
const prev = feeWaiver(l, a.id, addDays(t.date, -1));
|
|
248
|
+
out.push({ kind: "annual_fee_charged", module: "cards", severity: prev?.met ? "amber" : "info", date: t.date, title: `Card ${a.name}: annual fee charged on ${t.date}${prev?.met ? " although the waiver target was met: ask the bank for a refund" : ""}`, account: a.id, name: a.name, params: { met: prev?.met ? 1 : 0 }, link: link(a.id), ask: Boolean(prev?.met) });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// lifecycle and cancel plans (cards.yaml)
|
|
252
|
+
const hs = holdingsAt(l, asOf);
|
|
253
|
+
for (const r of cardsData(l).records) {
|
|
254
|
+
const a = r.account ? l.accounts.find((x) => x.id === r.account) : undefined;
|
|
255
|
+
const name = r.name ?? a?.name ?? r.id;
|
|
256
|
+
if (r.cancel_plan?.by && r.status !== "cancelled") {
|
|
257
|
+
const by = r.cancel_plan.by.length === 7 ? `${r.cancel_plan.by}-01` : r.cancel_plan.by;
|
|
258
|
+
const days = daysBetween(asOf, by);
|
|
259
|
+
if (days <= 30 && days >= -30)
|
|
260
|
+
out.push({ kind: "card_cancel_plan", module: "cards", severity: days <= 7 ? "red" : "amber", date: by, title: `${name}: planned cancel by ${r.cancel_plan.by}. Call ${r.bank ?? "the bank"} to cancel or ask for a fee waiver`, name, days, params: { bank: r.bank ?? null, note: r.cancel_plan.note ?? null }, link: a ? link(a.id) : "#/cards" });
|
|
261
|
+
}
|
|
262
|
+
const t = a ? termsOf(a, r) : null;
|
|
263
|
+
const feeMonth = t?.feeMonth ?? null;
|
|
264
|
+
if (feeMonth && (r.cancel_plan || r.status === "locked" || r.status === "inactive") && !r.keep_for_limit && (r.annual_fee ?? t?.annualFee ?? 0) > 0 && r.status !== "cancelled") {
|
|
265
|
+
const [y, m] = [Number(asOf.slice(0, 4)), Number(asOf.slice(5, 7))];
|
|
266
|
+
const next = `${feeMonth >= m ? y : y + 1}-${String(feeMonth).padStart(2, "0")}-01`;
|
|
267
|
+
const days = daysBetween(asOf, next);
|
|
268
|
+
if (days <= 30)
|
|
269
|
+
out.push({ kind: "card_fee_soon", module: "cards", severity: "amber", date: next, title: `${name}: annual fee ${r.annual_fee ?? t?.annualFee} due in ${next.slice(0, 7)} for a card you're not keeping`, amount: r.annual_fee ?? t?.annualFee ?? undefined, name, days, link: a ? link(a.id) : "#/cards" });
|
|
270
|
+
}
|
|
271
|
+
if (r.status === "cancelled" && a && !a.closed)
|
|
272
|
+
out.push({ kind: "card_cancelled_open", module: "cards", severity: "amber", date: asOf, title: `${name} is cancelled but its account ${a.id} is still open`, account: a.id, name, link: link(a.id), ask: true });
|
|
273
|
+
if (r.status === "locked" && a && (hs.get(a.id)?.amount ?? 0) !== 0)
|
|
274
|
+
out.push({ kind: "card_locked_balance", module: "cards", severity: "info", date: asOf, title: `${name} is locked but has a balance of ${-(hs.get(a.id)?.amount ?? 0)}`, amount: -(hs.get(a.id)?.amount ?? 0), account: a.id, name, link: link(a.id) });
|
|
275
|
+
}
|
|
276
|
+
// cards that cost more than they gave in the last 12 months, before the fee month
|
|
277
|
+
for (const v of cardValues(l, asOf)) {
|
|
278
|
+
if (v.status === "cancelled" || v.last12.net >= 0 || !v.feeMonth)
|
|
279
|
+
continue;
|
|
280
|
+
const [y, m] = [Number(asOf.slice(0, 4)), Number(asOf.slice(5, 7))];
|
|
281
|
+
const next = `${v.feeMonth >= m ? y : y + 1}-${String(v.feeMonth).padStart(2, "0")}-01`;
|
|
282
|
+
const days = daysBetween(asOf, next);
|
|
283
|
+
if (days <= 60)
|
|
284
|
+
out.push({ kind: "card_not_worth", module: "cards", severity: "info", date: next, title: `${v.name}: cost ${-v.last12.net} more than it gave in the last 12 months; fee month ${next.slice(0, 7)}`, amount: v.last12.net, account: v.account ?? undefined, name: v.name, days, link: v.account ? link(v.account) : "#/cards", ask: true });
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
function nextCloseOf(l, account, asOf) {
|
|
289
|
+
const a = l.accounts.find((x) => x.id === account);
|
|
290
|
+
const sd = a ? termsOf(a, recordFor(l, account)).statementDay : null;
|
|
291
|
+
if (!sd)
|
|
292
|
+
return null;
|
|
293
|
+
const [y, m] = [Number(asOf.slice(0, 4)), Number(asOf.slice(5, 7))];
|
|
294
|
+
const c = `${y}-${String(m).padStart(2, "0")}-${String(Math.min(sd, new Date(Date.UTC(y, m, 0)).getUTCDate())).padStart(2, "0")}`;
|
|
295
|
+
return c >= asOf ? c : addMonths(c, 1, sd);
|
|
296
|
+
}
|
|
297
|
+
export const cardsModule = {
|
|
298
|
+
name: "cards",
|
|
299
|
+
types: CARD_TYPES,
|
|
300
|
+
on: (l, asOf, files) => l.accounts.some((a) => a.type in CARD_TYPES && isOpen(a, asOf)) || cardsData(l).records.length > 0 || Boolean(files?.[CARDS_FILE]),
|
|
301
|
+
load: loadCards,
|
|
302
|
+
check: checkCards,
|
|
303
|
+
attention: cardAttention,
|
|
304
|
+
};
|