asxpio/views/admin/invoices/stats.erb
Sergei Poljanski 33ba7650c3
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
2026-10-10 09:17:09 +04:00

98 lines
4.5 KiB
Text

<% s = @stats %>
<% money = ->(v) { whole, frac = ('%.2f' % v).split('.'); "#{whole.reverse.scan(/\d{1,3}/).join(',').reverse.sub(/\A,/, '')}.#{frac}" } %>
<% gel = ->(v) { "₾ #{money.(v)}" } %>
<%== erb :'admin/_nav', layout: false %>
<div class="header">
<h1>Invoice statistics</h1>
<p class="tagline"><a href="/admin/invoices">← invoices</a></p>
</div>
<nav class="year-filter" aria-label="Period">
<a href="/admin/invoices/stats" class="<%= 'active' unless s.year %>">all time</a>
<% s.years.each do |y| %><a href="/admin/invoices/stats?year=<%= y %>" class="<%= 'active' if s.year == y %>"><%= y %></a><% end %>
</nav>
<section class="section">
<div class="stats stats-6">
<div><b><%= gel.(s.gel(s.live)) %></b><span>invoiced</span></div>
<div><b><%= gel.(s.gel(s.paid)) %></b><span>received</span></div>
<div><b><%= gel.(s.gel(s.unpaid)) %></b><span>outstanding</span></div>
<div><b><%= s.overdue.size %></b><span>overdue</span></div>
<div><b><%= s.avg_days_to_pay ? "#{s.avg_days_to_pay} d" : '—' %></b><span>avg. time to pay</span></div>
<div><b><%= s.live.size %><% if s.cancelled.any? %> <small>+<%= s.cancelled.size %> cancelled</small><% end %></b><span>invoices</span></div>
</div>
<p class="optional">GEL at each invoice's captured rate. Cancelled invoices are excluded from all amounts.</p>
</section>
<% rows = s.monthly %>
<% if rows.any? %>
<section class="section">
<h2>Received per month <span class="optional">(₾, by payment date)</span></h2>
<% peak = rows.map(&:last).max %>
<% peak = BigDecimal('1') if peak.nil? || peak.zero? %>
<figure class="bar-chart" role="img" aria-label="Received per month in GEL; exact values in the table below">
<div class="bar-axis"><span><%= gel.(peak) %></span><span>0</span></div>
<div class="bars">
<% rows.each do |m, _n, _inv, rec| %>
<div class="bar-col" tabindex="0" data-tip="<%= m.strftime('%b %Y') %>: <%= gel.(rec) %>">
<div class="bar" style="height: <%= (rec / peak * 100).round(1).to_f %>%"></div>
<span class="bar-label"><%= rows.size > 12 ? m.strftime('%m/%y') : m.strftime('%b') %></span>
</div>
<% end %>
</div>
</figure>
</section>
<section class="section">
<h2>By month</h2>
<table class="invoice-list">
<thead><tr><th>Month</th><th class="num">Issued</th><th class="num">Invoiced ₾</th><th class="num">Received ₾</th></tr></thead>
<tbody>
<% rows.reverse_each do |m, n, inv, rec| %>
<% next if n.zero? && rec.zero? %>
<tr><td class="mono"><%= m.strftime('%Y-%m') %></td><td class="num mono"><%= n %></td><td class="num mono"><%= money.(inv) %></td><td class="num mono"><%= money.(rec) %></td></tr>
<% end %>
</tbody>
</table>
</section>
<% end %>
<div class="dash">
<section class="section">
<h2>By currency</h2>
<table class="invoice-list">
<thead><tr><th>Currency</th><th class="num">Invoiced</th><th class="num">Received</th><th class="num">Outstanding</th></tr></thead>
<tbody>
<% s.by_currency.each do |cur, v| %>
<tr><td class="mono"><%= cur %></td><td class="num mono"><%= money.(v[:invoiced]) %></td><td class="num mono"><%= money.(v[:paid]) %></td><td class="num mono"><%= money.(v[:outstanding]) %></td></tr>
<% end %>
</tbody>
</table>
</section>
<section class="section">
<h2>Top clients</h2>
<table class="invoice-list">
<thead><tr><th>Client</th><th class="num">Invoices</th><th class="num">Invoiced ₾</th></tr></thead>
<tbody>
<% s.top_clients.each do |client, n, g| %>
<tr><td><%= client %></td><td class="num mono"><%= n %></td><td class="num mono"><%= money.(g) %></td></tr>
<% end %>
</tbody>
</table>
</section>
<% if s.crypto_usage.any? %>
<section class="section">
<h2>Crypto payment option</h2>
<table class="invoice-list">
<thead><tr><th>Asset</th><th class="num">Invoices</th></tr></thead>
<tbody>
<% s.crypto_usage.each do |coin, name, n| %>
<tr><td><span class="mono"><%= coin %></span> <span class="optional"><%= name %></span></td><td class="num mono"><%= n %></td></tr>
<% end %>
</tbody>
</table>
</section>
<% end %>
</div>