104 lines
9.6 KiB
JavaScript
104 lines
9.6 KiB
JavaScript
/* Ekeepa Financial v02 — data-driven static dashboard. Monetary values: million toman. */
|
|
(() => {
|
|
'use strict';
|
|
const $ = (s) => document.querySelector(s);
|
|
const fa = (value, digits = 0) => {
|
|
if (value === undefined || value === null || value === '') return '—';
|
|
const n = Number(value);
|
|
if (!Number.isFinite(n)) return '—';
|
|
return new Intl.NumberFormat('fa-IR', { maximumFractionDigits: digits, minimumFractionDigits: digits }).format(n);
|
|
};
|
|
const money = (v, digits = 0) => `${fa(v, digits)} م.ت`;
|
|
const billion = (v) => `${fa(v / 1000, 1)} میلیارد`;
|
|
const esc = (v) => String(v ?? '').replace(/[0-9]/g, d => '۰۱۲۳۴۵۶۷۸۹'[d]).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
const rowMap = (data) => Object.fromEntries((data.rows || []).map((r) => [r.key, r]));
|
|
const total = (r) => r ? Number(r?.total ?? (r?.values || []).reduce((a, b) => a + Number(b || 0), 0)) : NaN;
|
|
const val = (scenario, key) => scenario?.[key];
|
|
|
|
function renderHero(data, rows) {
|
|
$('#period').textContent = data.period || 'مدل ۱۲ماهه';
|
|
const base = (data.scenarios || []).find((s) => /base|پایه/i.test(s.name || '')) || data.scenarios?.[0] || {};
|
|
const items = [
|
|
['درآمد سال مدل', billion(val(base, 'revenue') ?? total(rows.revenue)), 'جمع ۱۲ ماه'],
|
|
['سود عملیاتی پیش از مالیات', billion(val(base, 'operating') ?? total(rows.operating)), 'برآورد سالانه'],
|
|
['سود پس از مالیات', billion(val(base, 'afterTax') ?? total(rows.afterTax)), 'برآورد سالانه'],
|
|
['دور پیشنهادی', billion(data.assumptions?.find((a) => a.key === 'round')?.value), 'مفروض تامین مالی؛ نه دور لازم']
|
|
];
|
|
$('#hero-kpis').innerHTML = items.map(([l, n, note]) => `<article class="kpi"><span class="label">${l}</span><strong>${n}</strong><small>${note}</small></article>`).join('');
|
|
}
|
|
|
|
function renderMonthly(data) {
|
|
const months = data.months || [];
|
|
const rows = data.rows || [];
|
|
const preferred = ['gmv', 'revenue', 'b2b_cogs', 'hr', 'infra', 'marketing', 'cost', 'operating', 'tax', 'afterTax', 'collections', 'receivables', 'cashflow', 'cash'];
|
|
const ordered = [...rows].sort((a, b) => {
|
|
const ai = preferred.indexOf(a.key), bi = preferred.indexOf(b.key);
|
|
return (ai < 0 ? 99 : ai) - (bi < 0 ? 99 : bi);
|
|
});
|
|
$('#monthly-table thead').innerHTML = `<tr><th>شاخص</th>${months.map((m) => `<th>${esc(m)}</th>`).join('')}<th>جمع / پایان</th></tr>`;
|
|
$('#monthly-table tbody').innerHTML = ordered.map((r) => {
|
|
const ending = ['cash', 'receivables'].includes(r.key);
|
|
return `<tr class="${['gmv','revenue','cost','operating','afterTax','cash'].includes(r.key) ? 'is-total' : ''}"><td title="${esc(r.note || '')}">${esc(r.label)}</td>${(r.values || []).map((x) => `<td>${money(x)}</td>`).join('')}<td>${money(ending ? r.values?.at(-1) : total(r))}</td></tr>`;
|
|
}).join('');
|
|
}
|
|
|
|
function renderCharts(data, rows) {
|
|
if (!window.Plotly) { $('#operating-chart').style.display='none'; $('#cash-chart').style.display='none'; $('#operating-fallback').style.display = 'block'; $('#cash-fallback').style.display = 'block'; return; }
|
|
const months = data.months || [];
|
|
const common = { responsive: true, displayModeBar: false, locale: 'fa' };
|
|
const layout = (title) => ({ title: { text: title, font: { family: 'Vazirmatn', size: 12, color: '#475569' }, x: .98, xanchor: 'right' }, font: { family: 'Vazirmatn', color: '#475569' }, paper_bgcolor: 'rgba(0,0,0,0)', plot_bgcolor: 'rgba(0,0,0,0)', margin: { l: 55, r: 12, t: 38, b: 55 }, hoverlabel: { font: { family: 'Vazirmatn' } }, xaxis: { tickfont: { size: 10 }, gridcolor: 'rgba(0,0,0,0)' }, yaxis: { tickfont: { size: 10 }, gridcolor: '#e2e8f0', zerolinecolor: '#94a3b8' }, legend: { orientation: 'h', y: 1.18, x: 1, xanchor: 'right', font: { size: 11 } } });
|
|
const series = (r) => r?.values || Array(months.length).fill(0);
|
|
Plotly.newPlot('operating-chart', [
|
|
{ x: months, y: series(rows.revenue), type: 'scatter', mode: 'lines+markers', name: 'درآمد', line: { color: '#4f46e5', width: 3 }, marker: { size: 5 }, hovertemplate: '%{x}<br>درآمد: %{y:,.0f} م.ت<extra></extra>' },
|
|
{ x: months, y: series(rows.operating), type: 'bar', name: 'سود عملیاتی', marker: { color: '#14b8a6' }, hovertemplate: '%{x}<br>سود عملیاتی: %{y:,.0f} م.ت<extra></extra>' }
|
|
], layout('درآمد و سود عملیاتی ماهانه'), common);
|
|
Plotly.newPlot('cash-chart', [
|
|
{ x: months, y: series(rows.cashflow), type: 'bar', name: 'جریان نقد', marker: { color: '#818cf8' }, hovertemplate: '%{x}<br>جریان نقد: %{y:,.0f} م.ت<extra></extra>' },
|
|
{ x: months, y: series(rows.cash), type: 'scatter', mode: 'lines+markers', name: 'مانده نقد', line: { color: '#047857', width: 3 }, marker: { size: 5 }, hovertemplate: '%{x}<br>مانده نقد: %{y:,.0f} م.ت<extra></extra>' }
|
|
], layout('نقدینگی ماهانه'), common);
|
|
}
|
|
|
|
function renderScenarios(data) {
|
|
const scenarios = data.scenarios || [];
|
|
const tabbar = $('#scenario-tabs'), detail = $('#scenario-detail');
|
|
const render = (s) => {
|
|
const parameters = [['نرخ', s.rate], ['B2B', s.b2b], ['حجم', s.volume], ['هزینه', s.cost]].filter(([, x]) => x !== undefined && x !== null).map(([k, x]) => `${k}: ${esc(x)}`).join(' · ');
|
|
const metrics = [['GMV', s.gmv], ['درآمد', s.revenue], ['سود عملیاتی', s.operating], ['مالیات برآوردی', s.tax], ['پس از مالیات', s.afterTax], ['حداقل نقد', s.minCash], ['نقد پایان دوره', s.endCash], ['دور لازم', s.requiredRound]];
|
|
detail.innerHTML = `<div><span class="eyebrow">سناریوی انتخابشده</span><h3>${esc(s.name || 'سناریو')}</h3><p>${parameters || 'پارامترهای سناریو در فایل داده ثبت نشدهاند.'}</p></div><div class="scenario-kpis">${metrics.map(([l, x]) => `<div class="scenario-kpi"><span>${l}</span><strong>${money(x)}</strong></div>`).join('')}</div>`;
|
|
};
|
|
tabbar.innerHTML = scenarios.map((s, i) => `<button type="button" role="button" aria-pressed="${i === 0}" aria-selected="${i === 0}" data-index="${i}">${esc(s.name || `سناریو ${i + 1}`)}</button>`).join('');
|
|
tabbar.addEventListener('click', (e) => { const b = e.target.closest('button'); if (!b) return; tabbar.querySelectorAll('button').forEach((x) => { x.setAttribute('aria-selected', x === b ? 'true' : 'false'); x.setAttribute('aria-pressed', x === b ? 'true' : 'false'); }); render(scenarios[Number(b.dataset.index)]); });
|
|
if (scenarios.length) render(scenarios[0]); else detail.innerHTML = '<p>سناریویی در data.json ثبت نشده است.</p>';
|
|
}
|
|
|
|
function renderAssumptions(data) {
|
|
$('#assumption-list').innerHTML = (data.assumptions || []).map((a) => `<div><dt>${esc(a.label)}</dt><dd>${esc(a.value)}</dd>${a.note ? `<small>${esc(a.note)}</small>` : ''}</div>`).join('') || '<div><dt>مفروضات</dt><dd>—</dd></div>';
|
|
$('#talent-table thead').innerHTML = '<tr><th>نقش</th><th>فاز</th><th>حقوق</th><th>کارمزد جذب</th><th>ESOP</th></tr>';
|
|
$('#talent-table tbody').innerHTML = (data.talent || []).map((x) => `<tr><td title="${esc(x.note || '')}">${esc(x.role)}</td><td>${esc(x.phase)}</td><td>${money(x.salary)}</td><td>${money(x.agency)}</td><td>${esc(x.esop)}</td></tr>`).join('') || '<tr><td colspan="5">داده برنامه تیم ثبت نشده است.</td></tr>';
|
|
}
|
|
|
|
function renderGovernance(data) {
|
|
const list = (items, target, titleKey, textKey) => $(target).innerHTML = (items || []).map((x) => `<div class="detail-item"><strong>${esc(x[titleKey])}</strong><p>${esc(x[textKey] || '')}</p></div>`).join('') || '<p class="micro-note">موردی ثبت نشده است.</p>';
|
|
list(data.notes, '#notes-list', 'title', 'text'); list(data.sources, '#sources-list', 'name', 'note');
|
|
$('#checks-list').innerHTML = (data.checks || []).map((x) => `<li class="${x.ok ? '' : 'fail'}">${esc(x.name)}</li>`).join('') || '<li class="fail">کنترلهای مدل ثبت نشده است.</li>';
|
|
}
|
|
|
|
async function start() {
|
|
try {
|
|
const response = await fetch('data.json', { cache: 'no-store' });
|
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
const data = await response.json();
|
|
const rows = rowMap(data);
|
|
renderHero(data, rows); renderMonthly(data); renderScenarios(data); renderAssumptions(data); renderGovernance(data);
|
|
$('#inputs-table thead').innerHTML = `<tr><th>ورودی</th>${data.months.map(m => `<th>${esc(m)}</th>`).join('')}<th>یادداشت / واحد</th></tr>`;
|
|
$('#inputs-table tbody').innerHTML = (data.inputs || []).map(r => `<tr><td>${esc(r.label)}</td>${r.values.map(v => `<td>${fa(v, 2)}</td>`).join('')}<td>${esc(r.note)}</td></tr>`).join('');
|
|
try { renderCharts(data, rows); } catch (e) { ['operating','cash'].forEach(k => { document.getElementById(k+'-chart').style.display='none'; document.getElementById(k+'-fallback').style.display='block'; }); }
|
|
} catch (error) {
|
|
console.error('data.json load failed:', error);
|
|
$('#period').textContent = 'data.json هنوز در این مسیر موجود نیست.';
|
|
$('#hero-kpis').innerHTML = '<article class="kpi"><span class="label">وضعیت مدل</span><strong>داده یافت نشد</strong><small>پس از قرار گرفتن data.json، داشبورد خودکار بارگذاری میشود.</small></article>';
|
|
['#operating-fallback','#cash-fallback'].forEach((s) => { $(s).style.display = 'block'; });
|
|
}
|
|
}
|
|
start();
|
|
})();
|