import { renderChart } from '../public/insight-charts.mjs'; import { escapeHtml as esc } from './html.mjs'; export const businessAssets = ['mero-insights.css', 'business-layouts.css', 'mero-insights.js']; const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }).format; const date = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' }).format; const stages = [ { id: 'discovery', name: 'Discovery', probability: .25 }, { id: 'proposal', name: 'Proposal', probability: .55 }, { id: 'negotiation', name: 'Negotiation', probability: .8 }, ]; const deals = [ { id: 'forma', company: 'Forma Works', mark: 'F', project: 'Retail rollout', stage: 'proposal', amount: 68400, contact: 'Lena Park', owner: 'Maya Chen', close: '2026-12-11', next: 'Walk Lena through the revised rollout estimate on November 24.' }, { id: 'arc', company: 'Arc & Field', mark: 'A', project: 'Identity system', stage: 'discovery', amount: 32500, contact: 'Theo Jones', owner: 'Jon Bell', close: '2026-12-18', next: 'Review the brand audit with Theo and agree the scope for phase one.' }, { id: 'stillwater', company: 'Stillwater', mark: 'S', project: 'Showroom fixtures', stage: 'negotiation', amount: 54200, contact: 'Nina Shah', owner: 'Maya Chen', close: '2026-11-27', next: 'Confirm the installation dates before procurement signs the agreement.' }, { id: 'tandem', company: 'Tandem Labs', mark: 'T', project: 'Member workspace', stage: 'proposal', amount: 41600, contact: 'Alex Reed', owner: 'Eli Rivera', close: '2026-12-14', next: 'Share the phased fit-out schedule with Alex ahead of the budget review.' }, { id: 'cinder', company: 'Cinder Studio', mark: 'C', project: 'Flagship concept', stage: 'discovery', amount: 28000, contact: 'Sofia Wolf', owner: 'Jon Bell', close: '2026-12-21', next: 'Run a materials workshop with Sofia and the retail team.' }, { id: 'union', company: 'Union House', mark: 'U', project: 'Hospitality collection', stage: 'negotiation', amount: 76300, contact: 'Leo Meyer', owner: 'Eli Rivera', close: '2026-11-30', next: 'Send the final sample board for purchasing approval.' }, ]; export { deals as studioDeals }; // Each posting has one signed USD amount. Balances, inflow and outflow derive from the same ledger. const ledger = [ ['2026-08-05', 'Forma Works', 'Client payment', 'FW-081', 18600], ['2026-08-12', 'Linehaul Freight', 'Shipping', 'LF-912', -2860], ['2026-08-18', 'Arc & Field', 'Client payment', 'AF-043', 14300], ['2026-08-24', 'Studio payroll', 'Payroll', 'PR-008', -19300], ['2026-08-28', 'Union House', 'Project deposit', 'UH-121', 22400], ['2026-09-02', 'Tandem Labs', 'Project deposit', 'TL-091', 14250], ['2026-09-08', 'Morrow Supply', 'Materials', 'MS-620', -8120], ['2026-09-17', 'Forma Works', 'Client payment', 'FW-094', 36100], ['2026-09-24', 'Studio payroll', 'Payroll', 'PR-009', -19800], ['2026-09-30', 'Cinder Studio', 'Client payment', 'CS-707', 9600], ['2026-10-02', 'Union House', 'Project deposit', 'UH-135', 24800], ['2026-10-09', 'Morrow Supply', 'Materials', 'MS-641', -12400], ['2026-10-16', 'Stillwater', 'Client payment', 'SW-182', 18500], ['2026-10-23', 'Studio payroll', 'Payroll', 'PR-010', -21600], ['2026-10-29', 'Forma Works', 'Client payment', 'FW-106', 32400], ].map(([day, name, category, reference, amount]) => ({ day, name, category, reference, amount })); const periods = [ { key: '2026-08', name: 'August', short: 'Aug', end: 31 }, { key: '2026-09', name: 'September', short: 'Sep', end: 30 }, { key: '2026-10', name: 'October', short: 'Oct', end: 31 }, ]; const initialBalance = 86400; function destination(screen, params = {}) { const query = new URLSearchParams(Object.entries(params).filter(([, value]) => value)).toString(); return '/layouts/' + screen + (query ? '?' + query : ''); } function link(href, label, { className = '', current = false, id = '', name = '' } = {}) { const safe = esc(href); return `${label}`; } function dateLabel(iso) { return date(new Date(iso + 'T12:00:00Z')); } function stat(label, value, className = '') { return `
${esc(label)}
${esc(value)}
`; } function stageText(id) { const stage = stages.find(item => item.id === id); return `${stage.name}`; } function mark(deal, large = false) { return ``; } function crmDetail(deal, count, initial) { return ``; } function crm(params) { const filter = stages.find(stage => stage.id === params.get('stage'))?.id || ''; const list = deals.filter(deal => !filter || deal.stage === filter); const selected = list.find(deal => deal.id === params.get('deal')) || list[0]; const totals = stages.map(stage => deals.filter(d => d.stage === stage.id).reduce((sum, d) => sum + d.amount, 0)); const total = totals.reduce((sum, n) => sum + n, 0); const weighted = totals.reduce((sum, n, i) => sum + n * stages[i].probability, 0); return `

Sales pipeline

Open opportunities · October–December 2026

Total pipeline

${money(total)}
${stages.map((stage, i) => `
${stageText(stage.id)}
${money(totals[i])}
`).join('')}
${stat('Weighted forecast', money(weighted))}

Opportunities

${list.length} ${list.length === 1 ? 'opportunity' : 'opportunities'}Value
${list.map(deal => link(destination('crm', { stage: filter, deal: deal.id }), `${mark(deal)}${esc(deal.company)}${esc(deal.project)}${stageText(deal.stage)}${money(deal.amount)}`, { className: 'bu-deal', current: selected === deal, id: 'deal-' + deal.id, name: `View ${deal.company} deal` }) + (selected === deal ? crmDetail(deal, list.length, params.get('deal') !== deal.id) : '')).join('')}
`; } function finance(params) { const period = periods.find(item => item.key === params.get('period')) || periods.at(-1); const entries = ledger.filter(item => item.day.startsWith(period.key)); const selected = entries.find(item => item.reference === params.get('transaction')); const opening = ledger.filter(item => item.day < period.key).reduce((sum, item) => sum + item.amount, initialBalance); const net = entries.reduce((sum, item) => sum + item.amount, 0); const closing = opening + net; const incoming = entries.reduce((sum, item) => sum + Math.max(0, item.amount), 0); const data = [1, 8, 15, 22, period.end].map(day => ({ label: `${period.short} ${day}`, values: [entries.filter(item => Number(item.day.slice(-2)) <= day).reduce((sum, item) => sum + item.amount, opening)] })); const chart = renderChart({ type: 'area', title: 'Operating balance', series: ['Closing balance'], rows: data, numberFormat: { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }, axisNumberFormat: { style: 'currency', currency: 'USD', notation: 'compact', maximumFractionDigits: 0 } }); return `

Cash overview

Operating account · USD

Closing balance

${money(closing)}${esc(period.name)} ${period.end}, 2026
${stat('Opening balance', money(opening))}${stat('Money in', '+' + money(incoming), 'bu-income')}${stat('Money out', '−' + money(incoming - net))}
${chart}

Transactions

${period.name} 2026 · ${entries.length} entries
${entries.toReversed().map(item => { const isSelected = selected === item; const href = destination('finance', { period: period.key, transaction: isSelected ? '' : item.reference }); const income = item.amount > 0; return `
${link(href, `${esc(item.name)}${esc(item.category)}${income ? '+' : '−'}${money(Math.abs(item.amount))}`, { className: 'bu-posting', id: 'posting-' + item.reference, current: isSelected, name: `${isSelected ? 'Close' : 'View'} ${item.name} transaction` })} ${isSelected ? `
${stat('Reference', item.reference)}${stat('Posted', dateLabel(item.day))}${stat('Direction', income ? 'Incoming transfer' : 'Outgoing transfer')}${stat('Status', 'Settled')}
` : ''}
`; }).join('')}
`; } export function renderBusinessLayout(screen, searchParams = new URLSearchParams()) { if (!['crm', 'finance'].includes(screen)) throw new RangeError('Unknown business layout'); return `
${screen === 'crm' ? crm(searchParams) : finance(searchParams)}
`; }