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

@@ -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