Files
fotospiel-app/app/Filament/Resources/Coupons/Schemas/CouponForm.php
Codex Agent 10c99de1e2
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Migrate billing from Paddle to Lemon Squeezy
2026-02-03 10:59:54 +01:00

149 lines
6.9 KiB
PHP

<?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(__('Lemon Squeezy sync'))
->columns(2)
->schema([
Select::make('lemonsqueezy_mode')
->label(__('Lemon Squeezy mode'))
->options([
'standard' => __('Standard'),
'custom' => __('Custom (one-off)'),
])
->default('standard'),
Placeholder::make('lemonsqueezy_discount_id')
->label(__('Lemon Squeezy Discount ID'))
->content(fn ($record) => $record?->lemonsqueezy_discount_id ?? '—'),
Placeholder::make('lemonsqueezy_last_synced_at')
->label(__('Last synced'))
->content(fn ($record) => $record?->lemonsqueezy_last_synced_at?->diffForHumans() ?? '—'),
Placeholder::make('redemptions_count')
->label(__('Total redemptions'))
->content(fn ($record) => number_format($record?->redemptions_count ?? 0)),
]),
]);
}
}