feat(админка): экран денег с графиками
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { h } from '/static/admin2/vendor/preact.module.js';
|
||||
import { useEffect, useState } from '/static/admin2/vendor/hooks.module.js';
|
||||
import htm from '/static/admin2/vendor/htm.module.js';
|
||||
import { api } from '/static/admin2/api.js';
|
||||
import { Card } from '/static/admin2/ui/card.js';
|
||||
import { Bars, Line } from '/static/admin2/ui/chart.js';
|
||||
|
||||
const html = htm.bind(h);
|
||||
const rub = (v) => v == null ? '—' : Number(v).toLocaleString('ru-RU',
|
||||
{maximumFractionDigits: 2}) + ' ₽';
|
||||
|
||||
export function MoneyPage() {
|
||||
const [days, setDays] = useState(30);
|
||||
const [data, setData] = useState(null);
|
||||
const [series, setSeries] = useState([]);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
setError('');
|
||||
Promise.all([api('/api/admin/v2/money?days=' + days),
|
||||
api('/api/admin/v2/finance?days=' + days)])
|
||||
.then(([m, f]) => { if (alive) { setData(m); setSeries(f.months || []); } })
|
||||
.catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить'); });
|
||||
return () => { alive = false; };
|
||||
}, [days]);
|
||||
|
||||
return html`
|
||||
<div class="toolbar">
|
||||
${[7, 30, 90].map((d) => html`<button type="button" key=${d}
|
||||
class=${d === days ? 'active' : ''} onClick=${() => setDays(d)}>${d} дней</button>`)}
|
||||
</div>
|
||||
${error ? html`<div class="card error" role="alert">${error}</div>`
|
||||
: !data ? html`<div class="card">Загрузка…</div>` : html`
|
||||
<div class="grid">
|
||||
<${Card} title="Поступления" value=${rub(data.revenue_rub)}
|
||||
note=${'возвраты ' + rub(data.refunded_rub)} />
|
||||
<${Card} title="Себестоимость" value=${rub(data.cost_rub)}
|
||||
note=${'цена клиентам ' + rub(data.charged_rub)} />
|
||||
<${Card} title="Поток" value=${rub(data.flow_rub)}
|
||||
note="поступления − себестоимость" />
|
||||
<${Card} title="На счетах" value=${rub(data.accounts_total_rub)}
|
||||
note=${'клиентов ' + data.accounts_count + ' · без цены ' + (data.unpriced ?? '—')} />
|
||||
</div>
|
||||
${series.length ? html`<div class="charts">
|
||||
<${Bars} values=${series.map((m) => m.revenue_rub)}
|
||||
labels=${series.map((m) => m.month)} label="Поступления по месяцам" />
|
||||
<${Line} values=${series.map((m) => m.cost_rub || 0)} label="Себестоимость по месяцам" />
|
||||
</div>${days < 31 ? html`<p class="note">Период короче месяца — столбцы неполные.</p>` : null}`
|
||||
: html`<section class="card"><p class="note">За период нет закрытых месяцев.</p></section>`}
|
||||
`}`;
|
||||
}
|
||||
Reference in New Issue
Block a user