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.
 
 
 

367 lines
13 KiB

<?php
namespace app\Models;
use app\Core\Connect;
use app\Core\Model;
class Supervisor extends Model
{
const USUARIOS = "pbx_usuarios";
const SUPERVISOR_AGENTE = "md_supervisor";
const EVENTO_AGENTE = 'pbx_eventos_agentes';
const BILHETE = 'pbx_bilhetes';
const EVENTOS_DACS = 'pbx_eventos_dacs';
private $supervisor = 'md_supervisor';
private $atendimento = 'md_atendimento';
public function listaAgentesDisponivel($status = null)
{
$data = [];
$this->query = " SELECT *,
(
SELECT
count(*)
FROM
md_atendimento ma
WHERE 'START' = (SELECT m2.evento FROM md_evento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) AS countAtendimentos
FROM md_supervisor ms
WHERE (
SELECT
count(*)
FROM
md_atendimento ma
WHERE 'START' = (SELECT m2.evento FROM md_evento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) < (SELECT prm_media_simultaneo FROM pbx_parametros pp LIMIT 1 )
";
if ($status) {
$this->query .= " AND ms.status = :status ";
$data['status'] = $status;
}
$this->query .= " ORDER BY countAtendimentos ";
return $this->read($this->query, $data)->fetchAll();
}
public function statusAgente($matricula)
{
$this->query = "SELECT
*,
(
SELECT
count(*)
FROM
md_atendimento ma
WHERE 'START' = (SELECT m2.evento FROM md_evento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) AS numero_atendimento
FROM
md_supervisor ms
WHERE ms.matricula = :matricula";
return $this->read($this->query, ['matricula' => $matricula])->fetch();
}
public function findByMatricula($matricula)
{
$this->query = "SELECT * "
. "FROM " . self::SUPERVISOR_AGENTE . " WHERE (matricula = :matricula)";
return $this->read($this->query, ['matricula' => $matricula])->fetch();
}
public function findAllAgentes($queue = null)
{
$data = [];
$this->query = "SELECT * FROM " . self::SUPERVISOR_AGENTE . " WHERE 1=1 ";
if ($queue) {
$this->query .= " AND fila = :queue ";
$data['queue'] = $queue;
}
return $this->read($this->query, $data)->fetchAll();
}
public function findByAgent($matricula)
{
$this->query = "SELECT * FROM " . self::USUARIOS . " WHERE matricula = :matricula;";
return $this->read($this->query, ['matricula' => $matricula])->fetch();
}
public function findAgentByRamal($ramal)
{
$this->query = "SELECT * FROM " . self::SUPERVISOR_AGENTE . " WHERE ramal = :ramal;";
return $this->read($this->query, ['ramal' => $ramal])->fetch();
}
public function findAgentByMatricula($matricula)
{
$this->query = "SELECT * FROM " . self::SUPERVISOR_AGENTE . " WHERE matricula = :matricula;";
return $this->read($this->query, ['matricula' => $matricula])->fetch();
}
public function findAgentByMatriculaPbx($matricula)
{
$this->query = "SELECT * FROM pbx_supervisor_agentes WHERE matricula = :matricula;";
return $this->read($this->query, ['matricula' => $matricula])->fetch();
}
public function findAgentByQueue($queue, $status)
{
$data = [];
$this->query = "SELECT *
FROM md_supervisor ms
WHERE ms.fila = :queue
AND ms.status = :status
ORDER BY (
SELECT
count(*)
FROM
md_atendimento ma
WHERE 'START' = (SELECT m2.evento FROM md_evento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
)
";
$data['queue'] = $queue;
$data['status'] = $status;
return $this->read($this->query, $data)->fetchAll();
}
########################################
### SUPERVISOR AGENTE ###
########################################
public function addAgent($matricula, $fila, $nome, $chamada_classificado = 1)
{
$this->query = "INSERT INTO " . self::SUPERVISOR_AGENTE . "
(
matricula,
tempo_login,
fila,
status,
duracao,
chamada_classificado,
nome
)
VALUES(
:matricula,
:tempo_login,
:fila,
:status,
:duracao,
:chamada_classificado,
:nome
);";
return $this->create($this->query, [
'matricula' => $matricula,
'tempo_login' => 'now()',
'fila' => $fila,
'status' => 'LIVRE',
'duracao' => 'now()',
'nome' => $nome,
'chamada_classificado' => $chamada_classificado
]);
}
public function addAgent2(
$nome,
$matricula,
$ramal,
$dac,
$tempo_login,
$status = 'LIVRE',
$motivoPausa = null,
$origemDestino = null,
$modo_atendimento = 'Automatico',
$uniqueid = null,
$chamada_classificado = 1
) {
$data = [];
$this->query = "INSERT INTO pbx_supervisor_agentes (
tempo_login,
nome,
ramal,
matricula,
origem_destino,
status,
motivo_pausa,
uniqueid,
chamada_classificado,
modo_atendimento,
dac,
duracao,
logado) VALUES(
:tempo_login,
:nome,
:ramal,
:matricula,
:origem_destino,
:status,
:motivo_pausa,
:uniqueid,
:chamada_classificado,
:modo_atendimento,
:dac,
:duracao,
:logado) ";
$data['tempo_login'] = $tempo_login;
$data['nome'] = $nome;
$data['matricula'] = $matricula;
$data['ramal'] = $ramal;
$data['duracao'] = 'now()';
$data['logado'] = 'now()';
$data['dac'] = $dac;
$data['origem_destino'] = $origemDestino;
$data['status'] = $status;
$data['motivo_pausa'] = $motivoPausa;
$data['uniqueid'] = $uniqueid;
$data['modo_atendimento'] = $modo_atendimento;
$data['chamada_classificado'] = ($chamada_classificado ? '1' : '0');
return $this->create($this->query, $data);
}
public function updateAgent(
$matricula,
$status = 'LIVRE',
$motivo_pausa = null
) {
$this->query = "UPDATE {$this->supervisor} SET status = :status, motivo_pausa = :motivo_pausa";
$this->query .= " WHERE matricula = :matricula";
$data = [];
$data['status'] = $status;
$data['motivo_pausa'] = $motivo_pausa;
$data['matricula'] = $matricula;
// logger('teste')->debug(print_r($data, true), true);
return $this->update($this->query, $data);
}
public function updateAgent2(
$matricula,
$ramal,
$dac,
$status = 'LIVRE',
$atualizaDuracao = false,
$motivoPausa = null,
$origemDestino = null,
$uniqueid = null,
$chamada_classificado = 1
) {
$data = [];
$this->query = "UPDATE pbx_supervisor_agentes SET
origem_destino = :origem_destino,
status = :status,
motivo_pausa = :motivo_pausa,
uniqueid = :uniqueid,
chamada_classificado = :chamada_classificado,
dac = :dac,
ramal = :ramal ";
if ($atualizaDuracao) {
$data['duracao'] = 'now()';
$this->query .= " , duracao = :duracao";
}
$this->query .= " WHERE matricula = :matricula";
$data['matricula'] = $matricula;
$data['ramal'] = $ramal;
$data['dac'] = $dac;
$data['origem_destino'] = $origemDestino;
$data['status'] = $status;
$data['motivo_pausa'] = $motivoPausa;
$data['uniqueid'] = $uniqueid;
$data['chamada_classificado'] = ($chamada_classificado ? '1' : '0');
return $this->update($this->query, $data);
}
public function updateRefreshAgent($matricula, $ramal, $duracao = false)
{
$data = [];
$this->query = "UPDATE " . self::SUPERVISOR_AGENTE . " SET logado = :logado ";
if ($duracao) {
$this->query .= ", duracao = :duracao ";
$data['duracao'] = 'now()';
}
$this->query .= " WHERE matricula = :matricula AND ramal = :ramal;";
$data['logado'] = 'now()';
$data['matricula'] = $matricula;
$data['ramal'] = $ramal;
return $this->update($this->query, $data);
}
public function updateSala2Agent($matricula, $ramal, $sala2 = null)
{
$data = [];
$this->query = "UPDATE " . self::SUPERVISOR_AGENTE . " SET sala_2 = :sala_2 WHERE matricula = :matricula AND ramal = :ramal;";
$data['sala_2'] = $sala2;
$data['matricula'] = $matricula;
$data['ramal'] = $ramal;
return $this->update($this->query, $data);
}
public function deleteAgent($matricula)
{
$data = [];
$this->query = "DELETE FROM " . self::SUPERVISOR_AGENTE . " WHERE matricula = :matricula";
$data['matricula'] = $matricula;
return $this->delete($this->query, $data);
}
public function updateLogadoAll($media = null)
{
$data = [];
$this->query = "UPDATE " . self::SUPERVISOR_AGENTE . " SET logado = :logado ";
if ($media) {
$this->query .= " WHERE sala_1 = :sala_1; ";
}
$data['logado'] = 'now()';
$data['sala_1'] = $media;
return $this->update($this->query, $data);
}
########################################
### EVENTOS ###
########################################
public function addEventoLoginAgent($matricula, $idDac, $flag = 1, $ramal)
{
$data = [];
$this->query = "INSERT INTO " . self::EVENTO_AGENTE . "(matricula, login, logoff, id_dac, flag, ramal) VALUES(:matricula, :login, :logoff, :id_dac, :flag, :ramal) ";
$data['matricula'] = $matricula;
$data['login'] = 'now()';
$data['logoff'] = 'now()';
$data['id_dac'] = $idDac;
$data['flag'] = $flag;
$data['ramal'] = $ramal;
return $this->create($this->query, $data);
}
public function updateEventoLogoffAgent($matricula, $ramal, $dac, $flag = 2)
{
$data = [];
$this->query = "UPDATE " . self::EVENTO_AGENTE . " SET logoff = :logoff, flag = :flag "
. "WHERE matricula = :matricula AND id_dac = :id_dac AND ramal = :ramal "
. "AND login = (SELECT MAX(login) FROM pbx_eventos_agentes WHERE matricula = :matricula AND id_dac = :id_dac AND ramal= :ramal);";
$data['logoff'] = 'now()';
$data['flag'] = $flag;
$data['matricula'] = $matricula;
$data['id_dac'] = $dac;
$data['ramal'] = $ramal;
return $this->update($this->query, $data);
}
}