Files
Paymenter-Version-Tracks/app/Admin/Resources/ProductResource.php
Muhammad Tamir b3933b9960 v1.4.0
2025-11-14 10:59:24 +07:00

335 lines
15 KiB
PHP

<?php
namespace App\Admin\Resources;
use App\Admin\Resources\ProductResource\Pages\CreateProduct;
use App\Admin\Resources\ProductResource\Pages\EditProduct;
use App\Admin\Resources\ProductResource\Pages\ListProducts;
use App\Classes\FilamentInput;
use App\Helpers\ExtensionHelper;
use App\Models\Currency;
use App\Models\Product;
use App\Models\Server;
use Exception;
use Filament\Actions\Action;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Infolists\Components\TextEntry;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Support\RawJs;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Livewire\Component;
class ProductResource extends Resource
{
protected static ?string $model = Product::class;
protected static string|\UnitEnum|null $navigationGroup = 'Administration';
protected static string|\BackedEnum|null $navigationIcon = 'ri-instance-line';
protected static string|\BackedEnum|null $activeNavigationIcon = 'ri-instance-fill';
protected static ?string $recordTitleAttribute = 'name';
public static function form(Schema $schema): Schema
{
return $schema
->components([
Tabs::make('Tabs')
->persistTabInQueryString()
->tabs([
Tab::make('General')
->columns(2)
->schema([
TextInput::make('name')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}
$set('slug', Str::slug($state));
}),
TextInput::make('slug')->required()->unique(ignoreRecord: true),
TextInput::make('stock')->integer()->nullable(),
TextInput::make('per_user_limit')->integer()->nullable(),
Select::make('allow_quantity')->options([
'disabled' => 'No',
'separated' => 'Separated',
'combined' => 'Combined',
])->default('separated')
->required(),
Textarea::make('email_template')
->hint('This snippet will be used in the email template.')
->nullable(),
Checkbox::make('hidden')
->label('Hide product')
->hint('Hide the product from the client area.'),
RichEditor::make('description')->nullable()->columnSpanFull(),
FileUpload::make('image')
->label('Image')
->nullable()
->visibility('public')
->imageEditor()
->image()
->disk('public')
->acceptedFileTypes(['image/*']),
Select::make('category_id')
->relationship('category', 'name')
->searchable()
->preload()
->createOptionForm(fn (Schema $schema) => CategoryResource::form($schema))
->required(),
]),
Tab::make('Pricing')
->schema([self::plan()]),
Tab::make('Upgrades')
->schema([
// Select input for the products this product can upgrade to (hasmany relationship)
Select::make('upgrades')
->label('Upgrades')
->relationship('upgrades', 'name', ignoreRecord: true)
->multiple()
->preload()
->placeholder('Select the products that this product can upgrade to'),
]),
Tab::make('Server')
->schema([
Select::make('server_id')
->relationship('server', 'name')
->searchable()
->preload()
->hintAction(
Action::make('refresh')
->label('Refresh')
->action(fn () => Cache::set('product_config', null, 0))
->hidden(fn (Get $get) => $get('server_id') === null)
)
->live()
->afterStateUpdated(fn (Select $component) => $component
->getContainer()
->getComponent('extension_settings', withHidden: true)
->getChildSchema()
->fill()),
Grid::make()
->hidden(fn (Get $get) => $get('server_id') === null)
->columns(2)
->key('extension_settings')
->schema(
function (Get $get, Component $livewire) {
$server = $get('server_id');
if ($server == null) {
return [];
}
$settings = [];
try {
foreach (ExtensionHelper::getProductConfigOnce(Server::findOrFail($server), $get('settings')) as $setting) {
// Easier to use dot notation for settings
$setting['name'] = 'settings.' . $setting['name'];
$settings[] = FilamentInput::convert($setting);
}
} catch (Exception $e) {
$settings[] = TextEntry::make('error')->state($e->getMessage());
}
return $settings;
}
),
]),
]),
])->columns(1);
}
public static function plan()
{
return Repeater::make('plan')
->addActionLabel('Add new plan')
->relationship('plans')
->name('name')
->reorderable()
->cloneable()
->collapsible()
->collapsed()
->orderColumn()
->defaultItems(1)
->minItems(1)
->columns(2)
->deleteAction(function (Action $action) {
$action->before(function (?Product $record, $state, Action $action, array $arguments) {
if (!$record) {
return;
}
$key = $arguments['item'];
if (!isset($state[$key]['id'])) {
return;
}
$plan = $record->plans()->find($state[$key]['id']);
if ($plan->services()->count() > 0) {
Notification::make()
->title('Whoops!')
->body('You cannot delete this plan because it is being used by one or more services.')
->danger()
->send();
$action->cancel();
}
});
})
->itemLabel(fn (array $state) => $state['name'])
->schema([
TextInput::make('name')
->required()
->live(onBlur: true)
->maxLength(255),
Select::make('type')
->options([
'free' => 'Free',
'one-time' => 'One Time',
'recurring' => 'Recurring',
])
->required()
->live(debounce: 300)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if ($state === 'free') {
$set('every', null);
$set('price', 0);
}
})
->placeholder('Select the type of the price')
->default('free'),
TextInput::make('billing_period')
->required()
->label('Time Interval')
->default(1)
->hidden(fn (Get $get) => $get('type') !== 'recurring'),
Select::make('billing_unit')
->options([
'day' => 'Day',
'week' => 'Week',
'month' => 'Month',
'year' => 'Year',
])
->label('Billing period')
->required()
->default('month')
->hidden(fn (Get $get) => $get('type') !== 'recurring'),
Repeater::make('pricing')
->hidden(fn (Get $get) => $get('type') === 'free')
->columns(3)
->addActionLabel('Add new price')
->reorderable(false)
->relationship('prices')
->columnSpanFull()
->maxItems(Currency::count())
->defaultItems(1)
->itemLabel(fn (array $state) => $state['currency_code'])
->schema([
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(),
TextInput::make('price')
->required()
->label('Price')
// Suffix based on chosen currency
->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)
->hidden(fn (Get $get) => $get('type') === 'free'),
TextInput::make('setup_fee')
->label('Setup fee')
->live(onBlur: true)
->mask(RawJs::make(
<<<'JS'
$money($input, '.', '', 2)
JS
))
->numeric()
->minValue(0)
->hidden(fn (Get $get) => $get('type') === 'free'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')->searchable(),
TextColumn::make('slug'),
TextColumn::make('category.name')->searchable(),
])
->filters([
SelectFilter::make('category')
->relationship('category', 'name')
->searchable()
->preload(),
])
->recordActions([
EditAction::make(),
])
->defaultSort(function (Builder $query): Builder {
return $query
->orderBy('sort', 'asc');
})
->defaultGroup('category.name');
}
public static function getPages(): array
{
return [
'index' => ListProducts::route('/'),
'create' => CreateProduct::route('/create'),
'edit' => EditProduct::route('/{record}/edit'),
];
}
}