up
This commit is contained in:
@@ -98,10 +98,14 @@ class OjalPresillaResource extends Resource
|
|||||||
->helperText('Especifica el tipo de accesorio'),
|
->helperText('Especifica el tipo de accesorio'),
|
||||||
|
|
||||||
TextInput::make('cantidad_enviada')
|
TextInput::make('cantidad_enviada')
|
||||||
->label('Cantidad total')
|
->label('Cantidad total disponible')
|
||||||
->numeric()
|
->numeric()
|
||||||
->readOnly()
|
->readOnly()
|
||||||
->helperText('Se calcula automáticamente desde confecciones recibidas'),
|
->helperText(function ($state) {
|
||||||
|
if (!$state) return 'Selecciona una OP primero';
|
||||||
|
return "Máximo disponible: {$state} unidades";
|
||||||
|
})
|
||||||
|
->live(),
|
||||||
|
|
||||||
Repeater::make('distribuciones')
|
Repeater::make('distribuciones')
|
||||||
->label('Distribución entre proveedores')
|
->label('Distribución entre proveedores')
|
||||||
@@ -116,11 +120,44 @@ class OjalPresillaResource extends Resource
|
|||||||
->label('Cantidad')
|
->label('Cantidad')
|
||||||
->numeric()
|
->numeric()
|
||||||
->required()
|
->required()
|
||||||
->minValue(1),
|
->minValue(1)
|
||||||
|
->live()
|
||||||
|
->afterStateUpdated(function ($state, $set, $get) {
|
||||||
|
// Validar que la suma no exceda el total
|
||||||
|
$total = (int) ($get('../../cantidad_enviada') ?? 0);
|
||||||
|
$distribuciones = $get('../../distribuciones') ?? [];
|
||||||
|
$suma = 0;
|
||||||
|
foreach ($distribuciones as $dist) {
|
||||||
|
$suma += (int) ($dist['cantidad'] ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($suma > $total) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'cantidad' => "La suma de las distribuciones ({$suma}) excede el total disponible ({$total})."
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}),
|
||||||
])
|
])
|
||||||
->defaultItems(1)
|
->defaultItems(1)
|
||||||
->addActionLabel('Agregar proveedor')
|
->addActionLabel('Agregar proveedor')
|
||||||
->helperText('Puedes distribuir la cantidad entre varios proveedores'),
|
->helperText(function ($get) {
|
||||||
|
$total = (int) ($get('cantidad_enviada') ?? 0);
|
||||||
|
$distribuciones = $get('distribuciones') ?? [];
|
||||||
|
$suma = 0;
|
||||||
|
foreach ($distribuciones as $dist) {
|
||||||
|
$suma += (int) ($dist['cantidad'] ?? 0);
|
||||||
|
}
|
||||||
|
$restante = $total - $suma;
|
||||||
|
|
||||||
|
if ($restante < 0) {
|
||||||
|
return "⚠️ Excede el total por " . abs($restante) . " unidades";
|
||||||
|
} elseif ($restante > 0) {
|
||||||
|
return "Faltan {$restante} unidades por distribuir";
|
||||||
|
} else {
|
||||||
|
return "✓ Total distribuido correctamente";
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->live(),
|
||||||
|
|
||||||
Checkbox::make('terminada')
|
Checkbox::make('terminada')
|
||||||
->label('Marcar como terminada')
|
->label('Marcar como terminada')
|
||||||
|
|||||||
@@ -19,17 +19,36 @@ class CreateOjalPresilla extends CreateRecord
|
|||||||
$cantidadTotal = $data['cantidad_enviada'];
|
$cantidadTotal = $data['cantidad_enviada'];
|
||||||
$terminada = $data['terminada'] ?? false;
|
$terminada = $data['terminada'] ?? false;
|
||||||
|
|
||||||
// Validar que las distribuciones sumen la cantidad total
|
// Validar que haya al menos una distribución
|
||||||
$sumaDistribucion = array_sum(array_column($distribuciones, 'cantidad'));
|
if (empty($distribuciones)) {
|
||||||
if ($sumaDistribucion != $cantidadTotal) {
|
|
||||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
'distribuciones' => "La suma de las distribuciones ({$sumaDistribucion}) debe ser igual a la cantidad total ({$cantidadTotal})."
|
'distribuciones' => 'Debes agregar al menos un proveedor con su cantidad.'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validar que las distribuciones no excedan la cantidad total
|
||||||
|
$sumaDistribucion = array_sum(array_column($distribuciones, 'cantidad'));
|
||||||
|
if ($sumaDistribucion > $cantidadTotal) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'distribuciones' => "La suma de las distribuciones ({$sumaDistribucion}) excede la cantidad total disponible ({$cantidadTotal})."
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advertir si no se distribuye todo (pero permitirlo)
|
||||||
|
if ($sumaDistribucion < $cantidadTotal) {
|
||||||
|
$diferencia = $cantidadTotal - $sumaDistribucion;
|
||||||
|
\Filament\Notifications\Notification::make()
|
||||||
|
->warning()
|
||||||
|
->title('Distribución parcial')
|
||||||
|
->body("Se distribuirán {$sumaDistribucion} de {$cantidadTotal} unidades. Quedarán {$diferencia} sin asignar.")
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
|
||||||
// Crear un registro por cada proveedor
|
// Crear un registro por cada proveedor
|
||||||
$primerRegistro = null;
|
$primerRegistro = null;
|
||||||
$cantidadRegistros = count($distribuciones);
|
$cantidadRegistros = count($distribuciones);
|
||||||
|
|
||||||
|
try {
|
||||||
foreach ($distribuciones as $distribucion) {
|
foreach ($distribuciones as $distribucion) {
|
||||||
$registro = \App\Models\Ojal::create([
|
$registro = \App\Models\Ojal::create([
|
||||||
'proveedor_id' => $distribucion['proveedor_id'],
|
'proveedor_id' => $distribucion['proveedor_id'],
|
||||||
@@ -44,11 +63,16 @@ class CreateOjalPresilla extends CreateRecord
|
|||||||
$primerRegistro = $registro;
|
$primerRegistro = $registro;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||||
|
'distribuciones' => 'Error al crear registros: ' . $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// Notificación de éxito
|
// Notificación de éxito
|
||||||
\Filament\Notifications\Notification::make()
|
\Filament\Notifications\Notification::make()
|
||||||
->title('Registros creados exitosamente')
|
->title('Registros creados exitosamente')
|
||||||
->body("Se crearon {$cantidadRegistros} registro(s) para los proveedores.")
|
->body("Se crearon {$cantidadRegistros} registro(s) de {$tipo} para {$sumaDistribucion} unidades.")
|
||||||
->success()
|
->success()
|
||||||
->send();
|
->send();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user