forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.7 KiB
109 lines
4.7 KiB
#!/usr/bin/php -q |
|
<?php |
|
function inicia_empresas($bdcon) { |
|
$sQuery = "select * from vds_nao_monitorado where data_script = now()::date - 1"; |
|
$query = pg_query($bdcon, $sQuery); |
|
if (pg_num_rows($query) == 0) { |
|
$valida_dia = "insert into vds_nao_monitorado " |
|
. "(emp_cnpj, data_script) " |
|
. "values ('00000000000000', now()::date - 1)"; |
|
pg_query($bdcon, $valida_dia); |
|
$sEmpresas = "select emp_cnpj, emp_vpn, emp_status from vds_empresas where emp_vpn <> ''"; |
|
$res = pg_fetch_all(pg_query($bdcon, $sEmpresas)); |
|
foreach($res as $empresa) { |
|
if (!$empresa['emp_status']) { |
|
$query = "insert into vds_nao_monitorado " |
|
. "(emp_cnpj, emp_vpn, data_script) " |
|
. "values ('{$empresa['emp_cnpj']}', '{$empresa['emp_vpn']}', now()::date - 1)"; |
|
pg_query($bdcon, $query); |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
function deleta_script($cnpj, $data, $bdcon) { |
|
return pg_query($bdcon, "delete from vds_nao_monitorado where emp_cnpj = '{$cnpj}' and data_script = '{$data}'"); |
|
} |
|
|
|
|
|
$problema_con = []; |
|
|
|
//$bdcon1 = pg_connect("host=127.0.0.1 port=5432 dbname=pbx user=contacte password=ctepgSQL"); |
|
$bdcon = pg_connect("host=algar.simplesip.com.br port=5432 dbname=pbx user=contacte password=ctepgSQL"); |
|
if (!$bdcon) { |
|
exit(0); |
|
} |
|
inicia_empresas($bdcon); |
|
$sel_script = "select emp_cnpj, emp_vpn, data_script from vds_nao_monitorado"; |
|
$scripts = pg_fetch_all(pg_query($bdcon, $sel_script)); |
|
|
|
|
|
|
|
foreach($scripts as $script) { |
|
if (!in_array($script['emp_cnpj'], $problema_con)) { |
|
$bdcon0 = pg_connect("host={$script['emp_vpn']} port=5432 dbname=pbx user=contacte password=ctepgSQL connect_timeout=10"); |
|
if (!$bdcon0) { |
|
$problema_con[] = $script['emp_cnpj']; |
|
continue; |
|
} |
|
$invertida = implode("-", array_reverse(explode('-', $script['data_script']))); |
|
$datas[0] = "'{$script['data_script']}'"; |
|
$datas[1] = "&DATA={$invertida}"; |
|
$query = "select a.uniqueid, a.protoparceiro, b.userfield " |
|
. "as arquivo_audio, b.data_bilhete, " |
|
. "case when(length(a.protoparceiro) = 12)then 1 else 0 end as status_protocolo " |
|
. "from pbx_protocolo_reg a, pbx_bilhetes b, pbx_classifica_reg c, pbx_classifica_atendimento d " |
|
. "where b.uniqueid = a.uniqueid and c.id_bilhetes = a.uniqueid and d.clas_id = c.clas_id " |
|
. "and a.protointegrado = 1 and d.clas_descricao = 'VENDA' and b.lastapp <> 'Transferred Call' " |
|
. "and b.billsec > '0'and b.data_bilhete = {$datas[0]}"; |
|
$audiosQuery = pg_query($bdcon0, $query); |
|
if (!$audiosQuery) { |
|
continue; |
|
} |
|
$audiosCliente = pg_fetch_all($audiosQuery); |
|
|
|
if (!$audiosCliente) { |
|
$del = deleta_script($script['emp_cnpj'], $script['data_script'], $bdcon); |
|
continue; |
|
} |
|
$ch = curl_init("https://algar.simplesip.com.br/apl/integracao/?method=DisponibilizaAudios&CNPJ=".$script['emp_cnpj']."{$datas[1]}&login=admin&senha=SiP1029384756&tipoRetorno=json"); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
$obj = json_decode(curl_exec($ch)); |
|
if (!(gettype($obj) == "array")) { |
|
continue; |
|
} |
|
|
|
$audios_algar[] = ''; |
|
foreach ($obj as $value) { |
|
if ($value -> enviado) { |
|
$audios_algar[] = $value -> chm_userfield; |
|
} |
|
} |
|
|
|
$controle = true; |
|
foreach ($audiosCliente as $value) { |
|
if (!(in_array($value['arquivo_audio'], $audios_algar))) { |
|
$insereAudio = "insert into vds_audios_desatualizados " |
|
. "(uniqueid, userfield, data_bilhete, emp_cnpj) " |
|
. "select * from " |
|
. "(select '".$value["uniqueid"]."' as uniqueid, '".$value['arquivo_audio']."' as userfield, DATE '".$value['data_bilhete']."' as data_bilhete, '{$script['emp_cnpj']}' as emp_cnpj) as tmp " |
|
. "where not exists (select * from vds_audios_desatualizados where uniqueid = '".$value["uniqueid"]."' " |
|
. "and userfield = '".$value['arquivo_audio']."' and data_bilhete = DATE '".$value['data_bilhete']."')" |
|
. " limit 1"; |
|
pg_query($bdcon, $insereAudio); |
|
if (!$bdcon) { |
|
$controle = false; |
|
} |
|
} |
|
|
|
} |
|
if ($controle) { |
|
$del = deleta_script($script['emp_cnpj'], $script['data_script'], $bdcon); |
|
} |
|
|
|
} |
|
} |
|
|
|
?>
|