-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathEditUser.php
More file actions
190 lines (160 loc) · 8.51 KB
/
EditUser.php
File metadata and controls
190 lines (160 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace App\Filament\Resources\UserResource\Pages;
use App\Enums\Subscription;
use App\Filament\Resources\UserResource;
use App\Jobs\CreateAnystackLicenseJob;
use App\Models\User;
use Filament\Actions;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Password;
use STS\FilamentImpersonate\Actions\Impersonate;
class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;
protected function getHeaderActions(): array
{
return [
Impersonate::make()->record($this->getRecord()),
Actions\ActionGroup::make([
Actions\Action::make('createStripeCustomer')
->label('Create Stripe Customer')
->color('gray')
->icon('heroicon-o-credit-card')
->action(function (User $record): void {
if ($record->hasStripeId()) {
Notification::make()
->danger()
->title('User is already a Stripe customer.')
->send();
return;
}
$record->createOrGetStripeCustomer();
})
->visible(fn (User $record) => empty($record->stripe_id)),
Actions\Action::make('compUltraSubscription')
->label('Comp Ultra Subscription')
->color('warning')
->icon('heroicon-o-sparkles')
->modalHeading('Comp Ultra Subscription')
->modalSubmitActionLabel('Comp Ultra')
->form(function (User $record): array {
$existingSubscription = $record->subscription('default');
$hasActiveSubscription = $existingSubscription && $existingSubscription->active();
$fields = [];
if ($hasActiveSubscription) {
$currentPlan = 'their current plan';
try {
$currentPlan = Subscription::fromStripePriceId(
$existingSubscription->items->first()?->stripe_price ?? $existingSubscription->stripe_price
)->name();
} catch (\Exception) {
}
$fields[] = Placeholder::make('info')
->label('')
->content("This user has an active {$currentPlan} subscription. Choose when to switch them to the comped Ultra plan.");
$fields[] = Radio::make('timing')
->label('When to switch')
->options([
'now' => 'Immediately — swap now and credit remaining value (swapAndInvoice)',
'renewal' => 'At renewal — keep current plan until period ends, then switch (swap)',
])
->default('now')
->required();
} else {
$fields[] = Placeholder::make('info')
->label('')
->content("This will create a free Ultra subscription for {$record->email}. A Stripe customer will be created if one doesn't exist.");
}
return $fields;
})
->action(function (array $data, User $record): void {
$compedPriceId = config('subscriptions.plans.max.stripe_price_id_comped');
if (! $compedPriceId) {
Notification::make()
->danger()
->title('STRIPE_ULTRA_COMP_PRICE_ID is not configured.')
->send();
return;
}
$record->createOrGetStripeCustomer();
$existingSubscription = $record->subscription('default');
if ($existingSubscription && $existingSubscription->active()) {
$timing = $data['timing'] ?? 'now';
if ($timing === 'now') {
$existingSubscription->skipTrial()->swapAndInvoice($compedPriceId);
$message = 'Subscription swapped to comped Ultra immediately. Remaining value has been credited.';
} else {
$existingSubscription->skipTrial()->swap($compedPriceId);
$message = 'Subscription will switch to comped Ultra at the end of the current billing period.';
}
Notification::make()
->success()
->title('Comped Ultra subscription applied.')
->body($message)
->send();
} else {
$record->newSubscription('default', $compedPriceId)->create();
Notification::make()
->success()
->title('Comped Ultra subscription created.')
->body("Ultra subscription created for {$record->email}.")
->send();
}
})
->visible(function (User $record): bool {
if (! config('subscriptions.plans.max.stripe_price_id_comped')) {
return false;
}
return ! $record->hasActiveUltraSubscription();
}),
Actions\Action::make('createAnystackLicense')
->label('Create Anystack License')
->color('gray')
->icon('heroicon-o-key')
->form([
Select::make('subscription')
->label('Subscription Plan')
->options(collect(Subscription::cases())->mapWithKeys(function ($case) {
return [$case->value => $case->name()];
}))
->required(),
])
->action(function (array $data, User $record): void {
$subscription = Subscription::from($data['subscription']);
dispatch(new CreateAnystackLicenseJob($record, $subscription, null, $record->first_name, $record->last_name));
}),
Actions\Action::make('sendPasswordReset')
->label('Send Password Reset')
->color('gray')
->icon('heroicon-o-envelope')
->requiresConfirmation()
->action(function (User $record): void {
Password::sendResetLink(
['email' => $record->email]
);
}),
Actions\Action::make('view_on_stripe')
->label('View on Stripe')
->color('gray')
->icon('heroicon-o-arrow-top-right-on-square')
->url(fn (User $record) => 'https://dashboard.stripe.com/customers/'.$record->stripe_id)
->openUrlInNewTab()
->visible(fn (User $record) => filled($record->stripe_id)),
Actions\Action::make('view_on_anystack')
->label('View on Anystack')
->color('gray')
->icon('heroicon-o-arrow-top-right-on-square')
->url(fn (User $record) => 'https://app.anystack.sh/contacts/'.$record->anystack_contact_id)
->openUrlInNewTab()
->visible(fn (User $record) => filled($record->anystack_contact_id)),
Actions\DeleteAction::make(),
])
->label('Actions')
->icon('heroicon-m-ellipsis-vertical'),
];
}
}