This commit is contained in:
228
api/app/Services/TransportePaciente/Motivo/MotivoService.php
Normal file
228
api/app/Services/TransportePaciente/Motivo/MotivoService.php
Normal file
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente\Motivo;
|
||||
|
||||
use App\Exceptions\Model\DeletingModelException;
|
||||
use App\Models\TransportePaciente\Grupo\Grupo;
|
||||
use App\Models\TransportePaciente\Motivo\Modulo;
|
||||
use App\Models\TransportePaciente\Motivo\Motivo;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Models\Sigtap\Procedimento;
|
||||
|
||||
class MotivoService extends Service
|
||||
{
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
|
||||
return [
|
||||
'data_atual' => $now->format('d/m/Y'),
|
||||
'grupos' => Grupo::orderBy('descricao')->get()->map(fn ($tipo) => [
|
||||
'id' => $tipo->id,
|
||||
'descricao' => $tipo->descricao,
|
||||
]),
|
||||
'modulos' => Modulo::orderBy('descricao')->get()->map(fn ($tipo) => [
|
||||
'id' => $tipo->id,
|
||||
'descricao' => $tipo->descricao,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData($motivo = null)
|
||||
{
|
||||
if($motivo) {
|
||||
$procedimento = Procedimento::where('codigo', $motivo->codigo_procedimento)->first();
|
||||
}
|
||||
$motivo = [
|
||||
'id' => $motivo->id ?? null,
|
||||
'descricao' => $motivo->descricao ?? null,
|
||||
'grupo_id' => $motivo->grupo_id ?? null,
|
||||
'modulo_id' => $motivo->modulo_id ?? null,
|
||||
'regulada' => $motivo->regulada ?? null,
|
||||
'bloqueado' => $motivo->bloqueado ?? null,
|
||||
'codigo_procedimento' => $motivo->codigo_procedimento ?? null,
|
||||
'procedimento' => $procedimento ?? null,
|
||||
];
|
||||
|
||||
return $motivo;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return Motivo::find($id);
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Motivo::where('id', $id)->update(request()->all());
|
||||
}
|
||||
|
||||
public static function bloquear($id)
|
||||
{
|
||||
$motivo = self::show($id);
|
||||
|
||||
return Motivo::where('id', $id)->update(['bloqueado' => $motivo->bloqueado == 1 ? 0 : 1]);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(self::show($id)),
|
||||
];
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Motivo::create(request()->all())->fresh();
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
try {
|
||||
return self::show($id)->delete();
|
||||
} catch (\Throwable $th) {
|
||||
throw new DeletingModelException();
|
||||
}
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
|
||||
$motivos = DB::table('transporte_paciente_motivo_viagem_procedimento')
|
||||
->leftJoin(
|
||||
'transporte_paciente_grupo',
|
||||
'transporte_paciente_grupo.id',
|
||||
'=',
|
||||
'transporte_paciente_motivo_viagem_procedimento.grupo_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'transporte_paciente_motivo_modulo',
|
||||
'transporte_paciente_motivo_modulo.id',
|
||||
'=',
|
||||
'transporte_paciente_motivo_viagem_procedimento.modulo_id'
|
||||
)
|
||||
->leftJoin('sigtap.procedimento', function ($join) {
|
||||
|
||||
$join->on('sigtap.procedimento.codigo', 'transporte_paciente_motivo_viagem_procedimento.codigo_procedimento')
|
||||
->where('sigtap.procedimento.competencia_id', '=', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderByDesc('sigtap.competencia.valor')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->select(
|
||||
'transporte_paciente_motivo_viagem_procedimento.id',
|
||||
'transporte_paciente_motivo_viagem_procedimento.descricao',
|
||||
'transporte_paciente_grupo.descricao AS grupo',
|
||||
'transporte_paciente_motivo_viagem_procedimento.bloqueado',
|
||||
DB::raw('CONCAT(sigtap.procedimento.codigo, " - ", sigtap.procedimento.nome) AS procedimento'),
|
||||
DB::raw('
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.modulo_id THEN
|
||||
UPPER(transporte_paciente_motivo_modulo.descricao)
|
||||
ELSE
|
||||
"TODOS"
|
||||
END as modulo'
|
||||
),
|
||||
DB::raw('
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.regulada THEN
|
||||
"SIM"
|
||||
ELSE
|
||||
"NÃO"
|
||||
END as regulada'
|
||||
),
|
||||
DB::raw("
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END AS status
|
||||
"),
|
||||
);
|
||||
|
||||
return datatables()
|
||||
->of($motivos)
|
||||
->filterColumn('transporte_paciente_motivo_viagem_procedimento.bloqueado', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('transporte_paciente_motivo_viagem_procedimento.regulada', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.regulada THEN
|
||||
'SIM'
|
||||
ELSE
|
||||
'NÃO'
|
||||
END LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('transporte_paciente_motivo_modulo.descricao', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CASE WHEN transporte_paciente_motivo_viagem_procedimento.modulo_id THEN
|
||||
UPPER(transporte_paciente_motivo_modulo.descricao)
|
||||
ELSE
|
||||
'TODOS'
|
||||
END LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('sigtap.procedimento.codigo', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CONCAT(sigtap.procedimento.codigo, ' - ', sigtap.procedimento.nome) LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function validate()
|
||||
{
|
||||
return request()->validate(
|
||||
[
|
||||
'descricao' => [
|
||||
'required',
|
||||
'min:5',
|
||||
'max:100',
|
||||
Rule::unique('transporte_paciente_motivo_viagem_procedimento')
|
||||
->ignore(request()->motivo_viagem_procedimento)
|
||||
],
|
||||
],
|
||||
[
|
||||
'descricao.unique' => 'O motivo já foi inserido anteriormente.',
|
||||
'descricao.min' => 'O campo descrição precisa ter no mínimo 5 caracteres.',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function autocomplete()
|
||||
{
|
||||
return DB::table('transporte_paciente_motivo_viagem_procedimento')
|
||||
->select([
|
||||
'id',
|
||||
'descricao',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user