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ã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á como indisponivel e caso nã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ç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ão foi possível cancelar o atendimento! " . $this->bilheteController->message()); return true; } } }