Files
pos_heidiver/tests/Feature/InventarioDistribucionTest.php
T
2026-01-19 20:41:29 -05:00

138 lines
6.6 KiB
PHP

<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Bodega;
use App\Models\Color;
use App\Models\Size;
use App\Models\Producto;
use App\Models\OrdenProduccion;
use App\Models\InventarioPrenda;
use App\Models\InventarioPrendaDistribucion;
use App\Models\ProductVariant;
class InventarioDistribucionTest extends TestCase
{
use RefreshDatabase;
public function test_distribution_create_updates_variant_and_bodega_stock()
{
$bodega = Bodega::create(['nombre' => 'Bodega A']);
$color = Color::create(['name' => 'Rojo', 'hex_code' => '#ff0000']);
$size = Size::create(['name' => 'M']);
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$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)]);
$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
$dist = InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => $bodega->id,
'color_id' => $color->id,
'size_id' => $size->id,
'cantidad' => 3,
]);
// El evento debe haber creado la variante
$variant = ProductVariant::where('producto_id', $producto->id)->where('color_id', $color->id)->where('size_id', $size->id)->first();
$this->assertNotNull($variant, 'Variant should be created');
$this->assertEquals(3, $variant->stock);
// Revisar pivot variante_bodega
$pivot = \DB::table('variante_bodega')->where('variante_id', $variant->id)->where('bodega_id', $bodega->id)->first();
$this->assertNotNull($pivot);
$this->assertEquals(3, $pivot->stock);
}
public function test_creating_inventario_with_excessive_distributions_throws_validation_exception()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Polo', '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-0002', 'prenda_modelo' => 'Polo', 'referencia' => 'P-01', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
$data = [
'orden_produccion_id' => $op->id,
'cantidad_terminada' => 5,
'cantidad_disponible' => 5,
'fecha_ingreso' => now()->toDateString(),
'producto_id' => $producto->id,
'estado' => 'en_bodega',
'distribuciones' => [
['bodega_id' => 1, 'cantidad' => 3],
['bodega_id' => 1, 'cantidad' => 3],
],
];
$this->expectException(\Illuminate\Validation\ValidationException::class);
$page = new \App\Filament\Resources\InventarioPrendaResource\Pages\CreateInventarioPrenda();
$ref = new \ReflectionClass($page);
$method = $ref->getMethod('handleRecordCreation');
$method->setAccessible(true);
$method->invoke($page, $data);
}
public function test_creating_distribution_directly_exceeding_throws_validation_exception()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Camisa', '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-0003', 'prenda_modelo' => 'Camisa', 'referencia' => 'C-02', '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' => $producto->id]);
// Crear primera distribución 2
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => 1,
'cantidad' => 2,
]);
$this->expectException(\Illuminate\Validation\ValidationException::class);
// Intentar crear una segunda distribución que exceda (4) => 2 + 4 = 6 > 5
InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => 1,
'cantidad' => 4,
]);
}
public function test_updating_distribution_exceeding_throws_validation_exception()
{
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
$producto = Producto::create(['nombre' => 'Saco', '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-0004', 'prenda_modelo' => 'Saco', 'referencia' => 'S-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' => $producto->id]);
// Crear dos distribuciones 2 y 2
$d1 = InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => 1,
'cantidad' => 2,
]);
$d2 = InventarioPrendaDistribucion::create([
'inventario_prenda_id' => $inv->id,
'bodega_id' => 1,
'cantidad' => 2,
]);
$this->expectException(\Illuminate\Validation\ValidationException::class);
// Intentar actualizar d1 a 4 => 4 + 2 = 6 > 5
$d1->cantidad = 4;
$d1->save();
}
}