- Create 7 new models with full relationships and business logic:
* PlanFeature: Define available features with categories and types
* PlanFeatureLimit: Manage usage limits per plan with trial overrides
* PlanPermission: Granular permissions system for features
* PlanProvider: Multi-provider payment configuration
* PlanTier: Hierarchical plan structure with upgrade paths
* PlanUsage: Real-time usage tracking and analytics
* TrialConfiguration: Advanced trial settings per plan
- Enhance Plan model with 25+ new methods:
* Feature checking: hasFeature(), canUseFeature(), getRemainingUsage()
* Permission system: hasPermission() with trial support
* Payment providers: getAllowedProviders(), supportsProvider()
* Trial management: hasTrial(), getTrialConfig()
* Upgrade paths: isUpgradeFrom(), getUpgradePath()
* Utility methods: getBillingCycleDisplay(), metadata handling
- Completely redesign PlanResource with tabbed interface:
* Basic Info: Core plan configuration with dynamic billing cycles
* Features & Limits: Dynamic feature management with trial overrides
* Payment Providers: Multi-provider configuration (Stripe, Lemon Squeezy, etc.)
* Trial Settings: Advanced trial configuration with always-visible toggle
- Create new Filament resources:
* PlanFeatureResource: Manage available features by category
* PlanTierResource: Hierarchical tier management with parent-child relationships
- Implement comprehensive data migration:
* Migrate legacy plan data to new enhanced system
* Create default features (mailbox accounts, email forwarding, etc.)
* Preserve existing payment provider configurations
* Set up trial configurations (disabled for legacy plans)
* Handle duplicate data gracefully with rollback support
- Add proper database constraints and indexes:
* Unique constraints on plan-feature relationships
* Foreign key constraints with cascade deletes
* Performance indexes for common queries
* JSON metadata columns for flexible configuration
- Fix trial configuration form handling:
* Add required validation for numeric fields
* Implement proper null handling with defaults
* Add type casting for all numeric fields
* Ensure database constraint compliance
264 lines
9.3 KiB
PHP
264 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\PlanResource\Pages\CreatePlan;
|
|
use App\Filament\Resources\PlanResource\Pages\EditPlan;
|
|
use App\Filament\Resources\PlanResource\Pages\ListPlans;
|
|
use App\Models\Plan;
|
|
use App\Models\PlanTier;
|
|
use BackedEnum;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\CreateAction;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Actions\ViewAction;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Log;
|
|
use UnitEnum;
|
|
|
|
class PlanResource extends Resource
|
|
{
|
|
protected static ?string $model = Plan::class;
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCreditCard;
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Subscription Management';
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
Grid::make(3)->schema([
|
|
TextInput::make('name')
|
|
->label('Plan Name')
|
|
->required()
|
|
->maxLength(255),
|
|
|
|
TextInput::make('price')
|
|
->label('Price')
|
|
->numeric()
|
|
->prefix('$')
|
|
->required(),
|
|
|
|
Select::make('billing_cycle_days')
|
|
->label('Billing Cycle')
|
|
->options([
|
|
30 => 'Monthly',
|
|
90 => 'Quarterly',
|
|
365 => 'Yearly',
|
|
60 => 'Bi-Monthly',
|
|
180 => 'Semi-Annual',
|
|
])
|
|
->default(30)
|
|
->required(),
|
|
]),
|
|
|
|
Grid::make(2)->schema([
|
|
TextInput::make('product_id')
|
|
->label('Product ID')
|
|
->required()
|
|
->helperText('External product identifier'),
|
|
|
|
TextInput::make('pricing_id')
|
|
->label('Pricing ID')
|
|
->required()
|
|
->helperText('External pricing identifier'),
|
|
]),
|
|
|
|
Textarea::make('description')
|
|
->label('Description')
|
|
->rows(3)
|
|
->maxLength(500),
|
|
|
|
Grid::make(3)->schema([
|
|
Select::make('plan_tier_id')
|
|
->label('Plan Tier')
|
|
->options(PlanTier::pluck('name', 'id'))
|
|
->nullable()
|
|
->searchable()
|
|
->helperText('Optional tier classification'),
|
|
|
|
Toggle::make('is_active')
|
|
->label('Active')
|
|
->default(true)
|
|
->helperText('Plan is available for new subscriptions'),
|
|
|
|
TextInput::make('sort_order')
|
|
->label('Sort Order')
|
|
->numeric()
|
|
->default(0)
|
|
->helperText('Display order in pricing tables'),
|
|
]),
|
|
|
|
Section::make('Legacy Settings')
|
|
->description('Legacy payment provider settings (will be migrated to new system)')
|
|
->collapsible()
|
|
->schema([
|
|
Grid::make(3)->schema([
|
|
Toggle::make('monthly_billing')
|
|
->label('Monthly Billing (Legacy)')
|
|
->helperText('Legacy monthly billing flag'),
|
|
|
|
TextInput::make('mailbox_limit')
|
|
->label('Mailbox Limit')
|
|
->numeric()
|
|
->default(10)
|
|
->helperText('Maximum number of mailboxes'),
|
|
|
|
TextInput::make('shoppy_product_id')
|
|
->label('Shoppy Product ID')
|
|
->nullable(),
|
|
]),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('name')
|
|
->label('Plan Name')
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Tables\Columns\TextColumn::make('planTier.name')
|
|
->label('Tier')
|
|
->badge()
|
|
->sortable()
|
|
->placeholder('No Tier'),
|
|
|
|
Tables\Columns\TextColumn::make('price')
|
|
->label('Price')
|
|
->money('USD')
|
|
->sortable(),
|
|
|
|
Tables\Columns\TextColumn::make('billing_cycle_display')
|
|
->label('Billing Cycle')
|
|
->badge()
|
|
->color('primary'),
|
|
|
|
Tables\Columns\IconColumn::make('is_active')
|
|
->label('Active')
|
|
->boolean()
|
|
->trueColor('success')
|
|
->falseColor('danger'),
|
|
|
|
Tables\Columns\TextColumn::make('planProviders_count')
|
|
->label('Providers')
|
|
->counts('planProviders')
|
|
->badge()
|
|
->color('info')
|
|
->sortable(false),
|
|
|
|
Tables\Columns\TextColumn::make('planFeatureLimits_count')
|
|
->label('Features')
|
|
->counts('planFeatureLimits')
|
|
->badge()
|
|
->color('warning')
|
|
->sortable(false),
|
|
|
|
Tables\Columns\TextColumn::make('sort_order')
|
|
->label('Order')
|
|
->sortable()
|
|
->alignCenter(),
|
|
])
|
|
->filters([
|
|
Tables\Filters\SelectFilter::make('plan_tier_id')
|
|
->label('Tier')
|
|
->options(PlanTier::pluck('name', 'id'))
|
|
->searchable(),
|
|
|
|
Tables\Filters\TernaryFilter::make('is_active')
|
|
->label('Active Status')
|
|
->placeholder('All plans')
|
|
->trueLabel('Active only')
|
|
->falseLabel('Inactive only'),
|
|
|
|
Tables\Filters\SelectFilter::make('billing_cycle_days')
|
|
->label('Billing Cycle')
|
|
->options([
|
|
30 => 'Monthly',
|
|
90 => 'Quarterly',
|
|
365 => 'Yearly',
|
|
]),
|
|
|
|
Tables\Filters\Filter::make('has_providers')
|
|
->label('Has Payment Providers')
|
|
->query(fn (Builder $query): Builder => $query->whereHas('planProviders'))
|
|
->toggle(),
|
|
])
|
|
->recordActions([
|
|
ViewAction::make(),
|
|
EditAction::make(),
|
|
DeleteAction::make()
|
|
->before(function (Plan $record) {
|
|
// Prevent deletion if plan has active subscriptions
|
|
if ($record->subscriptions()->where('status', 'active')->exists()) {
|
|
Log::error('Cannot delete plan with active subscriptions');
|
|
}
|
|
}),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make()
|
|
->before(function ($records) {
|
|
foreach ($records as $record) {
|
|
if ($record->subscriptions()->where('status', 'active')->exists()) {
|
|
Log::error('Cannot delete plan(s) with active subscriptions');
|
|
}
|
|
}
|
|
}),
|
|
]),
|
|
])
|
|
->emptyStateActions([
|
|
CreateAction::make(),
|
|
])
|
|
->defaultSort('sort_order', 'asc')
|
|
->groups([
|
|
Tables\Grouping\Group::make('planTier.name')
|
|
->label('Tier')
|
|
->collapsible(),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => ListPlans::route('/'),
|
|
'create' => CreatePlan::route('/create'),
|
|
'edit' => EditPlan::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getNavigationBadge(): ?string
|
|
{
|
|
return static::getModel()::active()->count();
|
|
}
|
|
|
|
public static function getNavigationBadgeColor(): ?string
|
|
{
|
|
return 'success';
|
|
}
|
|
}
|