-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathLicensesRelationManager.php
More file actions
69 lines (59 loc) · 2.76 KB
/
LicensesRelationManager.php
File metadata and controls
69 lines (59 loc) · 2.76 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
<?php
namespace App\Filament\Resources\PluginBundleResource\RelationManagers;
use App\Models\PluginLicense;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
class LicensesRelationManager extends RelationManager
{
protected static string $relationship = 'licenses';
protected static ?string $title = 'Bundle Purchases';
public function table(Table $table): Table
{
return $table
->modifyQueryUsing(function (Builder $query): void {
$bundleId = $this->ownerRecord->getKey();
$query
->select('plugin_licenses.*')
->addSelect([
'sale_total' => PluginLicense::query()
->selectRaw('SUM(price_paid)')
->whereColumn('user_id', 'plugin_licenses.user_id')
->whereColumn('purchased_at', 'plugin_licenses.purchased_at')
->where('plugin_bundle_id', $bundleId),
'sale_plugins_count' => PluginLicense::query()
->selectRaw('COUNT(*)')
->whereColumn('user_id', 'plugin_licenses.user_id')
->whereColumn('purchased_at', 'plugin_licenses.purchased_at')
->where('plugin_bundle_id', $bundleId),
])
->whereIn('plugin_licenses.id', function ($sub) use ($bundleId): void {
$sub->selectRaw('MIN(id)')
->from('plugin_licenses as pl')
->where('pl.plugin_bundle_id', $bundleId)
->groupBy('pl.user_id', 'pl.purchased_at');
});
})
->columns([
Tables\Columns\TextColumn::make('user.email')
->label('User')
->searchable(),
Tables\Columns\TextColumn::make('sale_plugins_count')
->label('Plugins')
->badge(),
Tables\Columns\TextColumn::make('sale_total')
->label('Total')
->formatStateUsing(fn (?int $state, PluginLicense $record): string => $record->is_grandfathered
? '$0.00'
: '$'.number_format(($state ?? 0) / 100, 2)),
Tables\Columns\IconColumn::make('is_grandfathered')
->label('Granted')
->boolean(),
Tables\Columns\TextColumn::make('purchased_at')
->dateTime()
->sortable(),
])
->defaultSort('purchased_at', 'desc');
}
}