Files
rkm-conectasus-ci-sandbox/api/app/Services/Cadastro/ObitoService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

212 lines
6.1 KiB
PHP

<?php
namespace App\Services\Cadastro;
use App\Models\Cadastro\SujeitoAtencao\Cidadao;
use App\Models\Cadastro\SujeitoAtencao\Obito;
use App\Models\Cadastro\SujeitoAtencao\Usuario;
use App\Models\Cadastro\SujeitoAtencao\PacienteHistorico;
use App\Models\Sigtap\Cid;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
class ObitoService extends Service
{
public static function create()
{
return [
'additionals' => [
'data_atual' => Carbon::now()->format('d/m/Y'),
],
'model' => [
'cidadao_id' => null,
'cid_id' => null,
'usuario_id' => null,
'obito_em' => null,
'hora_obito' => null,
'declaracao' => null,
'causa' => null,
],
];
}
public static function store()
{
self::validate();
$obito = Obito::create(request()->all())->fresh();
$paciente = Cidadao::where('id', request()->cidadao_id)
->first();
self::saveHistorico($paciente->paciente->id);
return $obito;
}
public static function show($id)
{
return Obito::find($id);
}
public static function edit($id)
{
$obito = self::show($id);
return [
'additionals' => [
'data_atual' => Carbon::now()->format('d/m/Y'),
],
'model' => [
'cidadao' => [
'id' => $obito->cidadao->id ?? null,
'cns' => $obito->cidadao->cns ?? null,
'nome' => $obito->cidadao->nome ?? null,
],
'cid' => [
'id' => $obito->cid->id ?? null,
'codigo' => $obito->cid->codigo ?? null,
'nome' => $obito->cid->nome ?? null,
],
'usuario' => [
'id' => $obito->usuario->id ?? null,
'cns' => $obito->usuario->cidadao->cns ?? null,
'nome' => $obito->usuario->cidadao->nome ?? null,
],
'obito_em' => $obito->obito_em->format('Y-m-d'),
'hora_obito' => $obito->obito_em->format('H:i:s'),
'obito_declaracao' => $paciente->cidadao->obito->declaracao ?? null,
'declaracao' => $obito->declaracao,
'causa' => $obito->causa,
],
];
}
public static function update($id)
{
self::validate();
return (tap(self::show($id))->update(request()->all()))->fresh();
}
public static function destroy($id)
{
DB::transaction(function () use ($id) {
$obito = Obito::with('cidadao.paciente')->findOrFail($id);
$paciente = $obito->cidadao->paciente ?? null;
if ($paciente) {
$paciente->cadastroIndividual()->update(['saida_cidadao_id' => null]);
$paciente->informacaoSocial()->update(['saida_cidadao_id' => null]);
}
$obito->delete();
});
}
public static function datatable()
{
return DB::table('cadastro_obito')
->select([
'cadastro_obito.id',
'cadastro_cidadao.nome',
'cadastro_cidadao.cns',
DB::raw("DATE_FORMAT(cadastro_obito.obito_em, '%d/%m/%Y') AS obito_em"),
'cadastro_obito.declaracao',
])
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_obito.cidadao_id')
->whereNull('cadastro_obito.deleted_at');
}
public static function autocompleteCidadao()
{
return DB::table('cadastro_cidadao')
->select([
'cadastro_cidadao.id',
'cadastro_cidadao.nome',
'cadastro_cidadao.cns',
])
->whereNotExists(fn ($q) => $q
->select(DB::raw(1))
->from('cadastro_obito')
->whereNull('cadastro_obito.deleted_at')
->whereColumn('cadastro_obito.cidadao_id', 'cadastro_cidadao.id')
)
->whereNull('cadastro_cidadao.deleted_at')
->orderBy('cadastro_cidadao.nome');
}
public static function autocompleteCid()
{
return DB::table('sigtap.cid')
->select([
'id',
'codigo',
'nome',
])
->where('is_ativo', true)
->orderBy('nome');
}
public static function autocompleteUsuario()
{
return DB::table('cadastro_usuario')
->select([
'cadastro_usuario.id',
'cadastro_cidadao.nome',
'cadastro_cidadao.cns',
])
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
->whereNull('cadastro_usuario.deleted_at')
->orderBy('cadastro_cidadao.nome');
}
public static function saveHistorico($pacienteId)
{
$historico = new PacienteHistorico;
$historico->paciente_id = $pacienteId;
$historico->save();
}
private static function validate()
{
$rules = [
'cidadao_id' => [
'required',
'integer',
Rule::exists(Cidadao::class, 'id'),
],
'cid_id' => [
'nullable',
'integer',
Rule::exists(Cid::class, 'id'),
],
'usuario_id' => [
'nullable',
'integer',
Rule::exists(Usuario::class, 'id'),
],
'obito_em' => [
'required',
'date',
],
'declaracao' => [
'nullable',
'string',
'between:1,13',
],
'causa' => [
'nullable',
'string',
'between:1,255',
],
];
request()->validate($rules);
}
}