Files
dtf-system/web/site-compra.js
Cauê Faleiros 083441583a
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 8m24s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m14s
feat: each product's metres are summed and rounded once, and the 1 m minimum is once per order (Marcus, 2026-12-03)
Marcus: "o pedido mínimo de um metro deve ser somente para o primeiro
metro, após isso deve se somar a metragem de cada arquivo", "Soma tudo e
arredonda uma vez só", and for different products "Pode ser sim": each
product apart at its price, the minimum for the whole order. Order #246
(three ready sheets of 3,74, 0,24 and 0,22 m) was 5,80 m with 1 m per item;
it is now 4,20 m.

pricing.bill_order replaces the per-item minimum and bill_splits: per
product the exact lengths (a ready sheet without its gaps) are rounded once
with the 8 mm grace, the minimum's difference goes to the first item's
product, and each product's total is shared back to its items in 10 cm
steps (each its length rounded down, the missing steps to the largest
fractions, the minimum to the product's first item), for Tiny's 10 cm SKUs
and the cards. A share of 0 m adds no Tiny line. The print file's guard
allows an item one 10 cm step under its length. The Site cart shares the
same way (cobrarPedido), split sheets round once, the buy box says the
items of a product are summed and rounded once, and the Montador's
estimate rounds its sum once. Contract: output/revisao/contrato-minimo-pedido.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:46:40 -03:00

105 lines
5.0 KiB
JavaScript

/* Site DTF — the product page's buy box: the grade, price per metre, billed length,
total and the add-to-cart button, beside the live sheet. It shows the item
the other site-*.js files already priced (itemAtual) and never computes a
price of its own, so the box and the cart always agree. */
(function(){
const vals=$('compraVals'), add=$('bAdd');
const nomeCurto=n=>{ n=n||''; return n.length>24 ? n.slice(0,23)+'…' : n; };
// C2 · o cliente escolhe entre Agrupado (linhas) e Misturado (pela forma).
function pintaEncaixe(it){
const box=$('cEncaixe'); if(!box) return;
const alt=encaixeAlt;
if(!it || !alt || ehFolha(it.modo) ||
Math.abs(alt.linhas-alt.forma)<1e-9){
box.hidden=true; box.innerHTML=''; return;
}
const efetivo = modoEncaixeEmUso();
const maior=Math.max(alt.linhas,alt.forma), menor=Math.min(alt.linhas,alt.forma);
const nCm=Math.round((maior-menor)*100);
const y=nCm/100*(it.unit||0);
const vence=alt.forma<=alt.linhas ? 'Misturado' : 'Agrupado';
const bt=(m,rot,sub)=>'<button type="button" class="cEncBt'+
(efetivo===m?' on':'')+'" data-enc="'+m+'" aria-pressed="'+(efetivo===m)+'">'+
'<b>'+rot+'</b><small>'+sub+'</small></button>';
box.hidden=false;
box.innerHTML='<div class="cEncBts" role="group" aria-label="Modo de encaixe">'+
bt('linhas','Agrupado','Mais fácil de cortar · '+fmtM(alt.linhas)+' m')+
bt('forma','Misturado','Menos filme · '+fmtM(alt.forma)+' m')+
'</div>'+
'<p class="cEncDif">'+vence+' usa '+nCm+' cm a menos ('+rs(y)+' a menos).</p>';
}
// C1 · o que ainda cabe na metragem já cobrada, sem aumentar o filme.
function pintaVao(it){
const box=$('cVao'); if(!box) return;
const lista=encaixeVao;
if(!it || ehFolha(it.modo) || !lista.length){
box.hidden=true; box.innerHTML=''; return;
}
const bts=lista.map(v=>{
const i=artes.indexOf(v.a);
return i<0?'':'<button type="button" class="cVaoBt" data-vao="'+i+'" data-k="'+v.k+'">+'+
v.k+' '+escapeHTML(nomeCurto(v.a.f.name))+'</button>';
}).join('');
if(!bts){ box.hidden=true; box.innerHTML=''; return; }
box.hidden=false;
box.innerHTML='<p class="cVaoT">Sem custo: cabem mais cópias nesta metragem.</p>'+
'<div class="cVaoBts">'+bts+'</div>';
}
function pintaCompra(){
const it=itemAtual;
if(!modo || !artes.length) modoEncaixe='auto';
vals.hidden=!it;
add.disabled=!it || !itemPodeEnviar(it);
pintaEncaixe(it); pintaVao(it);
if(!it) return;
$('cMetroLbl').textContent = it.extra ? 'Metro · com '+rs(it.extra)+' de arquivo manual' : 'Metro';
$('cMetro').textContent=rs(it.unit);
$('cMetragem').textContent=fmtM(it.cob)+' m';
// Partes de até 10 m (Marcus, 18/11/2026): o FlexiPRINT não abre mais que isso.
const partes=ehFolha(it.modo) ? Math.ceil(it.metros/10) : 1;
$('cPartes').textContent = partes>1 ? ' Sai em '+partes+' partes de até 10 m.' : '';
// A ready sheet is billed without the gaps between its copies (03/12/2026).
const base=it.cobravel??it.metros;
const sobraCm=Math.round((it.cob-base)*100);
// A metragem cobrada explicada como vantagem (Marcus, 27/11/2026): a cada
// 10 cm, e até 8 mm a mais ficam por nossa conta.
const exata=n1(base*100)+' cm';
const aMais=Math.round((base-it.cob)*1000);
// Com outros itens do mesmo produto no carrinho, a metragem é a da soma,
// arredondada uma vez só (Marcus, 03/12/2026).
const juntos=itensAPagar().some(x=>x!==it && x.modo===it.modo);
$('cObs').textContent = juntos
? (ehFolha(it.modo)?'A folha tem ':'A montagem tem ')+exata+
'. No carrinho, os itens deste produto somam e arredondam uma vez só, a cada 10 cm.'
: it.cob===MINIMO_M && base<MINIMO_M
? (ehFolha(it.modo) ? 'Pedido mínimo de 1 m.'
: 'Pedido mínimo de 1 m: ainda cabem '+sobraCm+' cm de artes sem custo.')
: it.cob<base-1e-6
? 'A folha tem '+exata+': cobramos a cada 10 cm e '+
(aMais>1?'os '+aMais+' mm a mais ficam':aMais===1?'o 1 mm a mais fica':'o que passou fica')+' por nossa conta.'
: it.cob>base+1e-6
? 'A folha tem '+exata+'. Cobramos a cada 10 cm, então fica '+fmtM(it.cob)+' m. '+
'Se passar até 8 mm de uma faixa, esse pedaço fica por nossa conta.'
: '';
$('cTotal').textContent=rs(it.total);
}
$('cEncaixe')?.addEventListener('click',e=>{
const b=e.target.closest('[data-enc]'); if(!b) return;
modoEncaixe=b.dataset.enc;
pintaCompra();
previaAoVivo();
});
$('cVao')?.addEventListener('click',e=>{
const b=e.target.closest('[data-vao]'); if(!b) return;
const a=artes[+b.dataset.vao]; if(!a) return;
a.q=Math.min(200,(a.q||1)+(+b.dataset.k||0));
encaixeVao=[];
const box=$('cVao'); box.hidden=true; box.innerHTML='';
pintaArtes();
});
add.addEventListener('click',()=>adicionaAoCarrinho());
addEventListener('dtf-cart-changed',pintaCompra);
window.pintaCompra=pintaCompra;
pintaCompra();
})();