up
This commit is contained in:
@@ -45,7 +45,19 @@ class InventarioPrendaResource extends Resource
|
||||
|
||||
TextInput::make('cantidad_terminada')
|
||||
->numeric()
|
||||
->required(),
|
||||
->required()
|
||||
->afterStateUpdated(function ($state, $set) {
|
||||
// Mantener la cantidad disponible igual a la terminada al crear
|
||||
$set('cantidad_disponible', $state);
|
||||
}),
|
||||
|
||||
Select::make('producto_id')
|
||||
->label('Producto (opcional)')
|
||||
->relationship('producto', 'nombre')
|
||||
->searchable()
|
||||
->preload()
|
||||
->nullable()
|
||||
->helperText('Si se selecciona, el stock del producto se incrementará automáticamente cuando se guarde.'),
|
||||
|
||||
TextInput::make('cantidad_disponible')
|
||||
->disabled()
|
||||
|
||||
@@ -59,6 +59,14 @@ class OrdenProduccionResource extends Resource
|
||||
->preload()
|
||||
->nullable(),
|
||||
|
||||
Select::make('producto_id')
|
||||
->label('Producto (opcional)')
|
||||
->relationship('producto', 'nombre')
|
||||
->searchable()
|
||||
->preload()
|
||||
->nullable()
|
||||
->helperText('Si no se selecciona, se intentará crear el producto automáticamente cuando la orden finalice.'),
|
||||
|
||||
TextInput::make('metros_requeridos')
|
||||
->label('Metros requeridos')
|
||||
->numeric()
|
||||
|
||||
@@ -12,6 +12,7 @@ class InventarioPrenda extends Model
|
||||
'cantidad_disponible',
|
||||
'fecha_ingreso',
|
||||
'estado',
|
||||
'producto_id',
|
||||
];
|
||||
|
||||
public function ordenProduccion()
|
||||
@@ -19,6 +20,11 @@ class InventarioPrenda extends Model
|
||||
return $this->belongsTo(OrdenProduccion::class);
|
||||
}
|
||||
|
||||
public function producto()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Producto::class, 'producto_id');
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function ($inventario) {
|
||||
@@ -36,6 +42,16 @@ class InventarioPrenda extends Model
|
||||
}
|
||||
|
||||
$op = $inventario->ordenProduccion;
|
||||
|
||||
// Priorizar producto explícito si fue seleccionado
|
||||
if ($inventario->producto_id) {
|
||||
$producto = \App\Models\Producto::find($inventario->producto_id);
|
||||
if ($producto && ! $producto->variants()->exists()) {
|
||||
$producto->increment('stock', $inventario->cantidad_terminada);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $op) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ class OrdenProduccion extends Model
|
||||
'referencia',
|
||||
'cantidad_total',
|
||||
'tela_id',
|
||||
'producto_id',
|
||||
'fecha_inicio',
|
||||
'fecha_entrega_estimada',
|
||||
'estado',
|
||||
@@ -52,6 +53,11 @@ class OrdenProduccion extends Model
|
||||
return $this->hasMany(\App\Models\InventarioPrenda::class);
|
||||
}
|
||||
|
||||
public function producto()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Producto::class, 'producto_id');
|
||||
}
|
||||
|
||||
public function avanzarEstado(): void
|
||||
{
|
||||
$flujo = [
|
||||
@@ -78,13 +84,34 @@ class OrdenProduccion extends Model
|
||||
|
||||
// Crear inventario solo si hay traslados a bodega con cantidad
|
||||
if ($cantidad > 0 && !\App\Models\InventarioPrenda::where('orden_produccion_id', $this->id)->exists()) {
|
||||
// Determinar producto asociado: preferir producto_id en la OP, sino crear/buscar por prenda_modelo
|
||||
$productoId = $this->producto_id;
|
||||
|
||||
if (! $productoId) {
|
||||
$nombre = $this->prenda_modelo ?? $this->referencia ?? null;
|
||||
if ($nombre) {
|
||||
$producto = \App\Models\Producto::firstOrCreate(
|
||||
['nombre' => $nombre],
|
||||
['descripcion' => 'Creado automáticamente desde OrdenProduccion #' . $this->id, 'stock' => 0, 'estado' => true, 'precio_compra' => 0, 'precio_venta' => 0, 'unidad_medida' => 'unidad']
|
||||
);
|
||||
$productoId = $producto->id;
|
||||
|
||||
// Guardar vínculo en la OP para futuras referencias
|
||||
$this->producto_id = $productoId;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
\App\Models\InventarioPrenda::create([
|
||||
'orden_produccion_id' => $this->id,
|
||||
'cantidad_terminada' => $cantidad,
|
||||
'cantidad_disponible' => $cantidad,
|
||||
'fecha_ingreso' => now(),
|
||||
'estado' => 'en_bodega',
|
||||
'producto_id' => $productoId ?? null,
|
||||
]);
|
||||
|
||||
// Si se creó inventario, también actualizar stock del producto asociado (handler en InventarioPrenda lo hará)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('inventario_prendas', function (Blueprint $table) {
|
||||
$table->foreignId('producto_id')->nullable()->constrained('productos')->nullOnDelete()->after('orden_produccion_id');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('inventario_prendas', function (Blueprint $table) {
|
||||
$table->dropForeign(['producto_id']);
|
||||
$table->dropColumn('producto_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('orden_produccions', function (Blueprint $table) {
|
||||
$table->foreignId('producto_id')->nullable()->constrained('productos')->nullOnDelete()->after('tela_id');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('orden_produccions', function (Blueprint $table) {
|
||||
$table->dropForeign(['producto_id']);
|
||||
$table->dropColumn('producto_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user