coupon code system eingeführt. coupons werden vom super admin gemanaged. coupons werden mit paddle synchronisiert und dort validiert. plus: einige mobil-optimierungen im tenant admin pwa.
This commit is contained in:
123
app/Filament/Resources/Coupons/CouponResource.php
Normal file
123
app/Filament/Resources/Coupons/CouponResource.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?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\Pages\ViewCoupon;
|
||||
use App\Filament\Resources\Coupons\RelationManagers\RedemptionsRelationManager;
|
||||
use App\Filament\Resources\Coupons\Schemas\CouponForm;
|
||||
use App\Filament\Resources\Coupons\Schemas\CouponInfolist;
|
||||
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 Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class CouponResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Coupon::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedTicket;
|
||||
|
||||
protected static ?int $navigationSort = 60;
|
||||
|
||||
protected static ?string $navigationLabel = 'Coupons';
|
||||
|
||||
public static function getNavigationGroup(): string
|
||||
{
|
||||
return __('Billing & Finanzen');
|
||||
}
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return CouponForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return CouponInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return CouponsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
RedemptionsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListCoupons::route('/'),
|
||||
'create' => CreateCoupon::route('/create'),
|
||||
'view' => ViewCoupon::route('/{record}'),
|
||||
'edit' => EditCoupon::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getRecordRouteBindingEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getRecordRouteBindingEloquentQuery()
|
||||
->withoutGlobalScopes([
|
||||
SoftDeletingScope::class,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getEloquentQuery()->withoutGlobalScopes([
|
||||
SoftDeletingScope::class,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$userId = Auth::id();
|
||||
if ($userId) {
|
||||
$data['created_by'] = $userId;
|
||||
$data['updated_by'] = $userId;
|
||||
}
|
||||
|
||||
return static::sanitizeFormData($data);
|
||||
}
|
||||
|
||||
public static function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
if ($userId = Auth::id()) {
|
||||
$data['updated_by'] = $userId;
|
||||
}
|
||||
|
||||
return static::sanitizeFormData($data);
|
||||
}
|
||||
|
||||
protected static function sanitizeFormData(array $data): array
|
||||
{
|
||||
if (! empty($data['code'])) {
|
||||
$data['code'] = strtoupper($data['code']);
|
||||
}
|
||||
|
||||
if (($data['type'] ?? null) === 'percentage') {
|
||||
$data['currency'] = null;
|
||||
} elseif (! empty($data['currency'])) {
|
||||
$data['currency'] = strtoupper($data['currency']);
|
||||
}
|
||||
|
||||
if (empty($data['metadata'])) {
|
||||
$data['metadata'] = null;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
17
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
17
app/Filament/Resources/Coupons/Pages/CreateCoupon.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use App\Jobs\SyncCouponToPaddle;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateCoupon extends CreateRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
SyncCouponToPaddle::dispatch($this->record);
|
||||
}
|
||||
}
|
||||
32
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
32
app/Filament/Resources/Coupons/Pages/EditCoupon.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use App\Jobs\SyncCouponToPaddle;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ForceDeleteAction;
|
||||
use Filament\Actions\RestoreAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditCoupon extends EditRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make()
|
||||
->after(fn ($record) => SyncCouponToPaddle::dispatch($record, true)),
|
||||
ForceDeleteAction::make(),
|
||||
RestoreAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function afterSave(): void
|
||||
{
|
||||
SyncCouponToPaddle::dispatch($this->record);
|
||||
}
|
||||
}
|
||||
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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Coupons/Pages/ViewCoupon.php
Normal file
19
app/Filament/Resources/Coupons/Pages/ViewCoupon.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Pages;
|
||||
|
||||
use App\Filament\Resources\Coupons\CouponResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewCoupon extends ViewRecord
|
||||
{
|
||||
protected static string $resource = CouponResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\RelationManagers;
|
||||
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class RedemptionsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'redemptions';
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('paddle_transaction_id')
|
||||
->columns([
|
||||
TextColumn::make('tenant.name')
|
||||
->label(__('Tenant'))
|
||||
->searchable(),
|
||||
TextColumn::make('user.name')
|
||||
->label(__('User'))
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('amount_discounted')
|
||||
->label(__('Discount'))
|
||||
->formatStateUsing(fn ($state, $record) => sprintf('%s %s', number_format($record->amount_discounted, 2), $record->currency))
|
||||
->sortable(),
|
||||
TextColumn::make('status')
|
||||
->label(__('Status'))
|
||||
->badge()
|
||||
->color(fn ($state) => match ($state) {
|
||||
'success' => 'success',
|
||||
'failed' => 'danger',
|
||||
default => 'warning',
|
||||
}),
|
||||
TextColumn::make('paddle_transaction_id')
|
||||
->label(__('Transaction'))
|
||||
->copyable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('redeemed_at')
|
||||
->label(__('Redeemed at'))
|
||||
->dateTime()
|
||||
->sortable(),
|
||||
TextColumn::make('metadata')
|
||||
->label(__('Metadata'))
|
||||
->formatStateUsing(fn ($state) => $state ? json_encode($state, JSON_PRETTY_PRINT) : '—')
|
||||
->toggleable(isToggledHiddenByDefault: true)
|
||||
->copyable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->label(__('Status'))
|
||||
->options([
|
||||
'pending' => __('Pending'),
|
||||
'success' => __('Success'),
|
||||
'failed' => __('Failed'),
|
||||
]),
|
||||
])
|
||||
->headerActions([
|
||||
// read-only
|
||||
])
|
||||
->recordActions([])
|
||||
->toolbarActions([]);
|
||||
}
|
||||
}
|
||||
148
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
148
app/Filament/Resources/Coupons/Schemas/CouponForm.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Schemas;
|
||||
|
||||
use App\Enums\CouponStatus;
|
||||
use App\Enums\CouponType;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Placeholder;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
class CouponForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
$typeOptions = collect(CouponType::cases())
|
||||
->mapWithKeys(fn (CouponType $type) => [$type->value => $type->label()])
|
||||
->all();
|
||||
|
||||
$statusOptions = collect(CouponStatus::cases())
|
||||
->mapWithKeys(fn (CouponStatus $status) => [$status->value => $status->label()])
|
||||
->all();
|
||||
|
||||
return $schema
|
||||
->columns(1)
|
||||
->components([
|
||||
Section::make(__('Coupon details'))
|
||||
->columns(3)
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label(__('Name'))
|
||||
->maxLength(191)
|
||||
->required(),
|
||||
TextInput::make('code')
|
||||
->label(__('Code'))
|
||||
->maxLength(32)
|
||||
->required()
|
||||
->helperText(__('Codes are stored uppercase and must be unique.'))
|
||||
->unique(ignoreRecord: true, modifyRuleUsing: fn (Unique $rule) => $rule->withoutTrashed())
|
||||
->rule('alpha_dash:ascii'),
|
||||
Select::make('status')
|
||||
->label(__('Status'))
|
||||
->options($statusOptions)
|
||||
->default(CouponStatus::DRAFT)
|
||||
->required(),
|
||||
Select::make('type')
|
||||
->label(__('Discount Type'))
|
||||
->options($typeOptions)
|
||||
->default(CouponType::PERCENTAGE)
|
||||
->required(),
|
||||
TextInput::make('amount')
|
||||
->label(__('Amount'))
|
||||
->numeric()
|
||||
->minValue(0)
|
||||
->step(0.01)
|
||||
->required(),
|
||||
TextInput::make('currency')
|
||||
->label(__('Currency'))
|
||||
->maxLength(3)
|
||||
->default('EUR')
|
||||
->helperText(__('Required for flat discounts. Hidden for percentage codes.'))
|
||||
->disabled(fn (Get $get) => $get('type') === CouponType::PERCENTAGE->value)
|
||||
->nullable(),
|
||||
Textarea::make('description')
|
||||
->label(__('Description'))
|
||||
->rows(3)
|
||||
->columnSpanFull(),
|
||||
Toggle::make('enabled_for_checkout')
|
||||
->label(__('Can be redeemed at checkout'))
|
||||
->default(true),
|
||||
Toggle::make('auto_apply')
|
||||
->label(__('Auto apply when query parameter matches'))
|
||||
->helperText(__('Automatically applies when marketing links include ?coupon=CODE.')),
|
||||
Toggle::make('is_stackable')
|
||||
->label(__('Allow stacking with other coupons'))
|
||||
->default(false),
|
||||
]),
|
||||
Section::make(__('Limits & Scheduling'))
|
||||
->columns(2)
|
||||
->schema([
|
||||
DateTimePicker::make('starts_at')
|
||||
->label(__('Starts at'))
|
||||
->seconds(false)
|
||||
->nullable(),
|
||||
DateTimePicker::make('ends_at')
|
||||
->label(__('Ends at'))
|
||||
->seconds(false)
|
||||
->nullable(),
|
||||
TextInput::make('usage_limit')
|
||||
->label(__('Global usage limit'))
|
||||
->numeric()
|
||||
->minValue(1)
|
||||
->nullable(),
|
||||
TextInput::make('per_customer_limit')
|
||||
->label(__('Per tenant limit'))
|
||||
->numeric()
|
||||
->minValue(1)
|
||||
->nullable(),
|
||||
]),
|
||||
Section::make(__('Package restrictions'))
|
||||
->schema([
|
||||
Select::make('packages')
|
||||
->label(__('Applicable packages'))
|
||||
->relationship('packages', 'name')
|
||||
->multiple()
|
||||
->preload()
|
||||
->searchable()
|
||||
->helperText(__('Leave empty to allow all packages.')),
|
||||
]),
|
||||
Section::make(__('Metadata'))
|
||||
->schema([
|
||||
KeyValue::make('metadata')
|
||||
->label(__('Internal metadata'))
|
||||
->keyLabel(__('Key'))
|
||||
->valueLabel(__('Value'))
|
||||
->nullable()
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
Section::make(__('Paddle sync'))
|
||||
->columns(2)
|
||||
->schema([
|
||||
Select::make('paddle_mode')
|
||||
->label(__('Paddle mode'))
|
||||
->options([
|
||||
'standard' => __('Standard'),
|
||||
'custom' => __('Custom (one-off)'),
|
||||
])
|
||||
->default('standard'),
|
||||
Placeholder::make('paddle_discount_id')
|
||||
->label(__('Paddle Discount ID'))
|
||||
->content(fn ($record) => $record?->paddle_discount_id ?? '—'),
|
||||
Placeholder::make('paddle_last_synced_at')
|
||||
->label(__('Last synced'))
|
||||
->content(fn ($record) => $record?->paddle_last_synced_at?->diffForHumans() ?? '—'),
|
||||
Placeholder::make('redemptions_count')
|
||||
->label(__('Total redemptions'))
|
||||
->content(fn ($record) => number_format($record?->redemptions_count ?? 0)),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
81
app/Filament/Resources/Coupons/Schemas/CouponInfolist.php
Normal file
81
app/Filament/Resources/Coupons/Schemas/CouponInfolist.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\KeyValueEntry;
|
||||
use Filament\Infolists\Components\Section;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CouponInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make(__('Summary'))
|
||||
->columns(3)
|
||||
->schema([
|
||||
TextEntry::make('name')->label(__('Name')),
|
||||
TextEntry::make('code')->label(__('Code'))->copyable(),
|
||||
TextEntry::make('status')
|
||||
->label(__('Status'))
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => Str::headline($state)),
|
||||
TextEntry::make('type')
|
||||
->label(__('Discount type'))
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => Str::headline($state)),
|
||||
TextEntry::make('amount')
|
||||
->label(__('Amount'))
|
||||
->formatStateUsing(fn ($state, $record) => $record?->type?->value === 'percentage'
|
||||
? sprintf('%s%%', $record?->amount ?? $state)
|
||||
: sprintf('%s %s', number_format((float) ($record?->amount ?? $state), 2), strtoupper($record?->currency ?? 'EUR'))),
|
||||
TextEntry::make('redemptions_count')
|
||||
->label(__('Total redemptions'))
|
||||
->badge(),
|
||||
]),
|
||||
Section::make(__('Limits & scheduling'))
|
||||
->columns(2)
|
||||
->schema([
|
||||
TextEntry::make('usage_limit')->label(__('Global usage limit'))->placeholder('—'),
|
||||
TextEntry::make('per_customer_limit')->label(__('Per tenant limit'))->placeholder('—'),
|
||||
TextEntry::make('starts_at')
|
||||
->label(__('Starts'))
|
||||
->dateTime(),
|
||||
TextEntry::make('ends_at')
|
||||
->label(__('Ends'))
|
||||
->dateTime(),
|
||||
]),
|
||||
Section::make(__('Packages'))
|
||||
->schema([
|
||||
TextEntry::make('packages_list')
|
||||
->label(__('Limited to packages'))
|
||||
->state(fn ($record) => $record?->packages?->pluck('name')->all() ?: null)
|
||||
->listWithLineBreaks()
|
||||
->placeholder(__('All packages')),
|
||||
]),
|
||||
Section::make(__('Metadata'))
|
||||
->schema([
|
||||
TextEntry::make('description')->label(__('Description'))->columnSpanFull(),
|
||||
KeyValueEntry::make('metadata')->label(__('Metadata'))->columnSpanFull(),
|
||||
]),
|
||||
Section::make(__('Paddle'))
|
||||
->columns(3)
|
||||
->schema([
|
||||
TextEntry::make('paddle_discount_id')
|
||||
->label(__('Discount ID'))
|
||||
->copyable()
|
||||
->placeholder('—'),
|
||||
TextEntry::make('paddle_last_synced_at')
|
||||
->label(__('Last synced'))
|
||||
->state(fn ($record) => $record?->paddle_last_synced_at?->diffForHumans() ?? '—'),
|
||||
TextEntry::make('paddle_mode')
|
||||
->label(__('Mode'))
|
||||
->badge()
|
||||
->placeholder('standard'),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
113
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
113
app/Filament/Resources/Coupons/Tables/CouponsTable.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Coupons\Tables;
|
||||
|
||||
use App\Enums\CouponStatus;
|
||||
use App\Enums\CouponType;
|
||||
use App\Jobs\SyncCouponToPaddle;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\ForceDeleteBulkAction;
|
||||
use Filament\Actions\RestoreBulkAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Filters\TrashedFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CouponsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')
|
||||
->label(__('Name'))
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('code')
|
||||
->label(__('Code'))
|
||||
->badge()
|
||||
->copyable()
|
||||
->sortable(),
|
||||
TextColumn::make('type')
|
||||
->label(__('Type'))
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => Str::headline($state))
|
||||
->sortable(),
|
||||
TextColumn::make('amount')
|
||||
->label(__('Amount'))
|
||||
->formatStateUsing(fn ($state, $record) => $record->type === CouponType::PERCENTAGE
|
||||
? sprintf('%s%%', $record->amount)
|
||||
: sprintf('%s %s', number_format($record->amount, 2), strtoupper($record->currency ?? 'EUR')))
|
||||
->sortable(),
|
||||
TextColumn::make('redemptions_count')
|
||||
->label(__('Redeemed'))
|
||||
->badge()
|
||||
->sortable(),
|
||||
IconColumn::make('enabled_for_checkout')
|
||||
->label(__('Checkout'))
|
||||
->boolean(),
|
||||
TextColumn::make('status')
|
||||
->label(__('Status'))
|
||||
->badge()
|
||||
->sortable()
|
||||
->formatStateUsing(fn ($state) => Str::headline($state)),
|
||||
TextColumn::make('starts_at')
|
||||
->label(__('Starts'))
|
||||
->date()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('ends_at')
|
||||
->label(__('Ends'))
|
||||
->date()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->label(__('Status'))
|
||||
->options(
|
||||
collect(CouponStatus::cases())->mapWithKeys(fn ($status) => [$status->value => $status->label()])->all()
|
||||
),
|
||||
SelectFilter::make('type')
|
||||
->label(__('Type'))
|
||||
->options(
|
||||
collect(CouponType::cases())->mapWithKeys(fn ($type) => [$type->value => $type->label()])->all()
|
||||
),
|
||||
TernaryFilter::make('is_active')
|
||||
->label(__('Currently active'))
|
||||
->placeholder(__('All'))
|
||||
->queries(
|
||||
true: fn ($query) => $query->active(),
|
||||
false: fn ($query) => $query->where(function ($inner) {
|
||||
$inner->where('status', '!=', CouponStatus::ACTIVE->value)
|
||||
->orWhere(function ($sub) {
|
||||
$sub->whereNotNull('ends_at')
|
||||
->where('ends_at', '<', now());
|
||||
});
|
||||
}),
|
||||
),
|
||||
TrashedFilter::make(),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
EditAction::make(),
|
||||
Action::make('sync')
|
||||
->label(__('Sync to Paddle'))
|
||||
->icon('heroicon-m-arrow-path')
|
||||
->action(fn ($record) => SyncCouponToPaddle::dispatch($record))
|
||||
->requiresConfirmation(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
ForceDeleteBulkAction::make(),
|
||||
RestoreBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
60
app/Filament/Widgets/CouponUsageWidget.php
Normal file
60
app/Filament/Widgets/CouponUsageWidget.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Coupon;
|
||||
use App\Models\CouponRedemption;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Widgets\TableWidget as BaseWidget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CouponUsageWidget extends BaseWidget
|
||||
{
|
||||
protected static ?int $sort = 7;
|
||||
|
||||
protected function getHeading(): string
|
||||
{
|
||||
return __('Coupon performance (30d)');
|
||||
}
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
$since = now()->subDays(30);
|
||||
|
||||
return Coupon::query()
|
||||
->withCount(['redemptions as recent_redemptions_count' => function (Builder $query) use ($since) {
|
||||
$query->where('status', CouponRedemption::STATUS_SUCCESS)
|
||||
->where('redeemed_at', '>=', $since);
|
||||
}])
|
||||
->withSum(['redemptions as recent_discount_sum' => function (Builder $query) use ($since) {
|
||||
$query->where('status', CouponRedemption::STATUS_SUCCESS)
|
||||
->where('redeemed_at', '>=', $since);
|
||||
}], 'amount_discounted')
|
||||
->orderByDesc('recent_discount_sum')
|
||||
->limit(5);
|
||||
}
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('code')
|
||||
->label(__('Code'))
|
||||
->badge(),
|
||||
TextColumn::make('status')
|
||||
->label(__('Status'))
|
||||
->badge(),
|
||||
TextColumn::make('recent_redemptions_count')
|
||||
->label(__('Uses (30d)'))
|
||||
->sortable(),
|
||||
TextColumn::make('recent_discount_sum')
|
||||
->label(__('Discount (30d)'))
|
||||
->formatStateUsing(function ($state, Coupon $record) {
|
||||
$currency = $record->currency ?? 'EUR';
|
||||
$value = (float) ($state ?? 0);
|
||||
|
||||
return sprintf('%s %0.2f', $currency, $value);
|
||||
})
|
||||
->sortable(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user