feat: Montar creates the invoiced order card (front 7a.2b)

The montador sends each stamp to storage, splits the list into items of
at most 20 sources and 4000 copies, packs each one with the Site's engine
and creates the order in Arte recebida: typed customer, pickup, the Site's
table price, payment 'faturado', origin dropflow for Expedição and
atendimento otherwise. Until Dropstar decides, these orders do not go to
Tiny and the customer gets no paid e-mail. Vendedor and Expedição may now
upload through the operator upload routes (can_any).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 15:27:04 -03:00
parent 8993eb25db
commit 2c80de0c0a
11 changed files with 312 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ const MONTAR_CODE=/^[A-Za-z0-9_-]{1,40}$/;
const MONTAR_DPI_REFUSE=96, MONTAR_DPI_WARN=200;
const MONTAR_FILM_CM=57, MONTAR_GAP_CM=0.5;
const MONTAR={sizes:[],lines:[],seq:0,preview:null,timer:null,dirty:false,token:0,
layout:null,layoutTimer:null,layoutToken:0,layoutPending:false};
layout:null,layoutTimer:null,layoutToken:0,layoutPending:false,creating:false,requestKey:null};
const montarFolderReady=()=>!!FOLDER.handle&&!!FOLDER.granted;
const montarDpi=found=>found.width/(found.widthCm/2.54);
@@ -113,6 +113,7 @@ function montarPaintTotals(){
$('montar-count').textContent=MONTAR.lines.length?plural(MONTAR.lines.length,'estampa','estampas'):'';
$('montar-clear').disabled=!MONTAR.lines.length;
$('montar-warning').hidden=!(MONTAR.lines.length>20||MONTAR.lines.some(line=>line.qty>200));
montarPaintActions();
}
// The line's image, loaded once and kept for the packing and the preview.
function montarStampImage(url){
@@ -127,6 +128,7 @@ function montarChanged(){
MONTAR.layoutToken+=1;
MONTAR.layout=null;
MONTAR.layoutPending=true;
MONTAR.requestKey=null;
montarPaintLines();
if(!MONTAR.lines.length)montarDrawPreview(null);
clearTimeout(MONTAR.layoutTimer);
@@ -186,6 +188,100 @@ function montarDrawPreview(layout){
ctx.restore();
}
}
// ---- Criar o pedido -------------------------------------------------------
// The typed customer, as the Site's own Customer model validates it.
const MONTAR_CUSTOMER=['montar-name','montar-doc','montar-zap','montar-mail'];
function montarCustomer(){
return {name:$('montar-name').value.trim(),cnpj:$('montar-doc').value.trim(),
zap:$('montar-zap').value.trim(),mail:$('montar-mail').value.trim()};
}
const montarCustomerFilled=()=>MONTAR_CUSTOMER.every(id=>$(id).value.trim());
function montarClearCustomer(){
for(const id of MONTAR_CUSTOMER)$(id).value='';
MONTAR.requestKey=null;
}
// Montar is the real thing now: a sheet, its layout and the customer.
function montarPaintActions(){
const build=$('montar-build');
const ready=!!(MONTAR.lines.length&&MONTAR.layout&&!MONTAR.layoutPending&&
montarCustomerFilled()&&!MONTAR.creating);
build.disabled=!ready;
build.title=ready?'':(MONTAR.lines.length?'Aguarde a montagem e preencha o cliente.':'Monte a folha primeiro.');
}
// The Site's grade for loose art (web/site-quality.js): each stamp's DPI over
// the ideal 200, from 6 to 100; the item's is the average of its stamps'.
const montarGrade=dpi=>Math.max(6,Math.min(100,Math.round(dpi/200*100)));
function montarItem(sources){
const cargas=[];
sources.forEach((source,sourceIndex)=>{
for(let copyIndex=0;copyIndex<source.copies;copyIndex++)
cargas.push({a:{sourceIndex,giro:0,esp:false},img:source.line.img,copyIndex,
w:source.line.widthCm,h:source.line.heightCm,travado:false});
});
const enc=encaixar(cargas,MONTAR_FILM_CM);
const height=+enc.altura.toFixed(4);
const grades=sources.map(source=>montarGrade(source.line.dpi));
const warning=sources.some(source=>source.line.dpi<MONTAR_DPI_WARN);
return {mode:'avulsa',metres:+(height/100).toFixed(4),
grade:Math.round(grades.reduce((sum,n)=>sum+n,0)/grades.length),
uploads:sources.map(source=>source.uploadId),
production:{version:3,film_width_cm:MONTAR_FILM_CM,height_cm:height,
sources:sources.map(source=>({upload_id:source.uploadId,kind:'artwork',
width_cm:source.line.widthCm,length_cm:+source.line.heightCm.toFixed(4),
copies:source.copies,rotation_degrees:0,mirrored:false,measurement:'file'})),
placements:placementsDe(enc.pos)},
quality_status:warning?'warning':'ok',quality_acknowledged:warning,residue_accepted:0};
}
// 20 sources and 4000 copies per item; a stamp above 200 copies becomes
// several sources of the same upload, 200 each; items in list order.
function montarItems(lines,uploads){
const pool=[];
for(const line of lines){
const uploadId=uploads.get(line.id);
for(let left=line.qty;left>0;left-=200)
pool.push({line,uploadId,copies:Math.min(200,left)});
}
const groups=[];let current=null;
for(const source of pool){
if(!current||current.sources.length>=20||current.copies+source.copies>4000){
current={sources:[],copies:0};groups.push(current);
}
current.sources.push(source);current.copies+=source.copies;
}
return groups.map(group=>montarItem(group.sources));
}
async function montarCreate(){
if(MONTAR.creating)return;
const customer=montarCustomer();
if(!montarCustomerFilled())return say('Preencha os dados do cliente.',true);
if(!MONTAR.lines.length)return say('Monte a folha primeiro.',true);
if(!MONTAR.layout||MONTAR.layoutPending)return say('A montagem ainda está sendo calculada.',true);
const lines=[...MONTAR.lines];
if(!await ask('Criar pedido','Criar o pedido com '+plural(lines.reduce((t,line)=>t+line.qty,0),'estampa','estampas')+
', '+metres(MONTAR.layout.altura/100)+', para '+customer.name+'?','Criar pedido'))return;
MONTAR.creating=true;montarPaintActions();
try{
const uploads=new Map();
for(let index=0;index<lines.length;index++){
const line=lines[index];
say('Enviando estampas '+(index+1)+' de '+lines.length+'…');
const id=await dtfUpload(line.file,{api,startPath:'/montador/uploads',
scope:'montador:'+line.code+'-'+line.letter,progress:say});
uploads.set(line.id,id);
}
say('Criando o pedido…');
if(!MONTAR.requestKey)MONTAR.requestKey=crypto.randomUUID();
const order=await api('/montador/orders',{request_key:MONTAR.requestKey,
customer,items:montarItems(lines,uploads)});
MONTAR.requestKey=null;
montarClear();
montarClearCustomer();
say('Pedido #'+order.number+' criado em Arte recebida.',false,10000);
load(true).catch(()=>{});
}catch(e){say(e.message,true);}
finally{MONTAR.creating=false;montarPaintActions();}
}
function montarLine(line){
const row=node('div',undefined,'montar-line');
const img=node('img',undefined,'montar-thumb');img.src=line.url;img.alt='';
@@ -330,7 +426,8 @@ $('montar-add').onclick=async()=>{
try{await montarAdd();}catch(e){say(e.message,true);}finally{$('montar-add').disabled=false;}
};
$('montar-clear').onclick=()=>montarClear();
$('montar-build').title='Em breve';
$('montar-build').onclick=()=>montarCreate();
for(const id of MONTAR_CUSTOMER)$(id).addEventListener('input',()=>{MONTAR.requestKey=null;montarPaintActions();});
$('montar-size-add').onclick=()=>{MONTAR.dirty=true;$('montar-sizes').append(montarSizeRow(null));};
$('montar-size-save').onclick=async()=>{
$('montar-size-save').disabled=true;