diff --git a/app/Core/Commands.php b/app/Core/Commands.php index da3ee61..f810cc2 100644 --- a/app/Core/Commands.php +++ b/app/Core/Commands.php @@ -18,43 +18,30 @@ use websocket\WsInterface; */ class Commands { - /* - * Permissoes - * 0 - CLIENTE & AGENTE - * 1 - AGENTE - */ - const PERMISSION = "PERMISSION"; - const DESCRIPTION = "DESCRIPTION"; + /** @var IApiMedia $api provider de mensagens */ + protected $api; - private $bilheteController; - public $commandsConfig = array(); - private $api; - private $systemController; + /** @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; public function __construct() { + $this->eventosModel = new Evento(); + $this->atendimentoModel = new Atendimento(); $this->agente = new AgentController(); $this->bilheteController = new BilheteController(); $this->systemController = new SystemMessageController(); - $this->commandsConfig = [ - "CANCELAR" => [ - self::PERMISSION => ["MIN" => 0, "MAX" => 0], - self::DESCRIPTION => "Cancelar posição na fila de atendimento." - ], - "FINALIZAR" => [ - self::PERMISSION => ["MIN" => 0, "MAX" => 1], - self::DESCRIPTION => "Encerrar o atendimento." - ] - ]; } - /* - * Verificar se é um comando valido - * @param string $phone - * @param string $message - * - * return string - */ public function setApi(IApiMedia $api) { $this->api = $api; @@ -77,16 +64,17 @@ class Commands public function finalizar($numero) { try { - $atendimentoModel = new Atendimento(); - $eventosModel = new Evento(); + $this->atendimentoModel->begin(); $supervisorModel = new SupervisorModel(); - $atendimento = $atendimentoModel->getAtendimentoByCliente($numero, CONF_EVENT_START); + $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ão foi encontrado atendimento em aberto"); return true; } - $ret = $eventosModel->createEvento( + //cria o evento pra finalizar + $ret = $this->eventosModel->createEvento( $atendimento->uniqueid, CONF_EVENT_TIMERMINO_CLIENTE, date('Y-m-d H:i:s'), @@ -96,7 +84,6 @@ class Commands if ($ret) { $ws = new WsInterface(); $agente = $supervisorModel->findAgentByMatricula($atendimento->matricula); - $ws->enviaMsg($ws->enviaActions('Atendimento finalizado', 'finish', $agente->matricula, $atendimento->uniqueid)); $messegeModel = new Message(); $messegeModel->addMessage( $atendimento->uniqueid, @@ -108,31 +95,39 @@ class Commands $atendimento->context, 'read' ); + //verifica se está como indisponivel e caso não tenha atendimento ele entra em pausa if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) { - $atends = $atendimentoModel->getAtendimentoAbertoByAgente($atendimento->matricula); + $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 = $atendimentoModel->getQuantiAtendimentSimultaneos(); - $atendimentosAbertos = $atendimentoModel->getAtendimentoAbertoByAgente($agente->matricula); + $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, [], $this->api, $numero ); + $this->atendimentoModel->commit(); + //notifica o agente da mudança + $ws->enviaMsg($ws->enviaActions('Atendimento finalizado', '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; } @@ -140,10 +135,8 @@ class Commands private function cancelar($numero) { - $atendimentoModel = new Atendimento(); - $eventosModel = new Evento(); - $atendimento = $atendimentoModel->getAtendimentoByCliente($numero, CONF_EVENT_ESPERA); - $ret = $eventosModel->createEvento( + $atendimento = $this->atendimentoModel->getAtendimentoByCliente($numero, CONF_EVENT_ESPERA); + $ret = $this->eventosModel->createEvento( $atendimento->uniqueid, CONF_EVENT_ABANDONADA, date('Y-m-d H:i:s'), @@ -151,6 +144,7 @@ class Commands $atendimento->fila ); if ($ret) { + //envia as mensagens do sistema do momento atual para o cliente $this->systemController->sendMessageSystem( CONF_MOMENT_CANCELAR_FILA, [],