-- "Gerar de novo" names the new files with a version (Marcus, 2026-11-27, -- #130): DTF-130-v2-item-1-p2.png, so neither the Kanban, the FlexiPRINT -- queue nor the Hoson list mixes them up with the first ones. ALTER TABLE dtf_local.print_files ADD COLUMN IF NOT EXISTS generation integer NOT NULL DEFAULT 1; -- A Prova OK approved only the first part of a film in parts (the print -- file's first upload), so the other parts never reached the folders (#114, -- #128, #130). For the orders still before Imprimindo, the missing parts join -- the parts the Prumo already approved. INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) SELECT gen_random_uuid(), p.order_id, p.upload_id, p.item_index, 'final', 'Parte completada (prova)', 'Prumo' FROM dtf_local.print_parts p JOIN dtf_local.orders o ON o.id=p.order_id WHERE o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND o.parent_id IS NULL AND EXISTS (SELECT 1 FROM dtf_local.order_files f JOIN dtf_local.print_parts q ON q.order_id=f.order_id AND q.item_index=f.item_index AND q.upload_id=f.upload_id WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.kind='final' AND f.active AND f.created_by='Prumo') AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.upload_id=p.upload_id);