From a8aed8cbef774ea61d9e806f47226386586da424 Mon Sep 17 00:00:00 2001 From: lucas cardoso Date: Wed, 13 Jul 2022 09:27:35 -0400 Subject: [PATCH] add methodo finalizarAtemdimento --- app/Controllers/AtendimentosController.php | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/app/Controllers/AtendimentosController.php b/app/Controllers/AtendimentosController.php index 9e0e329..821c280 100644 --- a/app/Controllers/AtendimentosController.php +++ b/app/Controllers/AtendimentosController.php @@ -2,19 +2,33 @@ namespace app\Controllers; +use app\Actions\SystemMessageAction; use app\Core\Controller; use app\Models\AtendimentoModel; +use app\Models\EventosAtendimento; +use app\Models\MessageModel; +use app\Models\ParametrosModel; +use app\Models\SupervisorModel; use Exception; use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Http\Message\ServerRequestInterface; use Throwable; +use websocket\WsInterface; class AtendimentosController extends Controller { private AtendimentoModel $atendimentoModel; + private SupervisorModel $supervisorModel; + private EventosAtendimento $eventosAtemdimentos; + private ParametrosModel $parametrosModel; function __construct() { $this->atendimentoModel = new AtendimentoModel; + $this->supervisorModel = new SupervisorModel; + $this->eventosAtemdimentos = new EventosAtendimento; + $this->parametrosModel = new ParametrosModel; } function listarAtendimentos(Request $request, Response $response, $args): Response @@ -37,4 +51,102 @@ class AtendimentosController extends Controller } return $response; } + + function finalizarAtemdimento(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface + { + try { + $this->supervisorModel->db->begin(); + $body = json_decode($request->getBody()->getContents(), true); + + if (empty($body['uniqueid'])) { + throw new Exception("Parametro uniqueid é obrigatório"); + } + $agente = $this->supervisorModel->get(['matricula' => $body['matricula']]); + //verifica se existe agente + if (empty($agente)) { + throw new Exception("Agente não encontrado"); + } + $atendimento = $this->atendimentoModel->get(['uniqueid' => $body['uniqueid']]); + //verifica se existe atendimento + if (empty($atendimento)) { + throw new Exception("Atendimento não encontrado"); + } + + $event = $this->eventosAtemdimentos->findEventFinish($body['uniqueid']); + //verifica se o atendimento ja foi finalizado + if (!empty($event)) { + throw new Exception("Atendimento já foi finalizado"); + } + + $ret = $this->eventosAtemdimentos->create( + [ + 'uniqueid' => $body['uniqueid'], + 'evento' => CONF_EVENT_TIMERMINO_AGENTE, + 'data_evento' => 'now()', + 'data_reg' => 'now()', + 'fila' => $atendimento->fila, + 'matricula' => $body['matricula'] + ] + ); + + $ws = new WsInterface(); + $mesg = "Atendimento finalizado pelo agente {$agente->nome}"; + $provedor = returnChannel((string) $atendimento->context); + $messegeModel = new MessageModel(); + $messegeModel->addMessage( + $body['uniqueid'], + $agente->matricula, + $agente->matricula, + 'finish', + $mesg, + $agente->nome, + $atendimento->context, + 'read' + ); + //remove o status indisponivel quando remover todos os atendimentos caso esteja em paiusa com atendimento + if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) { + $atends = $this->atendimentoModel->getAtendimentoAbertoByAgente($body['matricula']); + if (empty($atends)) { + $this->agentController->enterPause($body['matricula'], $agente->motivo_pausa); + $ws->enviaMsg($ws->enviaActions('Agente em pausa', 'att_status', $agente->matricula, $body['uniqueid'])); + } + } + //remove o status ocupado quando remover todos os atendimentos + if ($agente->status == CONF_AGENT_STATUS_OCUPADO) { + $atendimentosAbertos = $this->atendimentoModel->getAtendimentoAbertoByAgente($agente->matricula); + $param = $this->parametrosModel->list()[0]; + if (count($atendimentosAbertos) < $param->prm_media_simultaneo) { + $this->supervisorModel->update([ + 'matricula' => $agente->matricula, + 'status' => CONF_AGENT_STATUS_LIVRE + ]); + } + } + $this->supervisorModel->db->commit(); + $systemMessageAction = new SystemMessageAction; + $systemMessageAction( + momento: CONF_MOMENT_FINALIZAR_ATENDIMENTO, + variavels: [["nome" => "@autor_name", "valor" => $agente->nome]], + api: $provedor, + numero: $atendimento->cliente_id + ); + $ws->enviaMsg($ws->enviaActions($mesg, 'finish', $agente->matricula, $body['uniqueid'])); + + $response->getBody()->write( + $this->retorno( + $ret ? "Finalizado com sucesso" : "Erro", + $ret ? $ret : null, + $ret ? [$ret] : null + ) + ); + } catch (Throwable $th) { + $this->supervisorModel->db->rollback(); + $response->getBody()->write($this->retorno( + $th->getMessage(), + false, + [$th->getTraceAsString()] + )); + } + return $response; + } } \ No newline at end of file