invoices: self-contained payout addresses

Crypto options were limited to a hard-coded asset list, with one
address per coin in a side table. A payout address now carries
everything an invoice needs: ticker, name, QR URI scheme, amount
parameter, CoinGecko id, address and an active flag. Any coin can be
added, several addresses per ticker are fine, and known tickers fill the
QR and rate fields from the built-in templates.

The new-invoice form picks a payout address. The invoice snapshots the
ticker, address, name, scheme and amount parameter, and the PDF and QR
render from that copy, so editing, retiring or deleting a payout entry
never changes an issued invoice. "Fetch live" looks up the rate by the
payout address's CoinGecko id.

Migration 012 keeps the existing rows and backfills both the payout
metadata and the snapshots on existing crypto invoices.

Assisted-by: Claude:opus-5
This commit is contained in:
Sergei Poljanski 2026-10-10 09:17:09 +04:00
commit 33ba7650c3
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
19 changed files with 420 additions and 176 deletions

View file

@ -36,9 +36,8 @@
reindex();
});
// --- Crypto: coin select, live rate fetch + auto amount -------------
const coinSel = document.getElementById('crypto_coin');
const addressInput = document.getElementById('crypto_address');
// --- Crypto: payout address, live rate fetch + auto amount ----------
const payoutSel = document.getElementById('payout_id');
const rateInput = document.getElementById('crypto_rate');
const amountInput = document.getElementById('crypto_amount');
const fetchBtn = document.getElementById('fetch-crypto-rate');
@ -48,20 +47,13 @@
const currencySel = document.getElementById('currency');
const gelRateInput = document.getElementById('gel_rate');
// Default payout addresses per coin, rendered server-side from env.
let cryptoDefaults = {};
const defaultsEl = document.getElementById('crypto-defaults');
if (defaultsEl) {
try { cryptoDefaults = JSON.parse(defaultsEl.textContent); } catch (e) { /* leave empty */ }
function selectedPayout() {
return payoutSel ? payoutSel.selectedOptions[0] : null;
}
// On coin change: swap in the new coin's default address, but never
// clobber a hand-entered one (only replace blank or known defaults).
if (coinSel && addressInput) {
coinSel.addEventListener('change', function () {
const val = addressInput.value.trim();
const isDefault = val === '' || Object.values(cryptoDefaults).indexOf(val) !== -1;
if (isDefault) addressInput.value = cryptoDefaults[coinSel.value] || '';
// A different payout address means a different coin: stale rate/amount out.
if (payoutSel) {
payoutSel.addEventListener('change', function () {
rateInput.value = '';
if (amountInput) amountInput.value = '';
amountTouched = false;
@ -99,7 +91,11 @@
const ccy = currencySel ? currencySel.value : 'USD';
if (rateCcy) rateCcy.textContent = ccy;
if (gelRateCcy) gelRateCcy.textContent = ccy;
if (rateCoin && coinSel) rateCoin.textContent = coinSel.value;
const opt = selectedPayout();
const cryptoFields = document.getElementById('crypto-fields');
if (cryptoFields) cryptoFields.hidden = !(opt && opt.value); // rate/amount only with a payout address
if (rateCoin) rateCoin.textContent = (opt && opt.dataset.coin) || 'coin';
if (fetchBtn) fetchBtn.hidden = !(opt && opt.dataset.rate); // no CoinGecko id: type the rate
}
syncCcyLabel();
if (currencySel) currencySel.addEventListener('change', syncCcyLabel);
@ -110,8 +106,9 @@
if (fetchBtn) {
fetchBtn.addEventListener('click', function () {
const ccy = currencySel ? currencySel.value : 'USD';
const coin = coinSel ? coinSel.value : 'LTC';
const params = new URLSearchParams({ coin: coin, currency: ccy });
const opt = selectedPayout();
if (!opt || !opt.value) { rateStatus.textContent = ' choose a payout address first'; return; }
const params = new URLSearchParams({ payout: opt.value, currency: ccy });
if (gelRateInput && gelRateInput.value) params.set('gel_rate', gelRateInput.value);
rateStatus.textContent = ' fetching…';
fetch('/admin/crypto-rate?' + params.toString(), { headers: { Accept: 'application/json' } })

View file

@ -381,6 +381,19 @@ fieldset.plain { border: 0; margin: 0; padding: 0; min-width: 0; }
.stats b { display: block; font-size: 1.4rem; color: var(--text-bright); font-variant-numeric: tabular-nums; }
.stats span { font-size: 0.75rem; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.06em; }
.payout-card { border: 1px solid var(--border); border-radius: 4px; padding: 14px 16px 6px; margin-bottom: 14px; }
.payout-card.new { border-style: dashed; }
.payout-card.inactive { opacity: 0.55; }
.payout-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; color: var(--text-bright); }
.payout-head .optional { color: var(--text-faint); font-size: 0.8rem; }
.invoice-form input[type="checkbox"], .invoice-form input[type="radio"] { accent-color: var(--text-dim); }
.invoice-form [hidden] { display: none !important; } /* beats .invoice-grid's display: grid */
.payout-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 14px; }
.payout-grid .payout-address { grid-column: 1 / -1; }
.qr-line { font-size: 0.75rem; word-break: break-all; margin: 0 0 8px; }
.payout-meta { font-family: var(--font-sans); }
@media (max-width: 560px) { .payout-grid { grid-template-columns: 1fr; } }
/* Invoice statistics */
.year-filter { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin-bottom: 24px; }
.year-filter a { padding: 2px 10px; border: 1px solid var(--border); border-radius: 3px; text-decoration: none; color: var(--text-dim); font-family: var(--font-mono); font-size: 0.85rem; }