Skip to content

Commit 0f90297

Browse files
authored
Merge pull request #77 from chechojgb/buttonsLovers
add: agregar lista de inventariado
2 parents 9a945e5 + f0b7aec commit 0f90297

File tree

7 files changed

+356
-281
lines changed

7 files changed

+356
-281
lines changed

app/Http/Controllers/BLInventarioController.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,61 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\BlEmpaque;
6+
use App\Models\BlEstanteria;
7+
use App\Models\BlInventarioDetalle;
8+
use App\Models\BlProducto;
59
use Illuminate\Http\Request;
610
use Illuminate\Support\Facades\Auth;
711
use Illuminate\Support\Facades\DB;
812
use Inertia\Inertia;
913

1014
class BLInventarioController extends Controller
1115
{
16+
// public function index()
17+
// {
18+
// $user = Auth::user();
19+
// return Inertia::render('BLInventario', [
20+
// 'user' => $user,
21+
// ]);
22+
// }
1223
public function index()
1324
{
14-
$user = Auth::user();
25+
$productos = BlProducto::with([
26+
'color',
27+
'empaques.inventarioDetalle.posicion.nivel.estanteria'
28+
])
29+
->get()
30+
->map(function ($producto) {
31+
// Obtener todas las ubicaciones donde está este producto
32+
$ubicaciones = [];
33+
foreach ($producto->empaques as $empaque) {
34+
foreach ($empaque->inventarioDetalle as $inventario) {
35+
if ($inventario->posicion && $inventario->posicion->nivel && $inventario->posicion->nivel->estanteria) {
36+
$ubicaciones[] = $inventario->posicion->nivel->estanteria->nombre;
37+
}
38+
}
39+
}
40+
41+
// Si no tiene ubicación, mostrar "Sin ubicación"
42+
$estanteria = !empty($ubicaciones) ? implode(', ', array_unique($ubicaciones)) : 'Sin ubicación';
43+
44+
return [
45+
'id' => $producto->id,
46+
'tipo_producto' => $producto->tipo_producto,
47+
'tamanio' => $producto->tamanio,
48+
'color_nombre' => $producto->color->nombre,
49+
'descripcion' => $producto->descripcion,
50+
'stock_total' => $producto->empaques
51+
->where('estado', 'disponible')
52+
->sum('cantidad_unidades'),
53+
'estanteria' => $estanteria,
54+
'tiene_ubicacion' => !empty($ubicaciones),
55+
];
56+
});
57+
1558
return Inertia::render('BLInventario', [
16-
'user' => $user,
59+
'productos' => $productos,
1760
]);
1861
}
1962
}

app/Models/BlEmpaque.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,52 @@ public function pedidoItems(): HasMany
3939
{
4040
return $this->hasMany(BLPedidoItem::class, 'empaque_id');
4141
}
42+
public function inventarioDetalle()
43+
{
44+
return $this->hasMany(BlInventarioDetalle::class, 'empaque_id');
45+
}
46+
47+
// Relación directa con posiciones a través de inventario_detalle
48+
public function posiciones()
49+
{
50+
return $this->belongsToMany(BlPosicion::class, 'bl_inventario_detalle', 'empaque_id', 'posicion_id')
51+
->withPivot('cantidad_actual', 'estado', 'fecha_ubicacion')
52+
->withTimestamps();
53+
}
54+
55+
// Scope para empaques disponibles
56+
public function scopeDisponibles($query)
57+
{
58+
return $query->where('estado', 'disponible');
59+
}
60+
61+
// Scope para empaques sin ubicación
62+
public function scopeSinUbicacion($query)
63+
{
64+
return $query->whereDoesntHave('inventarioDetalle');
65+
}
66+
67+
// Scope para empaques con ubicación
68+
public function scopeConUbicacion($query)
69+
{
70+
return $query->whereHas('inventarioDetalle');
71+
}
72+
73+
// Método para verificar si tiene ubicación
74+
public function getTieneUbicacionAttribute()
75+
{
76+
return $this->inventarioDetalle()->exists();
77+
}
78+
79+
// Método para obtener la ubicación actual (si tiene)
80+
public function getUbicacionActualAttribute()
81+
{
82+
return $this->inventarioDetalle()->first();
83+
}
84+
85+
// Método para obtener la cantidad total en inventario
86+
public function getCantidadEnInventarioAttribute()
87+
{
88+
return $this->inventarioDetalle()->sum('cantidad_actual');
89+
}
4290
}

app/Models/BlEstanteria.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class BlEstanteria extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'bl_estanterias';
13+
protected $fillable = ['codigo', 'nombre', 'tipo', 'zona', 'capacidad_maxima', 'descripcion', 'activa', 'orden'];
14+
15+
// Relación con niveles
16+
public function niveles()
17+
{
18+
return $this->hasMany(BlNivelEstanteria::class, 'estanteria_id');
19+
}
20+
21+
// Relación directa con posiciones (a través de niveles)
22+
public function posiciones()
23+
{
24+
return $this->hasManyThrough(BlPosicion::class, BlNivelEstanteria::class, 'estanteria_id', 'nivel_id');
25+
}
26+
27+
// Relación con inventario detalle (a través de posiciones)
28+
public function inventarioDetalle()
29+
{
30+
return $this->hasManyThrough(BlInventarioDetalle::class, BlPosicion::class, 'nivel_id', 'posicion_id')
31+
->via('niveles');
32+
}
33+
34+
// Scope para estanterías activas
35+
public function scopeActivas($query)
36+
{
37+
return $query->where('activa', true);
38+
}
39+
40+
// Método para contar productos en esta estantería
41+
public function getTotalProductosAttribute()
42+
{
43+
return $this->inventarioDetalle()->count();
44+
}
45+
46+
// Método para contar unidades en esta estantería
47+
public function getTotalUnidadesAttribute()
48+
{
49+
return $this->inventarioDetalle()->sum('cantidad_actual');
50+
}
51+
}

app/Models/BlInventarioDetalle.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class BlInventarioDetalle extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'bl_inventario_detalle';
13+
protected $fillable = [
14+
'empaque_id', 'posicion_id', 'cantidad_actual',
15+
'fecha_ubicacion', 'fecha_vencimiento', 'estado', 'notas'
16+
];
17+
18+
protected $dates = ['fecha_ubicacion', 'fecha_vencimiento'];
19+
20+
// Relación con empaque
21+
public function empaque()
22+
{
23+
return $this->belongsTo(BlEmpaque::class, 'empaque_id');
24+
}
25+
26+
// Relación con posición
27+
public function posicion()
28+
{
29+
return $this->belongsTo(BlPosicion::class, 'posicion_id');
30+
}
31+
32+
// Relación con producto (a través de empaque)
33+
public function producto()
34+
{
35+
return $this->hasOneThrough(
36+
BlProducto::class,
37+
BlEmpaque::class,
38+
'id', // Foreign key on bl_empaques table
39+
'id', // Foreign key on bl_productos table
40+
'empaque_id', // Local key on bl_inventario_detalle table
41+
'producto_id' // Local key on bl_empaques table
42+
);
43+
}
44+
45+
// Scopes útiles
46+
public function scopeDisponibles($query)
47+
{
48+
return $query->where('estado', 'disponible');
49+
}
50+
51+
public function scopeEnEstanteria($query, $estanteriaId)
52+
{
53+
return $query->whereHas('posicion.nivel.estanteria', function($q) use ($estanteriaId) {
54+
$q->where('id', $estanteriaId);
55+
});
56+
}
57+
58+
public function scopeConProducto($query, $productoId)
59+
{
60+
return $query->whereHas('empaque', function($q) use ($productoId) {
61+
$q->where('producto_id', $productoId);
62+
});
63+
}
64+
}

app/Models/BlNivelEstanteria.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class BlNivelEstanteria extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'bl_niveles_estanteria';
13+
protected $fillable = ['estanteria_id', 'nivel', 'codigo', 'capacidad_maxima', 'orden', 'activo'];
14+
15+
// Relación con estantería
16+
public function estanteria()
17+
{
18+
return $this->belongsTo(BlEstanteria::class, 'estanteria_id');
19+
}
20+
21+
// Relación con posiciones
22+
public function posiciones()
23+
{
24+
return $this->hasMany(BlPosicion::class, 'nivel_id');
25+
}
26+
27+
// Relación con inventario detalle (a través de posiciones)
28+
public function inventarioDetalle()
29+
{
30+
return $this->hasManyThrough(BlInventarioDetalle::class, BlPosicion::class, 'nivel_id', 'posicion_id');
31+
}
32+
33+
public function scopeActivos($query)
34+
{
35+
return $query->where('activo', true);
36+
}
37+
}

app/Models/BlPosicion.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class BlPosicion extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'bl_posiciones';
13+
protected $fillable = ['nivel_id', 'posicion', 'codigo_completo', 'ancho', 'alto', 'profundidad', 'capacidad_maxima', 'ocupada', 'activa'];
14+
15+
// Relación con nivel
16+
public function nivel()
17+
{
18+
return $this->belongsTo(BlNivelEstanteria::class, 'nivel_id');
19+
}
20+
21+
// Relación con inventario detalle
22+
public function inventarioDetalle()
23+
{
24+
return $this->hasMany(BlInventarioDetalle::class, 'posicion_id');
25+
}
26+
27+
// Relación con empaques (a través de inventario detalle)
28+
public function empaques()
29+
{
30+
return $this->belongsToMany(BlEmpaque::class, 'bl_inventario_detalle', 'posicion_id', 'empaque_id')
31+
->withPivot('cantidad_actual', 'estado')
32+
->withTimestamps();
33+
}
34+
35+
public function scopeActivas($query)
36+
{
37+
return $query->where('activa', true);
38+
}
39+
40+
public function scopeDisponibles($query)
41+
{
42+
return $query->where('ocupada', false);
43+
}
44+
45+
// Método para obtener la cantidad actual ocupada
46+
public function getCantidadOcupadaAttribute()
47+
{
48+
return $this->inventarioDetalle()->sum('cantidad_actual');
49+
}
50+
}

0 commit comments

Comments
 (0)