schema([ Section::make('Datos iniciales') ->schema([ Select::make('orden_produccion_id') ->label('Orden de Producción') ->options(fn () => \App\Models\OrdenProduccion::whereRaw('( (select coalesce(sum(cantidad_recibida),0) from tintorerias where tintorerias.orden_produccion_id = orden_produccions.id) - ( select coalesce(sum(cantidad_enviada),0) from proceso_acabados where proceso_acabados.orden_produccion_id = orden_produccions.id and proceso_acabados.fecha_recepcion is null ) ) > 0')->pluck('numero_orden', 'id')->toArray()) ->searchable() ->reactive() ->required() ->afterStateUpdated(function ($state, $set) { if ($state) { // Sumar lo recibido en Tintorería $sumTint = \App\Models\Tintoreria::where('orden_produccion_id', $state)->sum('cantidad_recibida'); // Restar lo ya enviado a acabados y aún en proceso $sentInProcess = \App\Models\ProcesoAcabado::where('orden_produccion_id', $state) ->whereNull('fecha_recepcion') ->sum('cantidad_enviada'); $pending = max(0, (int)$sumTint - (int)$sentInProcess); $set('cantidad_enviada', $pending ?: 0); $set('fecha_envio', now()->toDateString()); } }), TextInput::make('tipo_proceso') ->label('Tipo de proceso') ->required(), Select::make('proveedor_id') ->label('Proveedor (opcional)') ->relationship('proveedor', 'nombre') ->searchable() ->preload() ->nullable(), TextInput::make('responsable_interno') ->label('Responsable interno') ->nullable(), TextInput::make('cantidad_enviada') ->numeric() ->required() ->reactive() ->helperText(fn ($get) => 'Sugerido: ' . (\App\Models\Tintoreria::where('orden_produccion_id', $get('orden_produccion_id'))->sum('cantidad_recibida') ?: 0)), DatePicker::make('fecha_envio') ->required() ->default(now()), ]) ->columns(2), Section::make('Recepción') ->schema([ DatePicker::make('fecha_recepcion') ->nullable(), TextInput::make('cantidad_recibida') ->numeric() ->reactive() ->afterStateUpdated(function ($state, $set, $get) { // Mantener residual o validaciones posteriores $cantidad_enviada = $get('cantidad_enviada') ?? 0; $cantidad_recibida = $state ?? 0; $residual = $cantidad_enviada - $cantidad_recibida; if ($residual < 0) $residual = 0; $set('observaciones', $get('observaciones') ?? 'Residual: ' . $residual); }), ]) ->columns(2), ]); } public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('ordenProduccion.numero_orden') ->label('OP') ->searchable(), TextColumn::make('tipo_proceso'), TextColumn::make('proveedor.nombre') ->label('Proveedor') ->toggleable(), TextColumn::make('responsable_interno') ->toggleable(), TextColumn::make('cantidad_enviada'), TextColumn::make('cantidad_recibida'), BadgeColumn::make('fecha_recepcion') ->label('Estado') ->formatStateUsing(fn($state) => $state ? 'Completado' : 'Pendiente') ->colors([ 'warning' => null, 'success' => fn($state) => $state !== null, ]), ]) ->filters([ Tables\Filters\SelectFilter::make('orden_produccion_id') ->label('Orden de Producción') ->relationship('ordenProduccion', 'numero_orden') ->searchable() ->preload(), ]) ->actions([ Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListProcesoAcabados::route('/'), 'create' => Pages\CreateProcesoAcabado::route('/create'), 'edit' => Pages\EditProcesoAcabado::route('/{record}/edit'), ]; } }