$query = "drop table if exists vds_chamadas_confere;
drop table if exists vds_protcolos_confere;
select * into vds_protcolos_confere from vds_protcolos a where coalesce(a.prt_proto_parceiro, '') <> '' and exists(select '' from vds_ver_proto where proto = a.prt_proto_parceiro);
create index \"idx_vds_protcolos_confere_emp_uid\" on vds_protcolos_confere(emp_id, uid);
create index \"idx_vds_protcolos_confere_prt_proto\" on vds_protcolos_confere(prt_proto_parceiro);
select * into vds_chamadas_confere from vds_chamadas a where exists(select '' from vds_protcolos_confere where emp_id = a.emp_id and uid = a.uid);
create index \"idx_vds_chamadas_confere_emp_uid\" on vds_chamadas_confere(emp_id, uid);
inner join vds_protcolos_confere b on b.prt_proto_parceiro = a.proto
inner join (select emp_id, chm_calldate, uid from vds_chamadas_confere where chm_data_bilhete >= '2020-01-01') c on c.emp_id = b.emp_id and c.uid = b.uid
inner join vds_empresas d on d.emp_id = c.emp_id;";
$result = pg_query($query);
while ($dados = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$id = $dados["id_proto"];
$origem = $dados["origem"];
$destino = $dados["destino"];
$controle = 9;
$arq = 'NE';
if (file_exists($destino)) {
$controle = 1;
$arq = $destino;
} else if (file_exists($origem)) {
$controle = 2;
$arq = $origem;
}
$query = "update vds_ver_proto set existe = '{$controle}', file_name = '{$arq}' where id_proto = '{$id}'\n";