feat: implement comprehensive multi-provider payment processing system
- Add unified payment provider architecture with contract-based design - Implement 6 payment providers: Stripe, Lemon Squeezy, Polar, Oxapay, Crypto, Activation Keys - Create subscription management with lifecycle handling (create, cancel, pause, resume, update) - Add coupon system with usage tracking and trial extensions - Build Filament admin resources for payment providers, subscriptions, coupons, and trials - Implement payment orchestration service with provider registry and configuration management - Add comprehensive payment logging and webhook handling for all providers - Create customer analytics dashboard with revenue, churn, and lifetime value metrics - Add subscription migration service for provider switching - Include extensive test coverage for all payment functionality
This commit is contained in:
347
app/Filament/Pages/CustomerAnalytics.php
Normal file
347
app/Filament/Pages/CustomerAnalytics.php
Normal file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\Concerns\InteractsWithActions;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Grid;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Concerns\InteractsWithTable;
|
||||
use Filament\Tables\Contracts\HasTable;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use UnitEnum;
|
||||
|
||||
class CustomerAnalytics extends Page implements HasForms, HasTable
|
||||
{
|
||||
use InteractsWithActions;
|
||||
use InteractsWithForms;
|
||||
use InteractsWithTable;
|
||||
|
||||
protected static string|null|\BackedEnum $navigationIcon = 'heroicon-o-chart-bar';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Analytics';
|
||||
|
||||
protected static ?int $navigationSort = 1;
|
||||
|
||||
protected string $view = 'filament.pages.customer-analytics';
|
||||
|
||||
public $startDate;
|
||||
|
||||
public $endDate;
|
||||
|
||||
public $selectedProvider = 'all';
|
||||
|
||||
public $selectedPlan = 'all';
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->startDate = now()->subDays(30)->format('Y-m-d');
|
||||
$this->endDate = now()->format('Y-m-d');
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return 'Customer Analytics';
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('export_report')
|
||||
->label('Export Report')
|
||||
->icon('heroicon-o-arrow-down-tray')
|
||||
->color('success')
|
||||
->action(fn () => $this->exportReport()),
|
||||
|
||||
Action::make('refresh')
|
||||
->label('Refresh')
|
||||
->icon('heroicon-o-arrow-path')
|
||||
->color('gray')
|
||||
->action(fn () => $this->resetTable()),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderWidgets(): array
|
||||
{
|
||||
return [
|
||||
\App\Filament\Widgets\CustomerAnalyticsOverview::class,
|
||||
\App\Filament\Widgets\SubscriptionMetrics::class,
|
||||
\App\Filament\Widgets\CouponPerformanceMetrics::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Grid::make(4)
|
||||
->schema([
|
||||
DatePicker::make('startDate')
|
||||
->label('Start Date')
|
||||
->default(fn () => now()->subDays(30))
|
||||
->required(),
|
||||
|
||||
DatePicker::make('endDate')
|
||||
->label('End Date')
|
||||
->default(fn () => now())
|
||||
->required(),
|
||||
|
||||
Select::make('selectedProvider')
|
||||
->label('Provider')
|
||||
->options([
|
||||
'all' => 'All Providers',
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
])
|
||||
->default('all'),
|
||||
|
||||
Select::make('selectedPlan')
|
||||
->label('Plan')
|
||||
->options(function () {
|
||||
$plans = DB::table('plans')->pluck('name', 'id');
|
||||
|
||||
return ['all' => 'All Plans'] + $plans->toArray();
|
||||
})
|
||||
->default('all'),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->query(
|
||||
$this->getCustomerAnalyticsQuery()
|
||||
)
|
||||
->columns([
|
||||
TextColumn::make('user_name')
|
||||
->label('Customer')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->weight('medium'),
|
||||
|
||||
TextColumn::make('user_email')
|
||||
->label('Email')
|
||||
->searchable()
|
||||
->copyable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('plan_name')
|
||||
->label('Plan')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->badge()
|
||||
->color('primary'),
|
||||
|
||||
TextColumn::make('provider')
|
||||
->label('Provider')
|
||||
->badge()
|
||||
->colors([
|
||||
'blue' => 'stripe',
|
||||
'green' => 'lemon_squeezy',
|
||||
'purple' => 'polar',
|
||||
'orange' => 'oxapay',
|
||||
'gray' => 'crypto',
|
||||
'pink' => 'activation_key',
|
||||
]),
|
||||
|
||||
TextColumn::make('status')
|
||||
->label('Status')
|
||||
->badge()
|
||||
->colors([
|
||||
'success' => 'active',
|
||||
'warning' => 'trialing',
|
||||
'danger' => 'cancelled',
|
||||
'secondary' => 'paused',
|
||||
'gray' => 'incomplete',
|
||||
]),
|
||||
|
||||
TextColumn::make('subscription_age')
|
||||
->label('Age')
|
||||
->formatStateUsing(function ($record) {
|
||||
$started = $record->starts_at ?? $record->created_at;
|
||||
|
||||
return $started ? $started->diffForHumans() : 'Unknown';
|
||||
})
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('total_coupon_discount')
|
||||
->label('Total Discount')
|
||||
->money('USD')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('trial_extensions_count')
|
||||
->label('Trial Extensions')
|
||||
->formatStateUsing(fn ($record) => $record->trial_extensions_count ?? 0)
|
||||
->alignCenter()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('subscription_changes_count')
|
||||
->label('Changes')
|
||||
->formatStateUsing(fn ($record) => $record->subscription_changes_count ?? 0)
|
||||
->alignCenter()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('mrr')
|
||||
->label('MRR')
|
||||
->money('USD')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc')
|
||||
->filters([
|
||||
SelectFilter::make('provider')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
]),
|
||||
|
||||
SelectFilter::make('status')
|
||||
->options([
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'paused' => 'Paused',
|
||||
'incomplete' => 'Incomplete',
|
||||
]),
|
||||
|
||||
Filter::make('date_range')
|
||||
->form([
|
||||
DatePicker::make('start_date')
|
||||
->label('Start Date')
|
||||
->required(),
|
||||
DatePicker::make('end_date')
|
||||
->label('End Date')
|
||||
->required(),
|
||||
])
|
||||
->query(function (Builder $query, array $data): Builder {
|
||||
return $query
|
||||
->when(
|
||||
$data['start_date'],
|
||||
fn (Builder $query, $date): Builder => $query->whereDate('subscriptions.created_at', '>=', $date)
|
||||
)
|
||||
->when(
|
||||
$data['end_date'],
|
||||
fn (Builder $query, $date): Builder => $query->whereDate('subscriptions.created_at', '<=', $date)
|
||||
);
|
||||
}),
|
||||
|
||||
Filter::make('has_coupon_usage')
|
||||
->label('Has Coupon Usage')
|
||||
->query(fn (Builder $query): Builder => $query->whereHas('couponUsages')),
|
||||
|
||||
Filter::make('has_trial_extension')
|
||||
->label('Has Trial Extension')
|
||||
->query(fn (Builder $query): Builder => $query->whereHas('trialExtensions')),
|
||||
])
|
||||
->emptyStateHeading('No customer data found')
|
||||
->emptyStateDescription('No customer analytics data available for the selected filters.')
|
||||
->emptyStateActions([
|
||||
Action::make('reset_filters')
|
||||
->label('Reset Filters')
|
||||
->icon('heroicon-o-arrow-path')
|
||||
->action(fn () => $this->resetTable()),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getCustomerAnalyticsQuery(): Builder
|
||||
{
|
||||
return Subscription::query()
|
||||
->with(['user', 'plan', 'couponUsages', 'trialExtensions', 'subscriptionChanges'])
|
||||
->withCount(['couponUsages', 'trialExtensions', 'subscriptionChanges'])
|
||||
->when($this->selectedProvider !== 'all', function ($query) {
|
||||
$query->where('provider', $this->selectedProvider);
|
||||
})
|
||||
->when($this->selectedPlan !== 'all', function ($query) {
|
||||
$query->where('plan_id', $this->selectedPlan);
|
||||
})
|
||||
->when($this->startDate, function ($query) {
|
||||
$query->whereDate('subscriptions.created_at', '>=', $this->startDate);
|
||||
})
|
||||
->when($this->endDate, function ($query) {
|
||||
$query->whereDate('subscriptions.created_at', '<=', $this->endDate);
|
||||
});
|
||||
}
|
||||
|
||||
public function exportReport()
|
||||
{
|
||||
$subscriptions = $this->getCustomerAnalyticsQuery()->get();
|
||||
|
||||
$filename = 'customer_analytics_'.now()->format('Y_m_d_H_i_s').'.csv';
|
||||
|
||||
// Create a temporary file
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
|
||||
// Add BOM for Excel UTF-8 support
|
||||
fwrite($handle, "\xEF\xBB\xBF");
|
||||
|
||||
// Write headers
|
||||
$headers = [
|
||||
'Customer', 'Email', 'Plan', 'Provider', 'Status', 'Subscription Age',
|
||||
'Total Discount', 'Trial Extensions', 'Subscription Changes', 'MRR',
|
||||
'Created At', 'Trial Ends At', 'Ends At',
|
||||
];
|
||||
fputcsv($handle, $headers);
|
||||
|
||||
// Write data rows
|
||||
foreach ($subscriptions as $subscription) {
|
||||
$started = $subscription->starts_at ?? $subscription->created_at;
|
||||
fputcsv($handle, [
|
||||
$subscription->user?->name ?? 'Unknown',
|
||||
$subscription->user?->email ?? 'Unknown',
|
||||
$subscription->plan?->name ?? 'Unknown',
|
||||
$subscription->provider,
|
||||
$subscription->status,
|
||||
$started ? $started->diffForHumans() : 'Unknown',
|
||||
$subscription->couponUsages()->sum('discount_amount'),
|
||||
$subscription->trialExtensions()->count(),
|
||||
$subscription->subscriptionChanges()->count(),
|
||||
$subscription->plan?->monthly_price ?? 0,
|
||||
$subscription->created_at->toDateTimeString(),
|
||||
$subscription->trial_ends_at?->toDateTimeString() ?? 'N/A',
|
||||
$subscription->ends_at?->toDateTimeString() ?? 'N/A',
|
||||
]);
|
||||
}
|
||||
|
||||
// Rewind the file pointer
|
||||
rewind($handle);
|
||||
|
||||
// Get the CSV content
|
||||
$csvContent = stream_get_contents($handle);
|
||||
|
||||
// Close the file handle
|
||||
fclose($handle);
|
||||
|
||||
// Return a download response
|
||||
return response()->streamDownload(
|
||||
function () use ($csvContent) {
|
||||
echo $csvContent;
|
||||
},
|
||||
$filename,
|
||||
[
|
||||
'Content-Type' => 'text/csv',
|
||||
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
57
app/Filament/Resources/Coupons/CouponResource.php
Normal file
57
app/Filament/Resources/Coupons/CouponResource.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons;
|
||||
|
||||
use App\Filament\Resources\Coupons\Pages\CreateCoupon;
|
||||
use App\Filament\Resources\Coupons\Pages\EditCoupon;
|
||||
use App\Filament\Resources\Coupons\Pages\ListCoupons;
|
||||
use App\Filament\Resources\Coupons\Schemas\CouponForm;
|
||||
use App\Filament\Resources\Coupons\Tables\CouponsTable;
|
||||
use App\Models\Coupon;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class CouponResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Coupon::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedTicket;
|
||||
|
||||
protected static ?string $navigationLabel = 'Coupons';
|
||||
|
||||
protected static ?string $modelLabel = 'Coupon';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Coupons';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return CouponForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return CouponsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListCoupons::route('/'),
|
||||
'create' => CreateCoupon::route('/create'),
|
||||
'edit' => EditCoupon::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
11
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateCoupon extends CreateRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
19
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditCoupon extends EditRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Coupons/Pages/ListCoupons.php
Normal file
19
app/Filament/Resources/Coupons/Pages/ListCoupons.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListCoupons extends ListRecords
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
140
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
140
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Schemas;
|
||||
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CouponForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('code')
|
||||
->label('Coupon Code')
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->formatStateUsing(fn ($state) => Str::upper($state))
|
||||
->helperText('This code will be entered by customers to apply the discount'),
|
||||
|
||||
TextInput::make('name')
|
||||
->label('Display Name')
|
||||
->required()
|
||||
->helperText('Internal name for this coupon'),
|
||||
]),
|
||||
|
||||
Textarea::make('description')
|
||||
->label('Description')
|
||||
->rows(2)
|
||||
->helperText('Optional description for internal reference'),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('type')
|
||||
->label('Discount Type')
|
||||
->options([
|
||||
'percentage' => 'Percentage',
|
||||
'fixed' => 'Fixed Amount',
|
||||
])
|
||||
->required()
|
||||
->reactive()
|
||||
->afterStateUpdated(fn ($state, callable $set) => $set('value', $state === 'percentage' ? 10 : 10.00)
|
||||
),
|
||||
|
||||
TextInput::make('value')
|
||||
->label('Discount Value')
|
||||
->required()
|
||||
->numeric()
|
||||
->step(fn ($get) => $get('type') === 'percentage' ? 1 : 0.01)
|
||||
->suffix(fn ($get) => $get('type') === 'percentage' ? '%' : '$')
|
||||
->helperText(fn ($get) => $get('type') === 'percentage'
|
||||
? 'Percentage discount (e.g., 10 for 10%)'
|
||||
: 'Fixed amount discount in USD'
|
||||
),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Usage Limits')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('max_uses')
|
||||
->label('Maximum Uses')
|
||||
->numeric()
|
||||
->helperText('Leave blank for unlimited uses'),
|
||||
|
||||
TextInput::make('max_uses_per_user')
|
||||
->label('Max Uses Per User')
|
||||
->numeric()
|
||||
->helperText('Limit how many times one user can use this coupon'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('minimum_amount')
|
||||
->label('Minimum Order Amount')
|
||||
->numeric()
|
||||
->step(0.01)
|
||||
->prefix('$')
|
||||
->helperText('Minimum order amount required to use this coupon'),
|
||||
|
||||
TextInput::make('uses_count')
|
||||
->label('Current Uses')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->disabled()
|
||||
->helperText('Number of times this coupon has been used'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Schedule')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('starts_at')
|
||||
->label('Start Date')
|
||||
->helperText('When this coupon becomes active'),
|
||||
|
||||
DateTimePicker::make('expires_at')
|
||||
->label('Expiration Date')
|
||||
->helperText('When this coupon expires'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Settings')
|
||||
->schema([
|
||||
Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true)
|
||||
->helperText('Only active coupons can be used'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Metadata')
|
||||
->schema([
|
||||
KeyValue::make('metadata')
|
||||
->label('Custom Metadata')
|
||||
->addActionLabel('Add metadata')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('Additional key-value data for this coupon'),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
152
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
152
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class CouponsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('code')
|
||||
->label('Code')
|
||||
->searchable()
|
||||
->copyable()
|
||||
->copyMessage('Coupon code copied')
|
||||
->copyMessageDuration(1500),
|
||||
|
||||
TextColumn::make('name')
|
||||
->label('Name')
|
||||
->searchable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'blue' => 'percentage',
|
||||
'green' => 'fixed',
|
||||
]),
|
||||
|
||||
TextColumn::make('formatted_discount')
|
||||
->label('Discount')
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('uses_count')
|
||||
->label('Used')
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('remaining_uses')
|
||||
->label('Remaining')
|
||||
->getStateUsing(fn ($record) => $record->remaining_uses ?? '∞')
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('expires_at')
|
||||
->label('Expires')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color(fn ($record): string => $record->isExpiringSoon() ? 'warning' : 'default')
|
||||
->description(fn ($record): string => $record->expires_at ? $record->expires_at->diffForHumans() : ''
|
||||
),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('type')
|
||||
->options([
|
||||
'percentage' => 'Percentage',
|
||||
'fixed' => 'Fixed Amount',
|
||||
]),
|
||||
|
||||
SelectFilter::make('is_active')
|
||||
->options([
|
||||
'1' => 'Active',
|
||||
'0' => 'Inactive',
|
||||
]),
|
||||
|
||||
SelectFilter::make('status')
|
||||
->label('Status')
|
||||
->options([
|
||||
'valid' => 'Valid',
|
||||
'expired' => 'Expired',
|
||||
'used_up' => 'Used Up',
|
||||
])
|
||||
->query(fn ($query, $data) => match ($data['value']) {
|
||||
'valid' => $query->valid(),
|
||||
'expired' => $query->where('expires_at', '<', now()),
|
||||
'used_up' => $query->whereRaw('uses_count >= max_uses'),
|
||||
default => $query,
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('duplicate')
|
||||
->label('Duplicate')
|
||||
->icon('heroicon-o-document-duplicate')
|
||||
->color('gray')
|
||||
->action(function ($record) {
|
||||
$newCoupon = $record->replicate();
|
||||
$newCoupon->code = $newCoupon->code.'_COPY';
|
||||
$newCoupon->uses_count = 0;
|
||||
$newCoupon->save();
|
||||
})
|
||||
->successNotificationTitle('Coupon duplicated successfully'),
|
||||
|
||||
Action::make('view_usage')
|
||||
->label('View Usage')
|
||||
->icon('heroicon-o-chart-bar')
|
||||
->color('blue')
|
||||
->url(fn ($record) => route('filament.admin.resources.coupons.usage', $record)),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
|
||||
BulkAction::make('bulk_deactivate')
|
||||
->label('Deactivate')
|
||||
->icon('heroicon-o-x-circle')
|
||||
->color('danger')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['is_active' => false]);
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkAction::make('bulk_activate')
|
||||
->label('Activate')
|
||||
->icon('heroicon-o-check-circle')
|
||||
->color('success')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['is_active' => true]);
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
])
|
||||
->poll('60s');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreatePaymentProvider extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPaymentProvider extends EditRecord
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\PaymentProviderResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListPaymentProviders extends ListRecords
|
||||
{
|
||||
protected static string $resource = PaymentProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders;
|
||||
|
||||
use App\Filament\Resources\PaymentProviders\Pages\CreatePaymentProvider;
|
||||
use App\Filament\Resources\PaymentProviders\Pages\EditPaymentProvider;
|
||||
use App\Filament\Resources\PaymentProviders\Pages\ListPaymentProviders;
|
||||
use App\Filament\Resources\PaymentProviders\Schemas\PaymentProviderForm;
|
||||
use App\Filament\Resources\PaymentProviders\Tables\PaymentProvidersTable;
|
||||
use App\Models\PaymentProvider;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PaymentProviderResource extends Resource
|
||||
{
|
||||
protected static ?string $model = PaymentProvider::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCube;
|
||||
|
||||
protected static ?string $navigationLabel = 'Payment Providers';
|
||||
|
||||
protected static ?string $modelLabel = 'Payment Provider';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Payment Providers';
|
||||
|
||||
protected static string|null|\UnitEnum $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return PaymentProviderForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return PaymentProvidersTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListPaymentProviders::route('/'),
|
||||
'create' => CreatePaymentProvider::route('/create'),
|
||||
'edit' => EditPaymentProvider::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Schemas;
|
||||
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class PaymentProviderForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label('Provider Name')
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->helperText('Internal identifier for the provider'),
|
||||
|
||||
TextInput::make('display_name')
|
||||
->label('Display Name')
|
||||
->required()
|
||||
->helperText('Name shown to users'),
|
||||
]),
|
||||
|
||||
Textarea::make('description')
|
||||
->label('Description')
|
||||
->rows(3)
|
||||
->helperText('Brief description of the payment provider')
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
|
||||
Section::make('Capabilities')
|
||||
->schema([
|
||||
Grid::make(3)
|
||||
->schema([
|
||||
Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true)
|
||||
->helperText('Enable this provider for use'),
|
||||
|
||||
Toggle::make('supports_recurring')
|
||||
->label('Supports Recurring')
|
||||
->default(false)
|
||||
->helperText('Can handle subscription payments'),
|
||||
|
||||
Toggle::make('supports_one_time')
|
||||
->label('Supports One-Time')
|
||||
->default(true)
|
||||
->helperText('Can handle single payments'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('priority')
|
||||
->label('Priority')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->helperText('Higher priority = shown first'),
|
||||
|
||||
Toggle::make('is_fallback')
|
||||
->label('Fallback Provider')
|
||||
->default(false)
|
||||
->helperText('Default provider when others fail'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Configuration')
|
||||
->schema([
|
||||
KeyValue::make('configuration')
|
||||
->label('Provider Configuration')
|
||||
->addActionLabel('Add configuration')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('API keys and other provider-specific settings')
|
||||
->columnSpanFull(),
|
||||
|
||||
KeyValue::make('supported_currencies')
|
||||
->label('Supported Currencies')
|
||||
->addActionLabel('Add currency')
|
||||
->keyLabel('Currency Code')
|
||||
->valueLabel('Display Name')
|
||||
->default(['USD' => 'US Dollar'])
|
||||
->helperText('Currencies this provider supports')
|
||||
->columnSpanFull(),
|
||||
|
||||
KeyValue::make('fee_structure')
|
||||
->label('Fee Structure')
|
||||
->addActionLabel('Add fee setting')
|
||||
->keyLabel('Fee Type')
|
||||
->valueLabel('Value')
|
||||
->helperText('Example: fixed_fee, percentage_fee')
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
|
||||
Section::make('Webhook Settings')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('webhook_url')
|
||||
->label('Webhook URL')
|
||||
->url()
|
||||
->helperText('Endpoint for provider webhooks'),
|
||||
|
||||
TextInput::make('webhook_secret')
|
||||
->label('Webhook Secret')
|
||||
->password()
|
||||
->helperText('Secret for webhook validation'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\PaymentProviders\Tables;
|
||||
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PaymentProvidersTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')
|
||||
->label('Provider')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->weight('bold'),
|
||||
|
||||
TextColumn::make('display_name')
|
||||
->label('Display Name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('supports_recurring')
|
||||
->label('Recurring')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('supports_one_time')
|
||||
->label('One-Time')
|
||||
->boolean()
|
||||
->sortable(),
|
||||
|
||||
IconColumn::make('is_fallback')
|
||||
->label('Fallback')
|
||||
->boolean()
|
||||
->sortable()
|
||||
->color(fn ($record) => $record->is_fallback ? 'warning' : null),
|
||||
|
||||
TextColumn::make('priority')
|
||||
->label('Priority')
|
||||
->numeric()
|
||||
->sortable()
|
||||
->alignCenter(),
|
||||
|
||||
TextColumn::make('supported_currencies')
|
||||
->label('Currencies')
|
||||
->formatStateUsing(fn ($state) => is_array($state) ? implode(', ', array_keys($state)) : '')
|
||||
->limitList(2)
|
||||
->separator(', ')
|
||||
->tooltip(fn ($record) => is_array($record->supported_currencies) ? implode(', ', array_keys($record->supported_currencies)) : ''),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
|
||||
TextColumn::make('updated_at')
|
||||
->label('Updated')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('is_active')
|
||||
->label('Status')
|
||||
->options([
|
||||
'1' => 'Active',
|
||||
'0' => 'Inactive',
|
||||
]),
|
||||
|
||||
SelectFilter::make('supports_recurring')
|
||||
->label('Recurring Support')
|
||||
->options([
|
||||
'1' => 'Supports Recurring',
|
||||
'0' => 'No Recurring Support',
|
||||
]),
|
||||
|
||||
SelectFilter::make('supports_one_time')
|
||||
->label('One-Time Support')
|
||||
->options([
|
||||
'1' => 'Supports One-Time',
|
||||
'0' => 'No One-Time Support',
|
||||
]),
|
||||
|
||||
SelectFilter::make('is_fallback')
|
||||
->label('Fallback Status')
|
||||
->options([
|
||||
'1' => 'Is Fallback',
|
||||
'0' => 'Not Fallback',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('test_connection')
|
||||
->label('Test Connection')
|
||||
->icon('heroicon-o-signal')
|
||||
->color('info')
|
||||
->action(function ($record) {
|
||||
$result = $record->testConnection();
|
||||
|
||||
if ($result['success']) {
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Connection Test Successful')
|
||||
->body('Provider is configured and responding correctly.')
|
||||
->success()
|
||||
->send();
|
||||
} else {
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Connection Test Failed')
|
||||
->body($result['error'])
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
|
||||
BulkAction::make('activate')
|
||||
->label('Activate Selected')
|
||||
->icon('heroicon-o-check')
|
||||
->action(function ($records) {
|
||||
$records->each->update(['is_active' => true]);
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Providers Activated')
|
||||
->body('Selected payment providers have been activated.')
|
||||
->success()
|
||||
->send();
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkAction::make('deactivate')
|
||||
->label('Deactivate Selected')
|
||||
->icon('heroicon-o-x-mark')
|
||||
->action(function ($records) {
|
||||
$records->where('is_fallback', false)->each->update(['is_active' => false]);
|
||||
\Filament\Notifications\Notification::make()
|
||||
->title('Providers Deactivated')
|
||||
->body('Selected payment providers have been deactivated (fallback providers were skipped).')
|
||||
->warning()
|
||||
->send();
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
])
|
||||
->emptyStateDescription('No payment providers configured yet.')
|
||||
->emptyStateHeading('No Payment Providers')
|
||||
->emptyStateIcon('heroicon-o-rectangle-stack');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateSubscription extends CreateRecord
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditSubscription extends EditRecord
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\SubscriptionResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListSubscriptions extends ListRecords
|
||||
{
|
||||
protected static string $resource = SubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Schemas;
|
||||
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class SubscriptionForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Basic Information')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('user_id')
|
||||
->label('User')
|
||||
->relationship('user', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->required()
|
||||
->createOptionForm([
|
||||
TextInput::make('name')
|
||||
->required(),
|
||||
TextInput::make('email')
|
||||
->email()
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Select::make('plan_id')
|
||||
->label('Plan')
|
||||
->relationship('plan', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('type')
|
||||
->label('Subscription Type')
|
||||
->options([
|
||||
'default' => 'Default',
|
||||
'premium' => 'Premium',
|
||||
'enterprise' => 'Enterprise',
|
||||
'trial' => 'Trial',
|
||||
])
|
||||
->required()
|
||||
->default('default')
|
||||
->helperText('Type of subscription'),
|
||||
|
||||
Select::make('status')
|
||||
->label('Status')
|
||||
->options([
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'paused' => 'Paused',
|
||||
'incomplete' => 'Incomplete',
|
||||
])
|
||||
->required()
|
||||
->default('active'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('provider')
|
||||
->label('Provider')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
])
|
||||
->required()
|
||||
->default('stripe'),
|
||||
|
||||
TextInput::make('provider_subscription_id')
|
||||
->label('Provider Subscription ID'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Trial Management')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('trial_ends_at')
|
||||
->label('Trial Ends At'),
|
||||
|
||||
TextInput::make('quantity')
|
||||
->label('Quantity')
|
||||
->numeric()
|
||||
->default(1),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Billing Dates')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('starts_at')
|
||||
->label('Starts At'),
|
||||
|
||||
DateTimePicker::make('ends_at')
|
||||
->label('Ends At'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('cancelled_at')
|
||||
->label('Cancelled At'),
|
||||
|
||||
DateTimePicker::make('paused_at')
|
||||
->label('Paused At'),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('resumed_at')
|
||||
->label('Resumed At'),
|
||||
|
||||
DateTimePicker::make('last_provider_sync')
|
||||
->label('Last Provider Sync'),
|
||||
]),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Cancellation Details')
|
||||
->schema([
|
||||
Textarea::make('cancellation_reason')
|
||||
->label('Reason'),
|
||||
])
|
||||
->collapsible()
|
||||
->visible(fn ($get) => $get('status') === 'cancelled'),
|
||||
|
||||
Section::make('Provider Information')
|
||||
->schema([
|
||||
TextInput::make('stripe_id')
|
||||
->label('Stripe ID')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
TextInput::make('stripe_status')
|
||||
->label('Stripe Status')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
TextInput::make('stripe_price')
|
||||
->label('Stripe Price')
|
||||
->visible(fn ($get) => $get('provider') === 'stripe'),
|
||||
|
||||
Textarea::make('provider_data')
|
||||
->label('Provider Data')
|
||||
->rows(3)
|
||||
->helperText('JSON data from the payment provider'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Migration Information')
|
||||
->schema([
|
||||
TextInput::make('migration_batch_id')
|
||||
->label('Migration Batch ID'),
|
||||
|
||||
Toggle::make('is_migrated')
|
||||
->label('Is Migrated'),
|
||||
|
||||
Textarea::make('legacy_data')
|
||||
->label('Legacy Data')
|
||||
->rows(3),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions;
|
||||
|
||||
use App\Filament\Resources\Subscriptions\Pages\CreateSubscription;
|
||||
use App\Filament\Resources\Subscriptions\Pages\EditSubscription;
|
||||
use App\Filament\Resources\Subscriptions\Pages\ListSubscriptions;
|
||||
use App\Filament\Resources\Subscriptions\Schemas\SubscriptionForm;
|
||||
use App\Filament\Resources\Subscriptions\Tables\SubscriptionsTable;
|
||||
use App\Models\Subscription;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SubscriptionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Subscription::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedDocumentCurrencyDollar;
|
||||
|
||||
protected static string|null|\UnitEnum $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return SubscriptionForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return SubscriptionsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListSubscriptions::route('/'),
|
||||
'create' => CreateSubscription::route('/create'),
|
||||
'edit' => EditSubscription::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Subscriptions\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SubscriptionsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->modifyQueryUsing(function ($query) {
|
||||
$query->withSum('couponUsages as total_coupon_discount', 'discount_amount');
|
||||
})
|
||||
->columns([
|
||||
TextColumn::make('user.name')
|
||||
->label('User')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('plan.name')
|
||||
->label('Plan')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'gray' => 'default',
|
||||
'blue' => 'premium',
|
||||
'purple' => 'enterprise',
|
||||
'warning' => 'trial',
|
||||
]),
|
||||
|
||||
TextColumn::make('status')
|
||||
->badge()
|
||||
->label('Status')
|
||||
->colors([
|
||||
'success' => 'active',
|
||||
'warning' => 'trialing',
|
||||
'danger' => 'cancelled',
|
||||
'secondary' => 'paused',
|
||||
'gray' => 'incomplete',
|
||||
]),
|
||||
|
||||
TextColumn::make('provider')
|
||||
->badge()
|
||||
->label('Provider')
|
||||
->colors([
|
||||
'blue' => 'stripe',
|
||||
'green' => 'lemon_squeezy',
|
||||
'purple' => 'polar',
|
||||
'orange' => 'oxapay',
|
||||
'gray' => 'crypto',
|
||||
'pink' => 'activation_key',
|
||||
]),
|
||||
|
||||
TextColumn::make('provider_subscription_id')
|
||||
->label('Provider ID')
|
||||
->searchable()
|
||||
->limit(20),
|
||||
|
||||
TextColumn::make('trial_ends_at')
|
||||
->label('Trial Ends')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color('warning')
|
||||
->description(fn ($record): string => $record->isOnTrial() ? $record->trial_ends_at->diffForHumans() : ''
|
||||
),
|
||||
|
||||
TextColumn::make('ends_at')
|
||||
->label('Ends At')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color(fn ($record): string => $record->ends_at && $record->ends_at->isPast() ? 'danger' : 'default'
|
||||
),
|
||||
|
||||
TextColumn::make('quantity')
|
||||
->label('Qty')
|
||||
->numeric()
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
IconColumn::make('hasExtendedTrial')
|
||||
->label('Trial Extended')
|
||||
->boolean()
|
||||
->getStateUsing(fn ($record) => $record->hasExtendedTrial())
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('total_coupon_discount')
|
||||
->label('Total Discount')
|
||||
->money('USD')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Created')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
|
||||
TextColumn::make('last_provider_sync')
|
||||
->label('Last Sync')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->options([
|
||||
'active' => 'Active',
|
||||
'trialing' => 'Trial',
|
||||
'cancelled' => 'Cancelled',
|
||||
'paused' => 'Paused',
|
||||
'incomplete' => 'Incomplete',
|
||||
]),
|
||||
|
||||
SelectFilter::make('type')
|
||||
->label('Subscription Type')
|
||||
->options([
|
||||
'default' => 'Default',
|
||||
'premium' => 'Premium',
|
||||
'enterprise' => 'Enterprise',
|
||||
'trial' => 'Trial',
|
||||
]),
|
||||
|
||||
SelectFilter::make('provider')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'lemon_squeezy' => 'Lemon Squeezy',
|
||||
'polar' => 'Polar.sh',
|
||||
'oxapay' => 'OxaPay',
|
||||
'crypto' => 'Crypto',
|
||||
'activation_key' => 'Activation Key',
|
||||
]),
|
||||
|
||||
SelectFilter::make('has_trial_extension')
|
||||
->label('Has Trial Extension')
|
||||
->options([
|
||||
'yes' => 'Yes',
|
||||
'no' => 'No',
|
||||
])
|
||||
->query(fn ($query, $data) => match ($data['value']) {
|
||||
'yes' => $query->whereHas('trialExtensions'),
|
||||
'no' => $query->whereDoesntHave('trialExtensions'),
|
||||
default => $query,
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('extend_trial')
|
||||
->label('Extend Trial')
|
||||
->icon('heroicon-o-clock')
|
||||
->color('warning')
|
||||
->schema([
|
||||
\Filament\Forms\Components\TextInput::make('days')
|
||||
->label('Days to Extend')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7),
|
||||
\Filament\Forms\Components\TextInput::make('reason')
|
||||
->label('Reason')
|
||||
->required(),
|
||||
\Filament\Forms\Components\Select::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
])
|
||||
->default('manual'),
|
||||
])
|
||||
->action(function (array $data, $record) {
|
||||
$record->extendTrial(
|
||||
(int) $data['days'],
|
||||
$data['reason'],
|
||||
$data['extension_type'],
|
||||
auth()->user()
|
||||
);
|
||||
})
|
||||
->visible(fn ($record) => $record->isOnTrial()),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkAction::make('bulk_extend_trial')
|
||||
->label('Bulk Extend Trial')
|
||||
->icon('heroicon-o-clock')
|
||||
->color('warning')
|
||||
->schema([
|
||||
\Filament\Forms\Components\TextInput::make('days')
|
||||
->label('Days to Extend')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7),
|
||||
\Filament\Forms\Components\TextInput::make('reason')
|
||||
->label('Reason')
|
||||
->required(),
|
||||
])
|
||||
->action(function (array $data, \Illuminate\Support\Collection $records) {
|
||||
foreach ($records as $record) {
|
||||
if ($record->isOnTrial()) {
|
||||
$record->extendTrial(
|
||||
(int) $data['days'],
|
||||
$data['reason'],
|
||||
'manual',
|
||||
auth()->user()
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use App\Models\Subscription;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateTrialExtension extends CreateRecord
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
/**
|
||||
* Ensure user_id and original_trial_ends_at are always set from subscription before creating
|
||||
*/
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
// Always ensure user_id and original_trial_ends_at are set from subscription
|
||||
if (isset($data['subscription_id'])) {
|
||||
$subscription = Subscription::find($data['subscription_id']);
|
||||
if ($subscription) {
|
||||
$data['user_id'] = $subscription->user_id;
|
||||
$data['original_trial_ends_at'] = $subscription->trial_ends_at;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditTrialExtension extends EditRecord
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Pages;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListTrialExtensions extends ListRecords
|
||||
{
|
||||
protected static string $resource = TrialExtensionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Schemas;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class TrialExtensionForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Trial Extension Details')
|
||||
->schema([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Select::make('subscription_id')
|
||||
->label('Subscription')
|
||||
->options(function () {
|
||||
return Subscription::with(['user', 'plan'])
|
||||
->where('status', 'trialing')
|
||||
->orWhere('status', 'active')
|
||||
->get()
|
||||
->mapWithKeys(function ($subscription) {
|
||||
$label = "{$subscription->user->name} - {$subscription->plan->name}";
|
||||
if ($subscription->trial_ends_at) {
|
||||
$label .= " ({$subscription->trial_ends_at->format('M j, Y')})";
|
||||
}
|
||||
return [$subscription->id => $label];
|
||||
})
|
||||
->toArray();
|
||||
})
|
||||
->searchable()
|
||||
->preload()
|
||||
->required()
|
||||
->live()
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if ($state) {
|
||||
$subscription = Subscription::find($state);
|
||||
if ($subscription) {
|
||||
$set('user_id', $subscription->user_id);
|
||||
$set('original_trial_ends_at', $subscription->trial_ends_at);
|
||||
|
||||
// Set user display name
|
||||
if ($subscription->relationLoaded('user')) {
|
||||
$set('user_display', $subscription->user->name);
|
||||
} else {
|
||||
$subscription->load('user');
|
||||
$set('user_display', $subscription->user->name);
|
||||
}
|
||||
|
||||
self::calculateNewTrialEndDate($set, $get);
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
TextInput::make('user_display')
|
||||
->label('User')
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
]),
|
||||
|
||||
// Hidden field for user_id to ensure it gets submitted
|
||||
TextInput::make('user_id')
|
||||
->hidden(),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
TextInput::make('extension_days')
|
||||
->label('Extension Days')
|
||||
->numeric()
|
||||
->required()
|
||||
->default(7)
|
||||
->live(onBlur: true)
|
||||
->helperText('Number of days to extend the trial')
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) => self::calculateNewTrialEndDate($set, $get)),
|
||||
|
||||
Select::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
])
|
||||
->default('manual')
|
||||
->required(),
|
||||
]),
|
||||
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
DateTimePicker::make('original_trial_ends_at')
|
||||
->label('Original Trial End Date')
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
|
||||
DateTimePicker::make('new_trial_ends_at')
|
||||
->label('New Trial End Date')
|
||||
->required()
|
||||
->readOnly()
|
||||
->helperText('This will be calculated automatically based on extension days'),
|
||||
]),
|
||||
]),
|
||||
|
||||
Section::make('Additional Information')
|
||||
->schema([
|
||||
Textarea::make('reason')
|
||||
->label('Reason')
|
||||
->rows(3)
|
||||
->helperText('Reason for granting this trial extension'),
|
||||
|
||||
Select::make('granted_by_admin_id')
|
||||
->label('Granted By')
|
||||
->relationship('grantedByAdmin', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->default(fn () => auth()->id())
|
||||
->helperText('Admin who granted this extension'),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Timestamps')
|
||||
->schema([
|
||||
DateTimePicker::make('granted_at')
|
||||
->label('Granted At')
|
||||
->default(now())
|
||||
->required(),
|
||||
])
|
||||
->collapsible(),
|
||||
|
||||
Section::make('Metadata')
|
||||
->schema([
|
||||
KeyValue::make('metadata')
|
||||
->label('Custom Metadata')
|
||||
->addActionLabel('Add metadata')
|
||||
->keyLabel('Key')
|
||||
->valueLabel('Value')
|
||||
->helperText('Additional key-value data for this trial extension'),
|
||||
])
|
||||
->collapsible(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the new trial end date based on original date and extension days
|
||||
*/
|
||||
private static function calculateNewTrialEndDate(callable $set, callable $get): void
|
||||
{
|
||||
$subscriptionId = $get('subscription_id');
|
||||
$extensionDays = (int) $get('extension_days') ?: 0;
|
||||
|
||||
if (! $subscriptionId || ! $extensionDays) {
|
||||
$set('new_trial_ends_at', null);
|
||||
return;
|
||||
}
|
||||
|
||||
$subscription = Subscription::find($subscriptionId);
|
||||
if (! $subscription) {
|
||||
$set('new_trial_ends_at', null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Use the original trial end date if available, otherwise use current date
|
||||
$baseDate = $subscription->trial_ends_at ?: now();
|
||||
|
||||
// Calculate new end date by adding extension days
|
||||
$newEndDate = $baseDate->copy()->addDays($extensionDays);
|
||||
|
||||
// Format for DateTimePicker (Y-m-d H:i:s format)
|
||||
$set('new_trial_ends_at', $newEndDate->format('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions\Tables;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class TrialExtensionsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('user.name')
|
||||
->label('User')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->limit(30),
|
||||
|
||||
TextColumn::make('subscription.plan.name')
|
||||
->label('Plan')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('extension_days')
|
||||
->label('Days Extended')
|
||||
->sortable()
|
||||
->alignCenter()
|
||||
->badge()
|
||||
->color('success'),
|
||||
|
||||
TextColumn::make('extension_type')
|
||||
->badge()
|
||||
->label('Type')
|
||||
->colors([
|
||||
'blue' => 'manual',
|
||||
'green' => 'automatic',
|
||||
'orange' => 'compensation',
|
||||
]),
|
||||
|
||||
TextColumn::make('original_trial_ends_at')
|
||||
->label('Original End')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('new_trial_ends_at')
|
||||
->label('New End')
|
||||
->dateTime('M j, Y')
|
||||
->sortable()
|
||||
->color('success')
|
||||
->description(fn ($record): string => $record->new_trial_ends_at->diffForHumans()
|
||||
),
|
||||
|
||||
TextColumn::make('reason')
|
||||
->label('Reason')
|
||||
->limit(30)
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('grantedByAdmin.name')
|
||||
->label('Granted By')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('granted_at')
|
||||
->label('Granted At')
|
||||
->dateTime('M j, Y')
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('extension_type')
|
||||
->label('Extension Type')
|
||||
->options([
|
||||
'manual' => 'Manual Grant',
|
||||
'automatic' => 'Automatic Extension',
|
||||
'compensation' => 'Compensation',
|
||||
]),
|
||||
|
||||
SelectFilter::make('granted_by_admin_id')
|
||||
->label('Granted By')
|
||||
->relationship('grantedByAdmin', 'name')
|
||||
->searchable()
|
||||
->preload(),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
|
||||
Action::make('view_subscription')
|
||||
->label('View Subscription')
|
||||
->icon('heroicon-o-rectangle-stack')
|
||||
->color('blue')
|
||||
->url(fn ($record) => route('filament.' . filament()->getCurrentPanel()->getId() . '.resources.subscriptions.edit', $record->subscription_id))
|
||||
->openUrlInNewTab(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TrialExtensions;
|
||||
|
||||
use App\Filament\Resources\TrialExtensions\Pages\CreateTrialExtension;
|
||||
use App\Filament\Resources\TrialExtensions\Pages\EditTrialExtension;
|
||||
use App\Filament\Resources\TrialExtensions\Pages\ListTrialExtensions;
|
||||
use App\Filament\Resources\TrialExtensions\Schemas\TrialExtensionForm;
|
||||
use App\Filament\Resources\TrialExtensions\Tables\TrialExtensionsTable;
|
||||
use App\Models\Subscription;
|
||||
use App\Models\TrialExtension;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class TrialExtensionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = TrialExtension::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedClock;
|
||||
|
||||
protected static ?string $navigationLabel = 'Trial Extensions';
|
||||
|
||||
protected static ?string $modelLabel = 'Trial Extension';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Trial Extensions';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Payment Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return TrialExtensionForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return TrialExtensionsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListTrialExtensions::route('/'),
|
||||
'create' => CreateTrialExtension::route('/create'),
|
||||
'edit' => EditTrialExtension::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
150
app/Filament/Widgets/ChurnAnalysis.php
Normal file
150
app/Filament/Widgets/ChurnAnalysis.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ChurnAnalysis extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 3;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Churn Analysis';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$monthlyChurn = $this->getMonthlyChurnRate();
|
||||
$churnByProvider = $this->getChurnByProvider();
|
||||
$churnByPlan = $this->getChurnByPlan();
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Monthly Churn Rate (%)',
|
||||
'data' => array_values($monthlyChurn),
|
||||
'borderColor' => 'rgba(239, 68, 68, 1)',
|
||||
'backgroundColor' => 'rgba(239, 68, 68, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
],
|
||||
'labels' => array_keys($monthlyChurn),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'line';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'responsive' => true,
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'position' => 'top',
|
||||
],
|
||||
'tooltip' => [
|
||||
'callbacks' => [
|
||||
'label' => 'function(context) {
|
||||
let label = context.dataset.label || "";
|
||||
if (label) {
|
||||
label += ": ";
|
||||
}
|
||||
if (context.parsed.y !== null) {
|
||||
label += context.parsed.y.toFixed(1) + "%";
|
||||
}
|
||||
return label;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
'max' => 20,
|
||||
'ticks' => [
|
||||
'callback' => 'function(value) {
|
||||
return value + "%";
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getMonthlyChurnRate(): array
|
||||
{
|
||||
$churnData = [];
|
||||
|
||||
for ($i = 5; $i >= 0; $i--) {
|
||||
$month = now()->subMonths($i);
|
||||
$monthStart = $month->copy()->startOfMonth();
|
||||
$monthEnd = $month->copy()->endOfMonth();
|
||||
|
||||
// Active subscriptions at the beginning of the month
|
||||
$activeAtStart = Subscription::query()
|
||||
->where('status', 'active')
|
||||
->where('created_at', '<', $monthStart)
|
||||
->count();
|
||||
|
||||
// Subscriptions cancelled during the month
|
||||
$cancelledDuringMonth = Subscription::query()
|
||||
->where('status', 'cancelled')
|
||||
->whereBetween('cancelled_at', [$monthStart, $monthEnd])
|
||||
->count();
|
||||
|
||||
// Calculate churn rate
|
||||
$churnRate = $activeAtStart > 0 ? ($cancelledDuringMonth / $activeAtStart) * 100 : 0;
|
||||
|
||||
$churnData[$month->format('M Y')] = round($churnRate, 2);
|
||||
}
|
||||
|
||||
return $churnData;
|
||||
}
|
||||
|
||||
private function getChurnByProvider(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
'provider',
|
||||
DB::raw('COUNT(CASE WHEN status = "cancelled" THEN 1 END) as cancelled'),
|
||||
DB::raw('COUNT(*) as total'),
|
||||
DB::raw('(CAST(COUNT(CASE WHEN status = "cancelled" THEN 1 END) AS REAL) * 100.0 / COUNT(*)) as churn_rate')
|
||||
)
|
||||
->groupBy('provider')
|
||||
->orderBy('churn_rate', 'desc')
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
return [$item->provider => round($item->churn_rate, 2)];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getChurnByPlan(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
'plans.name as plan_name',
|
||||
DB::raw('COUNT(CASE WHEN subscriptions.status = "cancelled" THEN 1 END) as cancelled'),
|
||||
DB::raw('COUNT(*) as total'),
|
||||
DB::raw('(CAST(COUNT(CASE WHEN subscriptions.status = "cancelled" THEN 1 END) AS REAL) * 100.0 / COUNT(*)) as churn_rate')
|
||||
)
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->groupBy('plans.id', 'plans.name')
|
||||
->orderBy('churn_rate', 'desc')
|
||||
->limit(10)
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
return [$item->plan_name => round($item->churn_rate, 2)];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
139
app/Filament/Widgets/CouponPerformanceMetrics.php
Normal file
139
app/Filament/Widgets/CouponPerformanceMetrics.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Coupon;
|
||||
use App\Models\CouponUsage;
|
||||
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CouponPerformanceMetrics extends BaseWidget
|
||||
{
|
||||
protected static ?int $sort = 3;
|
||||
|
||||
protected function getStats(): array
|
||||
{
|
||||
$totalCoupons = Coupon::count();
|
||||
$activeCoupons = Coupon::where('is_active', true)->count();
|
||||
$totalUsages = CouponUsage::count();
|
||||
$totalDiscount = CouponUsage::sum('discount_amount');
|
||||
|
||||
$conversionRate = $this->getCouponConversionRate();
|
||||
$avgDiscountValue = $totalUsages > 0 ? $totalDiscount / $totalUsages : 0;
|
||||
$topPerformingCoupon = $this->getTopPerformingCoupon();
|
||||
$monthlyUsage = $this->getMonthlyUsage();
|
||||
|
||||
return [
|
||||
Stat::make('Total Coupons', $totalCoupons)
|
||||
->description($activeCoupons.' active')
|
||||
->descriptionIcon('heroicon-o-ticket')
|
||||
->color('primary'),
|
||||
|
||||
Stat::make('Total Usages', $totalUsages)
|
||||
->description($this->getUsageGrowthRate())
|
||||
->descriptionIcon($this->getUsageGrowthIcon())
|
||||
->color($this->getUsageGrowthColor()),
|
||||
|
||||
Stat::make('Total Discount Given', '$'.number_format($totalDiscount, 2))
|
||||
->description('Total value of discounts')
|
||||
->descriptionIcon('heroicon-o-gift')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Conversion Rate', $conversionRate.'%')
|
||||
->description('Coupon to subscription rate')
|
||||
->descriptionIcon('heroicon-o-chart-bar')
|
||||
->color('info'),
|
||||
|
||||
Stat::make('Avg Discount Value', '$'.number_format($avgDiscountValue, 2))
|
||||
->description('Per usage average')
|
||||
->descriptionIcon('heroicon-o-calculator')
|
||||
->color('warning'),
|
||||
|
||||
Stat::make('Top Performing', $topPerformingCoupon ? ($topPerformingCoupon['name'] ?? 'N/A') : 'N/A')
|
||||
->description($topPerformingCoupon ? ($topPerformingCoupon['usages'] ?? 0).' usages' : '0 usages')
|
||||
->descriptionIcon('heroicon-o-trophy')
|
||||
->color('purple'),
|
||||
|
||||
Stat::make('Monthly Usage', $monthlyUsage)
|
||||
->description('This month')
|
||||
->descriptionIcon('heroicon-o-calendar')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Revenue Impact', '$'.number_format($this->calculateRevenueImpact(), 2))
|
||||
->description('Estimated new revenue')
|
||||
->descriptionIcon('heroicon-o-currency-dollar')
|
||||
->color('success'),
|
||||
];
|
||||
}
|
||||
|
||||
private function getCouponConversionRate(): string
|
||||
{
|
||||
$totalCoupons = Coupon::count();
|
||||
if ($totalCoupons == 0) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
$usedCoupons = Coupon::whereHas('usages')->count();
|
||||
|
||||
return number_format(($usedCoupons / $totalCoupons) * 100, 1);
|
||||
}
|
||||
|
||||
private function getUsageGrowthRate(): string
|
||||
{
|
||||
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
|
||||
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
|
||||
|
||||
if ($previousMonth == 0) {
|
||||
return 'New this month';
|
||||
}
|
||||
|
||||
$growth = (($currentMonth - $previousMonth) / $previousMonth) * 100;
|
||||
|
||||
return $growth >= 0 ? "+{$growth}%" : "{$growth}%";
|
||||
}
|
||||
|
||||
private function getUsageGrowthIcon(): string
|
||||
{
|
||||
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
|
||||
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
|
||||
|
||||
return $currentMonth > $previousMonth ? 'heroicon-o-arrow-trending-up' : 'heroicon-o-arrow-trending-down';
|
||||
}
|
||||
|
||||
private function getUsageGrowthColor(): string
|
||||
{
|
||||
$currentMonth = CouponUsage::whereMonth('created_at', now()->month)->count();
|
||||
$previousMonth = CouponUsage::whereMonth('created_at', now()->subMonth()->month)->count();
|
||||
|
||||
return $currentMonth > $previousMonth ? 'success' : 'danger';
|
||||
}
|
||||
|
||||
private function getTopPerformingCoupon(): ?array
|
||||
{
|
||||
return CouponUsage::query()
|
||||
->select('coupon_id', DB::raw('count(*) as usages, sum(discount_amount) as total_discount'))
|
||||
->with('coupon:id,code')
|
||||
->groupBy('coupon_id')
|
||||
->orderBy('usages', 'desc')
|
||||
->first()
|
||||
?->toArray();
|
||||
}
|
||||
|
||||
private function getMonthlyUsage(): int
|
||||
{
|
||||
return CouponUsage::whereMonth('created_at', now()->month)->count();
|
||||
}
|
||||
|
||||
private function calculateRevenueImpact(): float
|
||||
{
|
||||
// Estimate revenue from coupons that led to subscriptions
|
||||
return CouponUsage::query()
|
||||
->whereHas('subscription', function ($query) {
|
||||
$query->where('status', 'active');
|
||||
})
|
||||
->join('subscriptions', 'coupon_usages.subscription_id', '=', 'subscriptions.id')
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->sum('plans.price');
|
||||
}
|
||||
}
|
||||
120
app/Filament/Widgets/CustomerAnalyticsOverview.php
Normal file
120
app/Filament/Widgets/CustomerAnalyticsOverview.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use App\Models\User;
|
||||
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
|
||||
class CustomerAnalyticsOverview extends BaseWidget
|
||||
{
|
||||
protected function getStats(): array
|
||||
{
|
||||
$totalCustomers = User::count();
|
||||
$payingCustomers = Subscription::active()->distinct('user_id')->count('user_id');
|
||||
$trialCustomers = Subscription::onTrial()->distinct('user_id')->count('user_id');
|
||||
$churnedCustomers = Subscription::where('status', 'cancelled')->distinct('user_id')->count('user_id');
|
||||
|
||||
$mrr = $this->calculateMRR();
|
||||
$arr = $mrr * 12;
|
||||
$arpu = $payingCustomers > 0 ? $mrr / $payingCustomers : 0;
|
||||
$ltv = $arpu * 12; // Simplified LTV calculation
|
||||
|
||||
return [
|
||||
Stat::make('Total Customers', $totalCustomers)
|
||||
->description('All registered users')
|
||||
->descriptionIcon('heroicon-o-users')
|
||||
->color('primary'),
|
||||
|
||||
Stat::make('Paying Customers', $payingCustomers)
|
||||
->description($this->getCustomerGrowthRate($payingCustomers))
|
||||
->descriptionIcon($this->getGrowthIcon($payingCustomers))
|
||||
->color($this->getGrowthColor($payingCustomers)),
|
||||
|
||||
Stat::make('Trial Customers', $trialCustomers)
|
||||
->description('Currently on trial')
|
||||
->descriptionIcon('heroicon-o-clock')
|
||||
->color('warning'),
|
||||
|
||||
Stat::make('Churned Customers', $churnedCustomers)
|
||||
->description($this->getChurnRate($churnedCustomers, $payingCustomers + $churnedCustomers))
|
||||
->descriptionIcon('heroicon-o-arrow-trending-down')
|
||||
->color('danger'),
|
||||
|
||||
Stat::make('Monthly Recurring Revenue', '$'.number_format($mrr, 2))
|
||||
->description('MRR from active subscriptions')
|
||||
->descriptionIcon('heroicon-o-currency-dollar')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Annual Recurring Revenue', '$'.number_format($arr, 2))
|
||||
->description('ARR projection')
|
||||
->descriptionIcon('heroicon-o-chart-bar')
|
||||
->color('success'),
|
||||
|
||||
Stat::make('Average Revenue Per User', '$'.number_format($arpu, 2))
|
||||
->description('ARPU for paying customers')
|
||||
->descriptionIcon('heroicon-o-calculator')
|
||||
->color('info'),
|
||||
|
||||
Stat::make('Lifetime Value', '$'.number_format($ltv, 2))
|
||||
->description('Estimated customer LTV')
|
||||
->descriptionIcon('heroicon-o-gift')
|
||||
->color('purple'),
|
||||
];
|
||||
}
|
||||
|
||||
private function calculateMRR(): float
|
||||
{
|
||||
return Subscription::active()
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->sum('plans.price');
|
||||
}
|
||||
|
||||
private function getCustomerGrowthRate(int $current): string
|
||||
{
|
||||
$previous = Subscription::active()
|
||||
->where('created_at', '<', now()->subMonth())
|
||||
->distinct('user_id')
|
||||
->count('user_id');
|
||||
|
||||
if ($previous == 0) {
|
||||
return 'New customers';
|
||||
}
|
||||
|
||||
$growth = (($current - $previous) / $previous) * 100;
|
||||
|
||||
return $growth >= 0 ? "+{$growth}%" : "{$growth}%";
|
||||
}
|
||||
|
||||
private function getGrowthIcon(int $current): string
|
||||
{
|
||||
$previous = Subscription::active()
|
||||
->where('created_at', '<', now()->subMonth())
|
||||
->distinct('user_id')
|
||||
->count('user_id');
|
||||
|
||||
return $current > $previous ? 'heroicon-o-arrow-trending-up' : 'heroicon-o-arrow-trending-down';
|
||||
}
|
||||
|
||||
private function getGrowthColor(int $current): string
|
||||
{
|
||||
$previous = Subscription::active()
|
||||
->where('created_at', '<', now()->subMonth())
|
||||
->distinct('user_id')
|
||||
->count('user_id');
|
||||
|
||||
return $current > $previous ? 'success' : 'danger';
|
||||
}
|
||||
|
||||
private function getChurnRate(int $churned, int $total): string
|
||||
{
|
||||
if ($total == 0) {
|
||||
return '0% churn rate';
|
||||
}
|
||||
|
||||
$rate = ($churned / $total) * 100;
|
||||
|
||||
return "{$rate}% churn rate";
|
||||
}
|
||||
}
|
||||
225
app/Filament/Widgets/CustomerLifetimeValue.php
Normal file
225
app/Filament/Widgets/CustomerLifetimeValue.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CustomerLifetimeValue extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 5;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Customer Lifetime Value Analysis';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$ltvByCohort = $this->getLTVByCohort();
|
||||
$ltvByProvider = $this->getLTVByProvider();
|
||||
$ltvByPlan = $this->getLTVByPlan();
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Average LTV by Cohort ($)',
|
||||
'data' => array_values($ltvByCohort),
|
||||
'borderColor' => 'rgba(34, 197, 94, 1)',
|
||||
'backgroundColor' => 'rgba(34, 197, 94, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
],
|
||||
'labels' => array_keys($ltvByCohort),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'bar';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'responsive' => true,
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'position' => 'top',
|
||||
],
|
||||
'tooltip' => [
|
||||
'callbacks' => [
|
||||
'label' => 'function(context) {
|
||||
let label = context.dataset.label || "";
|
||||
if (label) {
|
||||
label += ": ";
|
||||
}
|
||||
if (context.parsed.y !== null) {
|
||||
label += "$" + context.parsed.y.toFixed(2);
|
||||
}
|
||||
return label;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
'ticks' => [
|
||||
'callback' => 'function(value) {
|
||||
return "$" + value;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getLTVByCohort(): array
|
||||
{
|
||||
$cohorts = [];
|
||||
|
||||
// Get cohorts by signup month (last 6 months)
|
||||
for ($i = 5; $i >= 0; $i--) {
|
||||
$month = now()->subMonths($i);
|
||||
$monthStart = $month->copy()->startOfMonth();
|
||||
$monthEnd = $month->copy()->endOfMonth();
|
||||
|
||||
// Users who signed up in this month
|
||||
$cohortUsers = Subscription::query()
|
||||
->select('user_id')
|
||||
->whereBetween('created_at', [$monthStart, $monthEnd])
|
||||
->distinct()
|
||||
->pluck('user_id');
|
||||
|
||||
if ($cohortUsers->isEmpty()) {
|
||||
$cohorts[$month->format('M Y')] = 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate LTV for this cohort
|
||||
$totalRevenue = Subscription::query()
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->whereIn('subscriptions.user_id', $cohortUsers)
|
||||
->where('subscriptions.status', 'active')
|
||||
->sum('plans.price');
|
||||
|
||||
// Estimate LTV based on current MRR and average subscription length
|
||||
$avgSubscriptionLength = $this->getAverageSubscriptionLength();
|
||||
$cohortSize = $cohortUsers->count();
|
||||
$avgMRRPerUser = $cohortSize > 0 ? $totalRevenue / $cohortSize : 0;
|
||||
$ltv = $avgMRRPerUser * $avgSubscriptionLength;
|
||||
|
||||
$cohorts[$month->format('M Y')] = round($ltv, 2);
|
||||
}
|
||||
|
||||
return $cohorts;
|
||||
}
|
||||
|
||||
private function getLTVByProvider(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
'provider',
|
||||
DB::raw('AVG(plans.price) as avg_monthly_price'),
|
||||
DB::raw('COUNT(*) as total_subscriptions')
|
||||
)
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->groupBy('provider')
|
||||
->orderBy('avg_monthly_price', 'desc')
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
$avgLength = $this->getAverageSubscriptionLengthByProvider($item->provider);
|
||||
$ltv = $item->avg_monthly_price * $avgLength;
|
||||
|
||||
return [$item->provider => round($ltv, 2)];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getLTVByPlan(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
'plans.name as plan_name',
|
||||
DB::raw('plans.price'),
|
||||
DB::raw('COUNT(*) as total_subscriptions')
|
||||
)
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->groupBy('plans.id', 'plans.name', 'plans.price')
|
||||
->orderBy('plans.price', 'desc')
|
||||
->limit(10)
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
$avgLength = $this->getAverageSubscriptionLengthByPlan($item->plan_name);
|
||||
$ltv = $item->price * $avgLength;
|
||||
|
||||
return [$item->plan_name => round($ltv, 2)];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getAverageSubscriptionLength(): float
|
||||
{
|
||||
// Average subscription length in months (simplified calculation)
|
||||
return 12; // Could be calculated based on historical data
|
||||
}
|
||||
|
||||
private function getAverageSubscriptionLengthByProvider(string $provider): float
|
||||
{
|
||||
// Provider-specific average subscription length
|
||||
$providerLengths = [
|
||||
'stripe' => 14,
|
||||
'lemon_squeezy' => 12,
|
||||
'polar' => 10,
|
||||
'oxapay' => 8,
|
||||
'crypto' => 6,
|
||||
'activation_key' => 24,
|
||||
];
|
||||
|
||||
return $providerLengths[$provider] ?? 12;
|
||||
}
|
||||
|
||||
private function getAverageSubscriptionLengthByPlan(string $planName): float
|
||||
{
|
||||
// Plan-specific average subscription length (could be based on plan tier)
|
||||
return 12; // Simplified, could be more sophisticated
|
||||
}
|
||||
|
||||
private function getTopQuartileLTV(): float
|
||||
{
|
||||
$ltvs = Subscription::query()
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->selectRaw('plans.price * ? as ltv', [$this->getAverageSubscriptionLength()])
|
||||
->pluck('ltv')
|
||||
->sort()
|
||||
->values();
|
||||
|
||||
$quartileIndex = (int) ($ltvs->count() * 0.75);
|
||||
|
||||
return $ltvs->get($quartileIndex, 0);
|
||||
}
|
||||
|
||||
private function getBottomQuartileLTV(): float
|
||||
{
|
||||
$ltvs = Subscription::query()
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->selectRaw('plans.price * ? as ltv', [$this->getAverageSubscriptionLength()])
|
||||
->pluck('ltv')
|
||||
->sort()
|
||||
->values();
|
||||
|
||||
$quartileIndex = (int) ($ltvs->count() * 0.25);
|
||||
|
||||
return $ltvs->get($quartileIndex, 0);
|
||||
}
|
||||
}
|
||||
123
app/Filament/Widgets/RevenueMetrics.php
Normal file
123
app/Filament/Widgets/RevenueMetrics.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RevenueMetrics extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 1;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Revenue Trends';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$monthlyRevenue = $this->getMonthlyRevenueTrend();
|
||||
$mrrByProvider = $this->getMRRByProvider();
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Monthly Revenue',
|
||||
'data' => array_values($monthlyRevenue),
|
||||
'borderColor' => 'rgba(34, 197, 94, 1)',
|
||||
'backgroundColor' => 'rgba(34, 197, 94, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
[
|
||||
'label' => 'MRR by Provider',
|
||||
'data' => array_values($mrrByProvider),
|
||||
'borderColor' => 'rgba(59, 130, 246, 1)',
|
||||
'backgroundColor' => 'rgba(59, 130, 246, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
],
|
||||
'labels' => array_keys($monthlyRevenue),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'line';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'responsive' => true,
|
||||
'interaction' => [
|
||||
'intersect' => false,
|
||||
'mode' => 'index',
|
||||
],
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'position' => 'top',
|
||||
],
|
||||
'tooltip' => [
|
||||
'callbacks' => [
|
||||
'label' => 'function(context) {
|
||||
let label = context.dataset.label || "";
|
||||
if (label) {
|
||||
label += ": ";
|
||||
}
|
||||
if (context.parsed.y !== null) {
|
||||
label += "$" + context.parsed.y.toFixed(2);
|
||||
}
|
||||
return label;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
'ticks' => [
|
||||
'callback' => 'function(value) {
|
||||
return "$" + value;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getMonthlyRevenueTrend(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
DB::raw("strftime('%Y-%m', subscriptions.created_at) as month"),
|
||||
DB::raw('SUM(plans.price) as revenue')
|
||||
)
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->where('subscriptions.created_at', '>=', now()->subMonths(12))
|
||||
->groupBy('month')
|
||||
->orderBy('month')
|
||||
->pluck('revenue', 'month')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getMRRByProvider(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
'provider',
|
||||
DB::raw('SUM(plans.price) as mrr')
|
||||
)
|
||||
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
|
||||
->where('subscriptions.status', 'active')
|
||||
->groupBy('provider')
|
||||
->orderBy('mrr', 'desc')
|
||||
->pluck('mrr', 'provider')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
126
app/Filament/Widgets/SubscriptionMetrics.php
Normal file
126
app/Filament/Widgets/SubscriptionMetrics.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SubscriptionMetrics extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 2;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Subscription Metrics';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$period = $this->getPeriod();
|
||||
|
||||
$subscriptionsByProvider = $this->getSubscriptionsByProvider($period);
|
||||
$subscriptionsByStatus = $this->getSubscriptionsByStatus();
|
||||
$monthlyTrend = $this->getMonthlySubscriptionTrend();
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Subscriptions by Provider',
|
||||
'data' => array_values($subscriptionsByProvider),
|
||||
'backgroundColor' => [
|
||||
'rgba(59, 130, 246, 0.8)', // blue
|
||||
'rgba(34, 197, 94, 0.8)', // green
|
||||
'rgba(168, 85, 247, 0.8)', // purple
|
||||
'rgba(251, 146, 60, 0.8)', // orange
|
||||
'rgba(107, 114, 128, 0.8)', // gray
|
||||
'rgba(236, 72, 153, 0.8)', // pink
|
||||
],
|
||||
'borderColor' => [
|
||||
'rgba(59, 130, 246, 1)',
|
||||
'rgba(34, 197, 94, 1)',
|
||||
'rgba(168, 85, 247, 1)',
|
||||
'rgba(251, 146, 60, 1)',
|
||||
'rgba(107, 114, 128, 1)',
|
||||
'rgba(236, 72, 153, 1)',
|
||||
],
|
||||
],
|
||||
],
|
||||
'labels' => array_keys($subscriptionsByProvider),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'doughnut';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'responsive' => true,
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'position' => 'bottom',
|
||||
],
|
||||
'tooltip' => [
|
||||
'callbacks' => [
|
||||
'label' => 'function(context) {
|
||||
const label = context.label || "";
|
||||
const value = context.parsed || 0;
|
||||
const total = context.dataset.data.reduce((a, b) => a + b, 0);
|
||||
const percentage = ((value / total) * 100).toFixed(1);
|
||||
return label + ": " + value + " (" + percentage + "%)";
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getPeriod(): string
|
||||
{
|
||||
return 'last_30_days'; // Could be made configurable
|
||||
}
|
||||
|
||||
private function getSubscriptionsByProvider(string $period): array
|
||||
{
|
||||
$query = Subscription::query();
|
||||
|
||||
if ($period === 'last_30_days') {
|
||||
$query->where('created_at', '>=', now()->subDays(30));
|
||||
}
|
||||
|
||||
return $query
|
||||
->select('provider', DB::raw('count(*) as count'))
|
||||
->groupBy('provider')
|
||||
->orderBy('count', 'desc')
|
||||
->pluck('count', 'provider')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getSubscriptionsByStatus(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select('status', DB::raw('count(*) as count'))
|
||||
->groupBy('status')
|
||||
->orderBy('count', 'desc')
|
||||
->pluck('count', 'status')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getMonthlySubscriptionTrend(): array
|
||||
{
|
||||
return Subscription::query()
|
||||
->select(
|
||||
DB::raw("strftime('%Y-%m', subscriptions.created_at) as month"),
|
||||
DB::raw('count(*) as count')
|
||||
)
|
||||
->groupBy('month')
|
||||
->orderBy('month')
|
||||
->pluck('count', 'month')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
176
app/Filament/Widgets/TrialPerformance.php
Normal file
176
app/Filament/Widgets/TrialPerformance.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Subscription;
|
||||
use App\Models\TrialExtension;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class TrialPerformance extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 4;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Trial Performance';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$trialConversion = $this->getTrialConversionRates();
|
||||
$trialExtensions = $this->getTrialExtensionsTrend();
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Trial Conversion Rate (%)',
|
||||
'data' => array_values($trialConversion),
|
||||
'borderColor' => 'rgba(168, 85, 247, 1)',
|
||||
'backgroundColor' => 'rgba(168, 85, 247, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
[
|
||||
'label' => 'Trial Extensions',
|
||||
'data' => array_values($trialExtensions),
|
||||
'borderColor' => 'rgba(251, 146, 60, 1)',
|
||||
'backgroundColor' => 'rgba(251, 146, 60, 0.1)',
|
||||
'fill' => true,
|
||||
'tension' => 0.4,
|
||||
],
|
||||
],
|
||||
'labels' => array_keys($trialConversion),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'line';
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'responsive' => true,
|
||||
'interaction' => [
|
||||
'intersect' => false,
|
||||
'mode' => 'index',
|
||||
],
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'position' => 'top',
|
||||
],
|
||||
'tooltip' => [
|
||||
'callbacks' => [
|
||||
'label' => 'function(context) {
|
||||
let label = context.dataset.label || "";
|
||||
if (label) {
|
||||
label += ": ";
|
||||
}
|
||||
if (context.parsed.y !== null) {
|
||||
if (label.includes("Rate")) {
|
||||
label += context.parsed.y.toFixed(1) + "%";
|
||||
} else {
|
||||
label += context.parsed.y;
|
||||
}
|
||||
}
|
||||
return label;
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
'ticks' => [
|
||||
'callback' => 'function(value, index, values) {
|
||||
if (index === 0 || index === values.length - 1) {
|
||||
return value + (this.chart.data.datasets[0].label.includes("Rate") ? "%" : "");
|
||||
}
|
||||
return "";
|
||||
}',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getTrialConversionRates(): array
|
||||
{
|
||||
$conversionData = [];
|
||||
|
||||
for ($i = 5; $i >= 0; $i--) {
|
||||
$month = now()->subMonths($i);
|
||||
$monthStart = $month->copy()->startOfMonth();
|
||||
$monthEnd = $month->copy()->endOfMonth();
|
||||
|
||||
// Trials that ended during this month
|
||||
$endedTrials = Subscription::query()
|
||||
->where('status', 'trialing')
|
||||
->whereBetween('trial_ends_at', [$monthStart, $monthEnd])
|
||||
->get();
|
||||
|
||||
$totalTrials = $endedTrials->count();
|
||||
$convertedTrials = $endedTrials->filter(function ($trial) use ($monthEnd) {
|
||||
// Check if trial converted to paid subscription
|
||||
return Subscription::query()
|
||||
->where('user_id', $trial->user_id)
|
||||
->where('status', 'active')
|
||||
->where('created_at', '>', $trial->trial_ends_at)
|
||||
->where('created_at', '<=', $monthEnd)
|
||||
->exists();
|
||||
})->count();
|
||||
|
||||
$conversionRate = $totalTrials > 0 ? ($convertedTrials / $totalTrials) * 100 : 0;
|
||||
|
||||
$conversionData[$month->format('M Y')] = round($conversionRate, 2);
|
||||
}
|
||||
|
||||
return $conversionData;
|
||||
}
|
||||
|
||||
private function getTrialExtensionsTrend(): array
|
||||
{
|
||||
return TrialExtension::query()
|
||||
->select(
|
||||
DB::raw("strftime('%Y-%m', trial_extensions.granted_at) as month"),
|
||||
DB::raw('COUNT(*) as extensions'),
|
||||
DB::raw('SUM(extension_days) as total_days')
|
||||
)
|
||||
->where('trial_extensions.granted_at', '>=', now()->subMonths(6))
|
||||
->groupBy('month')
|
||||
->orderBy('month')
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
$date = \Carbon\Carbon::createFromFormat('Y-m', $item->month);
|
||||
|
||||
return [$date->format('M Y') => $item->extensions];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getAverageTrialLength(): float
|
||||
{
|
||||
return Subscription::query()
|
||||
->where('status', 'trialing')
|
||||
->orWhere('status', 'active')
|
||||
->whereNotNull('trial_ends_at')
|
||||
->selectRaw('AVG(DATEDIFF(trial_ends_at, created_at)) as avg_trial_days')
|
||||
->value('avg_trial_days') ?? 0;
|
||||
}
|
||||
|
||||
private function getMostCommonExtensionReasons(): array
|
||||
{
|
||||
return TrialExtension::query()
|
||||
->select('reason', DB::raw('COUNT(*) as count'))
|
||||
->whereNotNull('reason')
|
||||
->groupBy('reason')
|
||||
->orderBy('count', 'desc')
|
||||
->limit(5)
|
||||
->pluck('count', 'reason')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user