up
This commit is contained in:
@@ -72,10 +72,8 @@ class InventarioPrendaResource extends Resource
|
||||
TextInput::make('cantidad_terminada')
|
||||
->numeric()
|
||||
->required()
|
||||
->afterStateUpdated(function ($state, $set) {
|
||||
// Mantener la cantidad disponible igual a la terminada al crear
|
||||
$set('cantidad_disponible', $state);
|
||||
}),
|
||||
->disabled()
|
||||
->helperText('Se calcula automáticamente desde los traslados a bodega asociados a la Orden de Producción.'),
|
||||
|
||||
Select::make('producto_id')
|
||||
->label('Producto (opcional)')
|
||||
|
||||
@@ -51,7 +51,7 @@ class InventarioPrenda extends Model
|
||||
}
|
||||
}
|
||||
|
||||
// Validación: la cantidad terminada no puede exceder lo recibido para esta OP (traslados a bodega)
|
||||
// Validación y auto-filling: la cantidad terminada se calcula automáticamente desde lo recibido para esta OP (traslados a bodega)
|
||||
if ($inventario->orden_produccion_id) {
|
||||
$totalRecibido = \App\Models\TrasladoPrenda::where('orden_produccion_id', $inventario->orden_produccion_id)
|
||||
->where('destino', 'bodega')
|
||||
@@ -61,11 +61,22 @@ class InventarioPrenda extends Model
|
||||
|
||||
$available = max(0, (int) $totalRecibido - (int) $assigned);
|
||||
|
||||
// Si no se proporcionó cantidad_terminada, asignar automáticamente lo disponible
|
||||
if (! isset($inventario->cantidad_terminada) || (int) $inventario->cantidad_terminada <= 0) {
|
||||
$inventario->cantidad_terminada = $available;
|
||||
}
|
||||
|
||||
if ((int) $inventario->cantidad_terminada > $available) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'cantidad_terminada' => 'No se puede registrar más cantidad terminada de la que está disponible desde acabados/traslados (' . $available . ').',
|
||||
]);
|
||||
}
|
||||
|
||||
if ((int) $inventario->cantidad_terminada <= 0) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'cantidad_terminada' => 'No hay cantidad terminada disponible para esta Orden de Producción.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Fecha de ingreso por defecto a hoy si no se proporcionó
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use App\Models\OrdenProduccion;
|
||||
use App\Models\InventarioPrenda;
|
||||
use App\Models\TrasladoPrenda;
|
||||
|
||||
class InventarioPrendaAutoCantidadTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_auto_fill_cantidad_terminada_from_traslados()
|
||||
{
|
||||
$op = OrdenProduccion::create(['numero_orden' => 'OP-500', 'prenda_modelo' => 'AutoCantidad', 'cantidad_total' => 100, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
|
||||
|
||||
// Crear dos traslados hacia bodega (simulando recibidos)
|
||||
TrasladoPrenda::create(['orden_produccion_id' => $op->id, 'origen' => 'acabados', 'destino' => 'bodega', 'cantidad_enviada' => 0, 'cantidad_recibida' => 20, 'fecha_recepcion' => now()]);
|
||||
TrasladoPrenda::create(['orden_produccion_id' => $op->id, 'origen' => 'acabados', 'destino' => 'bodega', 'cantidad_enviada' => 0, 'cantidad_recibida' => 10, 'fecha_recepcion' => now()]);
|
||||
|
||||
// Crear un inventario sin especificar cantidad_terminada
|
||||
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => null, 'fecha_ingreso' => now(), 'estado' => 'en_bodega']);
|
||||
|
||||
$this->assertEquals(30, $inv->cantidad_terminada);
|
||||
$this->assertEquals(30, $inv->cantidad_disponible);
|
||||
}
|
||||
|
||||
public function test_cannot_create_inventory_when_no_available()
|
||||
{
|
||||
$op = OrdenProduccion::create(['numero_orden' => 'OP-501', 'prenda_modelo' => 'AutoCantidad2', 'cantidad_total' => 100, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7)]);
|
||||
|
||||
$this->expectException(\Illuminate\Validation\ValidationException::class);
|
||||
|
||||
InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => null, 'fecha_ingreso' => now(), 'estado' => 'en_bodega']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user