"""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- 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""" Etiqueta {code}
{code}
{customer}
DTF · {sheets} · {metres} m
{delivery}
Finalizado em {finished}
Revisor: {revisor}
{qr_svg(fields['qr'])}
"""