#!/usr/bin/php -Cq getMessage(), $ex->getFile(), $ex->getLine(), $query, date('Y-m-d H:i:s')), $pathLog); @pg_close(); $conn = false; socket_close($socket); $socket = null; } /* * Verifica se o processo deve ser encerrado */ if (sig_status()) { break; } if ($debugLocal) { if (++$debugLocal == MAX_CLICLO_DEBUG) { break; } } sleep($wait); } /* * Marca o fim da execucao do script. */ GravaLog(sprintf("Finaliza Execucao: %s\n\n", date('Y-m-d H:i:s')), $pathLog); function __GetAgentesLivresRad() { $query = "select count(*) from pbx_supervisor_agentes b, pbx_supervisor_dacs c, pbx_queues_grupos d where c.dac = b.dac and d.nome = c.dac and b.status = 'LIVRE' and b.status_discador = 0 and b.disponivel_atendimento = 1 and strtoint(c.espera) = 0 and d.retornar_abandonadas = 1"; if (!$result = pg_query($query)) { RaiseExcept("Não foi possível consultar agentes livres!"); } if (!pg_num_rows($result)) { return 0; } $rows = pg_fetch_row($result); return $rows[0]; } function __GetQueryRad($agentesLivres, $diasAbandon) { $tentativasRetorno = RTABD_TENTATIVAS_RETORNO; $tempoRetorno = RTABD_TEMPO_RETORNO; return " select case when(upper(b.modo_atendimento) = 'MANUAL')then case when( upper(e.tipo_ramal) = 'KHOMP')then upper(e.tipo_ramal) || '/r' || e.nome else e.dispositivo end else 'Local/' || b.ramal || '@app-callcenter/n' end as canal_agente, b.matricula, abdsr_numero, b.ramal, abdsr_uniqueid, abdsr_fila, abdsr_data_hora_inicio, a.abdsr_callback from pbx_abandonadas_semretorno a, pbx_supervisor_agentes b, pbx_supervisor_dacs c, pbx_queues_grupos d, pbx_ramais e where b.dac = a.abdsr_fila and c.dac = b.dac and d.nome = c.dac and e.nome = b.ramal and a.abdsr_data_hora_retorno is null and length(a.abdsr_numero) >= 8 and ((a.abdsr_callback < $tentativasRetorno) and (extract(epoch from (now() - coalesce(abdsr_callback_data, now())))::int > $tempoRetorno) or (a.abdsr_callback = 0)) and b.status = 'LIVRE' and b.status_discador = 0 and b.disponivel_atendimento = 1 and strtoint(c.espera) = 0 and d.retornar_abandonadas = 1 and a.abdsr_data >= (now()::date - $diasAbandon) order by a.abdsr_data_hora_inicio desc limit $agentesLivres"; // and ((a.abdsr_callback <= $tentativasRetorno) and (extract(epoch from (now() - coalesce(abdsr_callback_data, now())))::int > $tempoRetorno) or (a.abdsr_callback = 0)) } function SincronizaAgente($matricula, $numero, $dataAbd, $tentativa) { global $pathLog; $dataAbd = str_replace(':', '.', FormataDBDataHora($dataAbd)); $contId = sprintf("%s|%s|%s%s", 0, $numero, 0, "|Retorno ABD:$numero|Data:$dataAbd|Tentativa:$tentativa"); $query = sprintf("update pbx_supervisor_agentes set cont_identificador = %s where matricula = '%s'", QuotedStr($contId), $matricula); if (!pg_query($query)) { /* * Registra chamada. */ GravaLog(sprintf("Erro: %s File: %s Line: %s \ncmd: %s\n", GetLasterror(), 'rad.php', '193', $query), $pathLog); } } function GetDaemon($notDaemon) { /* * Se o script não for chamado com daemon sai sem executar nada. */ if ($notDaemon) { return 0; } $pid = pcntl_fork(); if ($pid) { exit(0); //success } // posix_setsid(); } function GetRadHelp() { global $argc, $argv; $arg = isset($argv[1]) ? $argv[1] : ''; if ($argc && ((stripos($arg, '--h') !== false) || (stripos($arg, '-h') !== false) || (stripos($arg, '/h') !== false))) { return displayUsage(); } } function displayUsage() { echo "--------------------------------------------------------------------------------------------------------\n"; echo "- Servico de sincronizacao de dados para retorno de abandonadas -\n"; echo "--------------------------------------------------------------------------------------------------------\n"; echo "- Para rodar como um processo normal passe \"N\" como primeiro argumento:\"./retornoAbandonada.php N\"!-\n"; echo "- Para rodar como Daemon nao passe nenhum argumemento argumento:\"./retornoAbandonada.php \"! -\n"; echo "--------------------------------------------------------------------------------------------------------\n"; echo "\n"; exit(0); } function sig_handler($signo) { global $statusSignal; //echo "\n This signal is called. [$signo] \n"; $statusSignal = 1; } function sig_status() { global $statusSignal; pcntl_signal_dispatch(); return $statusSignal; } function __GetAgenteDiscagem($dados) { $dadosDiscagem = array(); /* * pega sempre o agente que recebeu uma chamda a mais tempo. */ $query = "select distinct case when(upper(b.modo_atendimento) = 'MANUAL')then case when( upper(e.tipo_ramal) = 'KHOMP')then upper(e.tipo_ramal) || '/r' || e.nome else e.dispositivo end else 'Agent/' || b.matricula end as canal_agente, b.matricula, b.ramal, b.duracao from pbx_abandonadas_semretorno a, pbx_supervisor_agentes b, pbx_supervisor_dacs c, pbx_queues_grupos d, pbx_ramais e where b.dac = a.abdsr_fila and c.dac = b.dac and d.nome = c.dac and e.nome = b.ramal and a.abdsr_data_hora_retorno is null and a.abdsr_callback = 0 and b.status = 'LIVRE' and b.status_discador = 0 and b.disponivel_atendimento = 1 and strtoint(c.espera) = 0 and d.retornar_abandonadas = 1 and a.abdsr_data >= (now()::date - 1) order by b.duracao desc limit 1"; $result = pg_query($query); if (!$result) { GeraExcept("Erro ao consultar \"Agente Livre\"!"); } if (pg_num_rows($result)) { $row = pg_fetch_array($result, null, PGSQL_ASSOC); $dadosDiscagem[] = $row['canal_agente']; $dadosDiscagem[] = $row['matricula']; $dadosDiscagem[] = $dados['abdsr_numero']; $dadosDiscagem[] = $row['ramal']; $dadosDiscagem[] = $dados['abdsr_uniqueid']; $dadosDiscagem[] = $dados['abdsr_fila']; $dadosDiscagem[] = $dados['abdsr_data_hora_inicio']; return $dadosDiscagem; } return $dadosDiscagem; /* * __GetQueryRad ---> abdsr_numero, abdsr_uniqueid, abdsr_fila, abdsr_data_hora_inicio $result = pg_query(__GetQueryRad($agentesLivres, $diasAbandon)); while ($rows = pg_fetch_array($result, null, PGSQL_ASSOC)) { $rows = __GetAgenteDiscagem($rows); list($channelAgente, $matricula, $numDisc, $ramal, $uid, $fila, $horaAbd) = array_values($rows); */ }