126 lines
5.3 KiB
PHP
126 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\WebService\Almoxarifado\Cliente;
|
|
|
|
use App\Models\WebService\Almoxarifado\Cliente\Material;
|
|
use App\Models\WebService\Almoxarifado\Cliente\Unidade;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PosicaoEstoqueService extends Service
|
|
{
|
|
public static function index($request)
|
|
{
|
|
self::validateIndex($request);
|
|
|
|
$conectaSusCentroCustoId = Unidade::find($request->query('unidade_id'))->conectasus_centro_custo_id;
|
|
|
|
$posicoesEstoques = DB::table('web_service_almoxarifado_cliente_material AS cliente_material')
|
|
->join(
|
|
'cadastro_material AS conectasus_material',
|
|
'conectasus_material.id',
|
|
'cliente_material.conectasus_material_id'
|
|
)
|
|
->leftJoin('almoxarifado_entrada_item AS entrada_item', fn ($j) => $j
|
|
->on('entrada_item.material_id', 'conectasus_material.id')
|
|
->whereExists(fn ($q) => $q
|
|
->selectRaw(1)
|
|
->from('almoxarifado_entrada AS entrada')
|
|
->whereColumn('entrada.id', 'entrada_item.entrada_id')
|
|
->where('entrada.finalizada', true)
|
|
->where('entrada.centro_custo_id', $conectaSusCentroCustoId)
|
|
)
|
|
)
|
|
->where(fn ($q) => $q
|
|
->where(fn ($q) => $q
|
|
->whereNull('conectasus_material.subgrupo_id')
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo')
|
|
->whereColumn('controle_estoque_grupo.grupo_id', 'conectasus_material.grupo_id')
|
|
->where('controle_estoque_grupo.centro_custo_id', $conectaSusCentroCustoId)
|
|
)
|
|
)
|
|
->orWhere(fn ($q) => $q
|
|
->whereNotNull('conectasus_material.subgrupo_id')
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('almoxarifado_parametrizacao_controle_estoque_subgrupo AS controle_estoque_subgrupo')
|
|
->join(
|
|
'almoxarifado_parametrizacao_controle_estoque_grupo AS controle_estoque_grupo',
|
|
'controle_estoque_grupo.id',
|
|
'controle_estoque_subgrupo.parametrizacao_controle_estoque_grupo_id'
|
|
)
|
|
->whereColumn('controle_estoque_subgrupo.subgrupo_id', 'conectasus_material.subgrupo_id')
|
|
->where('controle_estoque_grupo.centro_custo_id', $conectaSusCentroCustoId)
|
|
)
|
|
)
|
|
)
|
|
->when($request->query('material_id'), fn ($q, $id) => $q->where('cliente_material.id', $id))
|
|
->select([
|
|
'cliente_material.id',
|
|
'cliente_material.conectasus_material_id',
|
|
'cliente_material.nome',
|
|
DB::raw(
|
|
"COALESCE(SUM(
|
|
COALESCE(entrada_item.quantidade, 0)
|
|
-
|
|
COALESCE((
|
|
SELECT SUM(saida_item_entrada_item.quantidade_saida)
|
|
FROM almoxarifado_saida_item_entrada_item AS saida_item_entrada_item
|
|
JOIN almoxarifado_saida_item AS saida_item
|
|
ON saida_item.id = saida_item_entrada_item.saida_item_id
|
|
JOIN almoxarifado_saida AS saida
|
|
ON saida.id = saida_item.saida_id
|
|
WHERE saida_item_entrada_item.entrada_item_id = entrada_item.id
|
|
AND saida.finalizada = 1
|
|
), 0)
|
|
), 0) AS quantidade"
|
|
)
|
|
])
|
|
->groupBy([
|
|
'cliente_material.id',
|
|
'cliente_material.conectasus_material_id',
|
|
'cliente_material.nome',
|
|
])
|
|
->get();
|
|
|
|
return tap($posicoesEstoques)->each(function ($posicaoEstoque) {
|
|
|
|
$posicaoEstoque->quantidade = (int) $posicaoEstoque->quantidade;
|
|
});
|
|
}
|
|
|
|
private static function validateIndex($request)
|
|
{
|
|
$request->validate([
|
|
'unidade_id' => [
|
|
'bail',
|
|
'required',
|
|
'integer',
|
|
Rule::exists(Unidade::class, 'id'),
|
|
function ($attribute, $value, $fail) {
|
|
|
|
if (is_null(Unidade::find($value)->conectasus_centro_custo_id)) {
|
|
|
|
$fail("O campo {$attribute} selecionado não possui vínculo com um centro de custo do ConectaSUS.");
|
|
}
|
|
},
|
|
],
|
|
'material_id' => [
|
|
'bail',
|
|
'integer',
|
|
Rule::exists(Material::class, 'id'),
|
|
function ($attribute, $value, $fail) {
|
|
|
|
if (is_null(Material::find($value)->conectasus_material_id)) {
|
|
|
|
$fail("O campo {$attribute} selecionado não possui vínculo com um material do ConectaSUS.");
|
|
}
|
|
},
|
|
],
|
|
]);
|
|
}
|
|
}
|