|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\LicenseResource\Pages; |
| 6 | +use App\Models\License; |
| 7 | +use Filament\Forms; |
| 8 | +use Filament\Forms\Form; |
| 9 | +use Filament\Infolists\Components; |
| 10 | +use Filament\Infolists\Infolist; |
| 11 | +use Filament\Resources\Resource; |
| 12 | +use Filament\Tables; |
| 13 | +use Filament\Tables\Table; |
| 14 | + |
| 15 | +class LicenseResource extends Resource |
| 16 | +{ |
| 17 | + protected static ?string $model = License::class; |
| 18 | + |
| 19 | + protected static ?string $navigationIcon = 'heroicon-o-key'; |
| 20 | + |
| 21 | + public static function form(Form $form): Form |
| 22 | + { |
| 23 | + return $form |
| 24 | + ->schema([ |
| 25 | + Forms\Components\Section::make('License Information') |
| 26 | + ->schema([ |
| 27 | + Forms\Components\TextInput::make('id') |
| 28 | + ->disabled(), |
| 29 | + Forms\Components\TextInput::make('anystack_id') |
| 30 | + ->maxLength(36) |
| 31 | + ->disabled(), |
| 32 | + Forms\Components\Select::make('user_id') |
| 33 | + ->relationship('user', 'email') |
| 34 | + ->disabled(), |
| 35 | + Forms\Components\TextInput::make('policy_name') |
| 36 | + ->label('Plan') |
| 37 | + ->disabled(), |
| 38 | + Forms\Components\TextInput::make('key') |
| 39 | + ->disabled(), |
| 40 | + Forms\Components\DateTimePicker::make('expires_at') |
| 41 | + ->disabled(), |
| 42 | + Forms\Components\DateTimePicker::make('created_at') |
| 43 | + ->disabled(), |
| 44 | + ])->columns(2), |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + public static function table(Table $table): Table |
| 49 | + { |
| 50 | + return $table |
| 51 | + ->columns([ |
| 52 | + Tables\Columns\TextColumn::make('id') |
| 53 | + ->sortable() |
| 54 | + ->searchable(), |
| 55 | + Tables\Columns\TextColumn::make('user.email') |
| 56 | + ->searchable() |
| 57 | + ->sortable() |
| 58 | + ->copyable() |
| 59 | + ->url(fn (\App\Models\License $record): string => route('filament.admin.resources.users.edit', ['record' => $record->user_id])) |
| 60 | + ->openUrlInNewTab(), |
| 61 | + Tables\Columns\TextColumn::make('key') |
| 62 | + ->searchable() |
| 63 | + ->copyable(), |
| 64 | + Tables\Columns\TextColumn::make('policy_name') |
| 65 | + ->label('Plan') |
| 66 | + ->searchable(), |
| 67 | + Tables\Columns\TextColumn::make('expires_at') |
| 68 | + ->dateTime() |
| 69 | + ->sortable(), |
| 70 | + Tables\Columns\TextColumn::make('created_at') |
| 71 | + ->dateTime() |
| 72 | + ->sortable(), |
| 73 | + ]) |
| 74 | + ->filters([ |
| 75 | + // |
| 76 | + ]) |
| 77 | + ->actions([ |
| 78 | + Tables\Actions\ViewAction::make() |
| 79 | + ->slideOver() |
| 80 | + ->modalHeading('License Details') |
| 81 | + ->modalWidth('7xl') |
| 82 | + ->extraModalFooterActions([ |
| 83 | + Tables\Actions\Action::make('viewUser') |
| 84 | + ->label('View User') |
| 85 | + ->icon('heroicon-o-user') |
| 86 | + ->color('primary') |
| 87 | + ->url(fn (License $record) => route('filament.admin.resources.users.edit', ['record' => $record->user_id])) |
| 88 | + ->openUrlInNewTab() |
| 89 | + ->visible(fn (License $record) => $record->user_id !== null), |
| 90 | + ]), |
| 91 | + ]) |
| 92 | + ->defaultPaginationPageOption(25) |
| 93 | + ->bulkActions([]); |
| 94 | + } |
| 95 | + |
| 96 | + public static function getRelations(): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + // |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + public static function infolist(Infolist $infolist): Infolist |
| 104 | + { |
| 105 | + return $infolist |
| 106 | + ->schema([ |
| 107 | + Components\Section::make('License Information') |
| 108 | + ->schema([ |
| 109 | + Components\TextEntry::make('id'), |
| 110 | + Components\TextEntry::make('key') |
| 111 | + ->copyable(), |
| 112 | + Components\TextEntry::make('policy_name') |
| 113 | + ->label('Plan'), |
| 114 | + Components\TextEntry::make('expires_at') |
| 115 | + ->dateTime(), |
| 116 | + Components\TextEntry::make('created_at') |
| 117 | + ->dateTime(), |
| 118 | + Components\TextEntry::make('anystack_id') |
| 119 | + ->copyable(), |
| 120 | + ])->columns(2), |
| 121 | + |
| 122 | + Components\Section::make('User Information') |
| 123 | + ->schema([ |
| 124 | + Components\TextEntry::make('user.id') |
| 125 | + ->label('User ID') |
| 126 | + ->url(fn ($record) => route('filament.admin.resources.users.edit', ['record' => $record->user_id])) |
| 127 | + ->openUrlInNewTab(), |
| 128 | + Components\TextEntry::make('user.email') |
| 129 | + ->label('Email') |
| 130 | + ->copyable() |
| 131 | + ->url(fn ($record) => route('filament.admin.resources.users.edit', ['record' => $record->user_id])) |
| 132 | + ->openUrlInNewTab(), |
| 133 | + Components\TextEntry::make('user.name') |
| 134 | + ->label('Name'), |
| 135 | + Components\TextEntry::make('user.first_name') |
| 136 | + ->label('First Name'), |
| 137 | + Components\TextEntry::make('user.last_name') |
| 138 | + ->label('Last Name'), |
| 139 | + Components\TextEntry::make('user.stripe_id') |
| 140 | + ->label('Stripe ID') |
| 141 | + ->copyable() |
| 142 | + ->visible(fn ($record) => filled($record->user->stripe_id)) |
| 143 | + ->url(fn ($record) => filled($record->user->stripe_id) |
| 144 | + ? "https://dashboard.stripe.com/customers/{$record->user->stripe_id}" |
| 145 | + : null) |
| 146 | + ->openUrlInNewTab(), |
| 147 | + Components\TextEntry::make('user.anystack_contact_id') |
| 148 | + ->label('Anystack Contact ID') |
| 149 | + ->copyable() |
| 150 | + ->visible(fn ($record) => filled($record->user->anystack_contact_id)) |
| 151 | + ->url(fn ($record) => filled($record->user->anystack_contact_id) |
| 152 | + ? "https://app.anystack.sh/contacts/{$record->user->anystack_contact_id}" |
| 153 | + : null) |
| 154 | + ->openUrlInNewTab(), |
| 155 | + ])->columns(2), |
| 156 | + |
| 157 | + Components\Section::make('Subscription Information') |
| 158 | + ->schema([ |
| 159 | + Components\TextEntry::make('subscriptionItem.id') |
| 160 | + ->label('Subscription Item ID') |
| 161 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 162 | + Components\TextEntry::make('subscriptionItem.stripe_id') |
| 163 | + ->label('Stripe Subscription Item ID') |
| 164 | + ->copyable() |
| 165 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 166 | + Components\TextEntry::make('subscriptionItem.subscription.stripe_id') |
| 167 | + ->label('Stripe Subscription ID') |
| 168 | + ->copyable() |
| 169 | + ->visible(fn ($record) => $record->subscription_item_id !== null) |
| 170 | + ->url(fn ($record) => $record->subscription_item_id !== null && filled($record->subscriptionItem?->subscription?->stripe_id) |
| 171 | + ? "https://dashboard.stripe.com/subscriptions/{$record->subscriptionItem->subscription->stripe_id}" |
| 172 | + : null) |
| 173 | + ->openUrlInNewTab(), |
| 174 | + Components\TextEntry::make('subscriptionItem.stripe_price') |
| 175 | + ->label('Stripe Price ID') |
| 176 | + ->copyable() |
| 177 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 178 | + Components\TextEntry::make('subscriptionItem.stripe_product') |
| 179 | + ->label('Stripe Product ID') |
| 180 | + ->copyable() |
| 181 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 182 | + Components\TextEntry::make('subscriptionItem.subscription.stripe_status') |
| 183 | + ->label('Subscription Status') |
| 184 | + ->badge() |
| 185 | + ->color(fn ($state): string => match ($state) { |
| 186 | + 'active' => 'success', |
| 187 | + 'canceled' => 'danger', |
| 188 | + 'incomplete' => 'warning', |
| 189 | + 'incomplete_expired' => 'danger', |
| 190 | + 'past_due' => 'warning', |
| 191 | + 'trialing' => 'info', |
| 192 | + 'unpaid' => 'danger', |
| 193 | + default => 'gray', |
| 194 | + }) |
| 195 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 196 | + ])->columns(2) |
| 197 | + ->visible(fn ($record) => $record->subscription_item_id !== null), |
| 198 | + ]); |
| 199 | + } |
| 200 | + |
| 201 | + public static function getPages(): array |
| 202 | + { |
| 203 | + return [ |
| 204 | + 'index' => Pages\ListLicenses::route('/'), |
| 205 | + ]; |
| 206 | + } |
| 207 | +} |
0 commit comments