feat: customers show by their company's name, taken from the CNPJ registry

The Site asks for e-mail, CNPJ and WhatsApp only, so the Kanban showed an
e-mail where a name belongs and a new Tiny contact was named after it. The
worker now looks up each customer's CNPJ in the public registry (BrasilAPI,
no key) a few at a time, never while a customer waits, and keeps the razão
social and trade name in dtf_local.cnpj_names; a failed lookup is retried a
day later. Cards, the order panel, quotes and the search results show the
trade name (else the razão social) above the e-mail, the search finds
orders and quotes by either name, and a new Tiny contact gets the razão
social as its name and the trade name as fantasia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-05 12:19:35 -03:00
parent 581fbe6be4
commit b3c5f63553
9 changed files with 228 additions and 21 deletions

View File

@@ -34,7 +34,10 @@ let quoteQuery='', quoteProduct='', quoteLayout='', quoteFlag='', quoteTimer=nul
// Uploads that have an artwork picture (the customer's browser made it). Every
// list the API returns says which, so only pictures that exist are asked for.
const THUMBS=new Set();
const absorb=r=>{for(const id of r?.thumbnails||[])THUMBS.add(String(id));return r;};
// Company names by CNPJ, from the public registry (app/cnpj.py).
const NAMES=new Map();
const absorb=r=>{for(const id of r?.thumbnails||[])THUMBS.add(String(id));
for(const [cnpj,name] of Object.entries(r?.names||{}))NAMES.set(cnpj,name);return r;};
let olderFinished=[], finishedMore=null;
const money=cents=>(cents/100).toLocaleString('pt-BR',{style:'currency',currency:'BRL'});
@@ -123,13 +126,15 @@ function facts(pairs){
// receives the delivery), else the e-mail. With a name, the e-mail is the
// second line, still searchable.
function buyer(content){
const c=content.customer||{}, name=(c.name||content.destination?.recipient||'').trim();
return {title:name||c.mail, mail:name?c.mail:''};
const c=content.customer||{}, company=NAMES.get(String(c.cnpj||'').replace(/\D/g,''));
// The trade name people know the shop by, else its razão social.
const name=(c.name||company?.trade||company?.legal||content.destination?.recipient||'').trim();
return {title:name||c.mail, mail:name?c.mail:'', legal:company?.legal||''};
}
function buyerBlock(content,className){
const who=buyer(content);
const box=node('div',undefined,'buyer '+(className||''));
const title=node('b',who.title);title.title=who.mail||who.title;box.append(title);
const title=node('b',who.title);title.title=[who.legal,who.mail].filter(Boolean).join(' · ')||who.title;box.append(title);
if(who.mail)box.append(node('span',who.mail,'buyer-mail'));
return box;
}