334 lines
14 KiB
PHP
334 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\PainelSenha;
|
|
|
|
use App\Enums\Administracao\Modulo;
|
|
use App\Models\PainelSenha\CentroCustoCompartilhado;
|
|
use App\Models\PainelSenha\OpcaoEntrada;
|
|
use App\Models\PainelSenha\OpcaoEntradaDestino;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class OpcaoEntradaService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
return DB::table('painel_senha_opcao_entrada')
|
|
->select([
|
|
'painel_senha_opcao_entrada.id',
|
|
'painel_senha_opcao_entrada.nome AS opcao_entrada_nome',
|
|
'painel_senha_opcao_entrada.sigla AS opcao_entrada_sigla',
|
|
'cadastro_setor.nome AS setor_nome',
|
|
'administracao_modulo.nome AS modulo_nome',
|
|
])
|
|
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
|
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
|
->join('administracao_modulo', 'administracao_modulo.id', 'painel_senha_opcao_entrada.modulo_id')
|
|
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
|
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
|
->orderBy('painel_senha_opcao_entrada.nome')
|
|
->get();
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData(),
|
|
];
|
|
}
|
|
|
|
public static function store($request)
|
|
{
|
|
self::validate($request);
|
|
|
|
$opcaoEntrada = null;
|
|
|
|
DB::transaction(function() use ($request, &$opcaoEntrada) {
|
|
$opcaoEntrada = OpcaoEntrada::create($request->all())->fresh();
|
|
|
|
foreach ($request->centros_custo_compartilhado as $value) {
|
|
CentroCustoCompartilhado::updateOrCreate(
|
|
[
|
|
'opcao_entrada_id' => $opcaoEntrada->id,
|
|
'centro_custo_id' => $value['centro_custo_id'],
|
|
]
|
|
);
|
|
}
|
|
|
|
foreach ($request->opcoes_entrada_destino as $value) {
|
|
OpcaoEntradaDestino::updateOrCreate(
|
|
[
|
|
'opcao_entrada_origem_id' => $opcaoEntrada->id,
|
|
'opcao_entrada_destino_id' => $value['opcao_entrada_id'],
|
|
]
|
|
);
|
|
}
|
|
});
|
|
|
|
return $opcaoEntrada;
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
return OpcaoEntrada::find($id);
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'form' => self::getFormData($id),
|
|
'model' => self::getModelData($id),
|
|
];
|
|
}
|
|
|
|
public static function update($request, $id)
|
|
{
|
|
self::validate($request);
|
|
|
|
$opcaoEntrada = tap(self::show($id));
|
|
|
|
DB::transaction(function() use ($request, &$opcaoEntrada) {
|
|
$opcaoEntrada = $opcaoEntrada->update($request->all());
|
|
|
|
$centrosCustoCompartilhadoIds = collect($request->centros_custo_compartilhado)->pluck('id')->all();
|
|
$opcoesEntradaDestinoIds = collect($request->opcoes_entrada_destino)->pluck('id')->all();
|
|
|
|
CentroCustoCompartilhado::where('opcao_entrada_id', $opcaoEntrada->id)
|
|
->whereNotIn('id', $centrosCustoCompartilhadoIds)
|
|
->delete();
|
|
|
|
OpcaoEntradaDestino::where('opcao_entrada_origem_id', $opcaoEntrada->id)
|
|
->whereNotIn('id', $opcoesEntradaDestinoIds)
|
|
->delete();
|
|
|
|
foreach ($request->centros_custo_compartilhado as $value) {
|
|
CentroCustoCompartilhado::updateOrCreate(
|
|
['id' => $value['id']],
|
|
[
|
|
'opcao_entrada_id' => $opcaoEntrada->id,
|
|
'centro_custo_id' => $value['centro_custo_id'],
|
|
]
|
|
);
|
|
}
|
|
|
|
foreach ($request->opcoes_entrada_destino as $value) {
|
|
OpcaoEntradaDestino::updateOrCreate(
|
|
['id' => $value['id']],
|
|
[
|
|
'opcao_entrada_origem_id' => $opcaoEntrada->id,
|
|
'opcao_entrada_destino_id' => $value['opcao_entrada_id'],
|
|
]
|
|
);
|
|
}
|
|
});
|
|
|
|
return $opcaoEntrada;
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return self::show($id)->delete();
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
switch (request()->get('listar')) {
|
|
case 'centros-custo':
|
|
return self::getCentrosCusto(request()->get('modulo_id'), request()->get('opcao_entrada_id'));
|
|
break;
|
|
|
|
case 'opcoes-entrada':
|
|
return self::getOpcoesEntrada(request()->get('modulo_id'), request()->get('centro_custo_id'), request()->get('opcao_entrada_id'));
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static function validate($request)
|
|
{
|
|
return $request->validate([
|
|
'centro_custo_id' => [
|
|
'required',
|
|
Rule::unique('\App\Models\PainelSenha\OpcaoEntrada')->where(function ($query) use ($request) {
|
|
return $query
|
|
->where('nome', $request->nome)
|
|
->where('sigla', $request->sigla)
|
|
->where('modulo_id', $request->modulo_id)
|
|
->where('faixa_etaria_id', $request->faixa_etaria_id)
|
|
->whereNull('deleted_at');
|
|
})->ignore($request->opcao_entrada),
|
|
],
|
|
'modulo_id' => [
|
|
'required',
|
|
'integer',
|
|
],
|
|
'nome' => [
|
|
'required',
|
|
'min:3',
|
|
'max:100',
|
|
],
|
|
'sigla' => [
|
|
'required',
|
|
'min:2',
|
|
'max:2',
|
|
],
|
|
]);
|
|
}
|
|
|
|
private static function getFormData($opcaoEntradaId = null)
|
|
{
|
|
return [
|
|
'centros_custo' => self::getCentrosCusto(),
|
|
'modulos' => self::getModulos(),
|
|
'faixas_etarias' => self::getFaixasEtarias(),
|
|
'centro_custo_compartilhado' => [
|
|
'centros_custo' => self::getCentrosCusto(),
|
|
],
|
|
'opcao_entrada_destino' => [
|
|
'modulos' => self::getModulos(true, $opcaoEntradaId),
|
|
'centros_custo' => [],
|
|
'opcoes_entrada' => [],
|
|
],
|
|
];
|
|
}
|
|
|
|
private static function getModelData($id = null)
|
|
{
|
|
$opcaoEntrada = self::show($id);
|
|
|
|
$centrosCustoCompartilhado = DB::table('painel_senha_centro_custo_compartilhado')
|
|
->select([
|
|
'painel_senha_centro_custo_compartilhado.id',
|
|
'cadastro_centro_custo.id AS centro_custo_id',
|
|
'cadastro_setor.nome AS setor_nome',
|
|
])
|
|
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_centro_custo_compartilhado.centro_custo_id')
|
|
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
|
->where('painel_senha_centro_custo_compartilhado.opcao_entrada_id', $opcaoEntrada->id ?? null)
|
|
->orderBy('cadastro_setor.nome')
|
|
->get();
|
|
|
|
$opcoesEntradaDestino = DB::table('painel_senha_opcao_entrada_destino')
|
|
->select([
|
|
'painel_senha_opcao_entrada_destino.id',
|
|
'administracao_modulo.id AS modulo_id',
|
|
'administracao_modulo.nome AS modulo_nome',
|
|
'cadastro_centro_custo.id AS centro_custo_id',
|
|
'cadastro_setor.nome AS setor_nome',
|
|
'opcao_entrada_destino.id AS opcao_entrada_id',
|
|
'opcao_entrada_destino.nome AS opcao_entrada_nome',
|
|
])
|
|
->join('painel_senha_opcao_entrada', 'painel_senha_opcao_entrada.id', 'painel_senha_opcao_entrada_destino.opcao_entrada_destino_id')
|
|
->join('administracao_modulo', 'administracao_modulo.id', 'painel_senha_opcao_entrada.modulo_id')
|
|
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
|
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
|
->join('painel_senha_opcao_entrada AS opcao_entrada_destino', 'opcao_entrada_destino.id', 'painel_senha_opcao_entrada_destino.opcao_entrada_destino_id')
|
|
->where('painel_senha_opcao_entrada_destino.opcao_entrada_origem_id', $opcaoEntrada->id ?? null)
|
|
->orderBy('opcao_entrada_destino.nome')
|
|
->get();
|
|
|
|
return [
|
|
'id' => $opcaoEntrada->id ?? null,
|
|
'centro_custo_id' => $opcaoEntrada->centro_custo_id ?? null,
|
|
'modulo_id' => $opcaoEntrada->modulo_id ?? null,
|
|
'faixa_etaria_id' => $opcaoEntrada->faixa_etaria_id ?? null,
|
|
'nome' => $opcaoEntrada->nome ?? null,
|
|
'prioridade_obrigatoria' => $opcaoEntrada->prioridade_obrigatoria ?? false,
|
|
'sigla' => $opcaoEntrada->sigla ?? null,
|
|
'centro_custo_compartilhado' => [
|
|
'centro_custo_id' => null,
|
|
],
|
|
'opcao_entrada_destino' => [
|
|
'centro_custo_id' => null,
|
|
'modulo_id' => null,
|
|
'opcao_entrada_id' => null,
|
|
],
|
|
'centros_custo_compartilhado' => $centrosCustoCompartilhado ?? [],
|
|
'opcoes_entrada_destino' => $opcoesEntradaDestino ?? [],
|
|
];
|
|
}
|
|
|
|
private static function getModulos($hasOpcaoEntrada = false, $opcaoEntradaId = null)
|
|
{
|
|
return DB::table('administracao_modulo')
|
|
->select([
|
|
'administracao_modulo.id',
|
|
'administracao_modulo.nome',
|
|
'administracao_modulo.identificador',
|
|
])
|
|
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
|
->from('painel_senha_parametrizacao_infraestrutura_modulo')
|
|
->join(
|
|
'painel_senha_parametrizacao_infraestrutura',
|
|
'painel_senha_parametrizacao_infraestrutura.id',
|
|
'painel_senha_parametrizacao_infraestrutura_modulo.parametrizacao_infraestrutura_id'
|
|
)
|
|
->where('painel_senha_parametrizacao_infraestrutura.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
|
->whereColumn('painel_senha_parametrizacao_infraestrutura_modulo.modulo_id', 'administracao_modulo.id')
|
|
)
|
|
->when($hasOpcaoEntrada, fn ($q) => $q->whereIn('administracao_modulo.identificador', [Modulo::FARMACIA, Modulo::IMUNIZACAO, Modulo::PAINEL_SENHA])
|
|
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
|
->from('painel_senha_opcao_entrada')
|
|
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
|
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
|
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
|
->when($opcaoEntradaId, fn ($q) => $q->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId))
|
|
->whereColumn('painel_senha_opcao_entrada.modulo_id', 'administracao_modulo.id')
|
|
)
|
|
)
|
|
->orderBy('administracao_modulo.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getCentrosCusto($moduloId = null, $opcaoEntradaId = null)
|
|
{
|
|
return DB::table('cadastro_centro_custo')
|
|
->select([
|
|
'cadastro_centro_custo.id AS centro_custo_id',
|
|
'cadastro_setor.nome AS setor_nome',
|
|
])
|
|
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
|
->whereNull('cadastro_centro_custo.deleted_at')
|
|
->when($moduloId, function ($query) use ($moduloId, $opcaoEntradaId) {
|
|
$query->whereExists(function($query) use ($moduloId, $opcaoEntradaId) {
|
|
$query->select(DB::raw(1))
|
|
->from('painel_senha_opcao_entrada')
|
|
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
|
->where('painel_senha_opcao_entrada.modulo_id', $moduloId)
|
|
->when($opcaoEntradaId, fn ($q) => $q->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId))
|
|
->whereColumn('painel_senha_opcao_entrada.centro_custo_id', 'cadastro_centro_custo.id');
|
|
});
|
|
})
|
|
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
|
->orderBy('cadastro_setor.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getOpcoesEntrada($moduloId, $centroCustoId, $opcaoEntradaId)
|
|
{
|
|
return DB::table('painel_senha_opcao_entrada')
|
|
->select([
|
|
'painel_senha_opcao_entrada.id',
|
|
'painel_senha_opcao_entrada.nome',
|
|
])
|
|
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
|
->where('painel_senha_opcao_entrada.modulo_id', $moduloId)
|
|
->where('painel_senha_opcao_entrada.centro_custo_id', $centroCustoId)
|
|
->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId)
|
|
->orderBy('painel_senha_opcao_entrada.nome')
|
|
->get();
|
|
}
|
|
|
|
private static function getFaixasEtarias()
|
|
{
|
|
return DB::table('geral_faixa_etaria')
|
|
->select([
|
|
'geral_faixa_etaria.id',
|
|
'geral_faixa_etaria.nome',
|
|
])
|
|
->orderBy('geral_faixa_etaria.nome')
|
|
->get();
|
|
}
|
|
}
|