$workflowSelect="SELECT id FROM workflows ws WHERE workflows.main_document_id = main_document_id AND process_date IS NULL AND status IS NULL ORDER BY \"order\" LIMIT 1";
$workflowSelect="SELECT main_document_id FROM workflows WHERE user_id in (?) AND id in ({$workflowSelect})";
$where=["id in ({$workflowSelect}) OR typist = ?"];
$whereStatuses[]="(status = 'VAL' AND main_document_id not in (SELECT main_document_id FROM workflows WHERE status is null OR status in ('REF', 'END', 'STOP')))";