-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPluginLicensesRelationManager.php
More file actions
127 lines (117 loc) · 4.97 KB
/
PluginLicensesRelationManager.php
File metadata and controls
127 lines (117 loc) · 4.97 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
<?php
namespace App\Filament\Resources\UserResource\RelationManagers;
use App\Actions\RefundPluginPurchase;
use App\Enums\PluginType;
use App\Models\PluginLicense;
use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class PluginLicensesRelationManager extends RelationManager
{
protected static string $relationship = 'pluginLicenses';
protected static ?string $title = 'Plugins';
public function form(Schema $schema): Schema
{
return $schema
->schema([
Forms\Components\Select::make('plugin_id')
->relationship('plugin', 'name')
->searchable()
->preload()
->required(),
Forms\Components\Toggle::make('is_grandfathered')
->label('Comped')
->default(true),
Forms\Components\DateTimePicker::make('purchased_at')
->default(now()),
Forms\Components\DateTimePicker::make('expires_at'),
]);
}
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('plugin.name')
->label('Plugin')
->searchable()
->sortable()
->fontFamily('mono'),
Tables\Columns\TextColumn::make('plugin.type')
->label('Type')
->badge()
->color(fn (PluginType $state): string => match ($state) {
PluginType::Free => 'gray',
PluginType::Paid => 'success',
}),
Tables\Columns\TextColumn::make('price_paid')
->label('Price Paid')
->money('usd', divideBy: 100)
->sortable(),
Tables\Columns\IconColumn::make('is_grandfathered')
->label('Comped')
->boolean(),
Tables\Columns\TextColumn::make('purchased_at')
->dateTime()
->sortable(),
Tables\Columns\TextColumn::make('expires_at')
->dateTime()
->sortable()
->placeholder('Never'),
Tables\Columns\TextColumn::make('refunded_at')
->label('Refunded')
->dateTime()
->sortable()
->placeholder('-'),
])
->defaultSort('purchased_at', 'desc')
->filters([
Tables\Filters\TernaryFilter::make('is_grandfathered')
->label('Comped'),
])
->headerActions([
Actions\CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['price_paid'] = 0;
$data['currency'] = 'USD';
return $data;
}),
])
->actions([
Actions\Action::make('refund')
->label('Refund')
->color('danger')
->requiresConfirmation()
->modalHeading('Refund purchase')
->modalDescription(function (PluginLicense $record): string {
$amount = '$'.number_format($record->price_paid / 100, 2);
$description = "This will issue a full {$amount} refund to {$record->user->email} for {$record->plugin->name} and revoke their license.";
if ($record->wasPurchasedAsBundle()) {
$description .= ' This license was purchased as part of a bundle — all licenses in the bundle will be refunded.';
}
return $description;
})
->modalSubmitActionLabel('Yes, refund')
->action(function (PluginLicense $record): void {
try {
app(RefundPluginPurchase::class)->handle($record, auth()->user());
Notification::make()
->title('Purchase refunded successfully')
->success()
->send();
} catch (\Exception $e) {
Notification::make()
->title('Refund failed')
->body($e->getMessage())
->danger()
->send();
}
})
->visible(fn (PluginLicense $record): bool => $record->isRefundable()),
Actions\DeleteAction::make(),
]);
}
}