This commit is contained in:
Lizandro Guarnizo
2026-01-18 11:21:19 -05:00
parent 18c43fe06f
commit 33688d297c
21 changed files with 958 additions and 14 deletions
+24
View File
@@ -25,6 +25,11 @@ class OrdenProduccion extends Model
return $this->belongsTo(Tela::class);
}
public function traslados()
{
return $this->hasMany(\App\Models\TrasladoPrenda::class);
}
public function avanzarEstado(): void
{
$flujo = [
@@ -41,6 +46,25 @@ class OrdenProduccion extends Model
$this->update([
'estado' => $flujo[$actual + 1],
]);
// Si la orden llega a finalizada, crear entrada en inventario con lo que haya ingresado
if ($this->estado === 'finalizada') {
// Sumar traslados cuyo destino sea 'bodega'
$cantidad = \App\Models\TrasladoPrenda::where('orden_produccion_id', $this->id)
->where('destino', 'bodega')
->sum('cantidad_recibida');
// Crear inventario solo si hay traslados a bodega con cantidad
if ($cantidad > 0 && !\App\Models\InventarioPrenda::where('orden_produccion_id', $this->id)->exists()) {
\App\Models\InventarioPrenda::create([
'orden_produccion_id' => $this->id,
'cantidad_terminada' => $cantidad,
'cantidad_disponible' => $cantidad,
'fecha_ingreso' => now(),
'estado' => 'en_bodega',
]);
}
}
}
}