This commit is contained in:
lizandrogd
2026-01-20 01:01:32 -05:00
parent 58ed9609e1
commit 3709078404
3 changed files with 87 additions and 7 deletions
@@ -85,6 +85,36 @@ class InventarioDistribucionTest extends TestCase
$this->assertEquals(2, $pivot->stock);
}
public function test_distribution_does_not_double_increment_product_stock()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Doblez', '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-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
$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();
$this->assertEquals(5, $producto->stock, 'Stock should be increased by inventario creation');
// Crear distribución: no debe aumentar nuevamente el stock global del producto
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => 1,
'color_id' => null,
'size_id' => null,
'cantidad' => 2,
]);
$producto->refresh();
$this->assertEquals(5, $producto->stock, 'Stock should NOT be increased by distribution creator (no double count)');
$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()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);