feat: Gestão moves a printing order to another machine with a reason; the new one resumes from the part being ripped (Rodada 5, D4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-30 15:03:02 -03:00
parent 030b66c697
commit c2aa1d4937
7 changed files with 185 additions and 0 deletions

View File

@@ -219,6 +219,9 @@ def with_print_files(c, orders):
# clock leaves out the time a preview waited for the customer (D2).
previews = c.execute('''SELECT order_id,number,sent_at,sent_by,decided_at,result,reason,decided_by
FROM dtf_local.redesign_previews WHERE order_id=ANY(%s) ORDER BY order_id,number''', (ids,)).fetchall()
# Each order's machine transfers, oldest first (Rodada 5, D4).
transfers = c.execute('''SELECT order_id,from_name,to_name,reason,resumed_from,created_by,created_at
FROM dtf_local.machine_transfers WHERE order_id=ANY(%s) ORDER BY id''', (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
@@ -267,6 +270,9 @@ def with_print_files(c, orders):
order['previews'] = [{k: row[k] for k in ('number', 'sent_at', 'sent_by', 'decided_at', 'result',
'reason', 'decided_by')}
for row in previews if row['order_id'] == order['id']]
order['transfers'] = [{k: row[k] for k in ('from_name', 'to_name', 'reason', 'resumed_from',
'created_by', 'created_at')}
for row in transfers if row['order_id'] == order['id']]
return assign_machines(c, orders)

View File

@@ -133,6 +133,47 @@ def choose_machine(uid: UUID, body: MachineChoice, request: Request, user=Depend
return {'ok': True}
class MachineTransfer(StrictModel):
machine_id: UUID
reason: str = Field(max_length=500)
@router.post('/api/operator/orders/{uid}/transfer')
def transfer_machine(uid: UUID, body: MachineTransfer, user=Depends(can('manage'))):
"""A card already in Imprimindo changes machine (Rodada 5, D4), by Gestão
or an administrator and with its reason: the old machine keeps what Flexi
already took, and the new one starts from the part it was ripping."""
reason = ' '.join(body.reason.split())
with db.connect() as c:
current = c.execute('SELECT * FROM dtf_local.orders WHERE id=%s FOR UPDATE', (uid,)).fetchone()
if not current:
raise HTTPException(404, 'Pedido não encontrado.')
if current['cancelled_at'] or current['state'] != 'imp':
raise HTTPException(409, 'Só um pedido em Imprimindo muda de máquina por aqui.')
if len(reason) < 3:
raise HTTPException(422, 'Escreva o motivo da transferência.')
target = c.execute('SELECT name,products,active FROM dtf_local.machines WHERE id=%s',
(body.machine_id,)).fetchone()
if not target or not target['active']:
raise HTTPException(404, 'Máquina não encontrada ou desativada.')
if body.machine_id == current['machine_id']:
raise HTTPException(409, 'O pedido já está nessa máquina.')
if machines.product_family(current) not in target['products']:
raise HTTPException(409, f"A {target['name']} não imprime este produto.")
from_name = (c.execute('SELECT name FROM dtf_local.machines WHERE id=%s',
(current['machine_id'],)).fetchone() if current['machine_id'] else None)
from_name = from_name and from_name['name']
resumed = hotfolder.transfer(c, uid)
c.execute('UPDATE dtf_local.orders SET machine_id=%s, machine_chosen=true WHERE id=%s',
(body.machine_id, uid))
c.execute('''INSERT INTO dtf_local.machine_transfers(order_id,from_machine,to_machine,from_name,to_name,
reason,resumed_from,created_by) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)''',
(uid, current['machine_id'], body.machine_id, from_name, target['name'], reason, resumed, user))
audit('order_machine_transferred', order=current['number'], from_machine=str(current['machine_id'] or ''),
to_machine=str(body.machine_id), operator=user)
return {'ok': True}
@router.post('/api/operator/orders/{uid}/contraction')
def choose_contraction(uid: UUID, body: Contraction, user=Depends(can('produce'))):
"""The contraction class of an item (Dropstar, 2026-10-30): the browser's

View File

@@ -113,6 +113,24 @@ def stop(c, order_id):
WHERE order_id=%s AND state IN ('queued','claimed','written')''', (order_id,))
def transfer(c, order_id):
"""The order changed machine in Imprimindo (Rodada 5, D4): the old machine
keeps what Flexi already took, and what it had not leaves its folder; the
new one starts from the part the old was ripping, in a new round."""
last = c.execute('SELECT max(round) AS r FROM dtf_local.hot_parts WHERE order_id=%s',
(order_id,)).fetchone()['r']
if last is None:
return 1
taken = c.execute('''SELECT max(seq) AS s FROM dtf_local.hot_parts
WHERE order_id=%s AND round=%s AND state='consumed' ''', (order_id, last)).fetchone()['s']
stop(c, order_id)
c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name)
SELECT order_id, %s, row_number() OVER (ORDER BY seq), item_index, upload_id, name
FROM dtf_local.hot_parts WHERE order_id=%s AND round=%s AND seq >= %s''',
(last + 1, order_id, last, taken or 1))
return taken or 1
def claim(c, user):
"""The parts due in the folders now, claimed for this computer to write:
for each machine, the next parts of its orders in Imprimindo, in the order

View File

@@ -0,0 +1,16 @@
-- Rodada 5, D4: a card already in Imprimindo changes machine, by Gestão or an
-- administrator with a reason; the part the new machine resumed from is kept
-- for the Revisor to check the tone seam.
CREATE TABLE IF NOT EXISTS dtf_local.machine_transfers (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id uuid NOT NULL REFERENCES dtf_local.orders(id) ON DELETE CASCADE,
from_machine uuid,
to_machine uuid NOT NULL,
from_name text,
to_name text NOT NULL,
reason text NOT NULL,
resumed_from int,
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS machine_transfers_order ON dtf_local.machine_transfers(order_id);