up
This commit is contained in:
@@ -14,6 +14,7 @@ class InventarioPrendaDistribucion extends Model
|
||||
protected $fillable = [
|
||||
'inventario_prenda_id',
|
||||
'bodega_id',
|
||||
'proveedor_id',
|
||||
'color_id',
|
||||
'size_id',
|
||||
'cantidad',
|
||||
@@ -52,8 +53,8 @@ class InventarioPrendaDistribucion extends Model
|
||||
$cantidad = intval($dist->cantidad ?? 0);
|
||||
$sumExistentes = (int) self::where('inventario_prenda_id', $parentId)->sum('cantidad');
|
||||
|
||||
// Validar contra la cantidad disponible actual
|
||||
if (($sumExistentes + $cantidad) > (int) $parent->cantidad_disponible) {
|
||||
// Validar contra la cantidad terminada total (no distribuir más del total entregado)
|
||||
if (($sumExistentes + $cantidad) > (int) $parent->cantidad_terminada) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'cantidad' => 'No se puede añadir más prendas que las disponibles.',
|
||||
]);
|
||||
@@ -72,8 +73,8 @@ class InventarioPrendaDistribucion extends Model
|
||||
->where('id', '<>', $dist->id)
|
||||
->sum('cantidad');
|
||||
|
||||
// Validar contra la cantidad disponible actual (considerando otras distribuciones)
|
||||
if (($othersSum + $newCantidad) > (int) $parent->cantidad_disponible) {
|
||||
// Validar contra la cantidad terminada total (no distribuir más del total entregado)
|
||||
if (($othersSum + $newCantidad) > (int) $parent->cantidad_terminada) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'cantidad' => 'No se puede añadir más prendas que las disponibles.',
|
||||
]);
|
||||
@@ -87,6 +88,18 @@ class InventarioPrendaDistribucion extends Model
|
||||
|
||||
$bodegaId = $dist->bodega_id;
|
||||
|
||||
// If a proveedor_id is present, treat as assignment to a provider/person (no warehouse stock changes)
|
||||
if ($dist->proveedor_id && ! $bodegaId) {
|
||||
// Just decrement disponibilidad on parent
|
||||
$parent = $dist->inventarioPrenda;
|
||||
if ($parent) {
|
||||
$parent->cantidad_disponible = max(0, $parent->cantidad_disponible - $cantidad);
|
||||
$parent->save();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($dist->color_id && $dist->size_id) {
|
||||
// Variante
|
||||
// Determinar producto: preferir inventario.prenda.producto_id -> op.producto_id
|
||||
@@ -158,6 +171,41 @@ class InventarioPrendaDistribucion extends Model
|
||||
} else {
|
||||
// Producto
|
||||
$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);
|
||||
|
||||
$codigo = 'AUTOP-' . $op->id . '-' . time();
|
||||
$codigo = substr($codigo, 0, 50);
|
||||
while (\App\Models\Producto::where('codigo_barras', $codigo)->exists()) {
|
||||
$codigo .= '-' . rand(0, 9);
|
||||
$codigo = substr($codigo, 0, 50);
|
||||
}
|
||||
|
||||
$producto = \App\Models\Producto::create([
|
||||
'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',
|
||||
'codigo_barras' => $codigo,
|
||||
'categoria_id' => \App\Models\Categoria::first()?->id ?? \App\Models\Categoria::create(['nombre' => 'Generica'])->id,
|
||||
]);
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
if ($productoId) {
|
||||
$existing = \DB::table('producto_bodega')->where('producto_id', $productoId)->where('bodega_id', $bodegaId)->first();
|
||||
if ($existing) {
|
||||
|
||||
Reference in New Issue
Block a user