baseSmsService = new BaseSmsService(); $this->agendamento = Agendamento::findOrFail($agendamentoId); $this->parametrizacao = Parametrizacao::where( 'infraestrutura_id', $this->agendamento->jornada->infraestrutura_id )->first(); } public function send() { if ($this->agendamento->jornada->data->isToday()) { return; } $cellPhone = $this->getCellPhone(); if (is_null($cellPhone)) { return; } if (!$this->parametrizacao->envia_sms || ($this->parametrizacao->envia_sms && is_null($this->parametrizacao->sms))) { return; } $schedule = $this->getSchedule(); if (!$this->isAllowedToSend($schedule)) { return; } try { $smsEnvio = $this->baseSmsService->send($cellPhone, $this->getMessage(), $schedule); $this->agendamento->sms_envio_id = $smsEnvio->id; $this->agendamento->save(); } catch (RuntimeException $e) {} } private function isAllowedToSend($schedule) { if (is_null($schedule)) { $schedule = Carbon::now(); } $envioDe = $this->parametrizacao->sms->envio_de->startOfDay(); $envioAte = $this->parametrizacao->sms->envio_ate->endOfDay(); if ($schedule >= $envioDe && $schedule <= $envioAte) { return $this->agendamento->agendamentoExame ->pluck('exame_id') ->intersect($this->parametrizacao->sms->exames->pluck('id')) ->isNotEmpty(); } return false; } private function getCellPhone() { return $this->agendamento->paciente->cidadao->telefones() ->where('autoriza_sms', true) ->first()->numero ?? null; } private function getMessage() { $nomeRegistroPaciente = $this->agendamento->paciente->cidadao->nome; $nomeSocialPaciente = $this->agendamento->paciente->cidadao->nome_social; $preferenciaNome = $this->agendamento->paciente->cidadao->preferencia_nome_id === PreferenciaNome::NOME_SOCIAL; $nomePaciente = $preferenciaNome && $nomeSocialPaciente ? $nomeSocialPaciente : $nomeRegistroPaciente; $data = $this->agendamento->jornada->data->format('d/m'); $horario = (new Carbon($this->agendamento->hora_exame))->format('H:i'); $unidade = $this->agendamento->jornada->infraestrutura->unidade->nome_fantasia; $infraestrutura = $this->agendamento->jornada->infraestrutura->nome; $unidade .= $infraestrutura === $unidade ? '' : " ({$infraestrutura})"; $unidade = self::limitLocal($unidade); $unidade = trim($unidade); $nomePaciente = self::limitName($nomePaciente, 30); $mensagem = "Olá {$nomePaciente}, seu pedido de {$this->agendamento->grupo->nome}" . " agendado em" . " {$data} às {$horario}" . " a ser realizado na {$unidade}. Confirme e Leve o Comprovante."; return self::limitMsg($mensagem); } function limitName($nome) { $partes = explode(' ', $nome); $primeiroNome = array_shift($partes); $nomeFinal = $primeiroNome; return $nomeFinal; } function limitLocal($local, $limite = 30) { return mb_strlen($local) > $limite ? mb_substr($local, 0, $limite) : $local; } function limitMsg($msg, $limite = 160) { return mb_strlen($msg) > $limite ? mb_substr($msg, 0, $limite - 3) . '...' : $msg; } private function getSchedule() { $schedule = $this->agendamento->jornada->data->copy()->subDays($this->parametrizacao->sms->dia_antecedencia); if ($schedule <= Carbon::today()) { return null; } return $schedule; } public function cancel() { if (is_null($this->agendamento->sms_envio_id)) { return; } try { $this->baseSmsService->cancel($this->agendamento->sms_envio_id); } catch (RuntimeException $e) {} } }