Files
pos_heidiver/app/Filament/Resources/ConfeccionResource/Pages/EditConfeccion.php
T
2026-01-29 17:14:05 -05:00

106 lines
5.0 KiB
PHP

<?php
namespace App\Filament\Resources\ConfeccionResource\Pages;
use App\Filament\Resources\ConfeccionResource;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Pages\EditRecord;
class EditConfeccion extends EditRecord
{
protected static string $resource = ConfeccionResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\Action::make('agregarRecepcion')
->label('Agregar recepción')
->modalHeading('Agregar Recepción')
->disabled(fn (): bool => ($this->getRecord()->faltantes ?? 0) <= 0)
->form([
Forms\Components\DatePicker::make('fecha_recepcion')->default(now())->required(),
Forms\Components\TextInput::make('cantidad')
->numeric()
->required()
->minValue(1)
->maxValue(fn () => $this->getRecord()->faltantes ?? 0)
->helperText(fn () => 'Máximo: ' . ($this->getRecord()->faltantes ?? 0))
->default(fn () => $this->getRecord()->faltantes ?? 0),
Forms\Components\Textarea::make('notas')
->helperText('Si hay prendas defectuosas, regístralas después en "Arreglos".'),
])
->action(function (array $data): void {
$record = $this->getRecord();
try {
\App\Models\Recepcion::create([
'referencia_type' => \App\Models\Confeccion::class,
'referencia_id' => $record->id,
'cantidad' => (int) $data['cantidad'],
'fecha_recepcion' => $data['fecha_recepcion'],
'prendas_defectuosas' => 0,
'notas' => $data['notas'] ?? null,
]);
\Filament\Notifications\Notification::make()
->success()
->title('Recepción registrada')
->body('La recepción se creó correctamente.')
->send();
} catch (\Illuminate\Validation\ValidationException $e) {
\Filament\Notifications\Notification::make()
->danger()
->title('Error')
->body($e->validator->errors()->first() ?? 'Error al crear la recepción.')
->send();
} catch (\Throwable $e) {
\Filament\Notifications\Notification::make()
->danger()
->title('Error')
->body($e->getMessage())
->send();
}
// Refrescar la página para ver cambios (redirigir explicitando el record para evitar error de ruta)
$this->redirect(route('filament.admin.resources.confeccions.edit', ['record' => $record->id]));
}),
// Arreglos: restar cantidad del total recibido y crear traslado de reparaciones
Actions\Action::make('arreglos')
->label('Arreglos')
->modalHeading('Registrar Arreglo')
->form([
Forms\Components\TextInput::make('cantidad')
->label('Cantidad a arreglar')
->numeric()
->required()
->minValue(1)
->maxValue(fn () => $this->getRecord()->cantidad_recibida ?? 0)
->helperText(fn () => 'Máximo: ' . ($this->getRecord()->cantidad_recibida ?? 0)),
Forms\Components\Textarea::make('notas'),
])
->action(function (array $data): void {
$record = $this->getRecord();
$cantidad = (int) ($data['cantidad'] ?? 0);
try {
$record->registerArreglo($cantidad, $data['notas'] ?? null, auth()->id() ?? null);
\Filament\Notifications\Notification::make()->success()->title('Arreglo registrado')->body('Se registró el arreglo correctamente.')->send();
} catch (\Illuminate\Validation\ValidationException $e) {
\Filament\Notifications\Notification::make()->danger()->title('Error')->body($e->validator->errors()->first() ?? 'Error al registrar arreglo.')->send();
} catch (\Throwable $e) {
\Filament\Notifications\Notification::make()->danger()->title('Error')->body($e->getMessage())->send();
}
$this->redirect(route('filament.admin.resources.confeccions.edit', ['record' => $record->id]));
}),
];
}
}