up
This commit is contained in:
@@ -67,7 +67,7 @@ class InventarioPrendaResource extends Resource
|
|||||||
$set('producto_id', $op->producto_id);
|
$set('producto_id', $op->producto_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
})->helperText('La Orden de Producción es la fuente de verdad para el producto; si la OP tiene producto asociado, se usará automáticamente.'),
|
||||||
|
|
||||||
TextInput::make('cantidad_terminada')
|
TextInput::make('cantidad_terminada')
|
||||||
->numeric()
|
->numeric()
|
||||||
@@ -83,7 +83,7 @@ class InventarioPrendaResource extends Resource
|
|||||||
->searchable()
|
->searchable()
|
||||||
->preload()
|
->preload()
|
||||||
->nullable()
|
->nullable()
|
||||||
->helperText('Si se selecciona, el stock del producto se incrementará automáticamente cuando se guarde.'),
|
->helperText('Opcional. Si la Orden de Producción tiene un producto asociado, éste será la fuente de verdad. Si el producto seleccionado difiere, se mostrará un error y no se permitirá guardar.'),
|
||||||
|
|
||||||
TextInput::make('cantidad_disponible')
|
TextInput::make('cantidad_disponible')
|
||||||
->disabled()
|
->disabled()
|
||||||
|
|||||||
@@ -33,6 +33,24 @@ class InventarioPrenda extends Model
|
|||||||
protected static function booted()
|
protected static function booted()
|
||||||
{
|
{
|
||||||
static::creating(function ($inventario) {
|
static::creating(function ($inventario) {
|
||||||
|
// Sincronizar producto desde la Orden de Producción si no se especificó
|
||||||
|
if ($inventario->orden_produccion_id && ! $inventario->producto_id) {
|
||||||
|
$op = \App\Models\OrdenProduccion::find($inventario->orden_produccion_id);
|
||||||
|
if ($op && $op->producto_id) {
|
||||||
|
$inventario->producto_id = $op->producto_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar conflicto: si el OP tiene producto y difiere del seleccionado, bloquear
|
||||||
|
if ($inventario->producto_id && $inventario->orden_produccion_id) {
|
||||||
|
$op = \App\Models\OrdenProduccion::find($inventario->orden_produccion_id);
|
||||||
|
if ($op && $op->producto_id && $op->producto_id != $inventario->producto_id) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'producto_id' => 'El producto seleccionado difiere del producto asociado a la Orden de Producción (fuente de verdad).',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Validación: la cantidad terminada no puede exceder lo recibido para esta OP (traslados a bodega)
|
// Validación: la cantidad terminada no puede exceder lo recibido para esta OP (traslados a bodega)
|
||||||
if ($inventario->orden_produccion_id) {
|
if ($inventario->orden_produccion_id) {
|
||||||
$totalRecibido = \App\Models\TrasladoPrenda::where('orden_produccion_id', $inventario->orden_produccion_id)
|
$totalRecibido = \App\Models\TrasladoPrenda::where('orden_produccion_id', $inventario->orden_produccion_id)
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
// Backfill producto_id in inventario_prendas from la OP relacionada cuando sea NULL
|
||||||
|
DB::statement("UPDATE inventario_prendas ip SET producto_id = op.producto_id FROM orden_produccions op WHERE ip.orden_produccion_id = op.id AND ip.producto_id IS NULL AND op.producto_id IS NOT NULL");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// Revertir sólo las asignaciones que coinciden con la OP actual
|
||||||
|
DB::statement("UPDATE inventario_prendas ip SET producto_id = NULL FROM orden_produccions op WHERE ip.orden_produccion_id = op.id AND ip.producto_id = op.producto_id");
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use App\Models\OrdenProduccion;
|
||||||
|
use App\Models\InventarioPrenda;
|
||||||
|
use App\Models\Producto;
|
||||||
|
|
||||||
|
class InventarioPrendaProductSyncTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_creating_inventory_populates_product_from_op()
|
||||||
|
{
|
||||||
|
$producto = Producto::create(['nombre' => 'ProdA', 'descripcion' => '', 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad', 'codigo_barras' => '', 'categoria_id' => \App\Models\Categoria::create(['nombre' => 'Ropa'])->id, 'imagen' => '']);
|
||||||
|
|
||||||
|
$op = OrdenProduccion::create(['numero_orden' => 'OP-100', 'prenda_modelo' => 'ProdA', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7), 'producto_id' => $producto->id]);
|
||||||
|
|
||||||
|
$inv = InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 5, 'cantidad_disponible' => 5, 'fecha_ingreso' => now(), 'estado' => 'en_bodega']);
|
||||||
|
|
||||||
|
$this->assertNotNull($inv->producto_id);
|
||||||
|
$this->assertEquals($producto->id, $inv->producto_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_creating_inventory_throws_if_product_differs_from_op()
|
||||||
|
{
|
||||||
|
$categoria = \App\Models\Categoria::create(['nombre' => 'Ropa']);
|
||||||
|
$p1 = Producto::create(['nombre' => 'P1', 'descripcion' => '', 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad', 'codigo_barras' => '', 'categoria_id' => $categoria->id, 'imagen' => '']);
|
||||||
|
$p2 = Producto::create(['nombre' => 'P2', '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-101', 'prenda_modelo' => 'P1', 'cantidad_total' => 10, 'metros_requeridos' => 0, 'fecha_inicio' => now(), 'fecha_entrega_estimada' => now()->addDays(7), 'producto_id' => $p1->id]);
|
||||||
|
|
||||||
|
$this->expectException(\Illuminate\Validation\ValidationException::class);
|
||||||
|
|
||||||
|
InventarioPrenda::create(['orden_produccion_id' => $op->id, 'cantidad_terminada' => 3, 'cantidad_disponible' => 3, 'fecha_ingreso' => now(), 'estado' => 'en_bodega', 'producto_id' => $p2->id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user