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.

170 lines
6.6 KiB

<?php
namespace app\Core;
use app\Controllers\AgentController;
use app\Controllers\BilheteController;
use app\Controllers\QueueController;
use app\Controllers\SystemMessageController;
use app\Interfaces\IApiMedia;
use app\Models\Atendimento;
use app\Models\Evento;
use app\Models\Message;
use app\Models\SupervisorModel;
use websocket\WsInterface;
/**
*
* @author Lucas Awade
*/
class Commands
{
/** @var IApiMedia $api provider de mensagens */
protected $api;
/** @var SystemMessageController $systemController controller de mensagens de sistemas */
protected $systemController;
/** @var BilheteController $bilheteController controller de billhete */
protected $bilheteController;
/** @var Atendimento $atendimentoModel model de atendimento*/
protected $atendimentoModel;
/** @var Evento $eventosModel model de eventos */
protected $eventosModel;
/** @var QueueController $queue controller da queue*/
protected $queue;
public function __construct()
{
$this->queue = new QueueController();
$this->eventosModel = new Evento();
$this->atendimentoModel = new Atendimento();
$this->agente = new AgentController();
$this->bilheteController = new BilheteController();
$this->systemController = new SystemMessageController();
}
public function setApi(IApiMedia $api)
{
$this->api = $api;
}
public function isCommand(IApiMedia $api)
{
$this->api = $api;
$messageParams = explode(" ", $api->getMessage());
$command = $messageParams[0];
switch (strtoupper($command)) {
case '/FINALIZAR':
return $this->finalizar($api->getPhone());
case '/CANCELAR':
return $this->cancelar($api->getPhone());
default:
return false;
}
}
public function finalizar($numero)
{
try {
$this->atendimentoModel->begin();
$supervisorModel = new SupervisorModel();
$atendimento = $this->atendimentoModel->getAtendimentoByCliente($numero, CONF_EVENT_START);
//verifica se existe atendimento
if (empty($atendimento)) {
$this->atendimentoModel->rollback();
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : N<EFBFBD>o foi encontrado atendimento em aberto");
return true;
}
//cria o evento pra finalizar
$ret = $this->eventosModel->createEvento(
$atendimento->uniqueid,
CONF_EVENT_TIMERMINO_CLIENTE,
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s'),
$atendimento->fila
);
if ($ret) {
$ws = new WsInterface();
$agente = $supervisorModel->findAgentByMatricula($atendimento->matricula);
$messegeModel = new Message();
$mensagemW = 'Atendimento finalizado pelo cliente';
$messegeModel->addMessage(
$atendimento->uniqueid,
$numero,
$agente->matricula,
'finish',
$mensagemW,
$atendimento->nome,
$atendimento->context,
'read'
);
//verifica se est<EFBFBD> como indisponivel e caso n<EFBFBD>o tenha atendimento ele entra em pausa
if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) {
$atends = $this->atendimentoModel->getAtendimentoAbertoByAgente($atendimento->matricula);
if (empty($atends)) {
$agente->enterPause($atendimento->matricula, $agente->motivo_pausa);
}
}
//coloca o agente como livre caso esteja ocupado e com menos atendimento que o maximo de atendimento
if ($agente->status == CONF_AGENT_STATUS_OCUPADO) {
$param = $this->atendimentoModel->getQuantiAtendimentSimultaneos();
$atendimentosAbertos = $this->atendimentoModel->getAtendimentoAbertoByAgente($agente->matricula);
if (count($atendimentosAbertos) < $param->prm_media_simultaneo) {
$supervisorModel->updateAgent($agente->matricula, CONF_AGENT_STATUS_LIVRE);
}
}
//envia as mensagens do sistema do momento atual para o cliente
$this->systemController->sendMessageSystem(
CONF_MOMENT_FINALIZAR_ATENDIMENTO,
[["nome" => "@autor_name", "valor" => $atendimento->nome]],
$this->api,
$numero
);
$this->atendimentoModel->commit();
//notifica o agente da mudan<EFBFBD>a
$ws->enviaMsg($ws->enviaActions($mensagemW, 'finish', $agente->matricula, $atendimento->uniqueid));
return true;
} else {
$this->atendimentoModel->rollback();
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : Erro ao finalizar!");
return true;
}
} catch (\Exception $th) {
$this->atendimentoModel->rollback();
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : " . $th->getMessage());
return true;
}
}
private function cancelar($numero)
{
$atendimento = $this->atendimentoModel->getAtendimentoByCliente($numero, CONF_EVENT_ESPERA);
$ret = $this->eventosModel->createEvento(
$atendimento->uniqueid,
CONF_EVENT_ABANDONADA,
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s'),
$atendimento->fila
);
if ($ret) {
//envia as mensagens do sistema do momento atual para o cliente
$this->systemController->sendMessageSystem(
CONF_MOMENT_CANCELAR_FILA,
[],
$this->api,
$numero
);
$filas = $this->queue->listAllQueueWhatsApp($this->api->getMessage());
if ($filas['LIST']) {
$this->api->enviarMsg($this->api->getPhone(), $filas['LIST']);
}
return true;
} else {
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : N<EFBFBD>o foi poss<EFBFBD>vel cancelar o atendimento! " . $this->bilheteController->message());
return true;
}
}
}