This commit is contained in:
205
api/app/Services/Farmacia/SupervisorService.php
Normal file
205
api/app/Services/Farmacia/SupervisorService.php
Normal file
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Farmacia;
|
||||
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
||||
use App\Models\Farmacia\Supervisor;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SupervisorService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('farmacia_supervisor as supervisor')
|
||||
->join(
|
||||
'cadastro_infraestrutura as infraestrutura',
|
||||
'infraestrutura.id',
|
||||
'supervisor.infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade as unidade',
|
||||
'unidade.id',
|
||||
'infraestrutura.unidade_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_usuario as usuario',
|
||||
'usuario.id',
|
||||
'supervisor.usuario_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_cidadao as cidadao',
|
||||
'cidadao.id',
|
||||
'usuario.cidadao_id'
|
||||
)
|
||||
->select(
|
||||
'supervisor.id',
|
||||
DB::raw('unidade.nome_fantasia as unidade'),
|
||||
DB::raw('COALESCE(infraestrutura.nome, unidade.nome_fantasia) as infraestrutura'),
|
||||
DB::raw('cidadao.nome as usuario'),
|
||||
);
|
||||
}
|
||||
|
||||
public static function isSupervisor($usuarioId)
|
||||
{
|
||||
return Supervisor::where('usuario_id', $usuarioId)
|
||||
->where('infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public static function isUsuarioLogadoSupervisor()
|
||||
{
|
||||
$lotacaoLogada = self::getCurrentLotacao();
|
||||
|
||||
return Supervisor::where('infraestrutura_id', $lotacaoLogada->infraestrutura_id)
|
||||
->where('usuario_id', $lotacaoLogada->usuario_id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'unidades' => self::getUnidades(),
|
||||
'infraestruturas' => [],
|
||||
'usuarios' => [],
|
||||
],
|
||||
'model' => [
|
||||
'unidade_id' => null,
|
||||
'infraestrutura_id' => null,
|
||||
'usuario_id' => null,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$supervisor = Supervisor::findOrFail($id);
|
||||
|
||||
$params = (object) [
|
||||
'unidade_id' => $supervisor->infraestrutura->unidade_id,
|
||||
'infraestrutura_id' => $supervisor->infraestrutura_id
|
||||
];
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'unidades' => self::getUnidades($supervisor->infraestrutura->unidade_id),
|
||||
'infraestruturas' => self::getInfraestruturas($params, $supervisor->infraestrutura_id),
|
||||
'usuarios' => self::getUsuarios($params),
|
||||
],
|
||||
'model' => [
|
||||
'unidade_id' => $supervisor->infraestrutura->unidade_id,
|
||||
'infraestrutura_id' => $supervisor->infraestrutura_id,
|
||||
'usuario_id' => $supervisor->usuario_id,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$params = (object) request()->all();
|
||||
|
||||
switch (request()->listar) {
|
||||
|
||||
case 'infraestruturas': {
|
||||
|
||||
return self::getInfraestruturas($params);
|
||||
}
|
||||
case 'usuarios': {
|
||||
|
||||
return self::getUsuarios($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function getUnidades($unidadeId = null)
|
||||
{
|
||||
return Unidade::where(fn ($q) => $q
|
||||
->when($unidadeId, fn ($q, $id) => $q->where('id', $id))
|
||||
->orWhereHas('infraestruturas', fn ($q) => $q
|
||||
->whereHas('lotacoes')
|
||||
)
|
||||
)
|
||||
->orderBy('nome_fantasia')
|
||||
->get(['id', 'nome_fantasia']);
|
||||
}
|
||||
|
||||
private static function getInfraestruturas($params, $infraestruturaId = null)
|
||||
{
|
||||
return Infraestrutura::setEagerLoads([])
|
||||
->where('unidade_id', $params->unidade_id)
|
||||
->where(fn ($q) => $q
|
||||
->when($infraestruturaId, fn ($q, $id) => $q->where('id', $id))
|
||||
)
|
||||
->whereHas('lotacoes')
|
||||
->get(['id', 'unidade_id', 'nome', 'principal'])
|
||||
->map(fn ($infraestrutura) => [
|
||||
'id' => $infraestrutura->id,
|
||||
'nome' => $infraestrutura->nome
|
||||
])
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
}
|
||||
|
||||
private static function getUsuarios($params)
|
||||
{
|
||||
return Usuario::setEagerLoads([])
|
||||
->whereHas('lotacoes', fn ($q) => $q
|
||||
->where('infraestrutura_id', $params->infraestrutura_id)
|
||||
)
|
||||
->get(['id', 'cidadao_id'])
|
||||
->map(fn ($usuario) => [
|
||||
'id' => $usuario->id,
|
||||
'nome' => $usuario->cidadao()->setEagerLoads([])->first('nome')->nome,
|
||||
])
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return Supervisor::create(request()->all())->fresh();
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validate();
|
||||
|
||||
return (tap(Supervisor::findOrFail($id))->update(request()->all()))->fresh();
|
||||
}
|
||||
|
||||
public static function validate()
|
||||
{
|
||||
$rules = [
|
||||
'infraestrutura_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Infraestrutura::class, 'id'),
|
||||
],
|
||||
'usuario_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Usuario::class, 'id'),
|
||||
Rule::unique(Supervisor::class, 'usuario_id')
|
||||
->where('infraestrutura_id', request()->infraestrutura_id)
|
||||
->ignore(request()->route('supervisor'))
|
||||
],
|
||||
];
|
||||
|
||||
$messages = [
|
||||
'usuario_id.unique' => 'O local de atendimento e o profissional já foram inseridos anteriormente.',
|
||||
];
|
||||
|
||||
request()->validate($rules, $messages);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
Supervisor::findOrFail($id)->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user