feat: products and prices from the database, edited in the Kanban's Site tab (front 8.1)
Gestão and the administrators switch each product on and off and set its price from a start date; a price that has started is history and cannot change, a scheduled one can be cancelled. The Site and the server read the current price; with no row the code's prices and TREATMENT_CENTS still apply, so nothing changes until someone sets one. A switched-off product disappears from the Site and new quotes with it are refused. Approved quotes and paid orders keep their price. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
116
web/kanban-site.js
Normal file
116
web/kanban-site.js
Normal file
@@ -0,0 +1,116 @@
|
||||
/* Kanban DTF: the Site tab. What the Site sells and at what price, edited by
|
||||
Gestão and the administrators (Fase 2, front 8). Loaded after kanban.js,
|
||||
which defines api, may, money, when, say, node and button.
|
||||
A price is history: one that has started is never changed or deleted, so
|
||||
only a scheduled (future) price may be cancelled. */
|
||||
const SITE_UNIT_LABEL={metro:' /m',pedido:' por pedido'};
|
||||
|
||||
// Reais with a comma, as the Site shows them: "19,90" is 1990 cents.
|
||||
function moneyInputCents(value){
|
||||
let text=String(value||'').trim().replace(/R\$/g,'').replace(/\s/g,'');
|
||||
if(!text)return null;
|
||||
if(text.includes(','))text=text.replace(/\./g,'').replace(',','.');
|
||||
const number=Number(text);
|
||||
if(!Number.isFinite(number)||number<0)return null;
|
||||
return Math.round(number*100);
|
||||
}
|
||||
|
||||
// A price that already started is refused by the server (409).
|
||||
const cancelScheduledPrice=id=>api('/site/prices/'+id+'/cancel',{});
|
||||
|
||||
function siteProductRow(product){
|
||||
const tr=node('tr');
|
||||
const name=node('td');name.append(node('b',product.title));
|
||||
const price=node('td',money(product.unit_cents)+SITE_UNIT_LABEL[product.unit],'nowrap');
|
||||
if(product.fallback)price.append(node('span',' · padrão','muted'));
|
||||
const state=node('td');
|
||||
const label=node('label',undefined,'siteSwitch');
|
||||
const input=node('input');input.type='checkbox';input.checked=product.enabled;
|
||||
const word=node('span',product.enabled?'Ligado':'Desligado');
|
||||
input.onchange=async()=>{
|
||||
input.disabled=true;
|
||||
try{
|
||||
await api('/site/products/'+product.id,{enabled:input.checked});
|
||||
word.textContent=input.checked?'Ligado':'Desligado';
|
||||
say(product.title+(input.checked?' ligado.':' desligado.'));
|
||||
await loadSiteAdmin();
|
||||
}catch(error){input.checked=!input.checked;say(error.message,true);}
|
||||
finally{input.disabled=false;}
|
||||
};
|
||||
label.append(input,word);
|
||||
state.append(label);
|
||||
tr.append(name,state,price);
|
||||
return tr;
|
||||
}
|
||||
|
||||
function sitePriceBlock(product){
|
||||
const block=node('div',undefined,'sitePriceBlock');
|
||||
const heading=node('h3',product.title);
|
||||
heading.append(node('span',' · '+product.unit,'muted'));
|
||||
|
||||
// Novo preço: reais with a comma; empty "A partir de" means now.
|
||||
const form=node('div',undefined,'sitePriceForm');
|
||||
const priceLabel=node('label');priceLabel.append(node('span','Novo preço'));
|
||||
const price=node('input');price.type='text';price.inputMode='decimal';price.className='siteMoney';
|
||||
price.placeholder=(product.unit_cents/100).toFixed(2).replace('.',',');
|
||||
price.setAttribute('aria-label','Novo preço de '+product.title);
|
||||
priceLabel.append(price);
|
||||
const startsLabel=node('label');startsLabel.append(node('span','A partir de'));
|
||||
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);
|
||||
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});
|
||||
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);
|
||||
|
||||
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(button('Cancelar',async()=>{
|
||||
await cancelScheduledPrice(row.id);
|
||||
say('Preço agendado cancelado.');
|
||||
await loadSiteAdmin();
|
||||
},'btn ghost'));
|
||||
scheduled.append(line);
|
||||
}
|
||||
}else scheduled.append(node('span','Nenhum preço agendado.','muted'));
|
||||
|
||||
const table=node('table',undefined,'table');
|
||||
const head=node('thead');const headRow=node('tr');
|
||||
for(const title of ['Valor','A partir de','Quem','Quando'])headRow.append(node('th',title));
|
||||
head.append(headRow);
|
||||
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'),
|
||||
node('td',row.created_by),node('td',when(row.created_at),'nowrap'));
|
||||
body.append(tr);
|
||||
}
|
||||
if(!product.history.length){
|
||||
const tr=node('tr');const td=node('td','Nenhum preço registrado: vale o preço padrão.','muted');
|
||||
td.colSpan=4;tr.append(td);body.append(tr);
|
||||
}
|
||||
table.append(head,body);
|
||||
const history=node('div',undefined,'siteHistory');
|
||||
history.append(node('h4','Histórico'),table);
|
||||
|
||||
block.append(heading,form,scheduled,history);
|
||||
return block;
|
||||
}
|
||||
|
||||
async function loadSiteAdmin(){
|
||||
if(!may('manage'))return;
|
||||
const data=await api('/site/products');
|
||||
$('site-products').replaceChildren(...data.products.map(siteProductRow));
|
||||
$('site-prices').replaceChildren(...data.products.map(sitePriceBlock));
|
||||
}
|
||||
Reference in New Issue
Block a user