forked from SimplesIP/pabx-app
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.
385 lines
14 KiB
385 lines
14 KiB
<?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ão relacionada como WhatsApp!'; |
|
} |
|
|
|
if (!$queue) { |
|
return 'Fila não encontrada ou não relacionada como WhatsApp!'; |
|
} |
|
|
|
if (!$agent) { |
|
return 'Usuário não encontrado!'; |
|
} |
|
|
|
|
|
if ($this->agent->findByMatricula($matricula)) { |
|
return 'Agente já autenticado!'; |
|
} |
|
/** |
|
* VERIFICA CLASSIFICACAO ATENDIMENTO |
|
*/ |
|
// $classificacao = new ClassificacaoController(); |
|
// $chamadaSemClassificacao = $classificacao->agentClassificacaoPending($agent->matricula, $fila); |
|
|
|
if (!$this->agent->addAgent($agent->matricula, $fila, $agent->nome)) { |
|
return 'Não foi possével inserir o agente!'; |
|
} |
|
|
|
|
|
if (!$this->agent->addEventoLoginAgent($agent->matricula, $queue->id, 1, '0')) { |
|
return 'Não foi possível inserir as informações de autenticaçã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ão encontrado!'); |
|
} |
|
|
|
$queue = $this->queue->findQueueByName($agent->fila); |
|
if (!$queue) { |
|
throw new Exception('Agente nã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ão foi possível atualizar o status do agente!'); |
|
} |
|
if (!$this->pause->addEventoIndisponivelAgent($agent->matricula, '0', $pause->id, $queue->id)) { |
|
throw new Exception('Não foi possível atualizar informaçõ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ão identificado!'); |
|
} |
|
|
|
if (!$pause) { |
|
throw new Exception('Pausa não encontrada!'); |
|
} |
|
|
|
if (!$queue) { |
|
throw new Exception('Usuário nã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ão foi possível atualizar o status do agente!'); |
|
} |
|
|
|
if (!$this->pause->addEventoPauseAgent($agent->matricula, '0', $pause->id, $queue->id, $pause->produtiva)) { |
|
throw new Exception('Não foi possível atualizar informaçõ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ão identificado!'); |
|
} |
|
|
|
if (!$queue) { |
|
throw new Exception('Agente não está conectado!'); |
|
} |
|
|
|
if ($agent->status != CONF_AGENT_STATUS_PAUSA && $agent->status != CONF_AGENT_STATUS_INDISPONIVEL) { |
|
throw new Exception('Agente não está 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ão conectado!'); |
|
} |
|
|
|
$agentTransf = $this->agent->findAgentByMatricula($matDestino); |
|
if (!$agentTransf || $agentTransf->status != CONF_AGENT_STATUS_LIVRE) { |
|
throw new Exception('Agente indisponível para atendimento!'); |
|
} |
|
$atendAtual = $atendimento->findAtendId($uniqueid); |
|
if (!$atendAtual) { |
|
throw new Exception('Atendimento nã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ã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ã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; |
|
} |
|
} |