feat: progressive price bands per product (front 8.2)

A price may carry up to five bands, "from N m, R$ Y": the order's total
billed metres of that product chooses one price for its whole quantity,
the manual-file extra still on top. Bands start and become history with
their price row. The Site charges the same band for the cart, shows
"Faltam X m para R$ Y o metro" and lists the bands under the price
table; the Kanban's Site tab edits them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 14:30:15 -03:00
parent c5e7a80b64
commit 2a6732eb37
16 changed files with 334 additions and 23 deletions

View File

@@ -18,6 +18,12 @@ function moneyInputCents(value){
// A price that already started is refused by the server (409).
const cancelScheduledPrice=id=>api('/site/prices/'+id+'/cancel',{});
// "5" or "5,5": a band's start in metres, as the table reads it.
function bandMetres(value){return String(Number(value)).replace('.',',');}
function bandsText(bands){
return (bands||[]).map(band=>' · '+bandMetres(band.from_metres)+' m: '+money(band.unit_cents)).join('');
}
function siteProductRow(product){
const tr=node('tr');
const name=node('td');name.append(node('b',product.title));
@@ -59,23 +65,57 @@ function sitePriceBlock(product){
const starts=node('input');starts.type='datetime-local';
starts.setAttribute('aria-label','Quando o novo preço de '+product.title+' começa');
startsLabel.append(starts);
// Faixas progressivas (front 8.2): a nova tabela começa vazia; as faixas
// viajam com este preço. O tratamento é um valor por pedido, sem faixas.
const bandRows=node('div',undefined,'siteBandRows');
const addBand=node('button','Adicionar faixa','btn ghost');addBand.type='button';
const paintAdd=()=>{addBand.disabled=bandRows.children.length>=5;};
const bandRow=()=>{
const row=node('div',undefined,'siteBandRow');
const fromLabel=node('label');fromLabel.append(node('span','A partir de (m)'));
const from=node('input');from.type='text';from.inputMode='decimal';from.className='siteBandFrom';
from.setAttribute('aria-label','Metragem da faixa de '+product.title);
fromLabel.append(from);
const priceLabel=node('label');priceLabel.append(node('span','R$ por metro'));
const bandPrice=node('input');bandPrice.type='text';bandPrice.inputMode='decimal';bandPrice.className='siteBandPrice';
bandPrice.setAttribute('aria-label','Preço da faixa de '+product.title);
priceLabel.append(bandPrice);
const remove=button('Remover',()=>{row.remove();paintAdd();},'btn ghost');
row.append(fromLabel,priceLabel,remove);
return row;
};
addBand.onclick=()=>{if(bandRows.children.length>=5)return;bandRows.append(bandRow());paintAdd();};
const bands=node('div',undefined,'siteBands');
const bandsHead=node('div',undefined,'siteBandsHead');
bandsHead.append(node('span','Faixas'),addBand);
bands.append(bandsHead,bandRows);
const formBands=()=>[...bandRows.children].map(row=>{
const from=Number(row.querySelector('.siteBandFrom').value.replace(',','.'));
const unit=moneyInputCents(row.querySelector('.siteBandPrice').value);
if(!Number.isFinite(from)||from<=0||unit===null||unit<=0)
throw new Error('Confira as faixas: metragem e preço maiores que zero.');
return {from_metres:String(from),unit_cents:unit};
});
const save=button('Salvar',async()=>{
const cents=moneyInputCents(price.value);
if(cents===null)throw new Error('Informe o preço em reais, como 19,90.');
const whenDate=starts.value?new Date(starts.value):null;
if(whenDate&&Number.isNaN(whenDate.getTime()))throw new Error('Data inválida.');
await api('/site/products/'+product.id+'/prices',
{unit_cents:cents,starts_at:whenDate?whenDate.toISOString():null});
{unit_cents:cents,starts_at:whenDate?whenDate.toISOString():null,
bands:product.id==='tratamento'?[]:formBands()});
say(whenDate?'Preço salvo para '+when(whenDate)+'.':'Preço salvo. Vale a partir de agora.');
await loadSiteAdmin();
},'btn pri');
form.append(priceLabel,startsLabel,save);
if(product.id!=='tratamento')form.append(bands);
const scheduled=node('div',undefined,'siteScheduled');
if(product.scheduled.length){
for(const row of product.scheduled){
const line=node('div',undefined,'siteScheduledRow');
line.append(node('span',money(row.unit_cents)+SITE_UNIT_LABEL[product.unit]+' a partir de '+when(row.starts_at)));
line.append(node('span',money(row.unit_cents)+SITE_UNIT_LABEL[product.unit]+bandsText(row.bands)+
' a partir de '+when(row.starts_at)));
line.append(button('Cancelar',async()=>{
await cancelScheduledPrice(row.id);
say('Preço agendado cancelado.');
@@ -92,7 +132,7 @@ function sitePriceBlock(product){
const body=node('tbody');
for(const row of product.history){
const tr=node('tr');
tr.append(node('td',money(row.unit_cents),'nowrap'),node('td',when(row.starts_at),'nowrap'),
tr.append(node('td',money(row.unit_cents)+bandsText(row.bands),'nowrap'),node('td',when(row.starts_at),'nowrap'),
node('td',row.created_by),node('td',when(row.created_at),'nowrap'));
body.append(tr);
}