// FRAZIER Finance OS — Balance Sheet + Profit & Loss (master sheet, 8 Oct 2026). // Read-only. Both statements come from the shared sheet payload // (useFinSheet / useFinPeriod, CONTRACT §2.2 period.pl / period.bs), which // the server computes from the JV tab of the finance sheet (one sheet for // every date), laid out like the owner's PL 2026 / BS 2026 tabs (Indonesian // labels kept as written). // P&L: the selected month, or "Year to date" (added up here from // data.year.ledger.byAccountMonth with the same rules as the server), or // "All months" from the top selector (data.year.pl). // Balance sheet: as of the selected month's as-of date (period.bs). // Never zeros for nothing: a missing JV tab or a month with no dated rows // says so with a link to the sheet; stale copies carry "as of". Export CSV // + Print are client-side. Live updates keep the scope and the toggle. // Every top-level name in this file starts with finRep / FinRep / FIN_REP. finPeriodAllowAll('profit-loss'); // English glosses next to the owner's Indonesian section labels. const FIN_REP_EN = { revenue: 'Revenue', cogs: 'Cost of services', opex: 'Operating expenses', other: 'Other income & expenses', current_assets: 'Current assets', fixed_assets: 'Fixed assets', current_liabilities: 'Current liabilities', long_term_liabilities: 'Long-term liabilities', equity: 'Equity', assets: 'Assets', liabilitiesEquity: 'Liabilities & equity', gross_profit: 'Gross profit', net_income: 'Net profit before tax', }; function finRepEn(key) { const k = String(key || ''); return FIN_REP_EN[k] || FIN_REP_EN[k.replace(/_\d+$/, '')] || null; } function finRepBase(key) { return String(key || '').split('_')[0]; } // Money as the server rounds it (at most 2 decimals). function finRepM(x) { return Math.round((Number(x) || 0) * 100) / 100; } // 32.04 → "32,0%" (the app's number locale: comma decimals, true minus). function finRepPct(x) { if (x === null || x === undefined || !isFinite(x)) return '—'; const r = Math.round(x * 10) / 10; return (r < 0 ? '−' : '') + Math.abs(r).toFixed(1).replace('.', ',') + '%'; } // '2026-01-01','2026-10-07' → "1 Jan – 7 Oct 2026"; same month → "1–31 Oct 2026". function finRepRangeLabel(f, t) { const a = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(f || '')); const b = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(t || '')); if (!a || !b) return finDate(f) + ' – ' + finDate(t); if (a[1] === b[1] && a[2] === b[2]) return (a[3] === b[3] ? '' : Number(a[3]) + '–') + finDate(t); if (a[1] === b[1]) return finDate(f, { year: false }) + ' – ' + finDate(t); return finDate(f) + ' – ' + finDate(t); } // ── P&L maths (mirror of fin_sheet_pl_compute in api/lib/fin-sheet-derive.php) // layout = statements.pl; amt = { code: debit − credit } over the range. function finRepPlCompute(layout, amt, from, to) { const res = { from, to, sections: [], revenue: 0, cogs: 0, opex: 0, otherIncome: 0, otherExpense: 0 }; let net = 0; ((layout && layout.sections) || []).forEach((s) => { const base = finRepBase(s.key); const sec = { key: s.key, label: s.label, totalLabel: s.totalLabel, total: 0, accounts: [] }; (s.accounts || []).forEach((a) => { const has = Object.prototype.hasOwnProperty.call(amt, a.code); const dr = has ? amt[a.code] : 0; const natural = a.signToNet > 0 ? -dr : dr; // revenue / other income: credit − debit sec.accounts.push({ code: a.code, label: a.label, amount: finRepM(natural), signToNet: a.signToNet, active: has }); sec.total += base === 'other' ? a.signToNet * natural : natural; net += a.signToNet * natural; if (a.category === 'revenue') res.revenue += natural; else if (a.category === 'cogs') res.cogs += natural; else if (a.category === 'opex') res.opex += natural; else if (a.category === 'other_income') res.otherIncome += natural; else res.otherExpense += natural; }); sec.total = finRepM(sec.total); res.sections.push(sec); }); ['revenue', 'cogs', 'opex', 'otherIncome', 'otherExpense'].forEach((k) => { res[k] = finRepM(res[k]); }); res.grossProfit = finRepM(res.revenue - res.cogs); res.otherNet = finRepM(res.otherIncome - res.otherExpense); res.netIncome = finRepM(net); return res; } // Debit − credit per account over the given period keys. function finRepLedgerAmounts(byAccountMonth, keys) { const amt = {}; Object.keys(byAccountMonth || {}).forEach((code) => { const per = byAccountMonth[code] || {}; keys.forEach((k) => { const v = per[k]; if (!v) return; amt[code] = (amt[code] || 0) + (Number(v.debit) || 0) - (Number(v.credit) || 0); }); }); return amt; } // Year to date for the selected month: every month of the same year up to // it. The months are calendar months derived from the sheet's own dates, so // a month the sheet has no row in has no journal line either: nothing is // left out. function finRepYtd(data, period) { const year = String(period.key).slice(0, 4); const list = (data.periods || []).filter((p) => String(p.key).slice(0, 4) === year && p.start <= period.start); const layout = data.statements && data.statements.pl; const by = data.year && data.year.ledger && data.year.ledger.byAccountMonth; if (!layout || !by) return null; const from = list.length ? list[0].start : period.start; const pl = finRepPlCompute(layout, finRepLedgerAmounts(by, list.map((p) => p.key)), from, period.end); return { pl, from, to: period.end, months: list.length }; } // One calendar year of the sheet (several years in one grand sheet): every // month of that year the sheet covers, added up the same way. function finRepYear(data, year) { const list = (data.periods || []).filter((p) => String(p.key).slice(0, 4) === year); const layout = data.statements && data.statements.pl; const by = data.year && data.year.ledger && data.year.ledger.byAccountMonth; if (!list.length || !layout || !by) return null; const from = list[0].start; const to = list[list.length - 1].end; const pl = finRepPlCompute(layout, finRepLedgerAmounts(by, list.map((p) => p.key)), from, to); return { pl, from, to, scope: 'year', year, months: list.length }; } // The P&L in view: { pl, from, to, scope, months } or null. // scope: 'month' | 'ytd' (a month selected) · 'all' | 'y2025' (All months). function finRepPlModel(data, period, isAll, scope) { if (isAll) { if (/^y\d{4}$/.test(String(scope || ''))) { const yr = finRepYear(data, String(scope).slice(1)); if (yr) return yr; } const y = data.year || {}; if (!y.pl) return null; return { pl: y.pl, from: y.from || y.pl.from, to: y.to || y.pl.to, scope: 'all', months: (data.periods || []).length }; } if (!period || !period.pl) return null; if (scope === 'ytd') { const y = finRepYtd(data, period); if (y) return Object.assign({ scope: 'ytd' }, y); } return { pl: period.pl, from: period.pl.from || period.start, to: period.pl.to || period.end, scope: 'month', months: 1 }; } // Rows the P&L table renders (and the CSV exports). // { kind:'sec', key, label, en } · { kind:'line', code, label, amount, active, notInCoa } // { kind:'total', label, amount } · { kind:'sub', key, label, en, amount } function finRepPlRows(pl, statements) { const inCoa = {}; const layout = statements && statements.pl; ((layout && layout.sections) || []).forEach((s) => (s.accounts || []).forEach((a) => { inCoa[a.code] = a.inCoa; })); const subs = (layout && layout.subtotals) || []; const gp = subs.find((s) => s.key === 'gross_profit') || { key: 'gross_profit', label: 'LABA KOTOR', after: 'cogs' }; const ni = subs.find((s) => s.key === 'net_income') || { key: 'net_income', label: 'LABA BERSIH SEBELUM PAJAK' }; const secs = pl.sections || []; let gpAt = -1; secs.forEach((s, i) => { if (finRepBase(s.key) === (gp.after || 'cogs')) gpAt = i; }); if (gpAt === -1) secs.forEach((s, i) => { if (['revenue', 'cogs'].indexOf(finRepBase(s.key)) !== -1) gpAt = i; }); const rows = []; secs.forEach((s, i) => { const other = finRepBase(s.key) === 'other'; rows.push({ kind: 'sec', key: s.key, label: s.label, en: finRepEn(s.key) }); (s.accounts || []).forEach((a) => { // In the mixed "other" section each line shows its effect on profit // (income +, expense −) so the lines add up to the section total. const amount = other ? finRepM(a.signToNet * a.amount) : a.amount; rows.push({ kind: 'line', code: a.code, label: a.label, amount, active: !!a.active, notInCoa: inCoa[a.code] === false }); }); rows.push({ kind: 'total', key: s.key, label: s.totalLabel || 'TOTAL ' + s.label, amount: s.total }); if (i === gpAt) rows.push({ kind: 'sub', key: 'gross_profit', label: gp.label, en: finRepEn('gross_profit'), amount: pl.grossProfit }); }); return { rows, grand: { label: ni.label, en: finRepEn('net_income'), amount: pl.netIncome } }; } // Rows of one balance-sheet side. function finRepBsRows(side) { const rows = []; (side.sections || []).forEach((s) => { rows.push({ kind: 'sec', key: s.key, label: s.label, en: finRepEn(s.key) }); (s.lines || []).forEach((l) => rows.push({ kind: 'line', code: l.code, special: l.special, label: l.label, amount: l.amount, active: Number(l.amount) !== 0 || !!l.special })); rows.push({ kind: 'total', key: s.key, label: s.totalLabel || 'TOTAL ' + s.label, amount: s.total }); }); return rows; } function finRepCsvAmt(x) { return finRepM(x); } // ── Per-viewer convenience: hide accounts with nothing booked ──────────── const FIN_REP_HIDE_KEY = 'fin-rep-hide-empty'; function finRepUseHideEmpty() { const [hide, setHide] = React.useState(() => { try { return window.localStorage.getItem(FIN_REP_HIDE_KEY) === '1'; } catch (e) { return false; } }); const set = (v) => { setHide(v); try { window.localStorage.setItem(FIN_REP_HIDE_KEY, v ? '1' : '0'); } catch (e) { /* private window: keep it for this view only */ } }; return [hide, set]; } // ── Pieces ────────────────────────────────────────────────────────────── function FinRepActions({ period, onExport, ready }) { return ( <> {ready ? ( <> ) : null} ); } function FinRepLine({ row, withPct, pctBase }) { const amt = Number(row.amount) || 0; const tag = row.special === 'current_year_result' ? { t: 'From P&L', tip: 'Profit or loss of this year, computed from the revenue and expense accounts in the JV tab' } : row.special === 'retained_earnings' ? { t: 'Earlier years', tip: 'Profit or loss of earlier years in the JV tab' } : row.notInCoa ? { t: 'Not in COA', tip: 'The PL tab lists this account but the COA tab does not', warn: true } : null; return (
{row.code || '—'} {row.label} {tag ? {tag.t} : null}
{finIDR(amt)} {withPct ? {pctBase && row.active ? finRepPct((amt / pctBase) * 100) : '—'} : null} ); } function FinRepTotal({ row, withPct, pctBase }) { const sub = row.kind === 'sub'; const amt = Number(row.amount) || 0; return (
{row.label}{sub && row.en ? {row.en} : null}
{finIDR(amt)} {withPct ? {pctBase ? finRepPct((amt / pctBase) * 100) : '—'} : null} ); } // One statement table from a row list. hideEmpty drops lines with nothing // booked (a section left with no line says so). grand → tfoot (P&L). function FinRepTable({ rows, amountHead, withPct, pctBase, grand, label, hideEmpty }) { const cols = withPct ? 3 : 2; const out = []; let shown = 0; let secAt = -1; rows.forEach((r, i) => { if (r.kind === 'sec') { shown = 0; secAt = i; out.push( {r.label}{r.en ? {r.en} : null} ); return; } if (r.kind === 'line') { if (hideEmpty && !r.active && !(Number(r.amount))) return; shown += 1; out.push(); return; } if (r.kind === 'total' && secAt !== -1 && !shown) { out.push(Nothing booked in these accounts.); } out.push(); }); return ( {withPct ? : null} {out} {grand ? ( {withPct ? : null} ) : null}
Account {amountHead}% of revenue
{grand.label} {grand.en ? {grand.en} : null} {grand.meta ? {grand.meta} : null}
{finIDR(grand.amount)}{grand.pct}
); } // The statement is missing from the data for this view (should not // happen): say so, never show zeros. function FinRepMissing({ what, meta }) { const url = meta && meta.sheet && meta.sheet.url; return ( : null} /> ); } // The finance sheet has no JV tab. function FinRepNoJournal({ period, meta }) { const j = (period && period.journal) || {}; const url = meta && meta.sheet && meta.sheet.url; return ( : null} /> ); } // Reconciliation notes: how the JV tab compares with the other tabs. function finRepNotes(period, statements, order) { const notes = []; (period.reconciliation || []).forEach((r) => { if (r.key !== 'no_opening_balances') notes.push(r); }); const seen = {}; ((statements && statements.pl && statements.pl.sections) || []).forEach((s) => (s.accounts || []).forEach((a) => { if (a.inCoa === false && !seen[a.code]) { seen[a.code] = true; notes.push({ key: 'not_in_coa_' + a.code, kind: 'not_in_coa', level: 'info', code: a.code, label: a.sheetLabel || a.label, links: { coa: finSheetTabUrl(period, 'coa'), pl: finSheetTabUrl(period, 'pl') }, }); } })); const rank = (k) => { const i = order.indexOf(String(k).replace(/_\d+$/, '').replace(/^not_in_coa.*/, 'not_in_coa')); return i === -1 ? 99 : i; }; return notes.sort((a, b) => rank(a.key) - rank(b.key)); } const FIN_REP_TAB_LABEL = { bank: 'Open Bank Book', jv: 'Open JV tab', sales: 'Open Sales tab', coa: 'Open COA tab', pl: 'Open PL tab', bs: 'Open BS tab', vendor: 'Open Vendor tab' }; function FinRepNote({ note, period }) { const v = note.values || {}; let title = note.title; let body = note.message; let rows = null; if (note.key === 'cash_bank_vs_jv') { title = 'Kas & Bank: Bank Book vs JV tab'; body = 'Through ' + finDate(v.asOf || period.end) + '. Cash everywhere else in the app follows the Bank Book; this statement follows the JV tab.'; rows = [ { id: 'b', label: 'Bank Book', value: finIDR(v.bankBook) }, v.jvOpening !== null && v.jvOpening !== undefined ? { id: 'jo', label: 'JV starting balance', note: 'first year row, 111201', value: finIDR(v.jvOpening), tone: 'muted' } : null, { id: 'j', label: 'Kas & Bank (111201) in JV', value: finIDR(v.jv) }, { id: 'd', label: 'Difference', value: finIDR(v.diff), tone: Number(v.diff) ? 'warn' : 'ok', strong: true, sep: true }, ]; } else if (note.key === 'sales_vs_jv_revenue') { title = 'Sales recap vs revenue in JV · ' + (period.label || finMonthLabel(period.key)); body = 'The P&L follows the JV tab, so revenue there is what was journalled, not what was invoiced.'; rows = [ { id: 's', label: 'Sales DPP (Sales tab)', value: finIDR(v.salesDpp) }, Number(v.salesDppWithoutAddons) !== Number(v.salesDpp) ? { id: 'a', label: 'Without add-on lines', value: finIDR(v.salesDppWithoutAddons) } : null, { id: 'j', label: 'Revenue booked in JV', value: finIDR(v.jvRevenue) }, { id: 'd', label: 'Difference', value: finIDR(v.diff), tone: Number(v.diff) ? 'warn' : 'ok', strong: true, sep: true }, ]; } else if (note.key === 'sales_in_jv') { title = 'Invoices in the JV tab · ' + (period.label || finMonthLabel(period.key)); body = (v.inJournal !== undefined ? v.inJournal + ' of ' + v.invoices + ' invoices of this month are in the JV tab' : note.message) + ' (matched on the invoice number in column D).'; } else if (note.kind === 'not_in_coa') { title = note.code + ' "' + note.label + '" is not in the COA tab'; body = 'The PL tab lists this account, the COA tab does not. It is shown on the P&L under its PL-tab name with a "Not in COA" tag.'; } const links = Object.keys(note.links || {}).filter((k) => note.links[k]); return (
  • {title}
    {body ?

    {body}

    : null} {rows ? : null} {links.length ? (
    {links.map((k) => )}
    ) : null}
  • ); } function FinRepNotes({ period, statements, order, sheetTitle }) { const notes = finRepNotes(period, statements, order); if (!notes.length) return null; const sheet = sheetTitle ? '“' + sheetTitle + '”' : 'the finance sheet'; return (
      {notes.map((n) => )}
    ); } function FinRepHideToggle({ hide, setHide }) { return ; } function FinRepFoot({ children }) { return

    {children}

    ; } // No month at all (should not happen inside the shell's gate, which shows // "Link your finance sheet"); kept so a page never renders blank. function FinRepNoPeriod({ title }) { return (
    ); } // ── Balance Sheet ─────────────────────────────────────────────────────── function FinBalanceSheet() { const sheet = useFinSheet(); const per = useFinPeriod(); const data = sheet.data || {}; const period = per.period; const [hide, setHide] = finRepUseHideEmpty(); const bs = period ? period.bs : null; const model = React.useMemo(() => { if (!bs) return null; const sides = (bs.sides || []).map((s) => ({ side: s, rows: finRepBsRows(s) })); const anyActive = sides.some((s) => s.rows.some((r) => r.kind === 'line' && Number(r.amount) !== 0)); return { sides, anyActive }; }, [bs]); if (!period) return ; const journalMissing = !!(period.journal && period.journal.available === false); const asOf = bs ? bs.asOf : period.end; const meta = sheet.meta || {}; const today = meta.today || finTodayIso(); const sheetTitle = meta.sheet && meta.sheet.title; const exportCsv = () => { if (!model) return; const rows = []; model.sides.forEach(({ side, rows: rs }) => { let sec = ''; rs.forEach((r) => { if (r.kind === 'sec') { sec = r.label; return; } if (r.kind === 'line') rows.push([side.label, sec, r.code || (r.special === 'current_year_result' ? 'from P&L' : r.special === 'retained_earnings' ? 'earlier years' : ''), r.label, finRepCsvAmt(r.amount)]); else rows.push([side.label, sec, '', r.label, finRepCsvAmt(r.amount)]); }); rows.push([side.label, '', '', side.totalLabel, finRepCsvAmt(side.total)]); }); finDownloadCsv(finCsvName('balance-sheet', 'as-of-' + asOf), ['Side', 'Section', 'Code', 'Account', 'Amount (IDR)'], rows); }; const pill = bs ? ( {bs.balanced ? 'Balanced' : 'Off by ' + finIDR(Math.abs(Number(bs.diff) || 0))} ) : null; const lastJv = period.asOf && period.asOf.jv; const liabSecs = []; ((bs && bs.sides) || []).forEach((sd) => (sd.sections || []).forEach((s) => { if (/liabilities/.test(s.key)) liabSecs.push(s); })); const liabLabel = liabSecs.length === 1 && liabSecs[0].totalLabel ? liabSecs[0].totalLabel : 'Current + long-term'; return (
    } /> {journalMissing ? : !model ? : ( <> {bs.banner ? ( : }> {bs.banner}. {bs.bannerNote || ('The figures are ' + (finPeriodYears(data.periods).length > 1 ? 'the journal movements of every year in the sheet' : 'this year’s journal movements') + ', not the real position (Kas & Bank can go below zero). Opening balances in the JV tab, such as a 1 Jan “Saldo Awal” entry, would fix that.')} ) : null}
    0 ? ' · ' + finRepPct((bs.liabilities / bs.assets) * 100) + ' of assets' : '')} /> 1 ? 'in ' + String(asOf).slice(0, 4) : 'this year')} /> today ? ' (month still running)' : '')} end={<>{pill}} />
    {model.sides.map(({ side, rows }) => (
    {side.label} {finRepEn(side.key) ? {finRepEn(side.key)} : null}
    {side.totalLabel} {finIDR(side.total)}
    ))}
    Built from every line of the JV tab dated up to {finDate(asOf)}, laid out like the {data.statements && data.statements.bs && data.statements.bs.source === 'built-in' ? 'standard balance sheet' : 'BS tab'}. {' '}{lastJv ? 'Last journal line in ' + (period.label || '') + ': ' + finDate(lastJv) + '.' : 'No journal lines dated in ' + (period.label || 'this month') + '; the balances carry over from earlier months.'}
    )}
    ); } // ── Profit & Loss ─────────────────────────────────────────────────────── function FinProfitLoss({ args }) { const sheet = useFinSheet(); const per = useFinPeriod(); const data = sheet.data || {}; const period = per.period; const isAll = per.isAll; const [scope, setScope] = React.useState(() => (args && args.scope === 'ytd' ? 'ytd' : 'month')); React.useEffect(() => { if (args && (args.scope === 'ytd' || args.scope === 'month')) setScope(args.scope); }, [args && args.nonce]); // eslint-disable-line const [hide, setHide] = finRepUseHideEmpty(); const m = React.useMemo(() => finRepPlModel(data, period, isAll, scope), [data, period, isAll, scope]); const table = React.useMemo(() => (m ? finRepPlRows(m.pl, data.statements) : null), [m, data.statements]); if (!period && !isAll) return ; const ytdOk = !!(data.statements && data.statements.pl && data.year && data.year.ledger); const pl = m ? m.pl : null; const range = m ? finRepRangeLabel(m.from, m.to) : (period ? period.rangeLabel : ''); const empty = !!table && !table.rows.some((r) => r.kind === 'line' && (r.active || Number(r.amount))); const rev = pl ? Number(pl.revenue) || 0 : 0; const base = rev > 0 ? rev : 0; const net = pl ? Number(pl.netIncome) || 0 : 0; const margin = rev > 0 ? (net / rev) * 100 : null; const scopeLabel = isAll ? 'All months' : m && m.scope === 'ytd' ? 'Year to date' : (period ? period.label : ''); const meta = sheet.meta || {}; const sheetTitle = meta.sheet && meta.sheet.title; // A month with no row of any tab dated in it: its own empty state. const noRows = !isAll && !!period && period.hasData === false && !!m && m.scope === 'month'; const exportCsv = () => { if (!table) return; const pct = (x) => (base ? (Math.round((x / base) * 1000) / 10).toFixed(1) : ''); const rows = []; let sec = ''; table.rows.forEach((r) => { if (r.kind === 'sec') { sec = r.label; return; } rows.push([sec, r.kind === 'line' ? r.code || '' : '', r.label, finRepCsvAmt(r.amount), pct(Number(r.amount) || 0)]); }); rows.push(['', '', table.grand.label, finRepCsvAmt(table.grand.amount), pct(table.grand.amount)]); const tag = m.scope === 'year' ? 'year-' + m.year : isAll ? 'all-months' : (m.scope === 'ytd' ? 'ytd-' : '') + period.key; finDownloadCsv(finCsvName('profit-loss', tag), ['Section', 'Code', 'Account', 'Amount (IDR)', '% of revenue'], rows); }; const linkPeriod = period || ((data.periods || []).find((p) => p.key === per.defaultKey) || (data.periods || [])[0]); const lastJv = period && period.asOf && period.asOf.jv; let body; if (!isAll && period.journal && period.journal.available === false) body = ; else if (!m) body = ; else if (noRows) { body = ( setScope('ytd')}>Show year to date : null} /> ); } else { body = ( <> {empty ? ( {!isAll && m.scope === 'month' && ytdOk ? : null} )} /> ) : (
    0 ? ' · ' + finRepPct((pl.grossProfit / rev) * 100) + ' of revenue' : '')} /> } /> Built from the JV tab{m.scope === 'month' ? ', lines dated in ' + (period.label || 'this month') : ', lines dated ' + range}, laid out like the {data.statements && data.statements.pl && data.statements.pl.source === 'built-in' ? 'standard P&L' : 'PL tab'}. Revenue is credit minus debit; costs are debit minus credit; in the last section income counts plus and expenses minus. {m.scope === 'month' ? (' ' + (lastJv ? 'Last journal line: ' + finDate(lastJv) + '.' : '')) : ''}
    )} {!isAll ? : null} ); } const showScope = !isAll && !!period; // All months over several years: one P&L per calendar year, or the whole // range. One year: no choice to make. const years = finPeriodYears(data.periods); const showYears = isAll && years.length > 1; return (
    } /> {showScope ? (
    {range}
    ) : showYears ? (
    ({ value: 'y' + y, label: y })))} /> {range}
    ) : null} {body}
    ); } Object.assign(window, { FinBalanceSheet, FinProfitLoss, finRepPlCompute, finRepLedgerAmounts, finRepYtd, finRepPlRows });