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

@@ -286,12 +286,17 @@ def money(cents):
return float(Decimal(cents) / 100)
def contact_payload(order):
def contact_payload(order, company=None):
"""A new contact, named after the company's razão social when the
registry gave it (app/cnpj.py), else the recipient or the e-mail."""
customer = order['customer']
destination = order.get('destination')
contact = {'nome': (destination or {}).get('recipient') or customer['mail'],
company = company or {}
contact = {'nome': company.get('legal') or (destination or {}).get('recipient') or customer['mail'],
'tipoPessoa': 'J', 'cpfCnpj': customer['cnpj'], 'email': customer['mail'],
'celular': customer['zap'], 'situacao': 'A'}
if company.get('trade'):
contact['fantasia'] = company['trade']
if destination:
contact['endereco'] = address(destination)
return contact
@@ -412,7 +417,7 @@ def check(auth=None, transport=None, orders=None):
class TinyOrders:
def __init__(self, auth=None, transport=None, today=None):
def __init__(self, auth=None, transport=None, today=None, names=None):
missing = [name for name in required_settings() if not os.environ.get(name)]
if auth is None and missing:
raise RuntimeError('Tiny needs ' + ', '.join(missing))
@@ -420,6 +425,8 @@ class TinyOrders:
self.http = httpx.Client(base_url=API, transport=transport, timeout=30)
self.today = today
self.skus = {}
# The company's names by CNPJ, for new contacts (app.cnpj.company).
self.names = names
def request(self, method, path, **kwargs):
headers = {'Authorization': f'Bearer {self.auth.access_token()}'}
@@ -449,7 +456,13 @@ class TinyOrders:
for entry in found.get('itens') or []:
if ''.join(ch for ch in str(entry.get('cpfCnpj') or '') if ch.isdigit()) == cnpj:
return entry['id']
return self.request('POST', '/contatos', json=contact_payload(order))['id']
company = None
if self.names:
try:
company = self.names(cnpj)
except Exception: # a name is a nicety; the order must still go
company = None
return self.request('POST', '/contatos', json=contact_payload(order, company))['id']
def find(self, payload, days=SEARCH_DAYS):
"""An order a previous delivery already created, or None."""