diff --git a/app/Models/InventarioPrendaDistribucion.php b/app/Models/InventarioPrendaDistribucion.php index 84cb7a3..c82f075 100644 --- a/app/Models/InventarioPrendaDistribucion.php +++ b/app/Models/InventarioPrendaDistribucion.php @@ -89,8 +89,38 @@ class InventarioPrendaDistribucion extends Model if ($dist->color_id && $dist->size_id) { // Variante + // Determinar producto: preferir inventario.prenda.producto_id -> op.producto_id + $productoId = $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null); + + // Si no existe producto, intentar crear/inferrir desde la Orden de Producción + if (! $productoId && $dist->inventarioPrenda && $dist->inventarioPrenda->ordenProduccion) { + $op = $dist->inventarioPrenda->ordenProduccion; + $nombre = $op->prenda_modelo ?? $op->referencia ?? ('Producto OP #' . $op->id); + + $producto = \App\Models\Producto::firstOrCreate( + ['nombre' => $nombre], + [ + 'descripcion' => 'Creado automáticamente desde distribución (OP #' . $op->id . ')', + 'stock' => 0, + 'estado' => true, + 'precio_compra' => 0, + 'precio_venta' => 0, + 'unidad_medida' => 'unidad', + ] + ); + + $productoId = $producto->id; + + // Guardar producto en el inventario padre para futuras referencias + $parent = $dist->inventarioPrenda; + if ($parent && ! $parent->producto_id) { + $parent->producto_id = $productoId; + $parent->save(); + } + } + $variant = \App\Models\ProductVariant::firstOrCreate([ - 'producto_id' => $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null), + 'producto_id' => $productoId, 'color_id' => $dist->color_id, 'size_id' => $dist->size_id, ], ['stock' => 0, 'sku' => 'AUTO-'.uniqid(), 'barcode' => '']); diff --git a/resources/views/filament/orden_produccion/timeline.blade.php b/resources/views/filament/orden_produccion/timeline.blade.php index 6a2c11c..1200172 100644 --- a/resources/views/filament/orden_produccion/timeline.blade.php +++ b/resources/views/filament/orden_produccion/timeline.blade.php @@ -12,6 +12,12 @@ if ($op) { 'id' => $c->id, 'fecha' => $c->fecha_envio ?? $c->created_at, 'detalle' => "Enviado: {$c->cantidad_enviada} - Recibido total: {$c->recibido_total} - Defectos: {$c->prendas_defectuosas}", + 'link' => route('filament.admin.resources.confeccions.edit', ['record' => $c->id]), + ]; + + // Eventos por cada recepción + $recepciones = $c->recepciones()->orderBy('fecha_recepcion')->get(); + foreach ($recepciones as $r) { $events[] = [ 'tipo' => 'Recepción (Confección)', 'id' => $r->id, diff --git a/tests/Feature/InventarioDistribucionTest.php b/tests/Feature/InventarioDistribucionTest.php index eb424f0..ffc767e 100644 --- a/tests/Feature/InventarioDistribucionTest.php +++ b/tests/Feature/InventarioDistribucionTest.php @@ -48,6 +48,41 @@ class InventarioDistribucionTest extends TestCase $pivot = \DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodega->id)->first(); $this->assertNotNull($pivot); $this->assertEquals(3, $pivot->stock); + + // El inventario padre debe quedar ligado al producto si se creó automáticamente + $inv->refresh(); + $this->assertNotNull($inv->producto_id); + $this->assertEquals($variant->producto_id, $inv->producto_id); + } + + public function test_distribution_creates_and_links_product_when_missing() + { + $categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']); + + $op = OrdenProduccion::create(['numero_orden' => 'OP-0100', 'prenda_modelo' => 'PrendaX', 'referencia' => 'PX-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]); + + $inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => null]); + + // Crear distribución sin producto previo + $dist = InventarioPrendaDistribucion::create([ + 'inventario_prenda_id' => $inv->id, + 'bodega_id' => 1, + 'color_id' => null, + 'size_id' => null, + 'cantidad' => 2, + ]); + + // Verificar que se creó un producto y que el inventario padre quedó ligado + $inv->refresh(); + $this->assertNotNull($inv->producto_id); + + $producto = \App\Models\Producto::find($inv->producto_id); + $this->assertNotNull($producto); + + // Y que la distribución llevó stock al producto en la bodega + $pivot = \DB::table('producto_bodega')->where('producto_id', $producto->id)->where('bodega_id', 1)->first(); + $this->assertNotNull($pivot); + $this->assertEquals(2, $pivot->stock); } public function test_creating_inventario_with_excessive_distributions_throws_validation_exception()