// FRAZIER Finance OS — Create Invoice (2026-10-12). A standalone client // invoice generator with the official letterhead: its own table // (fin_created_invoices), never the Google Sheet. Sheet mode's bookkeeping // stays sheet-only and read-only (see api/fin.php's header) — an invoice // made here is a document to send to the client; the owner re-enters it in // the sheet themselves if it needs to be booked. // // Numbering: FRZ/CI/YYYY/MM/#### (see api/lib/fin-created-invoices.php), // deliberately different from the sheet's own INH/FN/YY/MM/### so the two // histories can never be confused. // // Export: Print/Save as PDF reuses the app's .fin-print-doc convention // (fin-kit.css) — the whole page becomes a clean white page with nothing // but the document. "Download Word" and "Download HTML" build a standalone // HTML string (finCiDocHtml) and save it as .doc / .html; the .doc is the // classic HTML-with-Word-namespace trick, needs no library and opens // directly in Word or Google Docs (File → Open). // // Every top-level name starts with finCi / FinCi / FIN_CI. const FIN_CI_COMPANY = { name: 'CV FRAZIER NETWORK', address: 'Ruko Arcade, Jl. Pantai Indah Utara 2 Unit PB, RT.2/RW.7, Kapuk Muara, Kecamatan Penjaringan, Jkt Utara, Daerah Khusus Ibukota Jakarta 14460', phone: '0858-8291-0012', }; const FIN_CI_TAX_TYPES = { pph4_2: { label: 'PPh 4(2) — withheld by client', shortLabel: 'PPh 4(2)', defaultPct: 0.5, direction: 'withheld' }, ppn: { label: 'PPN — added to total', shortLabel: 'PPN', defaultPct: 11, direction: 'added' }, none: { label: 'No tax', shortLabel: 'Tax', defaultPct: 0, direction: 'none' }, }; const FIN_CI_STATUS = { draft: { label: 'Draft', tone: 'neutral' }, sent: { label: 'Sent', tone: 'info' }, paid: { label: 'Paid', tone: 'ok' }, void: { label: 'Void', tone: 'crit' }, }; const FIN_CI_STATUS_ORDER = ['draft', 'sent', 'paid', 'void']; function finCiPlural(n, one, many) { return n + ' ' + (n === 1 ? one : (many || one + 's')); } function finCiEmptyLine() { return { description: '', qty: '1', unitPrice: '' }; } function finCiNewDraft() { return { clientName: '', clientAddress: '', clientContact: '', issueDate: finTodayIso(), dueDate: '', taxType: 'pph4_2', taxPct: String(FIN_CI_TAX_TYPES.pph4_2.defaultPct), notes: '', lines: [finCiEmptyLine()], }; } function finCiDraftFromInvoice(inv) { return { clientName: inv.clientName || '', clientAddress: inv.clientAddress || '', clientContact: inv.clientContact || '', issueDate: inv.issueDate, dueDate: inv.dueDate || '', taxType: inv.taxType, taxPct: String(inv.taxPct), notes: inv.notes || '', lines: (inv.lines || []).map((l) => ({ description: l.description, qty: String(l.qty), unitPrice: String(l.unitPrice) })), }; } // ── Payment plan (2026-10-22) — one project split into N dated draft // invoices, created together in one request (api/lib/fin-created- // invoices.php's fin_ci_terms_create). Mirrors the backend's own split // and clamp exactly, so the preview the owner sees before submitting is // the real schedule, not an approximation. ───────────────────────────── function finCiTermsNewDraft() { return { clientName: '', clientAddress: '', clientContact: '', description: '', totalAmount: '', numTerms: '4', startDate: finTodayIso(), firstTermLabel: 'dp', taxType: 'pph4_2', taxPct: String(FIN_CI_TAX_TYPES.pph4_2.defaultPct), notes: '', }; } // Same clamp as the backend's fin_ci_add_months: 31 Jan + 1 month = 28/29 // Feb, never a March overflow. function finCiAddMonths(ymd, months) { if (!ymd) return ''; const [y, m, d] = ymd.split('-').map(Number); const base = new Date(Date.UTC(y, m - 1 + months, 1)); const lastDay = new Date(Date.UTC(base.getUTCFullYear(), base.getUTCMonth() + 1, 0)).getUTCDate(); base.setUTCDate(Math.min(d, lastDay)); return base.getUTCFullYear() + '-' + String(base.getUTCMonth() + 1).padStart(2, '0') + '-' + String(base.getUTCDate()).padStart(2, '0'); } function finCiTermsPreview(draft) { const total = Math.round(Number(draft.totalAmount) || 0); const n = Math.max(2, Math.min(24, Number(draft.numTerms) || 0)); if (total <= 0 || !n) return []; const perTerm = Math.floor(total / n); const remainder = total - perTerm * n; const project = draft.description.trim() || 'Project'; const terms = []; for (let i = 1; i <= n; i++) { const amount = perTerm + (i === n ? remainder : 0); const label = i === 1 ? (draft.firstTermLabel === 'dp' ? project + ' — Down Payment (DP)' : project + ' — First Phase Payment (1/' + n + ')') : project + ' — Term ' + i + ' of ' + n; terms.push({ term: i, label, amount, issueDate: finCiAddMonths(draft.startDate, i - 1) }); } return terms; } // ── Recurring templates (2026-10-22) — auto-creates a DRAFT each month, // never sends anything (see api/lib/fin-ci-recurring.php's header: this // app has no outbound email at all). Reuses the same line-item editor, // tax model and compute function as a one-off invoice — a template IS an // invoice's fields minus the dates, plus a day-of-month. ─────────────── function finCiRecEmptyLine() { return { description: '', qty: '1', unitPrice: '' }; } function finCiRecNewDraft() { return { clientName: '', clientAddress: '', clientContact: '', taxType: 'pph4_2', taxPct: String(FIN_CI_TAX_TYPES.pph4_2.defaultPct), dayOfMonth: '1', notes: '', lines: [finCiRecEmptyLine()], }; } function finCiRecDraftFromTemplate(t) { return { clientName: t.clientName || '', clientAddress: t.clientAddress || '', clientContact: t.clientContact || '', taxType: t.taxType, taxPct: String(t.taxPct), dayOfMonth: String(t.dayOfMonth), notes: t.notes || '', lines: (t.lines || []).map((l) => ({ description: l.description, qty: String(l.qty), unitPrice: String(l.unitPrice) })), }; } // Mirrors api/lib/fin-created-invoices.php's fin_ci_compute exactly, for // the wizard's live total box. DPP is the sum of the line items (entered at // their real price — never grossed up from a target). function finCiCompute(dpp, taxType, taxPct) { const d = Math.round(Number(dpp) || 0); const pct = Number(taxPct) || 0; if (taxType === 'pph4_2') { const tax = Math.round(d * pct / 100); return { dpp: d, tax, total: d, nett: d - tax }; } if (taxType === 'ppn') { const tax = Math.round(d * pct / 100); return { dpp: d, tax, total: d + tax, nett: d + tax }; } return { dpp: d, tax: 0, total: d, nett: d }; } function finCiLineAmount(l) { return Math.round((Number(l.qty) || 0) * (Number(l.unitPrice) || 0)); } function finCiQty(q) { const n = Number(q) || 0; if (Number.isInteger(n)) return String(n); return String(Math.round(n * 100) / 100).replace(/\.00$/, ''); } function finCiEsc(s) { return String(s === null || s === undefined ? '' : s) .replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } // ── Export: a standalone HTML document (used for both the .html and the // .doc download — the .doc is this same HTML with a Word XML namespace). ── function finCiDocHtml(invoice) { const tax = FIN_CI_TAX_TYPES[invoice.taxType] || FIN_CI_TAX_TYPES.none; const rows = (invoice.lines || []).map((l) => ( '' + finCiEsc(l.description) + '' + '' + finCiQty(l.qty) + '' + '' + finCiEsc(finIDR(l.unitPrice)) + '' + '' + finCiEsc(finIDR(l.amount)) + '' )).join(''); const taxRow = invoice.taxType !== 'none' ? '' + finCiEsc(tax.shortLabel) + ' (' + invoice.taxPct + '%)' + '' + (tax.direction === 'withheld' ? '−' : '+') + finCiEsc(finIDR(invoice.taxAmount)) + '' : ''; const nettRow = invoice.taxType === 'pph4_2' ? 'Amount to transfer (Nett)' + finCiEsc(finIDR(invoice.nettAmount)) + '' : ''; return ( '' + finCiEsc(invoice.number) + '' + '
' + finCiEsc(FIN_CI_COMPANY.name) + '' + '' + finCiEsc(FIN_CI_COMPANY.address) + '' + finCiEsc(FIN_CI_COMPANY.phone) + '
' + '
INVOICE
' + '
No.
' + finCiEsc(invoice.number) + '
' + '
Date
' + finCiEsc(finDate(invoice.issueDate)) + '
' + (invoice.dueDate ? '
Due
' + finCiEsc(finDate(invoice.dueDate)) + '
' : '') + '
' + '
Bill To
' + finCiEsc(invoice.clientName) + '' + (invoice.clientAddress ? '

' + finCiEsc(invoice.clientAddress) + '

' : '') + (invoice.clientContact ? '

' + finCiEsc(invoice.clientContact) + '

' : '') + '
' + '' + '' + rows + '' + taxRow + '' + nettRow + '
DescriptionQtyUnit PriceAmount
Subtotal' + finCiEsc(finIDR(invoice.subtotal)) + '
Total Invoice' + finCiEsc(finIDR(invoice.totalAmount)) + '
' + (invoice.notes ? '
Notes

' + finCiEsc(invoice.notes) + '

' : '') + '
Thank you for your business. — ' + finCiEsc(FIN_CI_COMPANY.name) + ' · ' + finCiEsc(FIN_CI_COMPANY.phone) + '
' + '' ); } function finCiFilename(invoice, ext) { const slug = String(invoice.number || 'invoice').replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-+|-+$/g, '').toLowerCase(); return 'frazier-invoice-' + slug + '.' + ext; } function finCiDownloadBlob(text, mime, filename) { const url = URL.createObjectURL(new Blob([text], { type: mime })); const a = document.createElement('a'); a.href = url; a.download = filename; a.rel = 'noopener'; a.style.display = 'none'; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 2000); } function finCiDownloadHtml(invoice) { finCiDownloadBlob(finCiDocHtml(invoice), 'text/html;charset=utf-8', finCiFilename(invoice, 'html')); } function finCiDownloadWord(invoice) { // The classic "HTML saved as .doc" trick: Word and Google Docs (File → // Open) both accept this body of markup once the document carries // Word's own XML namespace. No library, no server round-trip. const html = finCiDocHtml(invoice).replace( '', "" ); finCiDownloadBlob(html, 'application/msword', finCiFilename(invoice, 'doc')); } function FinCiPill({ status }) { const m = FIN_CI_STATUS[status] || FIN_CI_STATUS.draft; return {m.label}; } // ── Recurring template: create / edit ─────────────────────────────────── function FinCiRecurringWizard({ initial, editingId, onClose, onSaved }) { const [draft, setDraft] = React.useState(initial); const [saving, setSaving] = React.useState(false); const [err, setErr] = React.useState(null); const [tried, setTried] = React.useState(false); const set = (patch) => setDraft((d) => Object.assign({}, d, patch)); const setLine = (i, patch) => setDraft((d) => Object.assign({}, d, { lines: d.lines.map((l, idx) => (idx === i ? Object.assign({}, l, patch) : l)) })); const addLine = () => setDraft((d) => Object.assign({}, d, { lines: d.lines.concat([finCiRecEmptyLine()]) })); const removeLine = (i) => setDraft((d) => Object.assign({}, d, { lines: d.lines.length > 1 ? d.lines.filter((_, idx) => idx !== i) : d.lines })); const validLines = draft.lines.filter((l) => l.description.trim() && (Number(l.unitPrice) || 0) > 0); const dpp = validLines.reduce((s, l) => s + finCiLineAmount(l), 0); const calc = finCiCompute(dpp, draft.taxType, draft.taxPct); const taxInfo = FIN_CI_TAX_TYPES[draft.taxType] || FIN_CI_TAX_TYPES.pph4_2; const nameErr = tried && !draft.clientName.trim() ? 'Enter the client name.' : null; const linesErr = tried && !validLines.length ? 'Add at least one line with a description and a price.' : null; const save = async () => { setTried(true); if (saving) return; if (!draft.clientName.trim() || !validLines.length) return; setSaving(true); setErr(null); try { const body = { clientName: draft.clientName.trim(), clientAddress: draft.clientAddress.trim(), clientContact: draft.clientContact.trim(), taxType: draft.taxType, taxPct: Number(draft.taxPct) || 0, dayOfMonth: Number(draft.dayOfMonth) || 1, notes: draft.notes.trim(), lines: validLines.map((l) => ({ description: l.description.trim(), qty: Number(l.qty) || 1, unitPrice: Number(l.unitPrice) || 0 })), }; const d = editingId ? await finApi('fin_ci_recurring_update', { body: Object.assign({ id: editingId }, body) }) : await finApi('fin_ci_recurring_create', { body }); onSaved(d.template); } catch (e) { setErr((e && e.message) || 'Could not save the template.'); } finally { setSaving(false); } }; return ( Cancel} footerRight={}>
set({ clientName: e.target.value })} autoFocus /> set({ clientContact: e.target.value })} placeholder="Phone or email" />