This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\FabricanteNotFoundException;
|
||||
use App\Models\Cadastro\Fabricante\Fabricante;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FabricanteService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_fabricante AS fabricante')
|
||||
->select([
|
||||
'fabricante.id',
|
||||
'fabricante.nome',
|
||||
'fabricante.cnpj',
|
||||
])
|
||||
->orderBy('fabricante.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$fabricante = self::getFabricanteModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $fabricante->id,
|
||||
'nome' => $fabricante->nome,
|
||||
'cnpj' => $fabricante->cnpj,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFabricanteModel($id)
|
||||
{
|
||||
$fabricante = Fabricante::find($id);
|
||||
|
||||
if (is_null($fabricante)) {
|
||||
|
||||
throw new FabricanteNotFoundException();
|
||||
}
|
||||
|
||||
return $fabricante;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\FornecedorNotFoundException;
|
||||
use App\Models\Cadastro\Fornecedor\Fornecedor;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FornecedorService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_fornecedor AS fornecedor')
|
||||
->select([
|
||||
'fornecedor.id',
|
||||
'fornecedor.nome_fantasia AS nome',
|
||||
'fornecedor.cnpj',
|
||||
])
|
||||
->orderBy('fornecedor.nome_fantasia')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$fornecedor = self::getFornecedorModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $fornecedor->id,
|
||||
'nome' => $fornecedor->nome_fantasia,
|
||||
'cnpj' => $fornecedor->cnpj,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFornecedorModel($id)
|
||||
{
|
||||
$fornecedor = Fornecedor::find($id);
|
||||
|
||||
if (is_null($fornecedor)) {
|
||||
|
||||
throw new FornecedorNotFoundException();
|
||||
}
|
||||
|
||||
return $fornecedor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Almoxarifado\ConectaSus;
|
||||
|
||||
use App\Exceptions\WebService\Almoxarifado\ConectaSus\MaterialNotFoundException;
|
||||
use App\Models\Cadastro\Material\Material;
|
||||
use App\Services\Cadastro\Material\MaterialService as CadastroMaterialService;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MaterialService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('cadastro_material AS material')
|
||||
->join(
|
||||
'cadastro_apresentacao AS apresentacao',
|
||||
'apresentacao.id',
|
||||
'material.apresentacao_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->whereNull('material.deleted_at')
|
||||
->where('material.is_ativo', true)
|
||||
->select([
|
||||
'material.id',
|
||||
'material.codigo_material AS codigo',
|
||||
DB::raw(CadastroMaterialService::getDescricaoCompletaRaw(
|
||||
'material',
|
||||
'apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " AS descricao"),
|
||||
])
|
||||
->orderByRaw(CadastroMaterialService::getDescricaoCompletaRaw(
|
||||
'material',
|
||||
'apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
))
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
$material = self::getMaterialModel($id);
|
||||
|
||||
return (object) [
|
||||
'id' => $material->id,
|
||||
'codigo' => $material->codigo_material,
|
||||
'descricao' => $material->descricao_completa,
|
||||
];
|
||||
}
|
||||
|
||||
private static function getMaterialModel($id)
|
||||
{
|
||||
$material = Material::find($id);
|
||||
|
||||
if (is_null($material)) {
|
||||
|
||||
throw new MaterialNotFoundException();
|
||||
}
|
||||
|
||||
return $material;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user