This commit is contained in:
Lizandro Guarnizo
2026-01-18 13:04:24 -05:00
parent 7f97757038
commit c358774862
6 changed files with 110 additions and 1 deletions
+16
View File
@@ -12,6 +12,7 @@ class InventarioPrenda extends Model
'cantidad_disponible',
'fecha_ingreso',
'estado',
'producto_id',
];
public function ordenProduccion()
@@ -19,6 +20,11 @@ class InventarioPrenda extends Model
return $this->belongsTo(OrdenProduccion::class);
}
public function producto()
{
return $this->belongsTo(\App\Models\Producto::class, 'producto_id');
}
protected static function booted()
{
static::creating(function ($inventario) {
@@ -36,6 +42,16 @@ class InventarioPrenda extends Model
}
$op = $inventario->ordenProduccion;
// Priorizar producto explícito si fue seleccionado
if ($inventario->producto_id) {
$producto = \App\Models\Producto::find($inventario->producto_id);
if ($producto && ! $producto->variants()->exists()) {
$producto->increment('stock', $inventario->cantidad_terminada);
}
return;
}
if (! $op) {
return;
}
+27
View File
@@ -15,6 +15,7 @@ class OrdenProduccion extends Model
'referencia',
'cantidad_total',
'tela_id',
'producto_id',
'fecha_inicio',
'fecha_entrega_estimada',
'estado',
@@ -52,6 +53,11 @@ class OrdenProduccion extends Model
return $this->hasMany(\App\Models\InventarioPrenda::class);
}
public function producto()
{
return $this->belongsTo(\App\Models\Producto::class, 'producto_id');
}
public function avanzarEstado(): void
{
$flujo = [
@@ -78,13 +84,34 @@ class OrdenProduccion extends Model
// Crear inventario solo si hay traslados a bodega con cantidad
if ($cantidad > 0 && !\App\Models\InventarioPrenda::where('orden_produccion_id', $this->id)->exists()) {
// Determinar producto asociado: preferir producto_id en la OP, sino crear/buscar por prenda_modelo
$productoId = $this->producto_id;
if (! $productoId) {
$nombre = $this->prenda_modelo ?? $this->referencia ?? null;
if ($nombre) {
$producto = \App\Models\Producto::firstOrCreate(
['nombre' => $nombre],
['descripcion' => 'Creado automáticamente desde OrdenProduccion #' . $this->id, 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad']
);
$productoId = $producto->id;
// Guardar vínculo en la OP para futuras referencias
$this->producto_id = $productoId;
$this->save();
}
}
\App\Models\InventarioPrenda::create([
'orden_produccion_id' => $this->id,
'cantidad_terminada' => $cantidad,
'cantidad_disponible' => $cantidad,
'fecha_ingreso' => now(),
'estado' => 'en_bodega',
'producto_id' => $productoId ?? null,
]);
// Si se creó inventario, también actualizar stock del producto asociado (handler en InventarioPrenda lo hará)
}
}
}