up
This commit is contained in:
@@ -98,10 +98,14 @@ class OjalPresillaResource extends Resource
|
||||
->helperText('Especifica el tipo de accesorio'),
|
||||
|
||||
TextInput::make('cantidad_enviada')
|
||||
->label('Cantidad total')
|
||||
->label('Cantidad total disponible')
|
||||
->numeric()
|
||||
->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')
|
||||
->label('Distribución entre proveedores')
|
||||
@@ -116,11 +120,44 @@ class OjalPresillaResource extends Resource
|
||||
->label('Cantidad')
|
||||
->numeric()
|
||||
->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)
|
||||
->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')
|
||||
->label('Marcar como terminada')
|
||||
|
||||
@@ -19,36 +19,60 @@ class CreateOjalPresilla extends CreateRecord
|
||||
$cantidadTotal = $data['cantidad_enviada'];
|
||||
$terminada = $data['terminada'] ?? false;
|
||||
|
||||
// Validar que las distribuciones sumen la cantidad total
|
||||
$sumaDistribucion = array_sum(array_column($distribuciones, 'cantidad'));
|
||||
if ($sumaDistribucion != $cantidadTotal) {
|
||||
// Validar que haya al menos una distribución
|
||||
if (empty($distribuciones)) {
|
||||
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
|
||||
$primerRegistro = null;
|
||||
$cantidadRegistros = count($distribuciones);
|
||||
foreach ($distribuciones as $distribucion) {
|
||||
$registro = \App\Models\Ojal::create([
|
||||
'proveedor_id' => $distribucion['proveedor_id'],
|
||||
'orden_produccion_id' => $ordenProduccionId,
|
||||
'tipo' => $tipo,
|
||||
'fecha_envio' => now(),
|
||||
'cantidad_enviada' => $distribucion['cantidad'],
|
||||
'terminada' => $terminada,
|
||||
]);
|
||||
|
||||
if (!$primerRegistro) {
|
||||
$primerRegistro = $registro;
|
||||
|
||||
try {
|
||||
foreach ($distribuciones as $distribucion) {
|
||||
$registro = \App\Models\Ojal::create([
|
||||
'proveedor_id' => $distribucion['proveedor_id'],
|
||||
'orden_produccion_id' => $ordenProduccionId,
|
||||
'tipo' => $tipo,
|
||||
'fecha_envio' => now(),
|
||||
'cantidad_enviada' => $distribucion['cantidad'],
|
||||
'terminada' => $terminada,
|
||||
]);
|
||||
|
||||
if (!$primerRegistro) {
|
||||
$primerRegistro = $registro;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages([
|
||||
'distribuciones' => 'Error al crear registros: ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
// Notificación de éxito
|
||||
\Filament\Notifications\Notification::make()
|
||||
->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()
|
||||
->send();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user