"""The print proof (Dropstar, 2026-10-21). An order above 10 m prints a proof before the rest, checked by the print room without stopping the order. Dropstar's rule (2026-10-23): up to 30 cm, from the part with the thinnest lines, a piece of each different artwork, at most three proofs per order, never billed. On an order above 10 m the customer may also ask for a proof at checkout (below 10 m there is none, Dropstar 2026-10-28): that order waits for them, marked "Aguardando contato", until they approve the photo sent by WhatsApp. Either way the room marks "Prova OK" once the proof is checked, and without it the order does not reach Finalizado. """ from decimal import Decimal from uuid import NAMESPACE_URL, uuid4, uuid5 from psycopg.types.json import Jsonb from fastapi import HTTPException PROOF_ABOVE_METRES = Decimal('10') CUSTOMER_NOTE = ('O cliente pediu uma prova antes de imprimir: mande a foto da prova pelo WhatsApp ' 'e espere o aceite.') def billed_metres(snapshot): return sum((Decimal(str(item.get('billed_metres') or 0)) for item in snapshot.get('items') or []), Decimal(0)) def needs_proof(snapshot): """Whether the order prints a proof first: asked for, or above 10 m.""" return bool(snapshot.get('proof')) or billed_metres(snapshot) > PROOF_ABOVE_METRES # ---- The proof card (Dropstar's third round, 2026-10-30 and 2026-11-03) ----- # # An order with a proof becomes a parent and a child. The child, the card # "Prova", is an order of its own (orders.parent_id) with a pre-built proof # sheet of up to 15 cm per contraction class, and follows the normal flow up # to Revisão. The parent waits outside the queue, "Aguardando prova". On the # child, the Vendedor (Gestão and the Administrador too) marks Prova OK, and # the parent enters the queue; or Prova reprovada, with its reason, and the # parent goes back to Arte recebida and the child closes. The child is no # sale: it is not in Tiny, Números, the customer's account or their e-mails. PROOF_SHEET_CM = Decimal('15') # Three proofs at most per order, one per item (Dropstar, 2026-10-23). MAX_PROOFS = 3 PROVIDER = 'prova' def proof_identity(order_id): return uuid5(NAMESPACE_URL, f'dtf-proof:{order_id}') def create_child(c, parent): """The parent's proof card, in Arte recebida, its proof sheets queued to be generated. Returns it, or None when the order needs no proof.""" if parent.get('parent_id') or not needs_proof(parent['snapshot']): return None from .printjobs import queue # A class has one proof. When several items share it, use the one with the # finest measured line; no metre or product value is sold on this card. classes = {} for index, item in enumerate(parent['snapshot']['items']): kind = item.get('stroke_class') or 'C3' width = item.get('stroke_px') or 10000 if kind not in classes or width < classes[kind][0]: classes[kind] = (width, index, item) selected = [classes[kind] for kind in ('C1', 'C2', 'C3') if kind in classes][:MAX_PROOFS] proof_items = [{**item, 'billed_metres': '0', 'total_cents': 0, 'unit_cents': 0, 'proof_parent_item_index': index} for _, index, item in selected] freight = {**parent['snapshot'].get('freight', {}), 'total_cents': 0} snapshot = {**parent['snapshot'], 'items': proof_items, 'total_cents': 0, 'freight': freight, 'proof_of': parent['number']} child = c.execute('''INSERT INTO dtf_local.orders(id,owner,snapshot,payment,origin,parent_id,machine_id,machine_chosen) VALUES(%s,%s,%s,%s,%s,%s,%s,%s) RETURNING *''', (uuid4(), proof_identity(parent['id']), Jsonb(snapshot), Jsonb({'provider': PROVIDER}), parent['origin'], parent['id'], parent.get('machine_id'), parent.get('machine_chosen', False))).fetchone() queue(c, child['id'], len(snapshot['items'])) return child def open_child(c, parent_id): """The parent's proof card still waiting for its answer, if any.""" return c.execute('''SELECT * FROM dtf_local.orders WHERE parent_id=%s AND proof_result IS NULL AND cancelled_at IS NULL ORDER BY created_at DESC LIMIT 1''', (parent_id,)).fetchone() def waiting(c, order): """Whether the order waits for its proof: it has a proof card without an OK.""" return not order.get('parent_id') and not order.get('proof_ok_at') and bool(open_child(c, order['id'])) def require_unlocked(c, order): """A parent cannot enter production or have its final changed before the proof answer.""" if waiting(c, order): child = open_child(c, order['id']) raise HTTPException(409, f'Aguardando prova do #{child["number"]}: responda no card da prova.')