This commit is contained in:
@@ -0,0 +1,752 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Almoxarifado\Relatorios;
|
||||
|
||||
use App\Enums\Cadastro\Grupo\GrupoTipo;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\Estabelecimento\Infraestrutura;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\Grupo\Grupo;
|
||||
use App\Models\Cadastro\Material\Material;
|
||||
use App\Models\Cadastro\Subgrupo\Subgrupo;
|
||||
use App\Services\Cadastro\Material\MaterialService;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PosicaoEstoqueService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
$materiais = collect();
|
||||
|
||||
$materiaisComEstoque = DB::table('cadastro_material')
|
||||
->select([
|
||||
'cadastro_material.id AS material_id',
|
||||
'cadastro_material.codigo_material AS material_codigo',
|
||||
'cadastro_material.is_programa_remedio_em_casa',
|
||||
'cadastro_material.is_alto_custo',
|
||||
'cadastro_material.is_acao_judicial',
|
||||
'cadastro_material.is_remume',
|
||||
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
||||
'cadastro_material',
|
||||
'cadastro_apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " AS material_descricao"),
|
||||
'cadastro_grupo.grupo_tipo_id',
|
||||
'cadastro_grupo.nome AS grupo_nome',
|
||||
'cadastro_subgrupo.nome AS subgrupo_nome',
|
||||
'almoxarifado_entrada_item.lote',
|
||||
DB::raw("DATE_FORMAT(almoxarifado_entrada_item.validade_lote, '%d/%m/%Y') AS validade_lote"),
|
||||
'almoxarifado_parametrizacao_estoque.estoque_minimo',
|
||||
'almoxarifado_parametrizacao_estoque.estoque_maximo',
|
||||
DB::raw("
|
||||
COALESCE(SUM(
|
||||
COALESCE(almoxarifado_entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(almoxarifado_saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item
|
||||
ON almoxarifado_saida_item.id = almoxarifado_saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida
|
||||
ON almoxarifado_saida.id = almoxarifado_saida_item.saida_id
|
||||
WHERE almoxarifado_saida_item_entrada_item.entrada_item_id = almoxarifado_entrada_item.id
|
||||
AND almoxarifado_saida.finalizada = 1
|
||||
), 0)
|
||||
), 0) AS quantidade
|
||||
"),
|
||||
'almoxarifado_entrada_item.id AS entrada_item_id',
|
||||
])
|
||||
->join('cadastro_grupo', 'cadastro_grupo.id', 'cadastro_material.grupo_id')
|
||||
->leftJoin('cadastro_apresentacao', 'cadastro_apresentacao.id', 'cadastro_material.apresentacao_id')
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'cadastro_material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->leftJoin('cadastro_subgrupo', 'cadastro_subgrupo.id', 'cadastro_material.subgrupo_id')
|
||||
->leftJoin('almoxarifado_entrada_item', function ($join) {
|
||||
|
||||
$join->on('almoxarifado_entrada_item.material_id', 'cadastro_material.id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_entrada')
|
||||
->whereColumn('almoxarifado_entrada.id', 'almoxarifado_entrada_item.entrada_id')
|
||||
->where('almoxarifado_entrada.finalizada', true)
|
||||
->where('almoxarifado_entrada.centro_custo_id', request()->centro_custo_id);
|
||||
});
|
||||
})
|
||||
->leftJoin('almoxarifado_parametrizacao_estoque', function ($join) {
|
||||
|
||||
$join->on('almoxarifado_parametrizacao_estoque.material_id', 'cadastro_material.id')
|
||||
->where('almoxarifado_parametrizacao_estoque.centro_custo_id', request()->centro_custo_id);
|
||||
})
|
||||
->when(request()->query('grupo_id'), fn ($q, $id) => $q->where('cadastro_grupo.id', $id))
|
||||
->when(request()->query('subgrupo_id'), fn ($q, $id) => $q->where('cadastro_subgrupo.id', $id))
|
||||
->when(request()->query('material_id'), fn ($q, $id) => $q->where('cadastro_material.id', $id))
|
||||
->whereNull('cadastro_material.deleted_at')
|
||||
->when(request()->query('status'), function ($query) {
|
||||
$sql = "
|
||||
COALESCE((
|
||||
SELECT COALESCE(SUM(
|
||||
COALESCE(almoxarifado_entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(almoxarifado_saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item
|
||||
ON almoxarifado_saida_item.id = almoxarifado_saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida
|
||||
ON almoxarifado_saida.id = almoxarifado_saida_item.saida_id
|
||||
WHERE almoxarifado_saida_item_entrada_item.entrada_item_id = almoxarifado_entrada_item.id
|
||||
AND almoxarifado_saida.finalizada = 1
|
||||
), 0)
|
||||
), 0)
|
||||
FROM almoxarifado_entrada_item
|
||||
INNER JOIN almoxarifado_entrada
|
||||
ON almoxarifado_entrada.id = almoxarifado_entrada_item.entrada_id
|
||||
WHERE almoxarifado_entrada.finalizada = 1
|
||||
AND almoxarifado_entrada.centro_custo_id = " . request()->centro_custo_id . "
|
||||
AND almoxarifado_entrada_item.material_id = cadastro_material.id
|
||||
), 0)
|
||||
";
|
||||
|
||||
$where = null;
|
||||
|
||||
switch (request()->status) {
|
||||
case 'abaixo_minimo':
|
||||
$where = "$sql < COALESCE(almoxarifado_parametrizacao_estoque.estoque_minimo, 0)";
|
||||
break;
|
||||
|
||||
case 'acima_maximo':
|
||||
$where = "$sql > COALESCE(almoxarifado_parametrizacao_estoque.estoque_maximo, 0)";
|
||||
break;
|
||||
|
||||
case 'normal':
|
||||
$where = "$sql > 0 AND $sql >= COALESCE(almoxarifado_parametrizacao_estoque.estoque_minimo, 0) AND $sql <= COALESCE(almoxarifado_parametrizacao_estoque.estoque_maximo, 0)";
|
||||
break;
|
||||
|
||||
case 'zerado':
|
||||
$where = "$sql = 0";
|
||||
break;
|
||||
}
|
||||
|
||||
$query->whereRaw($where);
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereNull('cadastro_material.subgrupo_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_grupo')
|
||||
->whereColumn('almoxarifado_parametrizacao_controle_estoque_grupo.grupo_id', 'cadastro_material.grupo_id')
|
||||
->where('almoxarifado_parametrizacao_controle_estoque_grupo.centro_custo_id', request()->centro_custo_id);
|
||||
});
|
||||
})
|
||||
->orWhere(function ($query) {
|
||||
|
||||
$query
|
||||
->whereNotNull('cadastro_material.subgrupo_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_subgrupo')
|
||||
->join(
|
||||
'almoxarifado_parametrizacao_controle_estoque_grupo',
|
||||
'almoxarifado_parametrizacao_controle_estoque_grupo.id',
|
||||
'almoxarifado_parametrizacao_controle_estoque_subgrupo.parametrizacao_controle_estoque_grupo_id'
|
||||
)
|
||||
->whereColumn('almoxarifado_parametrizacao_controle_estoque_subgrupo.subgrupo_id', 'cadastro_material.subgrupo_id')
|
||||
->where('almoxarifado_parametrizacao_controle_estoque_grupo.centro_custo_id', request()->centro_custo_id);
|
||||
});
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'cadastro_material.id',
|
||||
'cadastro_material.codigo_material',
|
||||
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
||||
'cadastro_material',
|
||||
'cadastro_apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
)),
|
||||
'cadastro_grupo.nome',
|
||||
'cadastro_subgrupo.nome',
|
||||
'almoxarifado_entrada_item.lote',
|
||||
DB::raw("DATE_FORMAT(almoxarifado_entrada_item.validade_lote, '%d/%m/%Y')"),
|
||||
'almoxarifado_parametrizacao_estoque.estoque_minimo',
|
||||
'almoxarifado_parametrizacao_estoque.estoque_maximo',
|
||||
])
|
||||
->havingRaw("
|
||||
COALESCE(SUM(
|
||||
COALESCE(almoxarifado_entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(almoxarifado_saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item
|
||||
ON almoxarifado_saida_item.id = almoxarifado_saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida
|
||||
ON almoxarifado_saida.id = almoxarifado_saida_item.saida_id
|
||||
WHERE almoxarifado_saida_item_entrada_item.entrada_item_id = almoxarifado_entrada_item.id
|
||||
AND almoxarifado_saida.finalizada = 1
|
||||
), 0)
|
||||
), 0) > 0
|
||||
");
|
||||
|
||||
$materiaisSemEstoque = DB::table('cadastro_material')
|
||||
->select([
|
||||
'cadastro_material.id AS material_id',
|
||||
'cadastro_material.codigo_material AS material_codigo',
|
||||
'cadastro_material.is_programa_remedio_em_casa',
|
||||
'cadastro_material.is_alto_custo',
|
||||
'cadastro_material.is_acao_judicial',
|
||||
'cadastro_material.is_remume',
|
||||
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
||||
'cadastro_material',
|
||||
'cadastro_apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " AS material_descricao"),
|
||||
'cadastro_grupo.grupo_tipo_id',
|
||||
'cadastro_grupo.nome AS grupo_nome',
|
||||
'cadastro_subgrupo.nome AS subgrupo_nome',
|
||||
DB::raw('NULL AS lote'),
|
||||
DB::raw('NULL AS validade_lote'),
|
||||
'almoxarifado_parametrizacao_estoque.estoque_minimo',
|
||||
'almoxarifado_parametrizacao_estoque.estoque_maximo',
|
||||
DB::raw('0 AS quantidade'),
|
||||
'almoxarifado_entrada_item.id AS entrada_item_id',
|
||||
])
|
||||
->join('cadastro_grupo', 'cadastro_grupo.id', 'cadastro_material.grupo_id')
|
||||
->leftJoin('cadastro_apresentacao', 'cadastro_apresentacao.id', 'cadastro_material.apresentacao_id')
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'cadastro_material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->leftJoin('cadastro_subgrupo', 'cadastro_subgrupo.id', 'cadastro_material.subgrupo_id')
|
||||
->leftJoin('almoxarifado_parametrizacao_estoque', function ($join) {
|
||||
|
||||
$join->on('almoxarifado_parametrizacao_estoque.material_id', 'cadastro_material.id')
|
||||
->where('almoxarifado_parametrizacao_estoque.centro_custo_id', request()->centro_custo_id);
|
||||
})
|
||||
->leftJoin('almoxarifado_entrada_item', 'almoxarifado_entrada_item.material_id', 'cadastro_material.id')
|
||||
->when(request()->query('grupo_id'), fn ($q, $id) => $q->where('cadastro_grupo.id', $id))
|
||||
->when(request()->query('subgrupo_id'), fn ($q, $id) => $q->where('cadastro_subgrupo.id', $id))
|
||||
->when(request()->query('material_id'), fn ($q, $id) => $q->where('cadastro_material.id', $id))
|
||||
->whereNull('cadastro_material.deleted_at')
|
||||
->when(request()->query('status'), function ($query) {
|
||||
$sql = "
|
||||
COALESCE((
|
||||
SELECT COALESCE(SUM(
|
||||
COALESCE(almoxarifado_entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(almoxarifado_saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item
|
||||
ON almoxarifado_saida_item.id = almoxarifado_saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida
|
||||
ON almoxarifado_saida.id = almoxarifado_saida_item.saida_id
|
||||
WHERE almoxarifado_saida_item_entrada_item.entrada_item_id = almoxarifado_entrada_item.id
|
||||
AND almoxarifado_saida.finalizada = 1
|
||||
), 0)
|
||||
), 0)
|
||||
FROM almoxarifado_entrada_item
|
||||
INNER JOIN almoxarifado_entrada
|
||||
ON almoxarifado_entrada.id = almoxarifado_entrada_item.entrada_id
|
||||
WHERE almoxarifado_entrada.finalizada = 1
|
||||
AND almoxarifado_entrada.centro_custo_id = " . request()->centro_custo_id . "
|
||||
AND almoxarifado_entrada_item.material_id = cadastro_material.id
|
||||
), 0)
|
||||
";
|
||||
|
||||
$where = null;
|
||||
|
||||
switch (request()->status) {
|
||||
case 'abaixo_minimo':
|
||||
$where = "$sql < COALESCE(almoxarifado_parametrizacao_estoque.estoque_minimo, 0)";
|
||||
break;
|
||||
|
||||
case 'acima_maximo':
|
||||
$where = "$sql > COALESCE(almoxarifado_parametrizacao_estoque.estoque_maximo, 0)";
|
||||
break;
|
||||
|
||||
case 'normal':
|
||||
$where = "$sql > 0 AND $sql >= COALESCE(almoxarifado_parametrizacao_estoque.estoque_minimo, 0) AND $sql <= COALESCE(almoxarifado_parametrizacao_estoque.estoque_maximo, 0)";
|
||||
break;
|
||||
|
||||
case 'zerado':
|
||||
$where = "$sql = 0";
|
||||
break;
|
||||
}
|
||||
|
||||
$query->whereRaw($where);
|
||||
})
|
||||
->whereNotExists(function ($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('almoxarifado_entrada_item')
|
||||
->join('almoxarifado_entrada', 'almoxarifado_entrada.id', 'almoxarifado_entrada_item.entrada_id')
|
||||
->where('almoxarifado_entrada.finalizada', true)
|
||||
->where('almoxarifado_entrada.centro_custo_id', request()->centro_custo_id)
|
||||
->whereRaw("
|
||||
COALESCE((
|
||||
COALESCE(almoxarifado_entrada_item.quantidade, 0)
|
||||
-
|
||||
COALESCE((
|
||||
SELECT SUM(almoxarifado_saida_item_entrada_item.quantidade_saida)
|
||||
FROM almoxarifado_saida_item_entrada_item
|
||||
JOIN almoxarifado_saida_item
|
||||
ON almoxarifado_saida_item.id = almoxarifado_saida_item_entrada_item.saida_item_id
|
||||
JOIN almoxarifado_saida
|
||||
ON almoxarifado_saida.id = almoxarifado_saida_item.saida_id
|
||||
WHERE almoxarifado_saida_item_entrada_item.entrada_item_id = almoxarifado_entrada_item.id
|
||||
AND almoxarifado_saida.finalizada = 1
|
||||
), 0)
|
||||
), 0) > 0
|
||||
")
|
||||
->whereColumn('almoxarifado_entrada_item.material_id', 'cadastro_material.id');
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where(function ($query) {
|
||||
|
||||
$query
|
||||
->whereNull('cadastro_material.subgrupo_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_grupo')
|
||||
->whereColumn('almoxarifado_parametrizacao_controle_estoque_grupo.grupo_id', 'cadastro_material.grupo_id')
|
||||
->where('almoxarifado_parametrizacao_controle_estoque_grupo.centro_custo_id', request()->centro_custo_id);
|
||||
});
|
||||
})
|
||||
->orWhere(function ($query) {
|
||||
|
||||
$query
|
||||
->whereNotNull('cadastro_material.subgrupo_id')
|
||||
->whereExists(function ($query) {
|
||||
|
||||
$query
|
||||
->select(DB::raw(1))
|
||||
->from('almoxarifado_parametrizacao_controle_estoque_subgrupo')
|
||||
->join(
|
||||
'almoxarifado_parametrizacao_controle_estoque_grupo',
|
||||
'almoxarifado_parametrizacao_controle_estoque_grupo.id',
|
||||
'almoxarifado_parametrizacao_controle_estoque_subgrupo.parametrizacao_controle_estoque_grupo_id'
|
||||
)
|
||||
->whereColumn('almoxarifado_parametrizacao_controle_estoque_subgrupo.subgrupo_id', 'cadastro_material.subgrupo_id')
|
||||
->where('almoxarifado_parametrizacao_controle_estoque_grupo.centro_custo_id', request()->centro_custo_id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$materiaisComEstoque
|
||||
->union($materiaisSemEstoque)
|
||||
->get()
|
||||
->each(function ($query) use ($materiais) {
|
||||
$material = $materiais->where('material_id', $query->material_id)->first();
|
||||
if (is_null($material)) {
|
||||
$material = (object) [
|
||||
'material_id' => $query->material_id,
|
||||
'material_codigo' => $query->material_codigo,
|
||||
'material_descricao' => $query->material_descricao,
|
||||
'is_programa_remedio_em_casa' => $query->is_programa_remedio_em_casa,
|
||||
'is_alto_custo' => $query->is_alto_custo,
|
||||
'is_acao_judicial' => $query->is_acao_judicial,
|
||||
'is_remume' => $query->is_remume,
|
||||
'grupo_tipo_id' => $query->grupo_tipo_id,
|
||||
'grupo_nome' => $query->grupo_nome,
|
||||
'subgrupo_nome' => $query->subgrupo_nome,
|
||||
'lotes' => collect(),
|
||||
'estoque_minimo' => $query->estoque_minimo ?? 0,
|
||||
'estoque_maximo' => $query->estoque_maximo ?? 0,
|
||||
'estoque_disponivel' => 0,
|
||||
'status' => null,
|
||||
];
|
||||
|
||||
$materiais->push($material);
|
||||
}
|
||||
|
||||
$material->estoque_disponivel += $query->quantidade;
|
||||
|
||||
if (is_null($query->lote) && is_null($query->validade_lote) && $query->quantidade == 0) {
|
||||
$loteExistente = $material->lotes->firstWhere('lote', '-');
|
||||
if ($loteExistente) {
|
||||
$loteExistente->quantidade += $query->quantidade;
|
||||
} else {
|
||||
$material->lotes->push((object) [
|
||||
'lote' => '-',
|
||||
'validade_lote' => '-',
|
||||
'quantidade' => $query->quantidade,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$material->lotes->push((object) [
|
||||
'lote' => $query->lote ?? '-',
|
||||
'validade_lote' => $query->validade_lote ?? '-',
|
||||
'quantidade' => $query->quantidade,
|
||||
]);
|
||||
}
|
||||
|
||||
$programa = collect([]);
|
||||
|
||||
if($material->grupo_tipo_id === GrupoTipo::MEDICAMENTO) {
|
||||
if($material->is_programa_remedio_em_casa) $programa->push("Programa remédio em casa");
|
||||
if($material->is_alto_custo) $programa->push("Alto custo");
|
||||
if($material->is_acao_judicial) $programa->push("Ação judicial");
|
||||
if($material->is_remume) $programa->push("REMUME");
|
||||
}
|
||||
|
||||
$material->programa = $programa->isNotEmpty() ? implode(', ', $programa->toArray()) : '-';
|
||||
});
|
||||
|
||||
$materiais->each(function ($query) {
|
||||
if ($query->estoque_disponivel < $query->estoque_minimo) {
|
||||
$query->status = ['value' => 'abaixo_minimo', 'text' => 'Abaixo do mínimo'];
|
||||
}
|
||||
else if ($query->estoque_disponivel > $query->estoque_maximo) {
|
||||
$query->status = ['value' => 'acima_maximo', 'text' => 'Acima do máximo'];
|
||||
}
|
||||
else {
|
||||
$query->status = ['value' => 'normal', 'text' => 'Normal'];
|
||||
}
|
||||
});
|
||||
|
||||
$sortKey = self::setOrderBy(request()->sortBy);
|
||||
|
||||
$desc = DataUtil::getBooleanValueFromStringBoolean(request()->sortDesc);
|
||||
|
||||
$materiais = collect($materiais);
|
||||
|
||||
$materiais = $desc ? $materiais->sortByDesc($sortKey) : $materiais->sortBy($sortKey);
|
||||
|
||||
$materiais = $materiais->values();
|
||||
|
||||
return $materiais;
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
$data = (object) [
|
||||
'form' => [],
|
||||
'model' => [],
|
||||
];
|
||||
|
||||
$params = self::getParams();
|
||||
|
||||
$sets = [
|
||||
'unidade' => function () use ($data, $params) {
|
||||
|
||||
$unidadesData = self::getUnidadesData($params);
|
||||
|
||||
$data->form['unidades'] = $unidadesData['form'];
|
||||
$data->model['unidade_id'] = $params->unidade_id = $unidadesData['model'];
|
||||
},
|
||||
'infraestrutura' => function () use ($data, $params) {
|
||||
|
||||
$infraestruturasData = self::getInfraestruturasData($params);
|
||||
|
||||
$data->form['infraestruturas'] = $infraestruturasData['form'];
|
||||
$data->model['infraestrutura_id'] = $params->infraestrutura_id = $infraestruturasData['model'];
|
||||
},
|
||||
'centro_custo' => function () use ($data, $params) {
|
||||
|
||||
$centrosDeCustoData = self::getCentrosDeCustoData($params);
|
||||
|
||||
$data->form['centros_custo'] = $centrosDeCustoData['form'];
|
||||
$data->model['centro_custo_id'] = $params->centro_custo_id = $centrosDeCustoData['model'];
|
||||
},
|
||||
'grupo' => function () use ($data, $params) {
|
||||
|
||||
$gruposData = self::getGruposData($params);
|
||||
|
||||
$data->form['grupos'] = $gruposData['form'];
|
||||
$data->model['grupo_id'] = $params->grupo_id = $gruposData['model'];
|
||||
},
|
||||
'subgrupo' => function () use ($data, $params) {
|
||||
|
||||
$subgruposData = self::getSubgruposData($params);
|
||||
|
||||
$data->form['subgrupos'] = $subgruposData['form'];
|
||||
$data->model['subgrupo_id'] = $params->subgrupo_id = $subgruposData['model'];
|
||||
},
|
||||
'material' => function () use ($data) {
|
||||
|
||||
$data->model['material'] = null;
|
||||
},
|
||||
'status' => function () use ($data, $params) {
|
||||
$statusData = self::getStatusData();
|
||||
|
||||
$data->form['status'] = $statusData['form'];
|
||||
$data->model['status'] = $params->status = $statusData['model'];
|
||||
},
|
||||
];
|
||||
|
||||
$load = is_null(request()->load_from);
|
||||
|
||||
foreach ($sets as $field => $set) {
|
||||
|
||||
if ($load) {
|
||||
|
||||
$set();
|
||||
}
|
||||
else if ($field === request()->load_from) {
|
||||
|
||||
$load = true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $property => $itens) {
|
||||
|
||||
if (empty($itens)) {
|
||||
|
||||
unset($data->{$property});
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private static function setOrderBy($sortBy)
|
||||
{
|
||||
if (is_null($sortBy)) {
|
||||
return 'estoque_minimo';
|
||||
}
|
||||
|
||||
$columns = [
|
||||
'materialDescricao' => 'material_descricao',
|
||||
'estoqueMinimo' => 'estoque_minimo',
|
||||
'estoqueMaximo' => 'estoque_maximo',
|
||||
'estoqueDisponivel' => 'estoque_disponivel',
|
||||
'status' => 'status',
|
||||
];
|
||||
|
||||
return $columns[$sortBy] ?? 'estoque_minimo';
|
||||
}
|
||||
|
||||
private static function getParams()
|
||||
{
|
||||
$params = (object) request()->only([
|
||||
'unidade_id',
|
||||
'infraestrutura_id',
|
||||
'centro_custo_id',
|
||||
'grupo_id',
|
||||
'subgrupo_id',
|
||||
]);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private static function getUnidadesData($params)
|
||||
{
|
||||
$unidades = Unidade::setEagerLoads([])
|
||||
->whereHas('infraestruturas.centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
->orderBy('nome_fantasia')
|
||||
->get(['id', 'nome_fantasia']);
|
||||
|
||||
return ['form' => $unidades, 'model' => null];
|
||||
}
|
||||
|
||||
private static function getInfraestruturasData($params)
|
||||
{
|
||||
$infraestruturas = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->unidade_id)) {
|
||||
|
||||
$infraestruturas = Infraestrutura::setEagerLoads([])
|
||||
->where('unidade_id', $params->unidade_id)
|
||||
->whereHas('centrosCusto', fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
->get(['id', 'nome', 'principal', 'unidade_id'])
|
||||
->map(fn ($i) => ['id' => $i->id, 'nome' => $i->nome])
|
||||
->sortBy('nome')
|
||||
->values();
|
||||
|
||||
if ($infraestruturas->count() === 1) {
|
||||
$id = $infraestruturas->first()['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return ['form' => $infraestruturas, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getCentrosDeCustoData($params)
|
||||
{
|
||||
$centrosCusto = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->infraestrutura_id)) {
|
||||
|
||||
$centrosCusto = CentroCusto::setEagerLoads([])
|
||||
->where('infraestrutura_id', $params->infraestrutura_id)
|
||||
->where(fn ($q) => self::setWhereForFilterCentroCusto($q, $params))
|
||||
->get(['id', 'setor_id'])
|
||||
->map(fn ($cc) => [
|
||||
'id' => $cc->id,
|
||||
'setor_nome' => $cc->setor()->setEagerLoads([])->first('nome')->nome
|
||||
])
|
||||
->sortBy('setor_nome')
|
||||
->values();
|
||||
|
||||
if ($centrosCusto->count() === 1) {
|
||||
$id = $centrosCusto->first()['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return ['form' => $centrosCusto, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function setWhereForFilterCentroCusto($query)
|
||||
{
|
||||
return $query->has('almoxarifadoParametrizacoesControleEstoqueGrupo');
|
||||
}
|
||||
|
||||
private static function getGruposData($params)
|
||||
{
|
||||
$grupos = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->unidade_id)) {
|
||||
|
||||
$grupos = Grupo::setEagerLoads([])
|
||||
->whereHas('almoxarifadoParametrizacoesControleEstoqueGrupo', fn ($q) => self::setWhereForFilterControlePermissaoGrupo($q, $params))
|
||||
->orderBy('nome')
|
||||
->get(['id', 'nome']);
|
||||
}
|
||||
|
||||
return ['form' => $grupos, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getSubgruposData($params)
|
||||
{
|
||||
$subgrupos = collect();
|
||||
$id = null;
|
||||
|
||||
if (!is_null($params->grupo_id)) {
|
||||
|
||||
$subgrupos = Subgrupo::setEagerLoads([])
|
||||
->where('grupo_id', $params->grupo_id)
|
||||
->whereHas('almoxarifadoParametrizacoesControleEstoqueSubgrupo.parametrizacaoControleEstoqueGrupo', fn ($q) =>
|
||||
self::setWhereForFilterControlePermissaoGrupo($q, $params)
|
||||
)
|
||||
->orderBy('nome')
|
||||
->get(['id', 'nome']);
|
||||
}
|
||||
|
||||
return ['form' => $subgrupos, 'model' => $id];
|
||||
}
|
||||
|
||||
private static function getStatusData()
|
||||
{
|
||||
$status = [
|
||||
['value' => 'abaixo_minimo', 'text' => 'Abaixo do mínimo'],
|
||||
['value' => 'acima_maximo', 'text' => 'Acima do máximo'],
|
||||
['value' => 'normal', 'text' => 'Normal'],
|
||||
['value' => 'zerado', 'text' => 'Zerado'],
|
||||
];
|
||||
|
||||
return ['form' => $status, 'model' => null];
|
||||
}
|
||||
|
||||
public static function setWhereForFilterControlePermissaoGrupo($query, $params)
|
||||
{
|
||||
$query->where('centro_custo_id', $params->centro_custo_id);
|
||||
}
|
||||
|
||||
public static function autocompleteMaterial()
|
||||
{
|
||||
$params = self::getParams();
|
||||
|
||||
$materiais = Material::setEagerLoads([])
|
||||
->when($params->grupo_id, fn ($q, $grupoId) => $q->where('grupo_id', $grupoId))
|
||||
->when($params->subgrupo_id, fn ($q, $subgrupoId) => $q->where('subgrupo_id', $subgrupoId))
|
||||
->where('cadastro_material.deleted_at', null)
|
||||
->where(fn ($q) =>
|
||||
$q->where(fn ($q) =>
|
||||
$q->whereNull('subgrupo_id')
|
||||
->whereHas('grupo.almoxarifadoParametrizacoesControleEstoqueGrupo', fn ($q) =>
|
||||
self::setWhereForFilterControlePermissaoGrupo($q, $params)
|
||||
)
|
||||
)
|
||||
->orWhere(fn ($q) =>
|
||||
$q->whereNotNull('subgrupo_id')
|
||||
->whereHas('subgrupo.almoxarifadoParametrizacoesControleEstoqueSubgrupo.parametrizacaoControleEstoqueGrupo', fn ($q) =>
|
||||
self::setWhereForFilterControlePermissaoGrupo($q, $params)
|
||||
)
|
||||
)
|
||||
)
|
||||
->getQuery()
|
||||
->join(
|
||||
'cadastro_apresentacao',
|
||||
'cadastro_apresentacao.id',
|
||||
'cadastro_material.apresentacao_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'cadastro_unidade_medida as apresentacao_unidade_medida',
|
||||
'apresentacao_unidade_medida.id',
|
||||
'cadastro_material.apresentacao_unidade_medida_id'
|
||||
)
|
||||
->select(
|
||||
'cadastro_material.id',
|
||||
'cadastro_material.codigo_material',
|
||||
DB::raw(MaterialService::getDescricaoCompletaRaw(
|
||||
'cadastro_material',
|
||||
'cadastro_apresentacao',
|
||||
'apresentacao_unidade_medida'
|
||||
) . " as descricao"),
|
||||
'cadastro_material.possui_lote'
|
||||
);
|
||||
|
||||
return $materiais;
|
||||
}
|
||||
|
||||
public static function getPrintData()
|
||||
{
|
||||
$unidade = Unidade::find(request()->unidade_id);
|
||||
$infraestrutura = Infraestrutura::find(request()->infraestrutura_id);
|
||||
$centroCusto = CentroCusto::find(request()->centro_custo_id);
|
||||
$grupo = Grupo::find(request()->grupo_id);
|
||||
$subgrupo = Subgrupo::find(request()->subgrupo_id);
|
||||
$material = Material::find(request()->material_id);
|
||||
|
||||
$status = 'Todos';
|
||||
|
||||
switch (request()->status) {
|
||||
case 'abaixo_minimo':
|
||||
$status = 'Abaixo do mínimo';
|
||||
break;
|
||||
|
||||
case 'acima_maximo':
|
||||
$status = 'Acima do máximo';
|
||||
break;
|
||||
|
||||
case 'normal':
|
||||
$status = 'Normal';
|
||||
break;
|
||||
|
||||
case 'zerado':
|
||||
$status = 'Zerado';
|
||||
break;
|
||||
}
|
||||
|
||||
return [
|
||||
'unidade_nome_fantasia' => $unidade->nome_fantasia ?? 'Todas',
|
||||
'infraestrutura_nome' => $infraestrutura->nome ?? 'Todos',
|
||||
'setor_nome' => $centroCusto->setor->nome ?? 'Todos',
|
||||
'grupo_nome' => $grupo->nome ?? 'Todos',
|
||||
'subgrupo_nome' => $subgrupo->nome ?? 'Todos',
|
||||
'material_descricao' => $material->descricao_completa ?? 'Todos',
|
||||
'status' => $status,
|
||||
'itens' => self::index(),
|
||||
'colunas' => collect(request()->selectedCols),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user