56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cadastro\SujeitoAtencao;
|
|
|
|
use App\Models\Cadastro\SujeitoAtencao\Cidadao;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Services\Integracao\IntegracaoCidadaoService;
|
|
use Carbon\Carbon;
|
|
|
|
class PacienteDesconhecidoSaveService
|
|
{
|
|
private $sequencialDesconhecidoEm;
|
|
private $sexoId;
|
|
private $caracteristicasFisicas;
|
|
|
|
public function __construct($sequencialDesconhecidoEm, $sexoId, $caracteristicasFisicas = null)
|
|
{
|
|
$this->sequencialDesconhecidoEm = new Carbon($sequencialDesconhecidoEm);
|
|
$this->sexoId = $sexoId;
|
|
$this->caracteristicasFisicas = $caracteristicasFisicas;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$id = $this->getNextId();
|
|
|
|
$cidadao = Cidadao::create([
|
|
'nome' => "DESCONHECIDO $id",
|
|
'sexo_id' => $this->sexoId,
|
|
'caracteristicas_fisicas' => $this->caracteristicasFisicas,
|
|
]);
|
|
|
|
$paciente = $cidadao->paciente()->create([
|
|
'sequencial_desconhecido' => $this->getNextSequencialDesconhecido(),
|
|
'sequencial_desconhecido_em' => $this->sequencialDesconhecidoEm,
|
|
]);
|
|
|
|
IntegracaoCidadaoService::integrar($cidadao->id);
|
|
|
|
return $paciente;
|
|
}
|
|
|
|
private function getNextId()
|
|
{
|
|
return Cidadao::max('id') + 1;
|
|
}
|
|
|
|
private function getNextSequencialDesconhecido()
|
|
{
|
|
$date = $this->sequencialDesconhecidoEm->format('Y-m-d');
|
|
|
|
return Paciente::whereDate('sequencial_desconhecido_em', $date)
|
|
->max('sequencial_desconhecido') + 1;
|
|
}
|
|
}
|