218 lines
8.9 KiB
PHP
218 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Resources;
|
|
|
|
use App\Admin\Clusters\Services;
|
|
use App\Admin\Components\UserComponent;
|
|
use App\Admin\Resources\Common\RelationManagers\PropertiesRelationManager;
|
|
use App\Admin\Resources\ServiceResource\RelationManagers;
|
|
use App\Helpers\ExtensionHelper;
|
|
use App\Models\Currency;
|
|
use App\Models\Product;
|
|
use App\Models\Service;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\Actions\Action;
|
|
use Filament\Forms\Components\Component;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Support\RawJs;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class ActiveServiceResource extends Resource
|
|
{
|
|
protected static ?string $model = Service::class;
|
|
|
|
protected static ?string $navigationIcon = 'ri-checkbox-circle-line';
|
|
|
|
protected static ?string $activeNavigationIcon = 'ri-checkbox-circle-fill';
|
|
|
|
protected static ?string $navigationLabel = 'Active';
|
|
|
|
protected static ?string $modelLabel = 'Active Service';
|
|
|
|
protected static ?string $pluralModelLabel = 'Active Services';
|
|
|
|
protected static ?string $slug = 'active-services';
|
|
|
|
protected static ?int $navigationSort = 0;
|
|
|
|
public static function getNavigationBadge(): ?string
|
|
{
|
|
return Service::where('status', 'active')->count() ?: null;
|
|
}
|
|
|
|
public static function getNavigationBadgeColor(): ?string
|
|
{
|
|
return 'success';
|
|
}
|
|
|
|
protected static ?string $cluster = Services::class;
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()->where('status', 'active');
|
|
}
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Forms\Components\Select::make('product_id')
|
|
->label('Product')
|
|
->required()
|
|
->options(Product::all()->pluck('name', 'id')->toArray())
|
|
->searchable()
|
|
->live()
|
|
->preload()
|
|
->placeholder('Select the product'),
|
|
Forms\Components\Select::make('plan_id')
|
|
->label('Plan')
|
|
->required()
|
|
->relationship('plan', 'name', fn (Builder $query, Get $get) => $query->where('priceable_id', $get('product_id'))->where('priceable_type', Product::class))
|
|
->searchable()
|
|
->preload()
|
|
->disabled(fn (Get $get) => !$get('product_id'))
|
|
->placeholder('Select the plan'),
|
|
UserComponent::make('user_id'),
|
|
Forms\Components\Select::make('status')
|
|
->label('Status')
|
|
->required()
|
|
->options([
|
|
'active' => 'Active',
|
|
'pending' => 'Pending',
|
|
'suspended' => 'Suspended',
|
|
'cancelled' => 'Cancelled',
|
|
])
|
|
->default('active'),
|
|
Forms\Components\TextInput::make('quantity')
|
|
->label('Quantity')
|
|
->required()
|
|
->placeholder('Enter the quantity'),
|
|
Forms\Components\DatePicker::make('expires_at')
|
|
->label('Expires At')
|
|
->required()
|
|
->placeholder('Select the expiration date'),
|
|
Forms\Components\Select::make('coupon_id')
|
|
->label('Coupon')
|
|
->relationship('coupon', 'code')
|
|
->searchable()
|
|
->placeholder('Select the coupon'),
|
|
Forms\Components\Select::make('currency_code')
|
|
->options(function (Get $get, ?string $state) {
|
|
$pricing = collect($get('../../pricing'))->pluck('currency_code');
|
|
if ($state !== null) {
|
|
$pricing = $pricing->filter(function ($code) use ($state) {
|
|
return $code !== $state;
|
|
});
|
|
}
|
|
$pricing = $pricing->filter(function ($code) {
|
|
return $code !== null;
|
|
});
|
|
|
|
return Currency::whereNotIn('code', $pricing)->pluck('code', 'code');
|
|
})
|
|
->live()
|
|
->default(config('settings.default_currency'))
|
|
->required(),
|
|
Forms\Components\TextInput::make('price')
|
|
->required()
|
|
->label('Price')
|
|
->prefix(fn (Get $get) => Currency::where('code', $get('currency_code'))->first()?->prefix)
|
|
->suffix(fn (Get $get) => Currency::where('code', $get('currency_code'))->first()?->suffix)
|
|
->live(onBlur: true)
|
|
->mask(RawJs::make(
|
|
<<<'JS'
|
|
$money($input, '.', '', 2)
|
|
JS
|
|
))
|
|
->numeric()
|
|
->minValue(0),
|
|
Forms\Components\TextInput::make('subscription_id')
|
|
->label('Subscription ID')
|
|
->nullable()
|
|
->placeholder('Enter the subscription ID')
|
|
->hintAction(
|
|
Action::make('Cancel Subscription ID')
|
|
->action(function (Component $component) {
|
|
if (ExtensionHelper::cancelSubscription($component->getRecord())) {
|
|
Notification::make('Subscription Cancelled')
|
|
->title('The subscription has been successfully cancelled')
|
|
->success()
|
|
->send();
|
|
} else {
|
|
Notification::make('Subscription Not Cancelled')
|
|
->title('The subscription could not be cancelled')
|
|
->error()
|
|
->send();
|
|
}
|
|
$component->getRecord()->update(['subscription_id' => null]);
|
|
})
|
|
->requiresConfirmation()
|
|
->label('Cancel Subscription')
|
|
->hidden(fn (Component $component) => !$component->getRecord()?->subscription_id),
|
|
),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('id')
|
|
->label('ID')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('user.name')
|
|
->label('User')
|
|
->searchable(true, fn (Builder $query, string $search) => $query->whereHas('user', fn (Builder $query) => $query->where('first_name', 'like', "%$search%")->orWhere('last_name', 'like', "%$search%"))),
|
|
Tables\Columns\TextColumn::make('product.name')
|
|
->label('Product')
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('status')
|
|
->badge()
|
|
->color('success')
|
|
->formatStateUsing(fn (string $state) => ucfirst($state))
|
|
->label('Status'),
|
|
Tables\Columns\TextColumn::make('expires_at')
|
|
->label('Expires At')
|
|
->date()
|
|
->searchable()
|
|
->sortable(),
|
|
])
|
|
->defaultSort(function (Builder $query): Builder {
|
|
return $query->orderBy('id', 'desc');
|
|
})
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RelationManagers\InvoicesRelationManager::class,
|
|
PropertiesRelationManager::class,
|
|
RelationManagers\ConfigOptionsRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => \App\Admin\Resources\ActiveServiceResource\Pages\ListActiveServices::route('/'),
|
|
'create' => \App\Admin\Resources\ActiveServiceResource\Pages\CreateActiveService::route('/create'),
|
|
'edit' => \App\Admin\Resources\ActiveServiceResource\Pages\EditActiveService::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|