Skip to main content

max / makenotwork

2.4 KB · 68 lines History Blame Raw
1 <div class="item-sales-tab-header">
2 <h2 class="subsection-title">Sales</h2>
3 {% if !sales.is_empty() %}
4 <button class="btn-secondary btn-compact" onclick="exportItemSalesCSV()">Export CSV</button>
5 {% endif %}
6 </div>
7
8 {% if sales.is_empty() %}
9 <div class="content-section">
10 <p class="muted">No sales yet. Sales will appear here once your first purchase is completed.</p>
11 </div>
12 {% else %}
13 <table class="data-table" id="item-sales-table">
14 <thead>
15 <tr>
16 <th>Date</th>
17 <th>Buyer</th>
18 <th>Amount</th>
19 <th>Status</th>
20 <th></th>
21 </tr>
22 </thead>
23 <tbody>
24 {% for sale in sales %}
25 <tr id="sale-{{ sale.transaction_id }}">
26 <td>{{ sale.date }}</td>
27 <td>{{ sale.buyer }}</td>
28 <td>{{ sale.amount_display }}</td>
29 <td><span class="badge {{ sale.status }}">{{ sale.status }}</span></td>
30 <td>
31 {% if sale.refundable %}
32 <button class="btn-secondary small danger-text"
33 hx-post="/api/items/{{ item.id }}/refund"
34 hx-vals='{"transaction_id": "{{ sale.transaction_id }}"}'
35 hx-confirm="Issue a full refund for {{ sale.amount_display }}? This cannot be undone."
36 hx-target="#sale-{{ sale.transaction_id }}"
37 hx-swap="outerHTML"
38 hx-on::after-request="if(event.detail.successful) htmx.ajax('GET', '/dashboard/item/{{ item.id }}/tabs/sales', '#tab-content')">Refund</button>
39 {% endif %}
40 </td>
41 </tr>
42 {% endfor %}
43 </tbody>
44 </table>
45 {% endif %}
46
47 <script>
48 function exportItemSalesCSV() {
49 var table = document.getElementById('item-sales-table');
50 if (!table) return;
51 var rows = table.querySelectorAll('tbody tr');
52 var csv = 'Date,Buyer,Amount,Status\n';
53 rows.forEach(function(row) {
54 var cells = row.querySelectorAll('td');
55 if (cells.length < 4) return;
56 csv += '"' + cells[0].textContent.trim() + '","'
57 + cells[1].textContent.trim() + '","'
58 + cells[2].textContent.trim() + '","'
59 + cells[3].textContent.trim() + '"\n';
60 });
61 var blob = new Blob([csv], { type: 'text/csv' });
62 var a = document.createElement('a');
63 a.href = URL.createObjectURL(blob);
64 a.download = '{{ item.title }}-sales.csv';
65 a.click();
66 }
67 </script>
68