up
This commit is contained in:
@@ -89,8 +89,38 @@ class InventarioPrendaDistribucion extends Model
|
||||
|
||||
if ($dist->color_id && $dist->size_id) {
|
||||
// Variante
|
||||
// Determinar producto: preferir inventario.prenda.producto_id -> op.producto_id
|
||||
$productoId = $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null);
|
||||
|
||||
// Si no existe producto, intentar crear/inferrir desde la Orden de Producción
|
||||
if (! $productoId && $dist->inventarioPrenda && $dist->inventarioPrenda->ordenProduccion) {
|
||||
$op = $dist->inventarioPrenda->ordenProduccion;
|
||||
$nombre = $op->prenda_modelo ?? $op->referencia ?? ('Producto OP #' . $op->id);
|
||||
|
||||
$producto = \App\Models\Producto::firstOrCreate(
|
||||
['nombre' => $nombre],
|
||||
[
|
||||
'descripcion' => 'Creado automáticamente desde distribución (OP #' . $op->id . ')',
|
||||
'stock' => 0,
|
||||
'estado' => true,
|
||||
'precio_compra' => 0,
|
||||
'precio_venta' => 0,
|
||||
'unidad_medida' => 'unidad',
|
||||
]
|
||||
);
|
||||
|
||||
$productoId = $producto->id;
|
||||
|
||||
// Guardar producto en el inventario padre para futuras referencias
|
||||
$parent = $dist->inventarioPrenda;
|
||||
if ($parent && ! $parent->producto_id) {
|
||||
$parent->producto_id = $productoId;
|
||||
$parent->save();
|
||||
}
|
||||
}
|
||||
|
||||
$variant = \App\Models\ProductVariant::firstOrCreate([
|
||||
'producto_id' => $dist->inventarioPrenda->producto_id ?? ($dist->inventarioPrenda->ordenProduccion->producto_id ?? null),
|
||||
'producto_id' => $productoId,
|
||||
'color_id' => $dist->color_id,
|
||||
'size_id' => $dist->size_id,
|
||||
], ['stock' => 0, 'sku' => 'AUTO-'.uniqid(), 'barcode' => '']);
|
||||
|
||||
Reference in New Issue
Block a user