96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Sigtap;
|
|
|
|
use App\Models\Cadastro\SujeitoAtencao\Sexo;
|
|
use App\Models\Sigtap\Ocupacao;
|
|
use App\Models\Sigtap\Procedimento;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ProcedimentoValidationService
|
|
{
|
|
private const QUANTIDADE_ULTIMAS_COMPENTENCIAS_VALIDAS = 4;
|
|
|
|
private Procedimento $procedimento;
|
|
private Collection $competenciasValidas;
|
|
|
|
public function __construct(Procedimento $procedimento)
|
|
{
|
|
$this->procedimento = $procedimento;
|
|
$this->competenciasValidas = $this->getCompetenciasValidas();
|
|
}
|
|
|
|
private function getCompetenciasValidas() : Collection
|
|
{
|
|
$competencias = new Collection;
|
|
$lastDate = null;
|
|
|
|
for ($i = 0; $i < self::QUANTIDADE_ULTIMAS_COMPENTENCIAS_VALIDAS; $i++) {
|
|
|
|
$date = null;
|
|
|
|
if (is_null($lastDate)) {
|
|
|
|
$date = Carbon::now();
|
|
}
|
|
else {
|
|
|
|
$isJanuary = $lastDate->month === 1;
|
|
$year = $isJanuary ? $lastDate->year - 1 : $lastDate->year;
|
|
$month = $isJanuary ? 12 : $lastDate->month - 1;
|
|
$date = Carbon::create($year, $month);
|
|
}
|
|
|
|
$competencia = $date->format('Ym');
|
|
|
|
if (Procedimento::whereHas('competencia', function($query) use ($competencia) {
|
|
$query->where('valor', $competencia);
|
|
})->exists()) {
|
|
$competencias->push($competencia);
|
|
}
|
|
|
|
$lastDate = $date;
|
|
}
|
|
|
|
return $competencias;
|
|
}
|
|
|
|
public function isOnCurrentCompetencia() : bool
|
|
{
|
|
return $this->procedimento->competencia->valor === $this->competenciasValidas->first();
|
|
}
|
|
|
|
public function isOnValidCompetencia() : bool
|
|
{
|
|
return $this->competenciasValidas->contains($this->procedimento->competencia->valor);
|
|
}
|
|
|
|
public function isOcupacaoValid(Ocupacao $ocupacao) : bool
|
|
{
|
|
return $this->procedimento->ocupacoes->isEmpty()
|
|
|| $this->procedimento->ocupacoes->contains($ocupacao);
|
|
}
|
|
|
|
public function isSexoValid(Sexo $sexo) : bool
|
|
{
|
|
return $this->procedimento->sexo === 'I'
|
|
|| $this->procedimento->sexo === 'N'
|
|
|| $this->procedimento->sexo === $sexo->identificador;
|
|
}
|
|
|
|
public function isIdadeValid(int $idadeEmAnos) : bool
|
|
{
|
|
$idadeEmMeses = $idadeEmAnos * 12;
|
|
|
|
return ($idadeEmMeses >= $this->procedimento->idade_minima || $this->procedimento->idade_minima === 9999)
|
|
&& ($idadeEmMeses <= $this->procedimento->idade_maxima || $this->procedimento->idade_maxima === 9999);
|
|
}
|
|
|
|
public function isQuantidadeValid(int $quantidade) : bool
|
|
{
|
|
return $quantidade >= 0
|
|
&& $quantidade <= $this->procedimento->quantidade_maxima_execucao;
|
|
}
|
|
}
|