This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente\Supervisor;
|
||||
|
||||
use App\Models\TransportePaciente\Supervisor\Supervisor;
|
||||
use App\Services\Service;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SupervisorService extends Service
|
||||
{
|
||||
|
||||
public static function getUnidades() {
|
||||
|
||||
$unidades = DB::table('cadastro_unidade')
|
||||
->select(
|
||||
'cadastro_unidade.id as id_unidade',
|
||||
'cadastro_unidade.nome_fantasia as nome_fantasia',
|
||||
'cadastro_infraestrutura.id as id_infraestrutura',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_infraestrutura.nome IS NOT NULL THEN
|
||||
cadastro_infraestrutura.nome
|
||||
ELSE
|
||||
cadastro_unidade.nome_fantasia
|
||||
END AS nome_infraestrutura
|
||||
"),
|
||||
'cadastro_infraestrutura.principal as principal'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.unidade_id',
|
||||
'cadastro_unidade.id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_lotacao',
|
||||
'cadastro_lotacao.infraestrutura_id',
|
||||
'cadastro_infraestrutura.id'
|
||||
)
|
||||
->where('cadastro_lotacao.usuario_id', request()->usuario_id)
|
||||
->groupBy('id_unidade', 'id_infraestrutura')
|
||||
->get();
|
||||
|
||||
$unidadesComInfraestruturas = [];
|
||||
|
||||
foreach ($unidades as $unidade) {
|
||||
$idUnidade = $unidade->id_unidade;
|
||||
$nomeUnidade = $unidade->nome_fantasia;
|
||||
|
||||
$infraestrutura = [
|
||||
'id' => $unidade->id_infraestrutura,
|
||||
'nome' => $unidade->nome_infraestrutura,
|
||||
'principal' => $unidade->principal
|
||||
];
|
||||
|
||||
if (!array_key_exists($idUnidade, $unidadesComInfraestruturas)) {
|
||||
$unidadesComInfraestruturas[$idUnidade] = [
|
||||
'id' => $idUnidade,
|
||||
'nome_fantasia' => $nomeUnidade,
|
||||
'infraestruturas' => []
|
||||
];
|
||||
}
|
||||
|
||||
$unidadesComInfraestruturas[$idUnidade]['infraestruturas'][] = $infraestrutura;
|
||||
}
|
||||
|
||||
return array_values($unidadesComInfraestruturas);
|
||||
}
|
||||
|
||||
private static function getModelData($supervisor = null)
|
||||
{
|
||||
|
||||
$supervisor = [
|
||||
'id' => $supervisor->id ?? null,
|
||||
'usuario' => [
|
||||
'id' => $supervisor ? $supervisor->usuario->id : null,
|
||||
'cidadao_nome' => $supervisor ? $supervisor->usuario->cidadao->nome : null
|
||||
],
|
||||
'usuario_id' => $supervisor->usuario_id ?? null,
|
||||
'origem_usuario_id' => $supervisor->usuario_id ?? null,
|
||||
'unidade_id' => $supervisor->unidade_id ?? null,
|
||||
'infraestrutura_id' => $supervisor->infraestrutura_id ?? null,
|
||||
];
|
||||
|
||||
return $supervisor;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return Supervisor::find($id);
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Supervisor::where('id', $id)->update(request()->all());
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
return [
|
||||
'model' => self::getModelData(self::show($id)),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getSupervisor()
|
||||
{
|
||||
return DB::table('transporte_paciente_supervisor')
|
||||
->select(
|
||||
'transporte_paciente_supervisor.id',
|
||||
'cadastro_cidadao.nome as usuario',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_unidade.nome_fantasia IS NOT NULL THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
'TODAS'
|
||||
END AS unidade
|
||||
"),
|
||||
DB::raw("
|
||||
CASE WHEN transporte_paciente_supervisor.infraestrutura_id IS NOT NULL THEN
|
||||
COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)
|
||||
ELSE
|
||||
'TODOS'
|
||||
END AS infraestrutura
|
||||
"),
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'transporte_paciente_supervisor.usuario_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'transporte_paciente_supervisor.unidade_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'transporte_paciente_supervisor.infraestrutura_id'
|
||||
)
|
||||
->where(function ($query) {
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
$query
|
||||
->where('transporte_paciente_supervisor.unidade_id', request()->unidade_id)
|
||||
->orWhereNull('transporte_paciente_supervisor.unidade_id');
|
||||
|
||||
} else {
|
||||
|
||||
$query
|
||||
->whereNotNull('transporte_paciente_supervisor.unidade_id')
|
||||
->orWhereNull('transporte_paciente_supervisor.unidade_id');
|
||||
}
|
||||
})
|
||||
->where(function ($query) {
|
||||
|
||||
if (request()->unidade_id) {
|
||||
|
||||
if (request()->infraestrutura_id) {
|
||||
|
||||
$query
|
||||
->where('transporte_paciente_supervisor.infraestrutura_id', request()->infraestrutura_id)
|
||||
->orWhereNull('transporte_paciente_supervisor.infraestrutura_id');
|
||||
} else {
|
||||
|
||||
$query
|
||||
->whereNotNull('transporte_paciente_supervisor.infraestrutura_id')
|
||||
->orWhereNull('transporte_paciente_supervisor.infraestrutura_id');
|
||||
}
|
||||
}
|
||||
})
|
||||
->where('cadastro_usuario.id', request()->usuario_id)
|
||||
->first();
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Supervisor::create(request()->all())->fresh();
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
return self::show($id)->delete();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
|
||||
$supervisor = DB::table('transporte_paciente_supervisor')
|
||||
->select(
|
||||
'transporte_paciente_supervisor.id',
|
||||
'cadastro_cidadao.nome as usuario',
|
||||
DB::raw("
|
||||
CASE WHEN cadastro_unidade.nome_fantasia IS NOT NULL THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
'TODAS'
|
||||
END AS unidade
|
||||
"),
|
||||
DB::raw("
|
||||
CASE WHEN transporte_paciente_supervisor.infraestrutura_id IS NOT NULL THEN
|
||||
COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)
|
||||
ELSE
|
||||
'TODOS'
|
||||
END AS infraestrutura
|
||||
"),
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario',
|
||||
'cadastro_usuario.id',
|
||||
'transporte_paciente_supervisor.usuario_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_cidadao.id',
|
||||
'cadastro_usuario.cidadao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade',
|
||||
'cadastro_unidade.id',
|
||||
'transporte_paciente_supervisor.unidade_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_infraestrutura',
|
||||
'cadastro_infraestrutura.id',
|
||||
'transporte_paciente_supervisor.infraestrutura_id'
|
||||
);
|
||||
|
||||
return datatables()
|
||||
->of($supervisor)
|
||||
->filterColumn('cadastro_infraestrutura.nome', function ($query, $keyword) {
|
||||
|
||||
$query
|
||||
->whereRaw("cadastro_infraestrutura.nome like ?", ["%{$keyword}%"])
|
||||
->orWhereRaw(
|
||||
"CASE WHEN transporte_paciente_supervisor.infraestrutura_id IS NOT NULL THEN
|
||||
COALESCE(cadastro_infraestrutura.nome, cadastro_unidade.nome_fantasia)
|
||||
ELSE
|
||||
'TODOS'
|
||||
END like ?",
|
||||
["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('cadastro_unidade.nome_fantasia', function ($query, $keyword) {
|
||||
|
||||
$query
|
||||
->whereRaw("cadastro_unidade.nome_fantasia like ?", ["%{$keyword}%"])
|
||||
->orWhereRaw(
|
||||
"CASE WHEN cadastro_unidade.nome_fantasia IS NOT NULL THEN
|
||||
cadastro_unidade.nome_fantasia
|
||||
ELSE
|
||||
'TODAS'
|
||||
END like ?",
|
||||
["%{$keyword}%"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function validate()
|
||||
{
|
||||
return request()->validate(
|
||||
[
|
||||
'usuario_id' => [
|
||||
'required',
|
||||
],
|
||||
],
|
||||
[
|
||||
'usuario_id.unique' => 'É obrigatório informar o profissional.',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user