82 lines
3.8 KiB
PHP
82 lines
3.8 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);
|
|
}
|
|
}
|