PABX da Simples IP
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.
 
 
 
 
 
 

274 lines
9.5 KiB

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbPort = "5432";
$dbHost = "127.0.0.1"; // "192.168.80.210";
$dbName = "pbx";
$dbUser = "contacte";
$dbPassword = "ctepgSQL";
$str = "host='$dbHost' port='$dbPort' dbname='$dbName' user='$dbUser' password='$dbPassword'";
$dbcon = @pg_connect($str);
InformacoesAtendimentoRT();
function InformacoesAtendimentoRT() {
$dbcon = $GLOBALS['dbcon'];
$nomeDac = 'Api'; //$_SESSION[SS_NOME_DAC];
$matricula = '1067'; // $_SESSION[AGT_MATRICULA];
$infoChamada = array(
"ramal" => '-', "matricula" => '-', "nome" => '-', "tempo_login" => '00:00:00', "modo_atendimento" => '-',
"origem_destino" => '-', "status" => 'LOGOFF', "duracao" => '00:00:00'
);
$infoFila = array(
"fila" => '-', "abandonadas" => '0', "atendidas_pa" => '0', "espera" => '0', "tme" => '00:00:00',
"tma" => '00:00:00', "qt_fila" => '0'
);
$infoFilaOld = array(
"fila" => '-', "abandonadas" => '0', "atendidas_pa" => '0', "espera" => '0', "tme" => '00:00:00',
"tma" => '00:00:00', "qt_fila" => '0'
);
/*
* Informações da chamada corrente
*/
$queryAgente = "select ramal, matricula, nome, (LOCALTIMESTAMP(0) - tempo_login) as tempo_login, modo_atendimento, origem_destino, status, (LOCALTIMESTAMP(0) - duracao) as duracao
from pbx_supervisor_agentes
where matricula = '$matricula' ";
$queryFila = GetQueryInfoFila($nomeDac, $matricula);
echo $queryFila;
try {
//Indica o tempo entre as requisições para verifica o status do agente no banco de dados
define("TEMPO_DORME", 10);
define("MAX_ERROS", 10);
//Variavel armazena o status do agente na sessao atual
// if(!$_SESSION[AGT_CONECT])
// throw new Exception ('O agente não esta logado!');
//Tolerancia de tentavias de acesso ao banco de dados
$numErros = 0;
//Id do bloco de informacoes retornado
$idBloco = 1;
//Buffer necessário para o browser comecar a receber atualizações
echo(str_repeat(' ', 256));
_flush();
//Contera blocos de informações sobre filas e agentes
/* $encode = $GLOBALS['server']->getEncoding();
$info = "\n<?xml version=\"1.0\" encoding=\"$encode\" ?>\n";
$info .= "<root>\n";
$info .= "<status>ok</status>\n";
*
*/
$info = "<message>Inicializando monitoramento de agente! Aguarde...</message>\n";
DisplayInfo($info); //simplexml_load_string($info)->asXml();
$info = "";
//Flag que indica se ha dados a enviar ao processo chamador
$display = 0;
//Variavel define o status atual do agente
//$statusAgente = $_SESSION[SS_STATUS_AGENTE];
$start = 0;
//Indica que houve mudança na fila
$statusFila = 0;
//Display das informações para o chamador
while (true) {
$info = "";
$display = 0;
$result = pg_query($dbcon, $queryAgente);
$ret = array();
if (!$result) {
$numErros++;
} else {
//Se o agente foi desconectado, gera uma excessão informando o processo chamador
if (!pg_num_rows($result))
throw new Exception('O agente foi desconectado!');
// Recupera informações do banco de dados sobre o agente logado
$row = @pg_fetch_array($result);
GetValoresInfo($row, $infoChamada);
if ((!$start) || ($statusAgente != $infoChamada["status"])) {
// $ret[] = $infoChamada;
$statusAgente = $infoChamada["status"];
$info = "<agente id=\"$idBloco\">\n";
$info .= ArrToStrXml($infoChamada);
$info .= "</agente>\n";
DisplayInfo($info);
$display++;
}
if ($display) {
$idBloco++;
$display = 0;
}
//Recupera informações sobre a fila de atendimento
$result = pg_query($dbcon, $queryFila);
if (!$result) {
$numErros++;
} else {
$row = @pg_fetch_array($result);
if (!empty($row["fila"])) {
GetValoresInfo($row, $infoFila);
if (!$start)
GetValoresInfo($row, $infoFilaOld);
$statusFila = GetStatusFila($infoFila, $infoFilaOld);
if (!$start || $statusFila) {
$ret = array();
// $ret[] = $infoFila;
$info = "<dac id=\"$idBloco\">\n";
$info .= $info .= ArrToStrXml($infoFila);
$info .= "</dac>\n";
$display++;
DisplayInfo($info);
GetValoresInfo($row, $infoFilaOld);
}
}
}
if ($display) {
$idBloco++;
$display = 0;
}
}
if ($numErros > MAX_ERROS)
throw new Exception('A aplicação foi encerrada porque atingiu o limite máximo de erros ao tentar acessar o banco de dados!');
$start++;
sleep(TEMPO_DORME);
}
} catch (Exception $ex) {
echo GetErroSimples("InformacoesAtendimentoRT", $ex);
}
}
function GetErroSimples($method, $ex) {
$xml = "<erro>\n";
$xml .= "<status>erro</status>\n";
$xml .= "<SIPID>%s</SIPID>\n";
$xml .= "<method>%s</method>\n";
$xml .= "<code>%s</code>\n";
$xml .= "<message>%s</message>\n";
$xml .= "<line>%s</line>\n";
$xml .= "<file>%s</file>\n";
$xml .= "</erro>\n";
$xml = sprintf($xml, session_id(), $method, $ex->getCode(), $ex->getMessage(), $ex->getLine(), $ex->getFile());
return $xml;
}
function DisplayInfo($info) {
echo $info; //simplexml_load_string($info)->asXml();
_flush();
}
function _flush() {
echo(str_repeat(' ', 256));
// check that buffer is actually set before flushing
if (ob_get_length()) {
@ob_flush();
@flush();
}
}
function GetStatusFila($infoFila, $infoFilaOld) {
$count = count($infoFila);
for ($i = 0; $i < $count; $i++) {
if ($infoFila[$i] != $infoFilaOld[$i])
return true;
}
return false;
}
function GetValoresInfo($row, &$infoArray) {
foreach ($row as $key => $value) {
if (array_key_exists($key, $infoArray)) {
$infoArray[$key] = $value;
}
}
}
function GetQueryInfoFila($nomeDac, $matricula) {
return "SELECT fila
,ABANDONADAS
,ATENDIDAS_PA
,ESPERA
,round( CASE WHEN(ESPERA = 0)THEN 0 ELSE (TEMPO_ESPERA / ESPERA) END ) * INTERVAL '1 SECOND' AS TME
,round( CASE WHEN(ATENDIDAS_PA = 0)THEN 0 ELSE (TEMPO_ATENDIMENTO / ATENDIDAS_PA)END) * INTERVAL '1 SECOND' AS TMA,
coalesce((select espera from pbx_supervisor_dacs where dac = DADOS.fila), '0') as qt_fila
FROM
(
SELECT fila
,SUM (CASE WHEN EVENTO = 'ABANDON' THEN 1 ELSE 0 END) AS ABANDONADAS
,SUM (CASE WHEN EVENTO IN ('CONNECT') THEN 1 ELSE 0 END) AS ATENDIDAS_PA
,SUM (CASE WHEN EVENTO IN ('CONNECT') AND to_number(param1,'999999999') > '3' THEN 1 ELSE 0 END) AS ESPERA
,SUM (CASE WHEN EVENTO IN ('CONNECT') AND to_number(param1,'999999999') > '1' THEN to_number(param1,'999999999') ELSE 0 END) AS TEMPO_ESPERA
,SUM (CASE WHEN EVENTO IN ('COMPLETEAGENT','COMPLETECALLER') AND to_number(param2,'999999999') > '1' THEN to_number(param2,'999999999') ELSE 0 END) AS TEMPO_ATENDIMENTO
FROM
( SELECT a.calldate,b.fila,b.evento,b.param1,b.param2
FROM ast_bilhetes a
INNER JOIN ast_eventos_dacs b on b.uid2 = a.uniqueid
WHERE b.evento in ('ABANDON','COMPLETEAGENT','COMPLETECALLER','CONNECT','ENTERQUEUE')
AND a.calldate IS NOT NULL
AND a.lastapp <> 'Transferred Call'
AND cast(a.calldate as date) = cast(now() as date)
AND substring(b.agente, 7,4) = '$matricula'
AND b.fila = '$nomeDac'
) AS DADOS
GROUP BY FILA
) AS DADOS
";
}
function UpdatePausa($db) {
$idPausa = $_SESSION[AGT_ID_PAUSA];
$query = "update pbx_eventos_agentes
set saida_pausa = now(),
flag = 1
where id = $idPausa ";
pg_query($db, $query);
}
function UpdateLogoff($db) {
$idLogoff = $_SESSION[AGT_ID_LOGIN];
$matricula = GetMatriculaAgente();
$query = "update pbx_supervisor_agentes
set logado = now()
where matricula = '$matricula'";
$result = pg_query($db, $query);
$query = "update pbx_eventos_agentes
set logoff = now(),
flag = 1
where id = $idLogoff";
pg_query($db, $query);
}
?>