feat: implement comprehensive enhanced plan management system
- 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
This commit is contained in:
@@ -2,74 +2,126 @@
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use UnitEnum;
|
||||
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\KeyValue;
|
||||
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\Tables\Columns\BooleanColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
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::OutlinedInboxStack;
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCreditCard;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Admin';
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Subscription Management';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make('Plan Information')
|
||||
->description('Add a new plan')
|
||||
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([
|
||||
TextInput::make('name')->label('Page Name')
|
||||
->required(),
|
||||
TextInput::make('description'),
|
||||
TextInput::make('product_id')->required(),
|
||||
TextInput::make('pricing_id')->required(),
|
||||
TextInput::make('shoppy_product_id')->nullable(),
|
||||
TextInput::make('oxapay_link')->nullable(),
|
||||
TextInput::make('price')->numeric()->required(),
|
||||
TextInput::make('mailbox_limit')->numeric()->required(),
|
||||
Select::make('monthly_billing')->options([
|
||||
1 => 'Monthly',
|
||||
0 => 'Yearly',
|
||||
])->required(),
|
||||
Select::make('accept_stripe')->options([
|
||||
1 => 'Activate',
|
||||
0 => 'Disable',
|
||||
])->required(),
|
||||
Select::make('accept_shoppy')->options([
|
||||
1 => 'Activate',
|
||||
0 => 'Disable',
|
||||
])->required(),
|
||||
Select::make('accept_oxapay')->options([
|
||||
1 => 'Activate',
|
||||
0 => 'Disable',
|
||||
])->required(),
|
||||
KeyValue::make('details')
|
||||
->label('Plan Details (Optional)')
|
||||
->keyPlaceholder('Name')
|
||||
->valuePlaceholder('Content')
|
||||
->reorderable(),
|
||||
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(),
|
||||
]),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
@@ -78,46 +130,108 @@ class PlanResource extends Resource
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')->label('Name')->searchable(),
|
||||
TextColumn::make('product_id')->label('Product'),
|
||||
TextColumn::make('pricing_id')->label('Pricing'),
|
||||
TextColumn::make('price')->label('Price'),
|
||||
BooleanColumn::make('monthly_billing')->label('Monthly Billing'),
|
||||
])
|
||||
->searchable()
|
||||
->filters([
|
||||
SelectFilter::make('payment_method')
|
||||
->label('Payment Method')
|
||||
->options([
|
||||
'stripe' => 'Stripe',
|
||||
'shoppy' => 'Shoppy',
|
||||
'oxapay' => 'OxaPay',
|
||||
])
|
||||
->query(function ($query, array $data) {
|
||||
if (isset($data['value'])) {
|
||||
if ($data['value'] === 'stripe') {
|
||||
return $query->where('accept_stripe', true);
|
||||
}
|
||||
if ($data['value'] === 'shoppy') {
|
||||
return $query->where('accept_shoppy', true);
|
||||
}
|
||||
if ($data['value'] === 'oxapay') {
|
||||
return $query->where('accept_oxapay', true);
|
||||
}
|
||||
}
|
||||
Tables\Columns\TextColumn::make('name')
|
||||
->label('Plan Name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
return $query;
|
||||
}),
|
||||
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(),
|
||||
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(),
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -136,4 +250,14 @@ class PlanResource extends Resource
|
||||
'edit' => EditPlan::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getNavigationBadge(): ?string
|
||||
{
|
||||
return static::getModel()::active()->count();
|
||||
}
|
||||
|
||||
public static function getNavigationBadgeColor(): ?string
|
||||
{
|
||||
return 'success';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user