135 lines
4.9 KiB
PHP
135 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
|
|
|
use App\Exceptions\WebService\Almoxarifado\ConectaSus\CentroCustoNotFoundException;
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Services\Service;
|
|
use App\Utils\DataUtil;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class CentroCustoService extends Service
|
|
{
|
|
public static function index($request)
|
|
{
|
|
self::validateIndex($request);
|
|
|
|
$centrosCusto = collect();
|
|
|
|
DB::table('cadastro_centro_custo AS centro_custo')
|
|
->join('cadastro_setor AS setor', 'setor.id', 'centro_custo.setor_id')
|
|
->join('cadastro_infraestrutura AS infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
|
|
->join('cadastro_unidade AS unidade', 'unidade.id', 'infraestrutura.unidade_id')
|
|
->whereNull('centro_custo.deleted_at')
|
|
->when(DataUtil::isStringBoolean($request->query('is_almoxarifado')), fn ($q) => $q
|
|
->when(DataUtil::isStringTrue($request->query('is_almoxarifado')),
|
|
fn ($q) => $q->whereExists(fn ($q) => $q
|
|
->selectRaw(1)
|
|
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
|
->whereColumn('controle_estoque_grupo.centro_custo_id', 'centro_custo.id')
|
|
),
|
|
fn ($q) => $q->whereNotExists(fn ($q) => $q
|
|
->selectRaw(1)
|
|
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
|
->whereColumn('controle_estoque_grupo.centro_custo_id', 'centro_custo.id')
|
|
)
|
|
)
|
|
)
|
|
->select([
|
|
'centro_custo.id',
|
|
'unidade.id as unidade_id',
|
|
'unidade.nome_fantasia AS unidade_nome',
|
|
'unidade.cnes AS unidade_cnes',
|
|
'infraestrutura.id as infraestrutura_id',
|
|
DB::raw("COALESCE(infraestrutura.nome, unidade.nome_fantasia) AS infraestrutura_nome"),
|
|
'setor.nome AS setor_nome',
|
|
])
|
|
->orderBy('unidade.nome_fantasia')
|
|
->orderByRaw("COALESCE(infraestrutura.nome, unidade.nome_fantasia)")
|
|
->orderBy('setor.nome')
|
|
->get()
|
|
->each(function ($centroCusto) use ($centrosCusto) {
|
|
|
|
$unidade = $centrosCusto->where('unidade_id', $centroCusto->unidade_id)->first();
|
|
|
|
if (is_null($unidade)) {
|
|
|
|
$unidade = (object) [
|
|
'unidade_id' => $centroCusto->unidade_id,
|
|
'unidade_nome' => $centroCusto->unidade_nome,
|
|
'unidade_cnes' => $centroCusto->unidade_cnes,
|
|
'locais_atendimento' => collect(),
|
|
];
|
|
|
|
$centrosCusto->push($unidade);
|
|
}
|
|
|
|
$infraestrutura = $unidade->locais_atendimento
|
|
->where('infraestrutura_id', $centroCusto->infraestrutura_id)
|
|
->first();
|
|
|
|
if (is_null($infraestrutura)) {
|
|
|
|
$infraestrutura = (object) [
|
|
'infraestrutura_id' => $centroCusto->infraestrutura_id,
|
|
'nome' => $centroCusto->infraestrutura_nome,
|
|
'setores' => collect(),
|
|
];
|
|
|
|
$unidade->locais_atendimento->push($infraestrutura);
|
|
}
|
|
|
|
$infraestrutura->setores->push((object) [
|
|
'centro_custo_id' => $centroCusto->id,
|
|
'nome' => $centroCusto->setor_nome,
|
|
]);
|
|
});
|
|
|
|
return tap($centrosCusto)->each(function ($centroCusto) {
|
|
|
|
unset($centroCusto->unidade_id);
|
|
|
|
$centroCusto->locais_atendimento->each(function ($localAtendimento) {
|
|
|
|
unset($localAtendimento->infraestrutura_id);
|
|
});
|
|
});
|
|
}
|
|
|
|
private static function validateIndex($request)
|
|
{
|
|
$request->validate([
|
|
'is_almoxarifado' => [
|
|
'string',
|
|
Rule::in('true', 'false'),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
$centroCusto = self::getCentroCustoModel($id);
|
|
|
|
return (object) [
|
|
'id' => $centroCusto->id,
|
|
'unidade_nome' => $centroCusto->infraestrutura->unidade->nome_fantasia,
|
|
'unidade_cnes' => $centroCusto->infraestrutura->unidade->cnes,
|
|
'local_atendimento_nome' => $centroCusto->infraestrutura->nome,
|
|
'setor_nome' => $centroCusto->setor->nome,
|
|
];
|
|
}
|
|
|
|
private static function getCentroCustoModel($id)
|
|
{
|
|
$centroCusto = CentroCusto::find($id);
|
|
|
|
if (is_null($centroCusto)) {
|
|
|
|
throw new CentroCustoNotFoundException();
|
|
}
|
|
|
|
return $centroCusto;
|
|
}
|
|
}
|