This commit is contained in:
Lizandro Guarnizo
2026-01-28 23:38:21 -05:00
parent 861d507536
commit 622b99ecd5
50 changed files with 1646 additions and 19 deletions
@@ -27,6 +27,8 @@ class InventarioDistribucionTest extends TestCase
$producto = Producto::create(['nombre' => 'Camiseta', 'descripcion' => '', 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad', 'codigo_barras' => '', 'categoria_id' => $categoria->id, 'imagen' => '']);
$op = OrdenProduccion::create(['numero_orden' => 'OP-0001', 'prenda_modelo' => 'Camiseta', 'referencia' => 'C-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $producto->id]);
// Crear distribución
@@ -61,6 +63,8 @@ class InventarioDistribucionTest extends TestCase
$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)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$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
@@ -93,6 +97,8 @@ class InventarioDistribucionTest extends TestCase
$op = OrdenProduccion::create(['numero_orden' => 'OP-0200', 'prenda_modelo' => 'Doblez', 'referencia' => 'D-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
// Crear inventario: esto incrementa el stock global del producto en +5
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $producto->id]);
$producto->refresh();
@@ -122,6 +128,8 @@ class InventarioDistribucionTest extends TestCase
$op = OrdenProduccion::create(['numero_orden' => 'OP-0002', 'prenda_modelo' => 'Polo', 'referencia' => 'P-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$data = [
'orden_produccion_id' => $op->id,
'cantidad_terminada' => 5,
@@ -151,6 +159,8 @@ class InventarioDistribucionTest extends TestCase
$op = OrdenProduccion::create(['numero_orden' => 'OP-0003', 'prenda_modelo' => 'Camisa', 'referencia' => 'C-02', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $producto->id]);
// Crear primera distribución 2
@@ -177,6 +187,8 @@ class InventarioDistribucionTest extends TestCase
$op = OrdenProduccion::create(['numero_orden' => 'OP-0004', 'prenda_modelo' => 'Saco', 'referencia' => 'S-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $producto->id]);
// Crear dos distribuciones 2 y 2
@@ -198,5 +210,72 @@ class InventarioDistribucionTest extends TestCase
$d1->cantidad = 4;
$d1->save();
}
public function test_distribution_to_providers_decreases_disponible_and_does_not_create_bodega_stock()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Equipo', 'descripcion' => '', 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad', 'codigo_barras' => '', 'categoria_id' => $categoria->id, 'imagen' => '']);
$op = OrdenProduccion::create(['numero_orden' => 'OP-1000', 'prenda_modelo' => 'Equipo', 'referencia' => 'E-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 10, 'fecha_recepcion' => now()]);
// La creación del ProcesoAcabado puede crear automáticamente el inventario al finalizar la OP.
$inv = \App\Models\InventarioPrenda::where('orden_produccion_id', $op->id)->first();
$this->assertNotNull($inv);
$p1 = \App\Models\Proveedor::create(['nombre' => 'Proveedor 1']);
$p2 = \App\Models\Proveedor::create(['nombre' => 'Proveedor 2']);
// Crear distribuciones a proveedores
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'proveedor_id' => $p1->id,
'cantidad' => 4,
]);
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'proveedor_id' => $p2->id,
'cantidad' => 5,
]);
// No deben crearse filas en producto_bodega / variante_bodega
$this->assertEquals(0, \DB::table('producto_bodega')->count());
$this->assertEquals(0, \DB::table('variante_bodega')->count());
$inv->refresh();
$this->assertEquals(1, $inv->cantidad_disponible);
}
public function test_provider_distributions_exceeding_throws_validation_exception()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Surtido', 'descripcion' => '', 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad', 'codigo_barras' => '', 'categoria_id' => $categoria->id, 'imagen' => '']);
$op = OrdenProduccion::create(['numero_orden' => 'OP-2000', 'prenda_modelo' => 'Surtido', 'referencia' => 'S-02', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
\App\Models\ProcesoAcabado::create(['orden_produccion_id' => $op->id, 'tipo_proceso' => 'Acabado', 'cantidad_enviada' => 0, 'cantidad_recibida' => 5, 'fecha_recepcion' => now()]);
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $producto->id]);
$p = \App\Models\Proveedor::create(['nombre' => 'Proveedor X']);
// Primera distribución 3
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'proveedor_id' => $p->id,
'cantidad' => 3,
]);
$this->expectException(\Illuminate\Validation\ValidationException::class);
// Intentar crear 3 nuevamente => 3 + 3 = 6 > 5
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'proveedor_id' => $p->id,
'cantidad' => 3,
]);
}
}