"""Which of an order's finals count: shared by the board, the proof and the worker.""" # An item's final counts while its file is still there and scanned clean: the # same rule that lets an order into the print queue. FINAL_OK = """f.kind='final' AND f.active AND u.expires_at>now() AND u.purged_at IS NULL AND u.scan_state='clean'""" # "Arte tratada" (Dropstar, 2026-10-27): a final someone prepared, not the # customer's own artwork. It is neither the file the system generated from # their art, nor one of their originals, nor a correction they sent. `f` is # the final in dtf_local.order_files, `o` its order. TREATED = """f.kind='final' AND NOT EXISTS (SELECT 1 FROM dtf_local.print_parts p WHERE p.order_id=f.order_id AND p.upload_id=f.upload_id) AND NOT EXISTS (SELECT 1 FROM jsonb_array_elements(o.snapshot->'items') i WHERE i->'uploads' ? f.upload_id::text) AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files k WHERE k.order_id=f.order_id AND k.upload_id=f.upload_id AND k.kind='correction')"""