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.
165 lines
6.9 KiB
165 lines
6.9 KiB
<?php |
|
|
|
namespace app\Actions; |
|
|
|
use app\Repositories\AtendimentoRepository; |
|
use app\Repositories\EventosAtendimentoRepository; |
|
use app\Repositories\SupervisorRepository; |
|
use app\Models\AtendimentoModel; |
|
use app\Interfaces\IApiMedia; |
|
use app\Models\EventosAtendimentosModel; |
|
use websocket\WsInterface; |
|
use app\Repositories\MessageRepository; |
|
use WebSocket\Exception; |
|
use app\Models\SupervisorModel; |
|
|
|
class CriarAtendimentoAction |
|
{ |
|
protected AtendimentoRepository $atendimentoRepository; |
|
protected EventosAtendimentoRepository $eventosAtendimentosRepository; |
|
protected SupervisorRepository $supervisorRepository; |
|
protected SystemMessageAction $systemMessageAction; |
|
protected MessageRepository $messageRepository; |
|
protected MostraPosiscaoNaFilaAction $mostraPosiscaoNaFilaAction; |
|
function __construct() |
|
{ |
|
$this->atendimentoRepository = new AtendimentoRepository; |
|
$this->eventosAtendimentosRepository = new EventosAtendimentoRepository; |
|
$this->supervisorRepository = new SupervisorRepository; |
|
$this->systemMessageAction = new SystemMessageAction; |
|
$this->messageRepository = new MessageRepository; |
|
$this->mostraPosiscaoNaFilaAction = new MostraPosiscaoNaFilaAction; |
|
} |
|
function __invoke($fila, $numero_client, ?string $uniqueid, IApiMedia $api) |
|
{ |
|
try { |
|
$this->atendimentoRepository->db->begin(); |
|
if (empty($uniqueid)) { |
|
$id = $this->atendimentoRepository->create(new AtendimentoModel( |
|
uniqueid: null, |
|
matricula: null, |
|
cliente_id: $numero_client, |
|
media: $api->getchannel(), |
|
nome: $api->getProfile(), |
|
data_reg: 'now()', |
|
id_empresa: $api->getNumberChannel()->id_empresa, |
|
id_number_channel: $api->getNumberChannel()->id |
|
)); |
|
$uniqueid = $this->atendimentoRepository->get(['id' => $id])->uniqueid; |
|
|
|
if ($uniqueid) { |
|
$this->eventosAtendimentosRepository->create(new EventosAtendimentosModel( |
|
id: null, |
|
uniqueid: $uniqueid, |
|
evento: 'EMESPERA', |
|
fila: $fila, |
|
data_reg: 'now()', |
|
data_evento: 'now()', |
|
id_usuario: null |
|
)); |
|
} |
|
} |
|
$agentes = $this->supervisorRepository->list(['fila' => $fila, 'id_empresa' => $api->getNumberChannel()->id_empresa]); |
|
if (empty($agentes)) { |
|
$this->systemMessageAction->__invoke( |
|
CONF_MOMENT_ENTRAR_FILA_SEM, |
|
[], |
|
$api, |
|
$numero_client, |
|
$fila |
|
); |
|
$this->atendimentoRepository->db->commit(); |
|
return null; |
|
} |
|
$agent = $this->supervisorRepository->findAgentByQueue($fila, 'LIVRE'); |
|
if (empty($agent)) { |
|
$this->systemMessageAction->__invoke( |
|
CONF_MOMENT_ENTRAR_FILA_COM, |
|
[], |
|
$api, |
|
$numero_client, |
|
$fila |
|
); |
|
$filaAtualClient = $this->mostraPosiscaoNaFilaAction->__invoke($numero_client, $fila); |
|
if ($filaAtualClient) { |
|
$api->enviarMsg($numero_client, $filaAtualClient['MESSAGE']); |
|
} |
|
$this->atendimentoRepository->db->commit(); |
|
return null; |
|
} |
|
$retServe = $this->atendimentoRepository->update(['uniqueid' => $uniqueid, 'matricula' => $agent[0]->matricula, 'id_usuario' => $agent[0]->id_usuario]); |
|
if (empty($retServe)) { |
|
$api->enviarMsg( |
|
$numero_client, |
|
'Erro ao gerar atendimento!' |
|
); |
|
|
|
throw new Exception("Erro ao gerar atendimento!", 1); |
|
} |
|
//Gera protocolo |
|
$action = new GenerateProtocolAction; |
|
$protocol = $action($uniqueid); |
|
//cria o evento de inicio de atendimento e criar o protocolo |
|
if (empty($protocol)) { |
|
throw new Exception("Não foi possivel gerar protocolo!", 1); |
|
} |
|
$event = $this->eventosAtendimentosRepository->getStatusAtendimento($uniqueid); |
|
$retCria = $this->eventosAtendimentosRepository->create( |
|
new EventosAtendimentosModel( |
|
null, |
|
$uniqueid, |
|
CONF_EVENT_START, |
|
'now()', |
|
'now()', |
|
$fila, |
|
$agent[0]->id_usuario |
|
) |
|
); |
|
if (!empty($retCria) && $event->evento != CONF_EVENT_START) { |
|
$action = new AtualizaStatusAgenteAction; |
|
$action(SupervisorModel::ArrayTo(json_decode(json_encode($agent[0]), true))); |
|
$this->systemMessageAction->__invoke( |
|
CONF_MOMENT_INICIAR_ATENDIMENTO, |
|
[["nome" => "@agente_name", "valor" => utf8_encode($agent[0]->nome)]], |
|
$api, |
|
$numero_client, |
|
$fila |
|
); |
|
$api->enviarMsg( |
|
$numero_client, |
|
"Número do protocolo do atendimento é $protocol\n" |
|
); |
|
$ws = new WsInterface(); |
|
try { |
|
if ($event->evento == CONF_EVENT_ERRO_ATEND) { |
|
$ws->enviaMsg($ws->enviaActions( |
|
'Este atendimento foi realocado para sua responsabilidade.', |
|
're_start', |
|
$agent[0]->id_empresa, |
|
$uniqueid |
|
)); |
|
$this->messageRepository->addMessage( |
|
$uniqueid, |
|
$agent[0]->matricula, |
|
$agent[0]->matricula, |
|
're_start', |
|
'Este atendimento foi realocado para sua responsabilidade.', |
|
$agent[0]->nome, |
|
$api->getchannel(), |
|
'read' |
|
); |
|
} else { |
|
$ws->enviaMsg($ws->enviaActions('Atendimento iniciado!', 'start', $agent[0]->matricula, $uniqueid)); |
|
} |
|
} catch (\Exception $th) { |
|
$ws->enviaActions('Atendimento iniciado!', 'start', $agent[0]->matricula, $uniqueid); |
|
} |
|
} |
|
$this->atendimentoRepository->db->commit(); |
|
return; |
|
} catch (\Throwable $th) { |
|
$this->atendimentoRepository->db->rollback(); |
|
throw new Exception($th->getMessage(), 1); |
|
} |
|
} |
|
} |