feat: Montar creates the invoiced order card (front 7a.2b)
The montador sends each stamp to storage, splits the list into items of at most 20 sources and 4000 copies, packs each one with the Site's engine and creates the order in Arte recebida: typed customer, pickup, the Site's table price, payment 'faturado', origin dropflow for Expedição and atendimento otherwise. Until Dropstar decides, these orders do not go to Tiny and the customer gets no paid e-mail. Vendedor and Expedição may now upload through the operator upload routes (can_any). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,7 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
from ..core import db
|
||||
from .. import proof
|
||||
from ..artwork import submit_files
|
||||
from ..core.auth import can, operator
|
||||
from ..core.auth import can, can_any, operator
|
||||
from ..core.models import ArtworkSubmission, UploadStart
|
||||
from ..runtime import file_rows, operator_identity
|
||||
from .uploads import begin_upload, cancel_upload, complete_upload, part_url, upload_status
|
||||
@@ -38,15 +38,15 @@ def final_status(uid: UUID, user=Depends(operator)):
|
||||
# The parts of an upload its operator started: a final (produce) or the
|
||||
# photo of a signed pickup receipt (deliver, which includes produce).
|
||||
@router.post('/api/operator/uploads/{uid}/parts/{number}')
|
||||
def final_part(uid: UUID, number: int, user=Depends(can('deliver'))):
|
||||
def final_part(uid: UUID, number: int, user=Depends(can_any('deliver', 'assemble'))):
|
||||
return part_url(uid,number,session_id=operator_identity(user))
|
||||
|
||||
@router.post('/api/operator/uploads/{uid}/complete')
|
||||
def final_complete(uid: UUID, user=Depends(can('deliver'))):
|
||||
def final_complete(uid: UUID, user=Depends(can_any('deliver', 'assemble'))):
|
||||
return complete_upload(uid,session_id=operator_identity(user))
|
||||
|
||||
@router.delete('/api/operator/uploads/{uid}')
|
||||
def final_cancel(uid: UUID, user=Depends(can('deliver'))):
|
||||
def final_cancel(uid: UUID, user=Depends(can_any('deliver', 'assemble'))):
|
||||
return cancel_upload(uid,session_id=operator_identity(user))
|
||||
|
||||
@router.get('/api/operator/orders/{oid}/files')
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
"""The montador's stamp sizes (Fase 2, front 7): Gestão edits the table."""
|
||||
from fastapi import APIRouter, Depends
|
||||
"""The montador's stamp sizes and its invoiced orders (Fase 2, front 7)."""
|
||||
import hashlib
|
||||
import json
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
from .. import payments, quote_review
|
||||
from ..core import db
|
||||
from ..core.auth import audit, can
|
||||
from ..core.models import StampSizeTable
|
||||
from ..core.models import Freight, MontadorOrder, QuoteRequest, StampSizeTable, UploadStart
|
||||
from ..runtime import operator_identity
|
||||
from .uploads import begin_upload
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -19,7 +27,44 @@ def stamp_sizes(user=Depends(can('assemble'))):
|
||||
def save_stamp_sizes(body: StampSizeTable, user=Depends(can('manage'))):
|
||||
with db.connect() as c:
|
||||
c.execute('DELETE FROM dtf_local.stamp_sizes')
|
||||
c.cursor().executemany('INSERT INTO dtf_local.stamp_sizes(letter,width_cm,updated_by) VALUES(%s,%s,%s)',
|
||||
c.executemany('INSERT INTO dtf_local.stamp_sizes(letter,width_cm,updated_by) VALUES(%s,%s,%s)',
|
||||
[(row.letter, row.width_cm, user) for row in body.sizes])
|
||||
audit('stamp_sizes_changed', operator=user, sizes=len(body.sizes))
|
||||
return {'ok': True}
|
||||
|
||||
|
||||
@router.post('/api/operator/montador/uploads')
|
||||
def montador_upload(body: UploadStart, user=Depends(can('assemble'))):
|
||||
if not body.name.lower().endswith('.png'):
|
||||
raise HTTPException(422, 'A estampa precisa ser um PNG.')
|
||||
return begin_upload(body, session_id=operator_identity(user))
|
||||
|
||||
|
||||
@router.post('/api/operator/montador/orders')
|
||||
def montador_order(body: MontadorOrder, request: Request, user=Depends(can('assemble'))):
|
||||
"""The order a montador sheet becomes, already invoiced (front 7a.2b):
|
||||
typed customer, pickup, the Site's table price, and held out of Tiny until
|
||||
Dropstar decides how it is invoiced there."""
|
||||
origin = 'dropflow' if request.state.operator['role'] == 'expedicao' else 'atendimento'
|
||||
draft = QuoteRequest(request_key=body.request_key, customer=body.customer, items=body.items,
|
||||
freight=Freight(service='pickup')).model_dump(mode='json', exclude={'request_key'})
|
||||
digest = hashlib.sha256(json.dumps(draft, sort_keys=True).encode()).hexdigest()
|
||||
owner = operator_identity(user)
|
||||
with db.connect() as c:
|
||||
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft)
|
||||
VALUES(%s,%s,%s,%s,%s) ON CONFLICT(owner,request_key) DO NOTHING''',
|
||||
(uuid4(), owner, body.request_key, digest, Jsonb(draft)))
|
||||
quote = c.execute('SELECT * FROM dtf_local.quotes WHERE owner=%s AND request_key=%s FOR UPDATE',
|
||||
(owner, body.request_key)).fetchone()
|
||||
if quote['request_hash'] != digest:
|
||||
raise HTTPException(409, 'A montagem mudou durante o envio. Tente de novo.')
|
||||
if not quote['approved']:
|
||||
quote_review.approve(c, quote, body.items, user)
|
||||
quote = c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s', (quote['id'],)).fetchone()
|
||||
receipt = {'provider': payments.MANUAL_PROVIDER, 'method': payments.INVOICED_METHOD, 'note': '',
|
||||
'status': 'paid', 'total_cents': quote['approved']['total_cents'], 'operator': user}
|
||||
order, created = payments.create_order(c, quote, receipt, origin)
|
||||
if not created:
|
||||
raise HTTPException(409, f"Esta montagem já virou o pedido #{order['number']}.")
|
||||
audit('montador_order_created', order=str(order['id']), origin=origin, operator=user)
|
||||
return order
|
||||
|
||||
@@ -241,4 +241,15 @@ def can(capability):
|
||||
return check
|
||||
|
||||
|
||||
def can_any(*capabilities):
|
||||
"""A dependency that lets through a profile with any of these capabilities,
|
||||
and returns the operator's e-mail, as the routes record it."""
|
||||
def check(request: Request):
|
||||
account = signed_in(request)
|
||||
if not any(account['role'] in CAPABILITIES[capability] for capability in capabilities):
|
||||
raise HTTPException(403, 'Seu perfil não permite esta ação.')
|
||||
return account['email']
|
||||
return check
|
||||
|
||||
|
||||
operator = can('view')
|
||||
|
||||
@@ -286,6 +286,19 @@ class QuoteRequest(StrictModel):
|
||||
raise ValueError('The delivery address CEP must be the CEP freight was quoted for')
|
||||
return self
|
||||
|
||||
class MontadorOrder(StrictModel):
|
||||
"""What the montador sends (front 7a.2b): the typed customer, pickup only
|
||||
and loose artwork only; the server prices and approves it as a quote."""
|
||||
request_key: UUID
|
||||
customer: Customer
|
||||
items: list[Item] = Field(min_length=1, max_length=30)
|
||||
|
||||
@model_validator(mode='after')
|
||||
def loose_art_only(self):
|
||||
if any(item.mode != 'avulsa' for item in self.items):
|
||||
raise ValueError('O montador só monta artes avulsas.')
|
||||
return self
|
||||
|
||||
class Review(StrictModel):
|
||||
items: list[Item] = Field(min_length=1, max_length=30)
|
||||
|
||||
|
||||
@@ -60,12 +60,19 @@ def is_test(order):
|
||||
# (Dropstar's rule D-35, 2026-10-26; the user's answers of 2026-10-27).
|
||||
MANUAL_PROVIDER = 'manual'
|
||||
OUTSIDE_METHODS = ('pix', 'card', 'cash', 'credit')
|
||||
# A montador order is invoiced, not paid at the counter: it is not one of the
|
||||
# Cotações "registrar" methods.
|
||||
INVOICED_METHOD = 'faturado'
|
||||
|
||||
|
||||
def in_tiny(order):
|
||||
"""Whether the order goes to Tiny: every order but a test or a proof card
|
||||
(app/proof.py), which is no sale. A Dropflow order goes in its own revenue
|
||||
category (app/tiny.py, CATEGORIES)."""
|
||||
if (order.get('payment') or {}).get('method') == INVOICED_METHOD:
|
||||
# Held until Dropstar decides how invoiced montador orders reach Tiny
|
||||
# (asked 2026-11-09).
|
||||
return False
|
||||
return not is_test(order) and not order.get('parent_id')
|
||||
|
||||
|
||||
@@ -146,7 +153,10 @@ def create_order(c, quote, payment, origin):
|
||||
proof.create_child(c, order)
|
||||
if is_test(order):
|
||||
return order, True
|
||||
mail.order_paid(c, order, PUBLIC_ORIGIN)
|
||||
# An invoiced montador order was not paid: no "pedido pago" e-mail until
|
||||
# Dropstar says what the customer hears (asked 2026-11-09).
|
||||
if payment.get('method') != INVOICED_METHOD:
|
||||
mail.order_paid(c, order, PUBLIC_ORIGIN)
|
||||
if not in_tiny(order):
|
||||
return order, True
|
||||
# How it was paid, for the order's payment in Tiny.
|
||||
|
||||
Reference in New Issue
Block a user