feat: the customer picks Agrupado or Misturado and sees free copies that fit the same metres (Rodada 5, C1, C2)

C2: encaixar takes an optional mode ('linhas', 'forma'; the Montar keeps
the automatic one); the buy box shows both billed lengths and the
difference in cm and R$ when they differ, marking the packing on screen.
C1: up to 5 arts are tried with up to 30 more copies, by binary search
with the packing in use, and up to 3 '+k file' buttons add the copies
that keep the billed metres. Both run after the order check, never
before it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-30 15:03:02 -03:00
parent 28c10aabf6
commit 030b66c697
7 changed files with 189 additions and 13 deletions

View File

@@ -83,6 +83,20 @@ function marcaDagua(ctx,w,h){
}
ctx.restore();
}
async function cargasDe(lista){
const itens=lista.map((a,sourceIndex)=>({...a,sourceIndex}));
const imagens=await Promise.all(itens.map(a=>imagemDaArte(a).then(img=>({a,img}))));
const cargas=imagens.flatMap(c=>Array.from({length:c.a.q||1},(_,copyIndex)=>({...c,copyIndex})));
cargas.forEach(c=>{
c.w = c.a.cm; // o motor trabalha em centímetros
// The file's own proportion: a large artwork is drawn from a smaller copy.
const prop = c.a.prop>0 ? c.a.prop : c.img ? c.img.height/c.img.width : 1;
c.h = c.w * (c.a.giro? 1/prop : prop);
c.travado = !!c.a.giro; // giro do cliente manda no encaixe
});
return cargas;
}
function desenhaMontagem(area,lista,mts,W,fora,aoTerminar){
const versao=area.montagemVersao=(area.montagemVersao||0)+1;
const filme=larguraFilme(), PX_CM=W/filme;
@@ -95,17 +109,9 @@ function desenhaMontagem(area,lista,mts,W,fora,aoTerminar){
return;
}
Promise.all(itens.map(a=>imagemDaArte(a).then(img=>({a,img})))).then(imagens=>{
cargasDe(lista).then(cargas=>{
if(area.montagemVersao!==versao) return;
const cargas=imagens.flatMap(c=>Array.from({length:c.a.q||1},(_,copyIndex)=>({...c,copyIndex})));
cargas.forEach(c=>{
c.w = c.a.cm; // o motor trabalha em centímetros
// The file's own proportion: a large artwork is drawn from a smaller copy.
const prop = c.a.prop>0 ? c.a.prop : c.img ? c.img.height/c.img.width : 1;
c.h = c.w * (c.a.giro? 1/prop : prop);
c.travado = !!c.a.giro; // giro do cliente manda no encaixe
});
const enc=encaixar(cargas,filme);
const enc=encaixar(cargas,filme,modoEncaixe);
const alturaCm=enc.altura;
const pos=enc.pos.map(p=>({...p, x:p.x*PX_CM, y:p.y*PX_CM, w:p.w*PX_CM, h:p.h*PX_CM}));
const altura=alturaCm*PX_CM;
@@ -160,10 +166,96 @@ function desenhaMontagem(area,lista,mts,W,fora,aoTerminar){
function previaArtes(){ desenhaMontagem($('pArea'), artes, metros, 560); }
// C2 · as mesmas peças nas duas formas de encaixar, já na metragem cobrada.
// Com as folhas de traço, cada folha arredonda sozinha e a soma tem o mínimo de 1 m.
let encaixeAlt=null; // {linhas, forma, alturasLinhas, alturasForma}
let encaixeVao=[]; // C1: [{a,k}] cópias que cabem sem custo
let tVao=null;
function metragemDasAlturas(alturas){
return cobrarFolhas(alturas.map(alturaCm=>({alturaCm})));
}
function alternativasDasCargas(grupos){
const filme=larguraFilme();
const alturas=modo=>grupos.map(cs=>encaixar(cs,filme,modo).altura);
const li=alturas('linhas'), fo=alturas('forma');
// Em automático cada folha fica pela forma só se ela encurta GANHO_FORMA_CM
// (web/packing.js): o botão marcado é o encaixe que está na tela.
const peloMotor=li.map((h,i)=>fo[i]<=h-GANHO_FORMA_CM ? 'forma' : 'linhas');
const auto=peloMotor.every(m=>m==='forma') ? 'forma' : peloMotor.every(m=>m==='linhas') ? 'linhas' : null;
return {linhas:metragemDasAlturas(li), forma:metragemDasAlturas(fo),
alturasLinhas:li, alturasForma:fo, auto};
}
function modoEncaixeEmUso(){
if(modoEncaixe!=='auto') return modoEncaixe;
return encaixeAlt ? encaixeAlt.auto : null;
}
function podeEncaixe(){
return !!modo && !ehFolha() && artes.length>0 &&
artes.every(a=>a.cm>0 && a.cm<=larguraFilme() && a.src && !a.semPrevia);
}
function pintaEncaixeUI(){
if(typeof window.pintaCompra==='function') window.pintaCompra();
}
async function preparaEncaixe(listas,versao){
if(!podeEncaixe() || !listas.length){
encaixeAlt=null; encaixeVao=[]; pintaEncaixeUI(); return;
}
let grupos;
try{ grupos=await Promise.all(listas.map(cargasDe)); }
catch(e){ return; }
if($('vArea').montagemVersao!==versao) return;
try{ encaixeAlt=alternativasDasCargas(grupos); }
catch(e){ encaixeAlt=null; encaixeVao=[]; pintaEncaixeUI(); return; }
encaixeVao=[];
pintaEncaixeUI();
clearTimeout(tVao);
tVao=setTimeout(()=>calculaVao(listas,grupos,versao),0);
}
async function calculaVao(listas,grupos,versao){
if(!encaixeAlt || $('vArea').montagemVersao!==versao) return;
// O mesmo encaixe que montou a folha na tela, automático incluído: o que
// cabe "sem custo" é medido contra a metragem cobrada de verdade.
const modo=modoEncaixe;
let base;
try{ base=grupos.map(cs=>encaixar(cs,larguraFilme(),modo).altura); }catch(e){ return; }
const alvo=metragemDasAlturas(base);
const cands=artes.filter(a=>(a.q||1)>=1).sort((a,b)=>(b.q||1)-(a.q||1)).slice(0,5);
const achados=[];
for(const a of cands){
await new Promise(r=>setTimeout(r,0));
if($('vArea').montagemVersao!==versao) return;
const gi=listas.findIndex(l=>l.includes(a));
if(gi<0) continue;
const q=Math.max(1,a.q||1), limite=Math.min(30,200-q);
if(limite<1) continue;
const testa=async k=>{
await new Promise(r=>setTimeout(r,0)); // a página respira entre as montagens
const lista=listas[gi].map(x=>x===a?{...x,q:q+k}:x);
const cargas=await cargasDe(lista);
let altura;
try{ altura=encaixar(cargas,larguraFilme(),modo).altura; }catch(e){ return Infinity; }
const alturas=base.slice(); alturas[gi]=altura;
return metragemDasAlturas(alturas);
};
let lo=1, hi=limite, k=0;
while(lo<=hi){
const mid=(lo+hi)>>1;
const total=await testa(mid);
if($('vArea').montagemVersao!==versao) return;
if(Math.abs(total-alvo)<1e-9){ k=mid; lo=mid+1; } else hi=mid-1;
}
if(k>=1) achados.push({a,k});
}
if($('vArea').montagemVersao!==versao) return;
encaixeVao=achados.sort((x,y)=>y.k-x.k).slice(0,3);
pintaEncaixeUI();
}
// montagem ao vivo, ao lado da fila · redesenha 140 ms depois da ultima mexida
let tMont=null;
function previaAoVivo(){
clearTimeout(tMont);
if(!artes.length){ modoEncaixe='auto'; encaixeAlt=null; encaixeVao=[]; }
montagemLayout=null;
$('vArea').montagemVersao=($('vArea').montagemVersao||0)+1;
if(!ehFolha()) $('bAmpliar').hidden=true;
@@ -231,6 +323,8 @@ function previaAoVivo(){
$('vMt').textContent = r.alturaCm? fmtM(cobrar(r.alturaCm/100))+' m' : '—';
resumoArtes(); // o resumo lê a mesma folha
agendaAvaliacao(); // a nota só sai com a metragem pronta
// C1/C2 depois, sem segurar a conferência nem o botão de compra.
preparaEncaixe([prontas],$('vArea').montagemVersao).catch(()=>{});
});
$('vArea').scrollTop=0;
},140);
@@ -268,6 +362,7 @@ function montaFolhasDeTraco(grupos,W){
$('vMt').textContent=montagemFolhas ? fmtM(cobrarFolhas(montagemFolhas))+' m' : '—';
resumoArtes();
agendaAvaliacao();
preparaEncaixe(grupos,versao).catch(()=>{});
});
});
area.replaceChildren(painel);