|
|
<?php |
|
|
|
|
|
namespace app\Models; |
|
|
|
|
|
use app\Core\Model; |
|
|
|
|
|
class SupervisorModel 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, $somenteLivre = true) |
|
|
{ |
|
|
$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, |
|
|
( |
|
|
SELECT |
|
|
count(*) |
|
|
FROM |
|
|
md_atendimento ma |
|
|
WHERE ma.data_reg >= current_date |
|
|
AND ma.matricula = ms.matricula |
|
|
) AS numero_atendimento_dia |
|
|
|
|
|
FROM md_supervisor ms |
|
|
WHERE 1=1 |
|
|
"; |
|
|
if ($somenteLivre) { |
|
|
$this->query .= " AND ( |
|
|
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) |
|
|
{ |
|
|
//Codigo acrescentado para n<EFBFBD>o permitir logar o agente no chat e pabx simultaneamente. |
|
|
$this->query = "SELECT matricula from ( SELECT matricula FROM " . self::SUPERVISOR_AGENTE |
|
|
. " UNION " |
|
|
. " SELECT matricula FROM pbx_supervisor_agentes ) AS agt 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 findAllAgentesPBX($queue = null, $media = null) |
|
|
{ |
|
|
|
|
|
$data = []; |
|
|
$this->query = "SELECT * FROM pbx_supervisor_agentes WHERE 1=1 "; |
|
|
if ($queue) { |
|
|
$this->query .= " AND fila = :queue "; |
|
|
$data['queue'] = $queue; |
|
|
} |
|
|
|
|
|
if($media){ |
|
|
$this->query .= " AND media <> :media"; |
|
|
$data['media'] = $media; |
|
|
} |
|
|
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, |
|
|
$media = 1, |
|
|
$modo_atendimento = 'Automatico', |
|
|
$chamada_classificado = 1 |
|
|
) { |
|
|
$data = []; |
|
|
$this->query = "INSERT INTO pbx_supervisor_agentes ( |
|
|
tempo_login, |
|
|
nome, |
|
|
ramal, |
|
|
matricula, |
|
|
origem_destino, |
|
|
status, |
|
|
motivo_pausa, |
|
|
chamada_classificado, |
|
|
modo_atendimento, |
|
|
dac, |
|
|
duracao, |
|
|
logado, |
|
|
media) VALUES( |
|
|
:tempo_login, |
|
|
:nome, |
|
|
:ramal, |
|
|
:matricula, |
|
|
:origem_destino, |
|
|
:status, |
|
|
:motivo_pausa, |
|
|
:chamada_classificado, |
|
|
:modo_atendimento, |
|
|
:dac, |
|
|
:duracao, |
|
|
:logado, |
|
|
:media) "; |
|
|
$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['media'] = $media; |
|
|
$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, duracao = :duracao"; |
|
|
$this->query .= " WHERE matricula = :matricula"; |
|
|
$data = []; |
|
|
$data['status'] = $status; |
|
|
$data['motivo_pausa'] = $motivo_pausa; |
|
|
$data['matricula'] = $matricula; |
|
|
$data['duracao'] = 'now()'; |
|
|
// 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, |
|
|
$disponivel_atendimento = 1 |
|
|
) { |
|
|
$data = []; |
|
|
$this->query = "UPDATE pbx_supervisor_agentes SET |
|
|
origem_destino = :origem_destino, |
|
|
status = :status, |
|
|
motivo_pausa = :motivo_pausa, |
|
|
dac = :dac, |
|
|
ramal = :ramal, |
|
|
logado = :logado, |
|
|
disponivel_atendimento = :disponivel_atendimento "; |
|
|
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['logado'] = 'now()'; |
|
|
$data['motivo_pausa'] = $motivoPausa; |
|
|
$data['disponivel_atendimento'] = $disponivel_atendimento; |
|
|
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 deleteAgentPbx($matricula, $media = null, $oper = '=') |
|
|
{ |
|
|
$data = []; |
|
|
$this->query = "DELETE FROM pbx_supervisor_agentes WHERE matricula = :matricula "; |
|
|
if($media){ |
|
|
$this->query .= "AND media {$oper} :media "; |
|
|
$data['media'] = $media; |
|
|
} |
|
|
$data['matricula'] = $matricula; |
|
|
return $this->delete($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); |
|
|
} |
|
|
} |