feat: Redesenho previews on the server: one at a time, counted, and the stage clock stops while one waits (Rodada 5, D2)
POST /api/operator/orders/{id}/redesign-preview (sent, approved,
rejected), redesign_previews table, previews on every board order, and
assign_late leaves out the time a preview waited for the customer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -137,7 +137,15 @@ def assign_late(c, orders, limits):
|
||||
if not since and order['state'] == 'fin':
|
||||
since = order.get('finished_at')
|
||||
since = since or order['created_at']
|
||||
hours = max(0.0, (now - since).total_seconds() / 3600)
|
||||
hours = (now - since).total_seconds() / 3600
|
||||
# A preview waiting for the customer stops the stage clock: the time
|
||||
# between sending it and its answer is left out (D2).
|
||||
for preview in order.get('previews') or []:
|
||||
if preview['sent_at'] < since:
|
||||
continue
|
||||
ended = preview['decided_at'] or now
|
||||
hours -= max(0.0, (ended - preview['sent_at']).total_seconds() / 3600)
|
||||
hours = max(0.0, hours)
|
||||
limit_hours = float(limit['hours'])
|
||||
# Paid designer work gives Art received its extra hours to prepare the
|
||||
# art: 48 h for a Redesenho, 24 h for the basic Ajuste Fino.
|
||||
@@ -207,6 +215,10 @@ def with_print_files(c, orders):
|
||||
# Why each order was last sent to correction, for the notice to the customer.
|
||||
reasons = {row['order_id']: row['reason'] for row in c.execute('''SELECT DISTINCT ON (order_id) order_id,reason
|
||||
FROM dtf_local.movements WHERE order_id=ANY(%s) AND to_state='cor' ORDER BY order_id,id DESC''', (ids,)).fetchall()}
|
||||
# A redesign's previews, oldest first: the card shows them and the stage
|
||||
# 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()
|
||||
# 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
|
||||
@@ -252,6 +264,9 @@ def with_print_files(c, orders):
|
||||
order['corrections'] = [{k: row[k] for k in ('item_index', 'upload_id', 'name', 'expired')}
|
||||
for row in fixes if row['order_id'] == order['id']]
|
||||
order['reworks'] = [row for row in reworks if row['order_id'] == order['id']]
|
||||
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']]
|
||||
return assign_machines(c, orders)
|
||||
|
||||
|
||||
|
||||
@@ -191,6 +191,46 @@ def contact(uid: UUID, body: ContactWait, user=Depends(can('produce'))):
|
||||
return {'ok': True}
|
||||
|
||||
|
||||
class RedesignPreview(StrictModel):
|
||||
action: Literal['sent', 'approved', 'rejected']
|
||||
reason: str = Field(default='', max_length=500)
|
||||
|
||||
|
||||
@router.post('/api/operator/orders/{uid}/redesign-preview')
|
||||
def redesign_preview(uid: UUID, body: RedesignPreview, user=Depends(can('produce'))):
|
||||
"""A redesign's preview: sent to the customer, then approved or rejected.
|
||||
One preview waits at a time; the stage clock stops while it waits."""
|
||||
with db.connect() as c:
|
||||
row = c.execute('SELECT id,number,snapshot,state,cancelled_at FROM dtf_local.orders WHERE id=%s FOR UPDATE',
|
||||
(uid,)).fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, 'Pedido não encontrado.')
|
||||
if not (row['snapshot'] or {}).get('redesign'):
|
||||
raise HTTPException(409, 'Este pedido não tem Ajuste Fino Redesenho.')
|
||||
if row['cancelled_at'] or row['state'] != 'rec':
|
||||
raise HTTPException(409, 'A prévia só vale com o pedido em Arte recebida.')
|
||||
prior = c.execute('''SELECT id,number,decided_at,result FROM dtf_local.redesign_previews
|
||||
WHERE order_id=%s ORDER BY number''', (uid,)).fetchall()
|
||||
if body.action == 'sent':
|
||||
if any(preview['decided_at'] is None for preview in prior):
|
||||
raise HTTPException(409, 'Já há uma prévia com o cliente.')
|
||||
if any(preview['result'] == 'approved' for preview in prior):
|
||||
raise HTTPException(409, 'A prévia já foi aprovada.')
|
||||
number = max((preview['number'] for preview in prior), default=0) + 1
|
||||
c.execute('''INSERT INTO dtf_local.redesign_previews(order_id,number,sent_by)
|
||||
VALUES(%s,%s,%s)''', (uid, number, user))
|
||||
else:
|
||||
opened = next((preview for preview in prior if preview['decided_at'] is None), None)
|
||||
if not opened:
|
||||
raise HTTPException(409, 'Nenhuma prévia com o cliente.')
|
||||
reason = ' '.join(body.reason.split()) if body.action == 'rejected' else ''
|
||||
c.execute('''UPDATE dtf_local.redesign_previews SET decided_at=now(),result=%s,reason=%s,decided_by=%s
|
||||
WHERE id=%s''', (body.action, reason, user, opened['id']))
|
||||
number = opened['number']
|
||||
audit('redesign_preview_' + body.action, order=row['number'], number=number, operator=user)
|
||||
return {'ok': True}
|
||||
|
||||
|
||||
class ProofAnswer(StrictModel):
|
||||
result: Literal['ok', 'rejected']
|
||||
reason: str = Field(default='', max_length=500)
|
||||
|
||||
Reference in New Issue
Block a user