feat: discount coupons (front 8.3)

Gestão creates coupons in the Site tab: a code, a percentage or an amount
per billed metre, validity, a minimum of metres, use limits in all and per
customer, one product or all, and a note; they can be paused and deleted
(kept for history). The customer types the code at checkout; the server
checks it when the quote is made and again at approval, discounts only the
eligible items after the bands (never freight or the treatment) and keeps
the coupon on the order. Tiny gets the discount, the e-mail and receipt
show it, the order panel carries a badge and Números counts orders,
metres, discount and money per coupon.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 15:24:44 -03:00
parent 07ee38d677
commit 1d0353fe54
18 changed files with 698 additions and 15 deletions

View File

@@ -1426,6 +1426,8 @@ function renderPanel(order){
code.append(node('span','DTF-'+order.number,'code'),copyButton(fileCode(order),'Copiar “'+fileCode(order)+'”, o começo do nome dos arquivos deste pedido'));
const theirs=node('span','Código do cliente ','tip-l');theirs.append(node('span',orderCode(order.id),'code strong'));theirs.dataset.tip='O código que aparece para o cliente';
sub.append(paid,node('span','Entrou pelo '+(ORIGINS[order.origin]||order.origin)),code,theirs);
if(s.coupon && !order.proof_parent)
sub.append(badge('Cupom '+s.coupon.code,'','Desconto de '+money(s.coupon.discount_cents)));
if(isTest(order))sub.append(badge('Teste','','Pedido de teste: sem pagamento, não vai ao Tiny'));
if(stalled(order)&&!order.cancelled_at)sub.append(badge('Parado há '+hoursSince(order.updated_at)+' h','bad','Sem mudar de etapa desde '+when(order.updated_at)));
const steps=node('ol',undefined,'stepper');steps.setAttribute('aria-label','Etapas');
@@ -3274,6 +3276,30 @@ async function loadStats(){
node('td',String(s.samples),'r'));
if(!s.samples)tr.classList.add('off');
return tr;}));
// Coupons of the period (front 8.3): the page's sections live in the HTML,
// so this panel is built here once and kept beside "Pedidos sem pagamento".
let couponPanel=$('stats-coupons-panel');
if(!couponPanel){
couponPanel=node('section',undefined,'panel clip');couponPanel.id='stats-coupons-panel';
const head=node('div',undefined,'phead');head.append(node('h2','Cupons'));
const table=node('table',undefined,'table');
const thead=node('thead'),headRow=node('tr');
for(const [label,cls] of [['Cupom',''],['Pedidos','r'],['Metros','r'],['Desconto','r'],['Recebido','r']])
headRow.append(node('th',label,cls));
thead.append(headRow);
const tbody=node('tbody');tbody.id='stats-coupons';
table.append(thead,tbody);
couponPanel.append(head,node('p','Cupons usados pelos pedidos pagos do período.','phint'),table);
const anchor=$('stats-unpaid')?.closest('section');
if(anchor)anchor.after(couponPanel);else $('tab-stats').append(couponPanel);
}
const couponRows=data.coupons||[];
$('stats-coupons').replaceChildren(...(couponRows.length?couponRows.map(coupon=>{
const tr=node('tr');
tr.append(node('td',coupon.code),node('td',String(coupon.orders),'r'),
node('td',metres(coupon.metres).replace(' m',''),'r'),
node('td',money(coupon.discount_cents),'r'),node('td',money(coupon.cents),'r'));
return tr;}):[emptyRow('Nenhum cupom usado no período.',5)]));
}
const emptyRow=(text,span)=>{const tr=node('tr');const td=node('td',text,'empty');td.colSpan=span;tr.append(td);return tr;};
// Metres per day: one series of columns from one baseline, the period's