up
This commit is contained in:
@@ -130,6 +130,29 @@ class OrdenProduccion extends Model
|
|||||||
if (! isset($orden->fecha_inicio) || ! $orden->fecha_inicio) {
|
if (! isset($orden->fecha_inicio) || ! $orden->fecha_inicio) {
|
||||||
$orden->fecha_inicio = now()->toDateString();
|
$orden->fecha_inicio = now()->toDateString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reserva de tela al crear cuando la OP ya nace en 'en_corte'
|
||||||
|
$estadoInicial = $orden->estado ?? 'en_corte';
|
||||||
|
if ($estadoInicial === 'en_corte' && $orden->tela_id && ($orden->metros_requeridos ?? 0) > 0) {
|
||||||
|
$tela = \App\Models\Tela::find($orden->tela_id);
|
||||||
|
if (! $tela) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'tela_id' => 'No se encontró la tela asociada.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tela->metros_disponibles < $orden->metros_requeridos) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'metros_requeridos' => 'No hay suficientes metros disponibles para reservar (' . $tela->metros_disponibles . ').',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reservar: restar metros y marcar orden
|
||||||
|
$tela->metros_disponibles = $tela->metros_disponibles - $orden->metros_requeridos;
|
||||||
|
$tela->save();
|
||||||
|
|
||||||
|
$orden->tela_reservada = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validar y reservar tela al cambiar de estado a 'en_corte'
|
// Validar y reservar tela al cambiar de estado a 'en_corte'
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use App\Models\OrdenProduccion;
|
||||||
|
use App\Models\Tela;
|
||||||
|
|
||||||
|
class OrdenProduccionTelaReservationTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_reserves_tela_metros_when_created_in_en_corte()
|
||||||
|
{
|
||||||
|
$tela = Tela::create([
|
||||||
|
'nombre' => 'Tela A',
|
||||||
|
'metros_disponibles' => 100,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$orden = OrdenProduccion::create([
|
||||||
|
'prenda_modelo' => 'Camisa X',
|
||||||
|
'referencia' => 'REF-1',
|
||||||
|
'cantidad_total' => 10,
|
||||||
|
'tela_id' => $tela->id,
|
||||||
|
'metros_requeridos' => 30,
|
||||||
|
'estado' => 'en_corte',
|
||||||
|
'fecha_inicio' => now()->toDateString(),
|
||||||
|
'fecha_entrega_estimada' => now()->addDays(7)->toDateString(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tela->refresh();
|
||||||
|
$this->assertEquals(70, $tela->metros_disponibles);
|
||||||
|
|
||||||
|
$orden->refresh();
|
||||||
|
$this->assertTrue((bool) $orden->tela_reservada);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_fails_if_not_enough_meters_on_create()
|
||||||
|
{
|
||||||
|
$this->expectException(\Illuminate\Validation\ValidationException::class);
|
||||||
|
|
||||||
|
$tela = Tela::create([
|
||||||
|
'nombre' => 'Tela B',
|
||||||
|
'metros_disponibles' => 20,
|
||||||
|
]);
|
||||||
|
|
||||||
|
OrdenProduccion::create([
|
||||||
|
'prenda_modelo' => 'Pantalon Y',
|
||||||
|
'referencia' => 'REF-2',
|
||||||
|
'cantidad_total' => 5,
|
||||||
|
'tela_id' => $tela->id,
|
||||||
|
'metros_requeridos' => 30,
|
||||||
|
'estado' => 'en_corte',
|
||||||
|
'fecha_inicio' => now()->toDateString(),
|
||||||
|
'fecha_entrega_estimada' => now()->addDays(7)->toDateString(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user