From b64947eb7632fe31c205c9bc36824e7a23e80790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 27 Nov 2026 15:03:36 -0300 Subject: [PATCH] fix: a proof sheet counts its real length in the machine's metres, in metres, not 15 m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PROOF_SHEET_CM (15) was added to a total in metres, so every proof counted 15 m of film: one proof was enough for the 10 m cleaning alert, and Números overstated production. A proof now counts the film it took, the finest stroke's window and the colour samples with the clear film between them (up to 30 cm, Marcus, 2026-11-26), read from its print file; 15 cm for a sheet made before the samples. One function serves the machine log and Números. The Kanban's proof rule says 30 cm. Co-Authored-By: Claude Opus 5.5 --- app/machine_log.py | 57 ++++++++++++++++++++++++++++--------------- app/machine_stats.py | 31 ++++++----------------- app/proof.py | 9 ++++--- tests/test_reprint.py | 22 ++++++++++++++--- web/kanban-board.js | 2 +- 5 files changed, 72 insertions(+), 49 deletions(-) diff --git a/app/machine_log.py b/app/machine_log.py index a7aaf29..07257b0 100644 --- a/app/machine_log.py +++ b/app/machine_log.py @@ -28,7 +28,8 @@ entry. Fewer than two entries, no estimate. "Printed" is what left Imprimindo for Revisão on the machine (orders.machine_id, the movement imp -> rev not back): the film metres of the items -(COALESCE(metres, billed_metres)), and PROOF_SHEET_CM per item for a proof card. +(COALESCE(metres, billed_metres)), and for a proof card the length of each +proof sheet (film_metres). """ from datetime import datetime, timezone from decimal import Decimal @@ -66,30 +67,48 @@ NOT_UNDONE = """NOT EXISTS (SELECT 1 FROM dtf_local.movements u WHERE u.order_id AND u.id=(SELECT min(n.id) FROM dtf_local.movements n WHERE n.order_id=m.order_id AND n.id>m.id))""" +# Each proof sheet's stretches of film, {item index: [[top, bottom], ...]}, +# from its print file (app/printpng.py); `o` is the order. +PROOF_WINDOWS = """(SELECT jsonb_object_agg(p.item_index::text, p.detail->'proof_windows') + FROM dtf_local.print_files p WHERE p.order_id=o.id AND p.detail ? 'proof_windows') AS windows""" + + +def film_metres(snapshot, items=None, windows=None): + """The film one pass to Revisão printed, in metres, as Decimal: each + item's metres, on a reprint only the `items` that went again (#105). A + proof card's item is its sheet: the finest stroke's window and the colour + samples with the clear film between them, up to 30 cm (Marcus, + 2026-11-26), from `windows` ({item index: [[top, bottom], ...]} in cm); + PROOF_SHEET_CM for a sheet made before the samples.""" + from .printpng import PROOF_GAP_CM + from .proof import PROOF_SHEET_CM + snapshot = snapshot or {} + proof = bool(snapshot.get('proof_of')) + total = Decimal(0) + for index, item in enumerate(snapshot.get('items') or []): + if items is not None and index not in items: + continue + if proof: + stretches = (windows or {}).get(str(index)) or [] + cm = (sum((Decimal(str(b)) - Decimal(str(a)) for a, b in stretches), Decimal(0)) + + PROOF_GAP_CM * (len(stretches) - 1)) if stretches else PROOF_SHEET_CM + total += cm / 100 + else: + value = item.get('metres') + if value is None: + value = item.get('billed_metres') + total += Decimal(str(value or 0)) + return total + + def printed_since(c, machine_id, since): """Film metres the machine printed after `since` (None: ever), as Decimal.""" - from .proof import PROOF_SHEET_CM - rows = c.execute(f'''SELECT o.snapshot, m.items FROM dtf_local.movements m + rows = c.execute(f'''SELECT o.snapshot, m.items, {PROOF_WINDOWS} FROM dtf_local.movements m JOIN dtf_local.orders o ON o.id=m.order_id WHERE o.machine_id=%s AND m.from_state='imp' AND m.to_state='rev' AND NOT m.back AND {NOT_UNDONE} AND (%s::timestamptz IS NULL OR m.created_at > %s::timestamptz)''', (machine_id, since, since)).fetchall() - total = Decimal(0) - for row in rows: - snapshot = row['snapshot'] or {} - proof = bool(snapshot.get('proof_of')) - for index, item in enumerate(snapshot.get('items') or []): - # A reprint counts only the items that went again (#105). - if row.get('items') is not None and index not in row['items']: - continue - if proof: - total += PROOF_SHEET_CM - else: - value = item.get('metres') - if value is None: - value = item.get('billed_metres') - total += Decimal(str(value or 0)) - return total + return sum((film_metres(row['snapshot'], row.get('items'), row.get('windows')) for row in rows), Decimal(0)) def _last_entry(c, machine_id, kind, item): diff --git a/app/machine_stats.py b/app/machine_stats.py index 91990dd..5ed4784 100644 --- a/app/machine_stats.py +++ b/app/machine_stats.py @@ -5,7 +5,7 @@ what the board already knows it printed, per machine and calendar month (America/Sao_Paulo): Produção printed metres (machine_log rule: imp -> rev, film metres, - PROOF_SHEET_CM per proof item); hours running = the union of the + a proof sheet its real length); hours running = the union of the time its orders spent in Imprimindo, clipped to the month (two orders printing together count once); metres per hour = printed / hours running; hours stopped = the "stop" events' minutes, in @@ -33,7 +33,7 @@ from datetime import datetime, time from decimal import Decimal, ROUND_HALF_UP from zoneinfo import ZoneInfo -from .machine_log import NOT_UNDONE, SUPPLIES +from .machine_log import NOT_UNDONE, PROOF_WINDOWS, SUPPLIES, film_metres SAO_PAULO = ZoneInfo('America/Sao_Paulo') # The five inks Números lists, in the order the sheet knows them. @@ -119,30 +119,15 @@ def ratios(printed, hours_running=None, supplies_cents=0, parts_cents=0, rolls=N def _printed_orders(c, machine_id, start, end): """The orders that left Imprimindo for Revisão on the machine in the period, with what is needed to count film and what they sold.""" - return c.execute(f'''SELECT o.id, o.snapshot, o.parent_id, o.cancelled_at, o.payment, m.items + return c.execute(f'''SELECT o.id, o.snapshot, o.parent_id, o.cancelled_at, o.payment, m.items, {PROOF_WINDOWS} FROM dtf_local.movements m JOIN dtf_local.orders o ON o.id=m.order_id WHERE o.machine_id=%s AND m.from_state='imp' AND m.to_state='rev' AND NOT m.back AND {NOT_UNDONE} AND m.created_at>=%s AND m.created_at<%s''', (machine_id, start, end)).fetchall() -def _film(snapshot, items=None): - """The film printed for one order (machine_log's rule), as Decimal; on a - reprint only the `items` that went again.""" - from .proof import PROOF_SHEET_CM - snapshot = snapshot or {} - proof = bool(snapshot.get('proof_of')) - total = Decimal(0) - for index, item in enumerate(snapshot.get('items') or []): - if items is not None and index not in items: - continue - if proof: - total += PROOF_SHEET_CM - else: - value = item.get('metres') - if value is None: - value = item.get('billed_metres') - total += Decimal(str(value or 0)) - return total +def _film(row): + """The film one printed order took (machine_log's rule), as Decimal.""" + return film_metres(row['snapshot'], row.get('items'), row.get('windows')) def _sold(row): @@ -157,7 +142,7 @@ def _sold(row): def printed_between(c, machine_id, start, end): """Film metres the machine printed in [start, end), as Decimal.""" - return sum((_film(row['snapshot'], row.get('items')) for row in _printed_orders(c, machine_id, start, end)), + return sum((_film(row) for row in _printed_orders(c, machine_id, start, end)), Decimal(0)) @@ -189,7 +174,7 @@ def machine_month(c, machine, start, end): 'contribution_cents'}. `machine` is its row from dtf_local.machines.""" mid = machine['id'] rows = _printed_orders(c, mid, start, end) - printed = sum((_film(row['snapshot'], row.get('items')) for row in rows), Decimal(0)) + printed = sum((_film(row) for row in rows), Decimal(0)) sold = sum((_sold(row) for row in rows), 0) ids = [row['id'] for row in rows] spans = c.execute('''WITH events AS ( diff --git a/app/proof.py b/app/proof.py index 63e0466..19e575a 100644 --- a/app/proof.py +++ b/app/proof.py @@ -1,8 +1,9 @@ """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 current rule: up to 15 cm, from -the part with the thinnest lines, a piece of each different artwork, at most +without stopping the order. Dropstar's current rule: 15 cm from the part +with the thinnest lines plus samples of the file's main colours, up to 30 cm +in all (Marcus, 2026-11-26), 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 @@ -37,12 +38,14 @@ def needs_proof(snapshot): # # 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 +# sheet of up to 30 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. +# The finest stroke's window; with the colour samples the sheet reaches +# app/printpng.py PROOF_MAX_CM. PROOF_SHEET_CM = Decimal('15') # Three proofs at most per order, one per item (Dropstar, 2026-10-23). MAX_PROOFS = 3 diff --git a/tests/test_reprint.py b/tests/test_reprint.py index c1ba9cb..e744c57 100644 --- a/tests/test_reprint.py +++ b/tests/test_reprint.py @@ -7,7 +7,7 @@ import unittest from decimal import Decimal from app.hotfolder import reprint_default -from app.machine_stats import _film +from app.machine_log import film_metres class ReprintTests(unittest.TestCase): @@ -27,8 +27,24 @@ class ReprintTests(unittest.TestCase): def test_the_machine_counts_only_the_items_printed_again(self): snapshot = {'items': [{'billed_metres': '2.0'}, {'billed_metres': '3.5'}]} - self.assertEqual(_film(snapshot), Decimal('5.5')) - self.assertEqual(_film(snapshot, [1]), Decimal('3.5')) + self.assertEqual(film_metres(snapshot), Decimal('5.5')) + self.assertEqual(film_metres(snapshot, [1]), Decimal('3.5')) + + +class ProofFilmTests(unittest.TestCase): + # A proof sheet is centimetres of film, counted in metres: it once counted + # 15 m per proof, enough for the machine's 10 m cleaning alert. + proof = {'proof_of': 81, 'items': [{'billed_metres': '0'}, {'billed_metres': '0'}]} + + def test_a_proof_with_colour_samples_counts_its_real_length(self): + # Marcus, 2026-11-26: the finest stroke's 15 cm and two 5 cm samples, + # 1 cm of clear film between them: 27 cm. + windows = {'0': [['10', '25'], ['40', '45'], ['80', '85']]} + self.assertEqual(film_metres(self.proof, [0], windows), Decimal('0.27')) + + def test_a_proof_made_before_the_samples_counts_15_cm(self): + self.assertEqual(film_metres(self.proof), Decimal('0.30')) + self.assertEqual(film_metres(self.proof, None, {'1': [['0', '15']]}), Decimal('0.30')) if __name__ == '__main__': diff --git a/web/kanban-board.js b/web/kanban-board.js index 456b14f..68c1f48 100644 --- a/web/kanban-board.js +++ b/web/kanban-board.js @@ -30,7 +30,7 @@ const contactWho=order=>(order.contact_by||'')+' em '+when(order.contact_since); // The print proof (app/proof.py): asked for at checkout, or an order above 10 m. // The room marks "Prova OK" once it is checked; without it, no Finalizado. const PROOF_ABOVE_METRES=10; -const PROOF_RULE='Até 15 cm por classe, do trecho de traço mais fino; no máximo 3 provas por pedido, sem cobrança.'; +const PROOF_RULE='Até 30 cm por classe: o trecho de traço mais fino e amostras das cores principais; no máximo 3 provas por pedido, sem cobrança.'; const needsProof=order=>!!order.snapshot.proof||order.snapshot.items.reduce((t,i)=>t+Number(i.billed_metres),0)>PROOF_ABOVE_METRES; const proofPending=order=>needsProof(order)&&!order.proof_ok_at&&order.state!=='fin'&&!order.cancelled_at&&!order.proof_parent; // The proof card (app/proof.py): the order's own card waits outside the queue