feat(админка): экран денег с графиками
This commit is contained in:
@@ -3,6 +3,7 @@ import htm from '/static/admin2/vendor/htm.module.js';
|
||||
import { SECTIONS, sectionFromPath, hrefFor } from '/static/admin2/router.js';
|
||||
import { DayPage } from '/static/admin2/pages/day.js';
|
||||
import { CustomersPage } from '/static/admin2/pages/customers.js';
|
||||
import { MoneyPage } from '/static/admin2/pages/money.js';
|
||||
|
||||
const html = htm.bind(h);
|
||||
const TITLES = {
|
||||
@@ -27,7 +28,8 @@ const App = () => html`
|
||||
<main class="main">
|
||||
<header class="top"><div><h1>${title}</h1><p>${subtitle}</p></div></header>
|
||||
${section === 'today' ? html`<${DayPage} />`
|
||||
: section === 'customers' ? html`<${CustomersPage} />` : html`
|
||||
: section === 'customers' ? html`<${CustomersPage} />`
|
||||
: section === 'money' ? html`<${MoneyPage} />` : html`
|
||||
<section class="card" aria-labelledby="placeholder">
|
||||
<h2 id="placeholder">Раздел готовится</h2>
|
||||
<p>Данные и действия подключит следующий этап.</p>
|
||||
|
||||
@@ -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>`}
|
||||
`}`;
|
||||
}
|
||||
@@ -67,3 +67,10 @@ tr.clickable{cursor:pointer} tr.clickable:hover td{background:#f3f6ff}
|
||||
.toast{display:flex;gap:8px;align-items:center;background:#ecfdf3;border:1px solid #abefc6;color:#067647;padding:8px 10px;border-radius:8px;margin:8px 0}
|
||||
.toast button{font:inherit;border:0;background:transparent;color:#067647;text-decoration:underline;cursor:pointer}
|
||||
.toast-close{margin-left:auto;text-decoration:none;font-size:18px}
|
||||
|
||||
.toolbar button.active{background:#2457d6;color:#fff;border-color:#2457d6}
|
||||
.charts{display:grid;grid-template-columns:1fr 1fr;gap:12px}
|
||||
.chart{margin:0;background:var(--card);border:1px solid var(--line);border-radius:12px;padding:10px}
|
||||
.chart figcaption{font-size:12px;color:var(--muted);margin-bottom:6px}
|
||||
.chart svg{width:100%;height:auto;display:block}
|
||||
@media(max-width:700px){.charts{grid-template-columns:1fr}}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { h } from '/static/admin2/vendor/preact.module.js';
|
||||
import htm from '/static/admin2/vendor/htm.module.js';
|
||||
const html = htm.bind(h);
|
||||
|
||||
// SVG-атрибуты, не style: CSP v2 запрещает inline-стили.
|
||||
export const Bars = ({ values, label, labels = [] }) => {
|
||||
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
|
||||
const w = 12, gap = 6, h0 = 90;
|
||||
const total = Math.max(1, values.length) * (w + gap);
|
||||
return html`<figure class="chart">
|
||||
<figcaption>${label}</figcaption>
|
||||
<svg viewBox=${`0 0 ${total} ${h0 + 18}`} role="img" aria-label=${label}>
|
||||
${values.map((v, i) => html`<rect key=${i} x=${i * (w + gap)} y=${h0 - Math.max(0, v) / max * h0}
|
||||
width=${w} height=${Math.max(1, v) / max * h0} rx="2" fill="#2457d6"></rect>`)}
|
||||
${labels.map((m, i) => html`<text key=${'l' + i} x=${i * (w + gap)} y=${h0 + 14}
|
||||
font-size="9" fill="#667085">${String(m).slice(5)}</text>`)}
|
||||
</svg>
|
||||
</figure>`;
|
||||
};
|
||||
|
||||
export const Line = ({ values, label }) => {
|
||||
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
|
||||
const pts = values.map((v, i) => `${i * 20},${90 - Math.max(0, v) / max * 80}`).join(' ');
|
||||
return html`<figure class="chart">
|
||||
<figcaption>${label}</figcaption>
|
||||
<svg viewBox=${`0 0 ${Math.max(1, (values.length - 1) * 20)} 94`} role="img" aria-label=${label}>
|
||||
${values.length > 1
|
||||
? html`<polyline points=${pts} fill="none" stroke="#067647" stroke-width="3"></polyline>`
|
||||
: html`<circle cx="0" cy=${90 - Math.max(0, values[0] || 0) / max * 80} r="3" fill="#067647"></circle>`}
|
||||
</svg>
|
||||
</figure>`;
|
||||
};
|
||||
Reference in New Issue
Block a user