feat: a reprint sends only the items chosen to the machine (Marcus, 2026-11-26, #105)
All checks were successful
Build and deploy / Validate source (push) Successful in 11s
Build and deploy / Integration suite on a real stack (push) Successful in 7m55s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m34s

An order the machine already printed, back in Arte conferida, sends again
only the items whose final changed since the machine took them; the panel's
"Vai para a máquina" shows and changes the choice, and the card says
"Reimpressão". The order stays one card. The pass to Revisão keeps the
items printed, so the machine's metres count only those.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-27 10:00:54 -03:00
parent 47f3251d3e
commit b8496b858f
11 changed files with 165 additions and 14 deletions

View File

@@ -209,6 +209,9 @@ def with_print_files(c, orders):
FROM dtf_local.movements WHERE order_id=ANY(%s) AND to_state='cor' ORDER BY order_id,id DESC''', (ids,)).fetchall()}
# Where a printing order's files are in its machine's hot folder (item 10).
hot = hotfolder.progress(c, [o['id'] for o in orders if o['state'] == 'imp'])
# Orders the machine already printed: a reprint chooses which items go
# again (#105), the ones whose final changed suggested.
taken = hotfolder.printed(c, [o['id'] for o in orders if o['state'] in ('rec', 'tra', 'cor')])
# Each parent's latest proof card, and each card's parent (app/proof.py).
children = {r['parent_id']: r for r in c.execute('''SELECT DISTINCT ON (parent_id) parent_id,id,number,state,
proof_result,proof_decided_at,proof_decided_by,proof_reason,machine_id FROM dtf_local.orders
@@ -239,6 +242,11 @@ def with_print_files(c, orders):
order['final_items'] = sorted({row['item_index'] for row in approved})
# The approved finals themselves: "Tratar a arte" keeps those it does not replace.
order['finals'] = [{'item_index': row['item_index'], 'upload_id': row['upload_id']} for row in approved]
if order['id'] in taken:
order['printed_before'] = True
order['print_suggested'] = hotfolder.reprint_default(
[(f['item_index'], f['upload_id']) for f in order.get('finals') or []], taken[order['id']],
len(order['snapshot']['items']))
mine = [row for row in treated if row['order_id'] == order['id']]
order['treated'] = {'items': [row['item_index'] for row in mine], 'by': mine[0]['created_by']} if mine else None
order['corrections'] = [{k: row[k] for k in ('item_index', 'upload_id', 'name', 'expired')}

View File

@@ -504,8 +504,24 @@ def move(uid: UUID, body: Move, request: Request, user=Depends(operator)):
if body.state == 'imp' and row['machine_id']:
machine_name = (c.execute('SELECT name FROM dtf_local.machines WHERE id=%s',
(row['machine_id'],)).fetchone() or {}).get('name')
c.execute('''INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason,back,machine)
VALUES(%s,%s,%s,%s,%s,%s,%s)''', (uid,row['state'],body.state,user,body.reason,back,machine_name))
# A pass from Imprimindo to Revisão keeps the items it printed, when
# only some went again (machine_log counts their metres).
printed_items = row.get('print_items') if row['state'] == 'imp' and body.state == 'rev' and not back else None
c.execute('''INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason,back,machine,items)
VALUES(%s,%s,%s,%s,%s,%s,%s,%s)''', (uid,row['state'],body.state,user,body.reason,back,machine_name,printed_items))
# Back in the queue after a print: only the items whose final changed
# go to the machine again, unless the team already chose (#105).
if body.state == 'tra' and not back and row.get('print_items') is None:
taken = hotfolder.printed(c, [uid]).get(uid)
if taken:
finals = [(r['item_index'], r['upload_id']) for r in c.execute('''SELECT item_index,upload_id
FROM dtf_local.order_files WHERE order_id=%s AND kind='final' AND active''', (uid,)).fetchall()]
default = hotfolder.reprint_default(finals, taken, len(row['snapshot']['items']))
if default:
c.execute('UPDATE dtf_local.orders SET print_items=%s WHERE id=%s', (default, uid))
# Finished: the next time it goes back, the choice starts over.
if body.state == 'fin' and not back:
c.execute('UPDATE dtf_local.orders SET print_items=NULL WHERE id=%s', (uid,))
# Pulled to Imprimindo, its files go to its machine's hot folder; out
# of it, what Flexi did not take leaves (item 10).
if row['state'] == 'imp':
@@ -537,6 +553,32 @@ def move(uid: UUID, body: Move, request: Request, user=Depends(operator)):
enqueue(c, key, 'tiny', tiny)
return changed
class PrintItems(StrictModel):
items: list[int] = Field(min_length=1, max_length=200)
@router.post('/api/operator/orders/{uid}/print-items')
def choose_print_items(uid: UUID, body: PrintItems, user=Depends(can('produce'))):
"""Which items go to the machine this time (Marcus, 2026-11-26, #105):
a reprint sends only those. Every item: all go, as on the first print."""
with db.connect() as c:
row = c.execute('SELECT id,number,state,snapshot,cancelled_at FROM dtf_local.orders WHERE id=%s FOR UPDATE',
(uid,)).fetchone()
if not row:
raise HTTPException(404, 'Pedido não encontrado.')
if row['cancelled_at'] or row['state'] not in ('rec', 'tra', 'cor'):
raise HTTPException(409, 'Os itens que vão para a máquina são escolhidos antes de Imprimindo.')
count = len(row['snapshot']['items'])
items = sorted(set(body.items))
if any(i < 0 or i >= count for i in items):
raise HTTPException(422, 'Item do pedido inválido.')
chosen = None if len(items) == count else items
c.execute('UPDATE dtf_local.orders SET print_items=%s, version=version+1, updated_at=now() WHERE id=%s',
(chosen, uid))
audit('order_print_items', order=row['number'], items=','.join(str(i + 1) for i in items), operator=user)
return {'ok': True, 'print_items': chosen}
@router.post('/api/operator/orders/{uid}/print-files')
def regenerate(uid: UUID, user=Depends(can('produce'))):
"""Queue generation again for items that failed or went to manual preparation,