|
|
|
|
#!/usr/bin/php -q
|
|
|
|
|
<?php
|
|
|
|
|
/* * ***************************************************************************
|
|
|
|
|
* Este script <EFBFBD> usado para comparar as chamadas locais comas as do cloud,
|
|
|
|
|
* preenche uma tabela com informa<EFBFBD><EFBFBD>es das chamadas por data. Outro script
|
|
|
|
|
* ira ler estas informacoes e importa as chamadas faltantes.
|
|
|
|
|
* Amarildo Pereira
|
|
|
|
|
* 18/12/2019
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
include("util/util.php");
|
|
|
|
|
error_reporting(E_ERROR);
|
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
$pathLog = '/var/log/asterisk/atualiza_conferencia.log';
|
|
|
|
|
$host = "127.0.0.1";
|
|
|
|
|
//$host = "algar.simplesip.com.br";
|
|
|
|
|
$port = "5432";
|
|
|
|
|
$dbname = "pbx";
|
|
|
|
|
$user = "contacte";
|
|
|
|
|
$password = "ctepgSQL";
|
|
|
|
|
$connAlgar = pg_connect("host='{$host}' port='{$port}' dbname='{$dbname}' user='{$user}' password='{$password}' connect_timeout='5'");
|
|
|
|
|
|
|
|
|
|
$init = (isset($argv[1]) && ($argv[1] == 'REINICIAR')) ? true : false;
|
|
|
|
|
//$init = true;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Processamento seletivo, devem ser passados os id separados por ";"(porto e virgula).
|
|
|
|
|
*/
|
|
|
|
|
$empIds = isset($argv[2]) ? __GetEmpId($argv[2]) : false;
|
|
|
|
|
//$empIds = __GetEmpId("2262;2268;2276;2277;2285;2292");
|
|
|
|
|
|
|
|
|
|
if (!$connAlgar) {
|
|
|
|
|
__Log("Nao foi conectar ao banco de dados da algar!!!!!\n");
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__Log(sprintf("Data: %s Iniciando o processamento!!!!!\n", date("Y-m-d H:i:s")));
|
|
|
|
|
|
|
|
|
|
$proc = 0;
|
|
|
|
|
$tpStatus = 1;
|
|
|
|
|
$query = "select distinct b.emp_id, b.emp_nome_fantasia, b.emp_vpn
|
|
|
|
|
from vds_empresas b
|
|
|
|
|
where b.user_reg = 0
|
|
|
|
|
and length(emp_vpn) > 7
|
|
|
|
|
and b.emp_id in(select min(emp_id) from vds_empresas where user_reg = 0 and length(emp_vpn) > 7 group by emp_vpn)\n";
|
|
|
|
|
if ($empIds) {
|
|
|
|
|
$query .= " and b.emp_id in({$empIds})\n";
|
|
|
|
|
}
|
|
|
|
|
$query .= " order by b.emp_id";
|
|
|
|
|
$resultAlgar = pg_query($connAlgar, $query);
|
|
|
|
|
$dadosConexoes = pg_fetch_all($resultAlgar);
|
|
|
|
|
|
|
|
|
|
foreach ($dadosConexoes as $dadosConexao) {
|
|
|
|
|
|
|
|
|
|
$empId = $dadosConexao["emp_id"];
|
|
|
|
|
$host = trim($dadosConexao["emp_vpn"]);
|
|
|
|
|
$nome = trim($dadosConexao["emp_nome_fantasia"]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$conexaoVpn = pg_connect("host='{$host}' port='{$port}' dbname='{$dbname}' user='{$user}' password='{$password}' connect_timeout='5'", PGSQL_CONNECT_FORCE_NEW);
|
|
|
|
|
if (!$conexaoVpn) {
|
|
|
|
|
throw new Exception(sprintf("Erro de conexao: %s %s Vpn: %s \n", $nome, $empId, $host), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AtualizaChamdas($conexaoVpn, $init);
|
|
|
|
|
AtualizaProtocolo($conexaoVpn, $init);
|
|
|
|
|
AtualizaClassificacoes($conexaoVpn, $init);
|
|
|
|
|
AtualizaUsuarios($conexaoVpn, $init);
|
|
|
|
|
__Log(sprintf("Revenda: %s %s Vpn: %s reprocessada!\n", $nome, $empId, $host));
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
__Log(sprintf("Data: %s Cliente: %s %s Host: %s Msg: %s\n", date('Y-m-d H:i:s'), $nome, $empId, $host, $ex->getMessage()));
|
|
|
|
|
}
|
|
|
|
|
if ($conexaoVpn) {
|
|
|
|
|
pg_close($conexaoVpn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AtualizaAlgar($connAlgar);
|
|
|
|
|
|
|
|
|
|
//exec("chown -R algar:algar /hdaux");
|
|
|
|
|
__Log(sprintf("\nData: %s Operacao finalizada !!!!!\n", date("Y-m-d H:i:s")));
|
|
|
|
|
|
|
|
|
|
function __Log($log) {
|
|
|
|
|
global $pathLog;
|
|
|
|
|
if (!$pathLog) {
|
|
|
|
|
$pathLog = '/var/log/asterisk/log_geral.log';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arq = fopen($pathLog, 'a');
|
|
|
|
|
fwrite($arq, $log);
|
|
|
|
|
fclose($arq);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AtualizaChamdas($conexaoVpn, $init = false) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Se a base ainda nao tiver estrutura forca a criacao.
|
|
|
|
|
*/
|
|
|
|
|
if (!$init && VerificaInit($conexaoVpn, 'vds_chamadas_confere')) {
|
|
|
|
|
$init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "DROP TABLE IF EXISTS vds_chamadas_confere;
|
|
|
|
|
create table vds_chamadas_confere(emp_id int not null, cfr_data date not null, cfr_fila varchar(128) not null, cfr_numero_chamadas int, cfr_numero_chamadas_local int,cfr_processada int not null default 0, constraint \"pkChamadasConfere\" primary key (emp_id, cfr_data, cfr_fila));";
|
|
|
|
|
if ($init) {
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_chamadas_confere(emp_id, cfr_data, cfr_fila, cfr_numero_chamadas)
|
|
|
|
|
select 0 as emp_id, a.calldate::date, b.fila, count(*) as numero_chamdas
|
|
|
|
|
from pbx_bilhetes a, pbx_eventos_dacs b
|
|
|
|
|
where b.uid2 = a.uniqueid
|
|
|
|
|
and b.evento in ('COMPLETEAGENT','COMPLETECALLER','COMPLETAAGENT','COMPLETACALLER', 'TRANSFER', 'TRANSFERORIG', 'NOANSWERS','COMPLETECALLERRAMAL', 'COMPLETACALLERRAMAL', 'COMPLETEAGENTRAMAL')
|
|
|
|
|
and calldate::date < now()::date
|
|
|
|
|
and not exists(select '' from vds_chamadas_confere where cfr_data = a.calldate::date and cfr_fila = b.fila)
|
|
|
|
|
group by a.calldate::date, b.fila;";
|
|
|
|
|
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AtualizaProtocolo($conexaoVpn, $init = false) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Se a base ainda nao tiver estrutura forca a criacao.
|
|
|
|
|
*/
|
|
|
|
|
if (!$init && VerificaInit($conexaoVpn, 'vds_protocolos_confere')) {
|
|
|
|
|
$init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "drop table if exists vds_protocolos_confere;
|
|
|
|
|
create table vds_protocolos_confere(emp_id int not null, cfr_data date not null, cfr_numero_protocolos int, cfr_numero_protocolos_local int, cfr_processada int not null default 0, constraint \"pkProtocolosConfere\" primary key (emp_id, cfr_data));";
|
|
|
|
|
if ($init) {
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = " insert into vds_protocolos_confere(emp_id, cfr_data, cfr_numero_protocolos)
|
|
|
|
|
select 0 as emp_id, \"dataReg\"::date as data_protocolo, count(*) as numero_protocolos
|
|
|
|
|
from pbx_protocolo_reg
|
|
|
|
|
where exists(select '' from pbx_bilhetes where uniqueid = pbx_protocolo_reg.uniqueid)
|
|
|
|
|
and \"dataReg\"::date < now()::date
|
|
|
|
|
and not exists(select '' from vds_protocolos_confere where cfr_data = pbx_protocolo_reg.\"dataReg\"::date )
|
|
|
|
|
group by 1, 2;";
|
|
|
|
|
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AtualizaClassificacoes($conexaoVpn, $init = false) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Se a base ainda nao tiver estrutura forca a criacao.
|
|
|
|
|
*/
|
|
|
|
|
if (!$init && VerificaInit($conexaoVpn, 'vds_classificacoes_confere')) {
|
|
|
|
|
$init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "drop table if exists vds_classificacoes_confere;
|
|
|
|
|
create table vds_classificacoes_confere(emp_id int not null, cfr_data date not null, cfr_fila varchar(128) not null, cfr_numero_classificacoes int, cfr_numero_classificacoes_local int, cfr_processada int not null default 0, constraint \"pkClassificacoesConfere\" primary key (emp_id, cfr_data, cfr_fila));";
|
|
|
|
|
|
|
|
|
|
if ($init) {
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = " insert into vds_classificacoes_confere(emp_id, cfr_data, cfr_fila, cfr_numero_classificacoes)
|
|
|
|
|
select 0 as emp_id, a.data_reg::date, d.nome, count(*)
|
|
|
|
|
from pbx_classifica_reg a, pbx_classifica_atendimento b, pbx_classifica_item c, pbx_dacs d
|
|
|
|
|
where b.clas_id = a.clas_id
|
|
|
|
|
and c.clit_id = a.clit_id
|
|
|
|
|
and d.id = a.id_dac
|
|
|
|
|
and not exists(select '' from vds_classificacoes_confere where cfr_data = a.data_reg::date and cfr_fila = d.nome)
|
|
|
|
|
group by 1,2,3;";
|
|
|
|
|
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AtualizaUsuarios($conexaoVpn, $init = false) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Se a base ainda nao tiver estrutura forca a criacao.
|
|
|
|
|
*/
|
|
|
|
|
if (!$init && VerificaInit($conexaoVpn, 'vds_usuarios_confere')) {
|
|
|
|
|
$init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "drop table if exists vds_usuarios_confere;
|
|
|
|
|
create table vds_usuarios_confere(emp_id int not null, cfr_matricula varchar(16), cfr_processada int not null default 0, constraint \"pkUsuariosConfere\" primary key (emp_id, cfr_matricula));";
|
|
|
|
|
if ($init) {
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_usuarios_confere
|
|
|
|
|
select 0 as emp_id, a.matricula
|
|
|
|
|
from pbx_usuarios a
|
|
|
|
|
where not exists(select '' from vds_usuarios_confere where cfr_matricula = a.matricula)";
|
|
|
|
|
|
|
|
|
|
__execQuery($conexaoVpn, $query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __execQuery($conn, $query) {
|
|
|
|
|
if (!pg_query($conn, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __GetEmpId($strEmpId) {
|
|
|
|
|
$empIds = explode(";", $strEmpId);
|
|
|
|
|
$valEmpIds = '';
|
|
|
|
|
|
|
|
|
|
foreach ($empIds as $empId) {
|
|
|
|
|
$empId = soNumero($empId);
|
|
|
|
|
$valEmpIds .= !$valEmpIds ? $empId : ",{$empId}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $valEmpIds ? $valEmpIds : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function VerificaInit($conn, $table) {
|
|
|
|
|
|
|
|
|
|
$result = pg_query($conn, "select count(*) from $table ");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A tabela nao existe.
|
|
|
|
|
*/
|
|
|
|
|
if (!$result) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ainda nao tem dados integrados.
|
|
|
|
|
*/
|
|
|
|
|
$row = pg_fetch_row($result);
|
|
|
|
|
if (!$row[0]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AtualizaAlgar($conexaoCloud) {
|
|
|
|
|
global $empIds, $init;
|
|
|
|
|
|
|
|
|
|
if ($init) {
|
|
|
|
|
if($empIds){
|
|
|
|
|
$query = "delete from vds_chamadas_confere where emp_id in($empIds);";
|
|
|
|
|
} else{
|
|
|
|
|
$query = "truncate table vds_chamadas_confere;";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pg_query($conexaoCloud, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s AtualizaAlgar: [vds_chamadas_confere] Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_chamadas_confere(emp_id, cfr_data, cfr_fila, cfr_numero_chamadas)
|
|
|
|
|
select emp_id, chm_calldate::date as data, chm_fila, count(*) as numero_chamdas
|
|
|
|
|
from vds_chamadas a
|
|
|
|
|
where chm_calldate::date < now()::date";
|
|
|
|
|
if ($empIds) {
|
|
|
|
|
$query .= " and a.emp_id in({$empIds}) ";
|
|
|
|
|
}
|
|
|
|
|
$query .= " and not exists(select '' from vds_chamadas_confere where emp_id = a.emp_id and cfr_data = a.chm_calldate::date and cfr_fila = a.chm_fila)
|
|
|
|
|
group by emp_id, chm_calldate::date, chm_fila;";
|
|
|
|
|
|
|
|
|
|
if (!pg_query($conexaoCloud, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s AtualizaAlgar: [vds_chamadas_confere] Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_protocolos_confere(emp_id, cfr_data, cfr_numero_protocolos)
|
|
|
|
|
select emp_id, data_protocolo::date as data, count(*) as num_proto -- CONSTRAINT vds_chamadas_pkey PRIMARY KEY (chm_id)
|
|
|
|
|
from vds_protcolos a
|
|
|
|
|
where data_protocolo::date < now()::date";
|
|
|
|
|
if ($empIds) {
|
|
|
|
|
$query .= " and a.emp_id in({$empIds}) ";
|
|
|
|
|
}
|
|
|
|
|
$query .= " and not exists(select '' from vds_protocolos_confere where emp_id = a.emp_id and cfr_data = a.data_protocolo::date)
|
|
|
|
|
group by emp_id, data_protocolo::date;";
|
|
|
|
|
|
|
|
|
|
if (!pg_query($conexaoCloud, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s AtualizaAlgar: [vds_protocolos_confere] Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_classificacoes_confere(emp_id, cfr_data, cfr_fila, cfr_numero_classificacoes)
|
|
|
|
|
select emp_id, clas_reg::date as data, clas_dac, count(*) as num_classificacoes
|
|
|
|
|
from vds_classificacoes a
|
|
|
|
|
where clas_reg::date < now()::date";
|
|
|
|
|
if ($empIds) {
|
|
|
|
|
$query .= " and a.emp_id in({$empIds}) ";
|
|
|
|
|
}
|
|
|
|
|
$query .= " and not exists(select '' from vds_classificacoes_confere where emp_id = a.emp_id and cfr_data = a.clas_reg::date and cfr_fila = a.clas_dac)
|
|
|
|
|
group by emp_id, clas_reg::date, clas_dac;";
|
|
|
|
|
|
|
|
|
|
if (!pg_query($conexaoCloud, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s AtualizaAlgar: [vds_classificacoes_confere] Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$query = "insert into vds_usuarios_confere
|
|
|
|
|
select emp_id, vdr_matricula
|
|
|
|
|
from vds_vendedores a
|
|
|
|
|
where not exists(select '' from vds_usuarios_confere where emp_id = a.emp_id and cfr_matricula = a.vdr_matricula)";
|
|
|
|
|
if ($empIds) {
|
|
|
|
|
$query .= " and a.emp_id in({$empIds}) ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!pg_query($conexaoCloud, $query)) {
|
|
|
|
|
__Log(sprintf("Data: %s AtualizaAlgar: [vds_usuarios_confere] Msg: %s\n", date('Y-m-d H:i:s'), GetLasterror()));
|
|
|
|
|
}
|
|
|
|
|
}
|