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:
@@ -15,7 +15,7 @@ from ..core.auth import (COOKIE_SECURE, DUMMY_PASSWORD_HASH, audit, client_ip, o
|
||||
login_failed, password_matches, throttle)
|
||||
from ..core.models import Move, OperatorLogin, Resolution, Review, StrictModel
|
||||
from ..printjobs import queue as queue_print_files
|
||||
from .. import payments, quote_review
|
||||
from .. import cnpj, payments, quote_review
|
||||
from .. import tiny
|
||||
from ..runtime import (BACK, BOARD_FINISHED_LIMIT, BOARD_QUOTE_LIMIT, ENVIRONMENT, PUBLIC_ORIGIN, STATES,
|
||||
TRANSITIONS, payment, enqueue, freight, quote_view, storage)
|
||||
@@ -122,9 +122,11 @@ def board(user=Depends(operator)):
|
||||
quotes = [quote_view(c, q) for q in pending + approved]
|
||||
return {'states': STATES, 'transitions': TRANSITIONS, 'back': BACK,
|
||||
'orders': orders, 'payment_issues_total': issues_total, 'tiny': tiny_status(), 'operator': user,
|
||||
'providers': {'payment': payment.name, 'freight': freight_status(), 'backup': backup}, 'environment': ENVIRONMENT, 'site_origin': PUBLIC_ORIGIN,
|
||||
'providers': {'payment': payment.name, 'freight': freight_status(), 'backup': backup},
|
||||
'environment': ENVIRONMENT, 'site_origin': PUBLIC_ORIGIN,
|
||||
'finished_shown': len(finished), 'finished_total': finished_total,
|
||||
'quotes': quotes, 'thumbnails': thumbnails(c, orders, quotes),
|
||||
'names': company_names(c, orders, quotes),
|
||||
'pending_total': pending_total, 'approved_total': approved_total}
|
||||
|
||||
|
||||
@@ -149,6 +151,12 @@ def thumbnails(c, *groups):
|
||||
return [str(r['upload_id']) for r in rows]
|
||||
|
||||
|
||||
def company_names(c, *groups):
|
||||
"""The registry's company names for these orders' and quotes' CNPJs (app/cnpj.py)."""
|
||||
return cnpj.names(c, [((row.get('snapshot') or row.get('draft') or {}).get('customer') or {}).get('cnpj')
|
||||
for rows in groups for row in rows])
|
||||
|
||||
|
||||
# An item's final counts while its file is still there and scanned clean: the
|
||||
# same rule that lets an order into the print queue.
|
||||
FINAL_OK = """f.kind='final' AND f.active AND u.expires_at>now() AND u.purged_at IS NULL AND u.scan_state='clean'"""
|
||||
@@ -197,7 +205,8 @@ def finished_page(before_created_at: datetime | None = None, before_id: UUID | N
|
||||
ORDER BY created_at DESC,id DESC LIMIT %s''',
|
||||
((before_created_at, before_id) if paged else ()) + (limit + 1,)).fetchall()
|
||||
orders = with_print_files(c, rows[:limit])
|
||||
return {'orders': orders, 'has_more': len(rows) > limit, 'thumbnails': thumbnails(c, orders)}
|
||||
return {'orders': orders, 'has_more': len(rows) > limit, 'thumbnails': thumbnails(c, orders),
|
||||
'names': company_names(c, orders)}
|
||||
|
||||
|
||||
@router.get('/api/operator/payment-events')
|
||||
@@ -259,13 +268,15 @@ def numeric(q):
|
||||
|
||||
|
||||
def customer_match(column, q):
|
||||
"""SQL and parameters matching a customer (e-mail, CNPJ or WhatsApp) in an
|
||||
order snapshot or a quote draft. CNPJ and phone match by their digits, and
|
||||
only when nothing but digits and punctuation was typed: the digits of an
|
||||
e-mail address are not a phone number."""
|
||||
"""SQL and parameters matching a customer (e-mail, company name, CNPJ or
|
||||
WhatsApp) in an order snapshot or a quote draft. CNPJ and phone match by
|
||||
their digits, and only when nothing but digits and punctuation was typed:
|
||||
the digits of an e-mail address are not a phone number."""
|
||||
text, digits = q.strip().lower(), numeric(q)
|
||||
sql = f"lower({column}->'customer'->>'mail') LIKE %s"
|
||||
params = [like(text)]
|
||||
cnpj = f"regexp_replace(coalesce({column}->'customer'->>'cnpj',''),'\\D','','g')"
|
||||
sql = (f"lower({column}->'customer'->>'mail') LIKE %s OR {cnpj} IN (SELECT n.cnpj FROM dtf_local.cnpj_names n"
|
||||
" WHERE lower(n.legal_name) LIKE %s OR lower(n.trade_name) LIKE %s)")
|
||||
params = [like(text)] * 3
|
||||
if len(digits) >= 3:
|
||||
sql += (f" OR regexp_replace(coalesce({column}->'customer'->>'cnpj',''),'\\D','','g') LIKE %s"
|
||||
f" OR regexp_replace(coalesce({column}->'customer'->>'zap',''),'\\D','','g') LIKE %s")
|
||||
@@ -309,7 +320,7 @@ def quote_page(kind: Literal['pending','approved'], before_created_at: datetime
|
||||
WHERE {where}''', params).fetchone()['n']
|
||||
quotes = [quote_view(c,row) for row in rows[:limit]]
|
||||
return {'quotes': quotes, 'has_more': len(rows)>limit, 'total': total,
|
||||
'thumbnails': thumbnails(c, quotes)}
|
||||
'thumbnails': thumbnails(c, quotes), 'names': company_names(c, quotes)}
|
||||
|
||||
|
||||
@router.get('/api/operator/search')
|
||||
@@ -338,7 +349,8 @@ def search(q: str = Query(min_length=2, max_length=100), user=Depends(operator))
|
||||
ORDER BY q.created_at DESC LIMIT 8''', quote_params).fetchall()
|
||||
orders = with_print_files(c, orders)
|
||||
quotes = [quote_view(c, row) for row in rows]
|
||||
return {'orders': orders, 'quotes': quotes, 'thumbnails': thumbnails(c, orders, quotes)}
|
||||
return {'orders': orders, 'quotes': quotes, 'thumbnails': thumbnails(c, orders, quotes),
|
||||
'names': company_names(c, orders, quotes)}
|
||||
|
||||
@router.post('/api/operator/quotes/{uid}/approve')
|
||||
def approve(uid: UUID, body: Review, user=Depends(operator)):
|
||||
|
||||
Reference in New Issue
Block a user