Files
dtf-system/app/label.py
Cauê Faleiros 223bd61798 feat: the 10x15 label at Finalizado (Marcus 10/11, point 10)
When an order reaches Finalizado, the Kanban on the computer that opted in
(Integrações > Etiqueta 10x15) prints a 10x15 label rendered by the server:
the Tiny order number in large type (DTF-<n> when there is none), customer,
DTF · N folhas · X m, the delivery line, date and time, the Revisor and a
QR with the number. One automatic print per order (label_prints, claimed
once); Revisor, Expedição, Gestão and the administrators can reprint.
Chrome with --kiosk-printing prints without the dialog.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-11 11:32:27 -03:00

130 lines
5.1 KiB
Python

"""The 10x15 cm thermal label printed at Finalizado (Fase 2, front 10).
Marcus, 2026-11-10: when a card reaches Finalizado the Kanban prints a label on
the Zebra ZD220: the Tiny order number, the customer, what the order is, pickup
or shipping, when it was released and by which Revisor, and a QR with the Tiny
order number only. The server renders the page so its content lives in one
place. The QR is an inline SVG from segno; nothing is loaded from outside.
`label_fields` is pure, for the tests; `label_html` only adds the markup and the
QR. Without a Tiny receipt the order keeps its own DTF-<number> as the number
and in the QR (an Interno order never reaches Tiny).
"""
import html
from datetime import timezone
from decimal import Decimal
from zoneinfo import ZoneInfo
LOCAL = ZoneInfo('America/Sao_Paulo')
SERVICE_NAMES = {'pac': 'Correios PAC', 'sedex': 'Correios SEDEX', 'jadlog': 'Jadlog'}
def _escape(value):
return html.escape(str(value if value is not None else ''), quote=True)
def _metres(items):
"""The order's billed metres with a comma and two decimals, as the card shows."""
total = sum((Decimal(str(item.get('billed_metres') or item.get('metres') or 0))
for item in items), Decimal(0))
return f'{total:.2f}'.replace('.', ',')
def _sheets(count):
return f'{count} folha' if count == 1 else f'{count} folhas'
def _delivery(snapshot):
"""Retirada, Envio with the carrier the card shows, junto com a roupa, or Interno."""
if snapshot.get('with_order'):
return f"Junto com pedido de roupa nº {snapshot['with_order']}"
if snapshot.get('kind') == 'interno':
return 'Interno'
if snapshot.get('destination'):
freight = snapshot.get('freight') or {}
service = freight.get('service')
return 'Envio · ' + (freight.get('description') or SERVICE_NAMES.get(service) or service or 'Frete')
return 'Retirada'
def _customer(snapshot):
customer = snapshot.get('customer') or {}
destination = snapshot.get('destination') or {}
return customer.get('name') or destination.get('recipient') or customer.get('mail') or '—'
def _finished_at(value):
if value is None:
return ''
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value.astimezone(LOCAL).strftime('%d/%m/%Y %H:%M')
def label_fields(order, tiny_number, finished_at, revisor):
"""What the label says, from one order and its Tiny sale receipt."""
snapshot = order.get('snapshot') or {}
items = snapshot.get('items') or []
code = str(tiny_number) if tiny_number else f"DTF-{order['number']}"
return {'code': code, 'customer': _customer(snapshot), 'sheets': len(items),
'metres': _metres(items), 'delivery': _delivery(snapshot),
'finished_at': _finished_at(finished_at), 'revisor': revisor or '—',
'qr': code}
def qr_svg(text):
"""The QR as an inline SVG, dark on the label's white; segno is pure Python.
`omitsize` keeps a viewBox and drops the fixed width/height, so the CSS can
print it at the label's 30 mm without the modules staying pixel-sized.
"""
import segno
return segno.make_qr(text, error='m').svg_inline(border=1, omitsize=True)
def label_html(fields):
"""The printable page: 100 x 150 mm, black on white, no external resources."""
code = _escape(fields['code'])
# The number is as large as its length allows on the 88 mm of usable width.
code_size = 22 if len(str(fields['code'])) <= 7 else 18 if len(str(fields['code'])) <= 9 else 15
customer = _escape(fields['customer'])
sheets = _escape(_sheets(int(fields['sheets'])))
metres = _escape(fields['metres'])
delivery = _escape(fields['delivery'])
finished = _escape(fields['finished_at'])
revisor = _escape(fields['revisor'])
return f"""<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<title>Etiqueta {code}</title>
<style>
@page {{ size: 100mm 150mm; margin: 0 }}
* {{ box-sizing: border-box }}
html, body {{ margin: 0; padding: 0; background: #fff; color: #000 }}
body {{ font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif }}
.label {{ width: 100mm; height: 150mm; padding: 6mm 6mm 5mm; display: flex;
flex-direction: column; gap: 2mm; overflow: hidden }}
.code {{ font-size: {code_size}mm; line-height: .95; font-weight: 800; letter-spacing: -.02em;
white-space: nowrap; overflow: hidden }}
.customer {{ font-size: 6mm; line-height: 1.15; font-weight: 700 }}
.line {{ font-size: 4.6mm; line-height: 1.2 }}
.foot {{ font-size: 3.6mm; line-height: 1.35; margin-top: 2mm }}
.qr {{ margin-top: auto; display: flex; justify-content: center }}
.qr svg {{ width: 30mm; height: 30mm; display: block }}
</style>
</head>
<body>
<div class="label">
<div class="code">{code}</div>
<div class="customer">{customer}</div>
<div class="line">DTF · {sheets} · {metres} m</div>
<div class="line">{delivery}</div>
<div class="foot"><div>Finalizado em {finished}</div><div>Revisor: {revisor}</div></div>
<div class="qr">{qr_svg(fields['qr'])}</div>
</div>
</body>
</html>
"""