|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\Controllers;
|
|
|
|
|
|
|
|
|
|
use app\Core\Controller;
|
|
|
|
|
use app\Models\Queue;
|
|
|
|
|
use app\Models\Ramal;
|
|
|
|
|
use app\Models\Pause;
|
|
|
|
|
use app\Models\Bilhete;
|
|
|
|
|
use app\Models\EventQueue;
|
|
|
|
|
use app\Models\Atendimento;
|
|
|
|
|
use app\Models\Evento;
|
|
|
|
|
use app\Models\Message;
|
|
|
|
|
use app\Models\SupervisorModel;
|
|
|
|
|
use Exception;
|
|
|
|
|
use websocket\WsInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Description of AgentController
|
|
|
|
|
*
|
|
|
|
|
* @author root
|
|
|
|
|
*/
|
|
|
|
|
class AgentController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/** @var SupervisorModel $agent model de supervisor */
|
|
|
|
|
protected $agent;
|
|
|
|
|
|
|
|
|
|
/** @var Queue $queue model de queue */
|
|
|
|
|
protected $queue;
|
|
|
|
|
|
|
|
|
|
/** @var Ramal $ramal model de Ramal */
|
|
|
|
|
protected $ramal;
|
|
|
|
|
|
|
|
|
|
/** @var Pause $pause model de Pause */
|
|
|
|
|
protected $pause;
|
|
|
|
|
|
|
|
|
|
/** @var Bilhete $bilhete model de Bilhete */
|
|
|
|
|
protected $bilhete;
|
|
|
|
|
|
|
|
|
|
/** @var EventQueue $eventqueue model de EventQueue */
|
|
|
|
|
protected $eventqueue;
|
|
|
|
|
|
|
|
|
|
/** @var Atendimento $atendModel model de Atendimento */
|
|
|
|
|
protected $atendModel;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->agent = new SupervisorModel();
|
|
|
|
|
$this->queue = new Queue();
|
|
|
|
|
$this->ramal = new Ramal();
|
|
|
|
|
$this->pause = new Pause();
|
|
|
|
|
$this->bilhete = new Bilhete();
|
|
|
|
|
$this->eventqueue = new EventQueue();
|
|
|
|
|
$this->atendModel = new Atendimento();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function login($fila, $matricula)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$queue = $this->queue->findQueueByName(strtoupper($fila));
|
|
|
|
|
$agent = $this->agent->findByAgent(strtolower($matricula));
|
|
|
|
|
|
|
|
|
|
if ($queue->midiafila == 'N') {
|
|
|
|
|
return 'Fila n<EFBFBD>o relacionada como WhatsApp!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$queue) {
|
|
|
|
|
return 'Fila n<EFBFBD>o encontrada ou n<EFBFBD>o relacionada como WhatsApp!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
return 'Usu<EFBFBD>rio n<EFBFBD>o encontrado!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->agent->findByMatricula($matricula)) {
|
|
|
|
|
return 'Agente j<EFBFBD> autenticado!';
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* VERIFICA CLASSIFICACAO ATENDIMENTO
|
|
|
|
|
*/
|
|
|
|
|
// $classificacao = new ClassificacaoController();
|
|
|
|
|
// $chamadaSemClassificacao = $classificacao->agentClassificacaoPending($agent->matricula, $fila);
|
|
|
|
|
|
|
|
|
|
if (!$this->agent->addAgent($agent->matricula, $fila, $agent->nome)) {
|
|
|
|
|
return 'N<EFBFBD>o foi poss<EFBFBD>vel inserir o agente!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!$this->agent->addEventoLoginAgent($agent->matricula, $queue->id, 1, '0')) {
|
|
|
|
|
return 'N<EFBFBD>o foi poss<EFBFBD>vel inserir as informa<EFBFBD><EFBFBD>es de autentica<EFBFBD><EFBFBD>o do login!';
|
|
|
|
|
}
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function logoff($matricula, $valida = true)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByMatricula($matricula);
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o encontrado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$queue = $this->queue->findQueueByName($agent->fila);
|
|
|
|
|
if (!$queue) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o conectado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($agent->status != CONF_AGENT_STATUS_LIVRE && $valida) {
|
|
|
|
|
throw new Exception('Saia da pausa para fazer logoff');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->agent->updateEventoLogoffAgent($agent->matricula, '0', $queue->id);
|
|
|
|
|
$this->agent->deleteAgent($agent->matricula);
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public function indisponivelAtendimento($matricula, $pausa)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByMatricula($matricula);
|
|
|
|
|
$queue = $this->queue->findQueueByName($agent->fila);
|
|
|
|
|
$pause = $this->pause->findPauseByName($pausa);
|
|
|
|
|
|
|
|
|
|
if (!$this->agent->updateAgent($agent->matricula, CONF_AGENT_STATUS_INDISPONIVEL, $pause->motivo)) {
|
|
|
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel atualizar o status do agente!');
|
|
|
|
|
}
|
|
|
|
|
if (!$this->pause->addEventoIndisponivelAgent($agent->matricula, '0', $pause->id, $queue->id)) {
|
|
|
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel atualizar informa<EFBFBD><EFBFBD>es complementares do agente!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public function enterPause($matricula, $pausa)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByMatricula($matricula);
|
|
|
|
|
$queue = $this->queue->findQueueByName($agent->fila);
|
|
|
|
|
$pause = $this->pause->findPauseByName($pausa);
|
|
|
|
|
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Telefone n<EFBFBD>o identificado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$pause) {
|
|
|
|
|
throw new Exception('Pausa n<EFBFBD>o encontrada!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$queue) {
|
|
|
|
|
throw new Exception('Usu<EFBFBD>rio n<EFBFBD>o conectado em uma fila!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($agent->status == 'PAUSA') {
|
|
|
|
|
throw new Exception('Agente precisa estar livre para entrar em pausa!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->agent->updateAgent($agent->matricula, CONF_AGENT_STATUS_PAUSA, $pause->motivo)) {
|
|
|
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel atualizar o status do agente!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->pause->addEventoPauseAgent($agent->matricula, '0', $pause->id, $queue->id, $pause->produtiva)) {
|
|
|
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel atualizar informa<EFBFBD><EFBFBD>es complementares do agente!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exitPause($matricula)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByMatricula($matricula);
|
|
|
|
|
$queue = $this->queue->findQueueByName($agent->fila);
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Telefone n<EFBFBD>o identificado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$queue) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o est<EFBFBD> conectado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($agent->status != CONF_AGENT_STATUS_PAUSA && $agent->status != CONF_AGENT_STATUS_INDISPONIVEL) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o est<EFBFBD> em pausa!');
|
|
|
|
|
}
|
|
|
|
|
$atendimentos = $this->atendModel->getAtendimentoAbertoByAgente($matricula);
|
|
|
|
|
$param = $this->atendModel->getQuantiAtendimentSimultaneos();
|
|
|
|
|
if (count($atendimentos) < $param->prm_media_simultaneo) {
|
|
|
|
|
$this->agent->updateAgent($agent->matricula, CONF_AGENT_STATUS_LIVRE);
|
|
|
|
|
} else {
|
|
|
|
|
$this->agent->updateAgent($agent->matricula, CONF_AGENT_STATUS_OCUPADO);
|
|
|
|
|
}
|
|
|
|
|
$this->pause->updateEventoOutPause($agent->matricula, $queue->id);
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function transfer($matOrigem, $matDestino, $uniqueid)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$atendimento = new Atendimento();
|
|
|
|
|
$eventModel = new Evento();
|
|
|
|
|
$agent = $this->agent->findAgentByMatricula($matOrigem);
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o conectado!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$agentTransf = $this->agent->findAgentByMatricula($matDestino);
|
|
|
|
|
if (!$agentTransf || $agentTransf->status != CONF_AGENT_STATUS_LIVRE) {
|
|
|
|
|
throw new Exception('Agente indispon<EFBFBD>vel para atendimento!');
|
|
|
|
|
}
|
|
|
|
|
$atendAtual = $atendimento->findAtendId($uniqueid);
|
|
|
|
|
if (!$atendAtual) {
|
|
|
|
|
throw new Exception('Atendimento n<EFBFBD>o encontrado');
|
|
|
|
|
}
|
|
|
|
|
$atendimento->updAtendimento($uniqueid, $agentTransf->matricula);
|
|
|
|
|
$eventModel->createEvento(
|
|
|
|
|
$uniqueid,
|
|
|
|
|
CONF_EVENT_TRANSFER,
|
|
|
|
|
date('Y-m-d H:i:s'),
|
|
|
|
|
date('Y-m-d H:i:s'),
|
|
|
|
|
$atendAtual->fila,
|
|
|
|
|
$agent->matricula
|
|
|
|
|
);
|
|
|
|
|
$eventModel->createEvento(
|
|
|
|
|
$uniqueid,
|
|
|
|
|
CONF_EVENT_START,
|
|
|
|
|
date('Y-m-d H:i:s'),
|
|
|
|
|
date('Y-m-d H:i:s'),
|
|
|
|
|
$atendAtual->fila,
|
|
|
|
|
$agentTransf->matricula
|
|
|
|
|
);
|
|
|
|
|
//$ws->enviaMsg($ws->enviaActions('Atendimento transferido', 'transfer', $agent->matricula, $uniqueid));
|
|
|
|
|
$provedor = returnChannel($atendAtual->context);
|
|
|
|
|
$provedor->enviarMsg($atendAtual->cliente_id, CONF_NAME_REPONSE . ": Atendimento transferido");
|
|
|
|
|
$messegeModel = new Message();
|
|
|
|
|
$messegeModel->addMessage(
|
|
|
|
|
$uniqueid,
|
|
|
|
|
$agent->matricula,
|
|
|
|
|
$agentTransf->matricula,
|
|
|
|
|
'transfer',
|
|
|
|
|
'Atendimento transferido',
|
|
|
|
|
$agent->nome,
|
|
|
|
|
$atendAtual->context,
|
|
|
|
|
'read'
|
|
|
|
|
);
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
$this->atualizaStatusAgente($agentTransf);
|
|
|
|
|
$this->atualizaStatusAgente($agent);
|
|
|
|
|
$ws = new WsInterface();
|
|
|
|
|
$ws->enviaMsg($ws->enviaActions('Atendimento transferido', 'transfer', $agentTransf->matricula, $uniqueid));
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
return $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function atualizaStatusAgente($agente)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$atendimentosAbertos = $this->atendModel->getAtendimentoAbertoByAgente($agente->matricula);
|
|
|
|
|
$param = $this->atendModel->getQuantiAtendimentSimultaneos();
|
|
|
|
|
if ($agente->status == CONF_AGENT_STATUS_LIVRE || $agente->status == CONF_AGENT_STATUS_OCUPADO) {
|
|
|
|
|
if (count($atendimentosAbertos) < $param->prm_media_simultaneo) {
|
|
|
|
|
$this->agent->updateAgent($agente->matricula, CONF_AGENT_STATUS_LIVRE);
|
|
|
|
|
} else {
|
|
|
|
|
$this->agent->updateAgent($agente->matricula, CONF_AGENT_STATUS_OCUPADO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) {
|
|
|
|
|
if (empty($atendimentosAbertos)) {
|
|
|
|
|
$this->enterPause($agente->matricula, $agente->motivo_pausa);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function infoAgentes($media, $queue = null)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$agent = $this->agent->findAllAgentes($media, $queue);
|
|
|
|
|
return $agent;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function status($ramal, $status, $origemDestino = null)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByRamal($ramal);
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Agente n<EFBFBD>o conectado!');
|
|
|
|
|
}
|
|
|
|
|
$this->agent->updateAgent($agent->matricula, $ramal, strtoupper($status), $origemDestino, null, 1);
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAgente($ramal)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->agent->begin();
|
|
|
|
|
$agent = $this->agent->findAgentByRamal($ramal);
|
|
|
|
|
$agent->nome = ucwords(explode(' ', str_replace('.', ' ', $agent->nome))[0]);
|
|
|
|
|
if (!$agent) {
|
|
|
|
|
throw new Exception('Telefone n<EFBFBD>o identificado!');
|
|
|
|
|
}
|
|
|
|
|
$this->agent->commit();
|
|
|
|
|
return $agent;
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$this->agent->rollback();
|
|
|
|
|
$this->message($ex->getMessage());
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function findRamal($ramal)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->ramal->findRamal($ramal);
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
logger()->error($ex->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|