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.
 
 
 
 
 
 

404 lines
15 KiB

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of DashboardHandler
*
* @author victor
*/
class AbstractDashboard {
private static $instance;
const ARRAY_ROOT_INDEX_NAME = 'rotinasFrequentes';
const PREFIX = "prog_";
const DASHBOARD_ID = 12100;
/**
*
* @var IDashboard $handler
*/
public $handler = null;
protected $conn = null;
private $matricula, $login, $id, $nome, $agenteContribuinte,
$ramalMonitorAgente, $canalMonitorAgente = null;
private $dacPadrao, $dacAtual, $isAgente = false;
protected static $listaDacsTemplate = <<<HTML
<tr>
<td align="center">[dac]</td>
<td align="center">[nome]</td>
<td align="center">[ramal_monitor]</td>
<td align="center"><a href="javaScript:NovaJanela('index.php?idProg=126&matricula=[matricula]&dac=[dac]', 'jnGraphAgente', '1048', '640', 'resizable=NO,scrollbars=NO');" style="color:black;">[matricula]</a></td>
<td align="center">[tempo_logado]</td>
<td align="center">[modo_atendimento]</td>
<td align="center">[status_chamada]</td>
<td align="center">[status]</td>
<td align="center">[chamadas_originadas]</td>
<td align="center">[chamadas_atendidas]</td>
<td align="center">[tempo_media_atendimento]</td>
<td align="center"><a href="javaScript:LogoffAgente('[matricula]','[modo_atendimento]','[dac]' );">Logoff do agente</a></td>
<td align="center"><a href="javaScript:PausaAgente('[matricula]','[ramal_monitor]','[dac]', '1');">Pausar agente</a></td>
</tr>
HTML;
private function __construct($session, $optionals = []) {
if (!isset($session) || empty($session)) {
return $this;
}
global $dbcon;
$this->conn = $dbcon;
$this->matricula = $session['SSmatriculaUser'];
$this->login = $session['SSlogin'];
$this->id = $session['SSidUser'];
$this->nome = $session['SSnomeUser'];
$this->agenteContribuinte = $session['prm_agente_contribuinte'];
$this->dacPadrao = $session['SSdacPadraoAgente'];
$this->dacAtual = $session['SSsupervisorDacAtu'];
$this->ramalMonitorAgente = $session['ramalMonitorAgente'];
$this->canalMonitorAgente = $session['canalMonitorAgente'];
$this->isAgente = (isset($session['SSagente']) && ($session['SSagente'] === true));
if (!empty($optionals)) {
array_walk($optionals, [$this, 'setOptional']);
}
}
private function setOptional($item, $key) {
$this->$item = $_SESSION[$item];
}
public function __clone() {
}
public function __wakeup() {
}
/**
*
* @param array $session
* @param array $optionals
* @return AbstractDashboard
*/
public static function init($session, $optionals = []) {
if (is_null(static::$instance)) {
static::$instance = new self($session, $optionals);
}
return static::$instance;
}
public static function ProcessaRotinasMaisUsadas($idProg) {
$isAjax = (getRequest('ajax', INPUT_GET) !== null);
$isPopup = (getRequest('window', INPUT_GET) !== null);
$isSubProg = (getRequest('idSubProg', INPUT_GET) !== null);
if ($isAjax || $isPopup) {
return;
}
if (!isset($idProg) || $idProg == self::DASHBOARD_ID || $isSubProg) {
return;
}
$name = self::ARRAY_ROOT_INDEX_NAME;
if (isset($_SESSION[$name][self::PREFIX . self::DASHBOARD_ID])) {
unset($_SESSION[$name][self::PREFIX . self::DASHBOARD_ID]);
}
if (!isset($_SESSION[$name])) {
$_SESSION[$name] = [];
}
$count = isset($_SESSION[$name][self::PREFIX . $idProg]) ? $_SESSION[$name][self::PREFIX . $idProg] : 0;
$_SESSION[$name][self::PREFIX . $idProg] = $count + 1;
}
private static function aksort(&$array, $valrev = false, $keyrev = false) {
if ($valrev) {
arsort($array);
} else {
asort($array);
}
$vals = array_count_values($array);
$i = 0;
foreach ($vals as $val => $num) {
$first = array_splice($array, 0, $i);
$tmp = array_splice($array, 0, $num);
if ($keyrev) {
krsort($tmp);
} else {
ksort($tmp);
}
$array = array_merge($first, $tmp, $array);
unset($tmp);
$i = $num;
}
}
public static function RotinasMaisUsadas() {
$name = self::ARRAY_ROOT_INDEX_NAME;
if (!isset($_SESSION[$name]) || empty($_SESSION[$name])) {
return [];
}
$rotinas = $_SESSION[$name];
self::aksort($rotinas);
$tmp = array_reverse(array_slice($rotinas, 0, 10), true);
$top = [];
foreach ($tmp as $prog => $count) {
$idProg = substr($prog, strlen(self::PREFIX . ''), strlen($prog));
if (intval($idProg) === self::DASHBOARD_ID) {
continue;
}
$linkPrefix = 'index.php?idProg=%s';
$idProgPai = \GetFieldFromIdProg($idProg, 'id_prog_pai');
$nomeRotina = GetDispProgSel($idProgPai);
if (empty($nomeRotina)) {
continue;
}
$sameProg = (intval($idProg) === intval($idProgPai));
$id = ($sameProg ? $idProg : $idProgPai);
$top[$id] = [
'rotina' => \GetDispProgSel($id),
'url' => sprintf($linkPrefix, $id),
'count' => $count
];
}
return $top;
}
public function UltimasOperacoes() {
//$query = "select full_data_hora, id_prog, full_prog_nome, full_log from pbx_audita_full where login = '{$this->login}' and id_prog not in ('12100','56','16','-1') order by full_id desc limit 20";
$query = "select max(full_id) as \"full_id\",id_prog,full_prog_nome,max(full_data_hora) as \"full_data_hora\", max(full_log) as \"full_log\" from pbx_audita_full where login = '{$this->login}' and id_prog not in ('12100','56','16','-1') and full_prog_nome <> '' group by id_prog,full_prog_nome order by max(full_id) desc limit 20";
$result = pg_query($this->conn, $query);
$logs = [];
$i = 0;
if ($result) {
while ($dados = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$log = json_decode($dados['full_log'], false, 512, JSON_BIGINT_AS_STRING);
if (property_exists($log, 'request')) {
$idProg = $log->request->idProg;
} else {
$idProg = $log->idProg;
}
$nomeRotina = GetDispProgSel($idProg);
$urlText = (!empty($nomeRotina) ? $nomeRotina : 'Desconhecido');
$unset = ['uid', 'ip', 'data', 'login', 'prog'];
if (strtolower($urlText) == 'desconhecido') {
continue;
}
$logs[$i] = [
'data' => ParseDbTimestamp($dados['full_data_hora'], 'D'),
'hora' => ParseDbTimestamp($dados['full_data_hora'], 'T'),
//'url' => "<a href='index.php?idProg={$idProg}'>{$urlText}</a>",
'url' => $urlText, # . " ({$dados['id_prog']})",
'prog_id' => $dados['id_prog'],
'prog_nome' => $dados['full_prog_nome'],
//'log' => $dados['full_log']
];
$i++;
}
} else {
throw new Exception(pg_last_error($this->conn));
}
return $logs;
}
public function DadosAgentes() {
$isDacPadrao = empty($this->dacAtual);
$dac = ($isDacPadrao ? $this->dacPadrao : $this->dacAtual);
$where = '';
if ($this->isAgente) {
$where = " AND a.nome = '{$dac}'";
}
$sqlDacs = "select * from pbx_dacs a where a.status = 'A' and exists(select '' from pbx_fila_grupos where id = a.id) {$where} order by 1";
$queryDacs = pg_query($sqlDacs);
$dacs = [];
while ($dac = pg_fetch_assoc($queryDacs)) {
$dacs[] = $dac;
}
foreach ($dacs as $dac) {
$dadosAgt = GetDadosAgt($dac['nome']);
$i = 0;
$result[$dac['nome']] = [];
foreach ($dadosAgt as $dadosAgente) {
$isAgenteAtual = intval($dadosAgente['matricula']) === intval($this->matricula);
if ($this->isAgente && !$isAgenteAtual) {
continue;
}
$dadosAgente['dacPadrao'] = !$isDacPadrao;
$dadosAgente['isPausado'] = (stripos($dadosAgente['status'], 'pausa') !== false);
if (isset($dadosAgente['tipo_ligacao']) && !empty($dadosAgente['tipo_ligacao'])) {
$dadosAgente['status_chamada'] = 'Chamada ' . ($dadosAgente['tipo_ligacao'] == 'E' ? 'Entrante' : 'Sainte') . " {$dadosAgente['fone']}";
} else {
$dadosAgente['status_chamada'] = 'Livre';
}
$chamadaAgt = $dadosAgente["chamadas_originadas"] + $dadosAgente["chamadas_atendidas"];
$strTmp = $chamadaAgt <> 0 ? round(($dadosAgente["tempo_atendimento"] / $chamadaAgt), 0) : 0;
$tempoAtd = SecondToStrTime($strTmp);
$dadosAgente['tempo_media_atendimento'] = $tempoAtd;
$html = preg_replace_callback('/\[(.*?)\]/', function($matches) use ($dadosAgente) {
if (isset($dadosAgente[$matches[1]])) {
return $dadosAgente[$matches[1]];
}
return $matches[0];
}, self::$listaDacsTemplate);
if ($isAgenteAtual) {
$html = str_replace('td', 'td style="background-color: palegreen"', $html);
}
$result[$dac['nome']]['agentes'][] = ['dados' => $dadosAgente, 'html' => $html, 'isAgenteAtual' => $isAgenteAtual];
$i++;
}
}
return $result;
}
public function ChamadasNaoClassificadas($ultimosXMeses = 3) {
if (empty($this->ramalMonitorAgente)) {
return [];
}
$where = ($this->isAgente ? " and b.src = '{$this->ramalMonitorAgente}'" : '');
$query = "select to_char(b.calldate, 'MM/YYYY') as \"competencia\", count(b.calldate) as \"total\"
from pbx_bilhetes as \"b\"
inner join pbx_eventos_dacs as \"e\" on e.uid2 = b.uniqueid
left join pbx_classifica_reg as \"c\" on c.id_bilhetes = b.uniqueid
where c.matricula is null
and b.src = '{$this->ramalMonitorAgente}'
group by to_char(b.calldate, 'MM/YYYY')
order by count(b.calldate) desc limit {$ultimosXMeses}
";
$result = pg_query($this->conn, $query);
$chamadas = [];
if ($result) {
while ($dados = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$exp = explode("/", $dados['competencia']);
$date = new \DateTime("{$exp[1]}-{$exp[0]}-01");
$chamadas[] = [
'timestamp' => $date->getTimestamp(),
'data' => $date->format('m/Y'),
'total' => $dados['total']
];
}
} else {
throw new Exception(pg_last_error($this->conn));
}
if (!empty($chamadas)) {
usort($chamadas, function($a, $b) {
return $b['timestamp'] - $a['timestamp'];
});
}
return $chamadas;
}
public function getMatricula() {
return $this->matricula;
}
public function getLogin() {
return $this->login;
}
public function getId() {
return $this->id;
}
public function getNome() {
return $this->nome;
}
public function getAgenteContribuinte() {
return $this->agenteContribuinte;
}
public function getRamalMonitorAgente() {
return (empty($this->ramalMonitorAgente) ? 'S/Ramal' : $this->ramalMonitorAgente);
}
public function getCanalMonitorAgente() {
return $this->canalMonitorAgente;
}
public function getDacPadrao() {
return $this->dacPadrao;
}
public function getDacAtual() {
return $this->dacAtual;
}
public function getIsAgente() {
return $this->isAgente;
}
/**
*
* @return \DashboardGraph
*/
public function getGraph() {
return new DashboardGraph($this);
}
public static function postarComunicado($data, &$msg) {
extract($data);
if (strlen($conteudo) <= 0) {
return;
}
global $dbcon;
$conteudo = QuotedStr($conteudo);
$sql = "INSERT INTO pbx_mural (autor_id,data,conteudo) VALUES ({$autor}, '{$timestamp}', $conteudo)";
$begin = pg_query($dbcon, "begin");
$result = pg_query($dbcon, $sql);
if ($result) {
$msg = "Publicado com sucesso";
$commit = pg_query($dbcon, "commit");
} else {
$msg = "N<EFBFBD>o foi poss<EFBFBD>vel publicar no mural.";
if (isset($_SESSION['DEBUG']) && intval($_SESSION['DEBUG']) === 1 && IsAdmin()) {
$msg .= "Erro: " . pg_last_error($dbcon);
}
$rollback = pg_query($dbcon, "rollback");
}
}
public static function getPublicacoes() {
global $dbcon;
$ultimaNotificacao = pg_escape_string(getRequest('ultimaNotificacao', INPUT_GET));
$where = '';
if (IsAjax()) {
$where .= " WHERE data BETWEEN now()::timestamp - (interval '10s') AND now()::timestamp + (interval '10s')";
if ($ultimaNotificacao) {
$where .= " AND id > {$ultimaNotificacao}";
}
}
$sql = "SELECT * FROM pbx_mural {$where} ORDER BY id DESC LIMIT 10";
$getPubs = pg_query($dbcon, $sql);
$publicacoes = [];
while ($dados = pg_fetch_assoc($getPubs)) {
if (IsAjax()) {
$dados = array_map('utf8_encode', $dados);
}
$dados['autor'] = GetLoginFromId($dbcon, $dados['autor_id']);
$dados['conteudo'] = pg_escape_string(trim($dados['conteudo']));
$publicacoes[] = $dados;
}
return $publicacoes;
}
}