This commit is contained in:
Lizandro Guarnizo
2026-01-29 23:43:45 -05:00
parent b5f109df1b
commit 6f3d3417fb
2 changed files with 82 additions and 21 deletions
@@ -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')