// FRAZIER Finance OS — Calendar page (#calendar), master sheet (8 Oct 2026). // One calendar month (period) per view, read-only. Data: period.calendar from // fin_sheet_dashboard (CONTRACT §2.2) through useFinSheet() / useFinPeriod(); // no other endpoint. Every row links back to its cell in Google Sheets. // ONE finance sheet covers every date: it is shown once, above the month. // // Views: // desktop / tablet (> 768px) Mon–Sun month grid. // phone (≤ 768px) agenda list (one row per day with activity). // ‹ › step through the months of the sheet (earliest month with a dated row // to the latest, always including this month). A month with no dated rows, // or a month outside that range asked for in the hash (?p=YYYY-MM), shows an // intentional "No rows dated in …" state, never a grid of zeros. // Live updates swap the payload in place: the open day stays open (it is // kept by its date), and it closes only when that month is gone. // Marker kinds, in display order. tone = the category key dot (legend). const FIN_CAL_KINDS = [ { id: 'issued', list: 'invoicesIssued', label: 'Invoices issued', key: 'Issued', short: 'Billed', one: 'invoice', many: 'invoices', tone: 'violet', amount: (t) => t.billed }, { id: 'paid', list: 'invoicesPaid', label: 'Payments received', key: 'Paid', short: 'Paid', one: 'payment', many: 'payments', tone: 'ok', amount: (t) => t.collected }, { id: 'in', list: 'bankIn', label: 'Bank in', key: 'Bank in', short: 'In', one: 'entry', many: 'entries', tone: 'info', amount: (t) => t.in }, { id: 'out', list: 'bankOut', label: 'Bank out', key: 'Bank out', short: 'Out', one: 'entry', many: 'entries', tone: 'neutral', amount: (t) => t.out }, { id: 'bill', list: 'vendorBills', label: 'Vendor bills', key: 'Bills', short: 'Bill', one: 'bill', many: 'bills', tone: 'warn', amount: (t) => t.vendorBilled }, { id: 'jv', list: 'journal', label: 'Journal lines', key: 'Journal', short: 'JV', one: 'line', many: 'lines', tone: 'ring', amount: null }, ]; const FIN_CAL_WD = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; const FIN_CAL_WD_LONG = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; // ── Date maths on 'YYYY-MM-DD' strings (UTC, so no timezone shift) ────── function finCalMs(iso) { const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(iso || '')); return m ? Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), 12) : NaN; } function finCalIso(ms) { const d = new Date(ms); return d.getUTCFullYear() + '-' + String(d.getUTCMonth() + 1).padStart(2, '0') + '-' + String(d.getUTCDate()).padStart(2, '0'); } function finCalAddDays(iso, n) { return finCalIso(finCalMs(iso) + n * 86400000); } // 1 = Monday … 7 = Sunday function finCalDow(iso) { const d = new Date(finCalMs(iso)).getUTCDay(); return d === 0 ? 7 : d; } function finCalDayNum(iso) { return Number(String(iso).slice(8, 10)); } function finCalLongDate(iso) { return FIN_CAL_WD_LONG[finCalDow(iso) - 1] + ', ' + finCalDayNum(iso) + ' ' + FIN_MON_LONG[Number(String(iso).slice(5, 7)) - 1] + ' ' + String(iso).slice(0, 4); } function finCalPlural(n, one, many) { return n + ' ' + (n === 1 ? one : many); } // Weeks (arrays of 7 ISO dates, Mon–Sun) covering one calendar month. function finCalWeeks(ym) { const first = ym + '-01'; const last = finMonthEnd(ym); let d = finCalAddDays(first, 1 - finCalDow(first)); const end = finCalAddDays(last, 7 - finCalDow(last)); const weeks = []; let w = []; for (let guard = 0; d <= end && guard < 50; guard++) { w.push(d); if (w.length === 7) { weeks.push(w); w = []; } d = finCalAddDays(d, 1); } return weeks; } function finCalUnitLabel(p) { return p ? (p.label || finMonthLabel(p.key)) : ''; } // Markers of one day: [{ kind, count, amount }] for the kinds present. function finCalMarkers(day) { if (!day || !day.hasActivity) return []; const c = day.counts || {}; const t = day.totals || {}; return FIN_CAL_KINDS.map((k) => { const n = Number(c[k.list]) || 0; if (!n) return null; return { kind: k, count: n, amount: k.amount ? Number(k.amount(t)) || 0 : null }; }).filter(Boolean); } function finCalMarkerText(m, full) { const k = m.kind; if (!k.amount) return k.label + ': ' + finCalPlural(m.count, k.one, k.many); return k.label + ': ' + finCalPlural(m.count, k.one, k.many) + ', ' + (full ? finIDR(m.amount) : finIDRCompact(m.amount)); } function finCalDayAria(iso, day, bankUnknown) { const parts = finCalMarkers(day).map((m) => finCalMarkerText(m, true)); return finCalLongDate(iso) + (parts.length ? ': ' + parts.join('; ') : ': nothing recorded') + (bankUnknown ? '; bank in and out not known, the Bank Book has no entries for this day yet' : ''); } // A section whose tab is missing from the sheet: never a zero. function finCalSectionGone(period, key) { const s = period ? period[key] : null; if (!s) return 'Not in the sheet'; if (s.available === false) return s.reason || 'Not in the sheet'; return null; } // Bank Book coverage of the period, read the same way as Home's Money in / // out / net: when its last entry is before the period starts, the month's // bank movement is UNKNOWN (never zero). partial = it covers only the first // part of the period (through knownThrough). // { ok, gone (no Bank Book tab), reason, knownThrough, partial } function finCalBankState(period, cal) { const bank = period && period.bank; if (!bank || bank.available === false || (cal && !cal.bankAvailable)) { return { ok: false, gone: true, reason: finCalSectionGone(period, 'bank') || 'No Bank Book tab in the sheet', knownThrough: null }; } const kt = bank.knownThrough || (cal && cal.bankKnownThrough) || null; if (!kt || kt < period.start) { return { ok: false, gone: false, reason: kt ? 'No Bank Book entries since ' + finDate(kt) : 'No Bank Book entries yet', knownThrough: kt }; } const asOf = (period.kpis && period.kpis.asOfDate) || period.end; return { ok: true, gone: false, reason: null, knownThrough: kt, partial: kt < asOf }; } // A past or current day after the Bank Book's last entry: its bank in / out // are not known (the sheet has not been filled in that far), not zero. // Future days are already dimmed and say nothing about money. function finCalBankUnknownOn(iso, bs, cal) { if (!bs || bs.gone) return false; if (cal && cal.today && iso > cal.today) return false; return !bs.knownThrough || iso > bs.knownThrough; } // ── Small parts ───────────────────────────────────────────────────────── function FinCalDot({ tone }) { return