79 lines
3.7 KiB
PHP
79 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Offers\Pages;
|
|
|
|
use App\Domain\Catalog\BundleVersion;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\RightsProfileVersion;
|
|
use App\Filament\Resources\Offers\OfferResource;
|
|
use App\Filament\Support\CatalogOptions;
|
|
use App\Filament\Support\ReportsDomainRules;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\Repeater;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Resources\Pages\ListRecords;
|
|
use Filament\Schemas\Components\Utilities\Get;
|
|
|
|
class ListOffers extends ListRecords
|
|
{
|
|
use ReportsDomainRules;
|
|
|
|
protected static string $resource = OfferResource::class;
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('create')->label('Angebot anlegen')->schema([
|
|
TextInput::make('name')->label('Name')->required(),
|
|
])->action(function (array $data): void {
|
|
$this->report(function () use ($data): void {
|
|
app(OfferService::class)->create($data['name']);
|
|
}, 'Das Angebot ist angelegt.');
|
|
}),
|
|
Action::make('activate')->label('Fassung aktivieren')->schema([
|
|
Select::make('offer_id')->label('Angebot')->options(fn (): array => CatalogOptions::offers())->required(),
|
|
Select::make('grant_kind')->label('Vergabe')->options([
|
|
'kauf' => 'Kauf',
|
|
'auf_abruf' => 'Auf Abruf',
|
|
'bei_registrierung' => 'Bei Registrierung',
|
|
])->required(),
|
|
TextInput::make('price_cents')->label('Preis in Cent')->numeric(),
|
|
Repeater::make('lines')->label('Positionen')->schema([
|
|
Select::make('kind')->label('Ziel')->options([
|
|
'article' => 'Lizenzartikel',
|
|
'bundle' => 'Paketfassung',
|
|
])->required()->live(),
|
|
Select::make('article_id')->label('Artikel')->options(fn (): array => CatalogOptions::articles())
|
|
->visible(fn (Get $get): bool => $get('kind') === 'article'),
|
|
Select::make('bundle_version_id')->label('Paketfassung')->options(fn (): array => CatalogOptions::bundleVersions())
|
|
->visible(fn (Get $get): bool => $get('kind') === 'bundle'),
|
|
Select::make('rights_id')->label('Rechte')->options(fn (): array => CatalogOptions::rights())->required(),
|
|
])->minItems(1),
|
|
])->action(function (array $data): void {
|
|
$this->report(function () use ($data): void {
|
|
$lines = [];
|
|
|
|
foreach ($data['lines'] as $line) {
|
|
$target = $line['kind'] === 'bundle'
|
|
? BundleVersion::query()->findOrFail($line['bundle_version_id'])
|
|
: LicenseArticle::query()->findOrFail($line['article_id']);
|
|
$lines[] = [
|
|
'target' => $target,
|
|
'rights' => RightsProfileVersion::query()->findOrFail($line['rights_id']),
|
|
];
|
|
}
|
|
|
|
app(OfferService::class)->activate(Offer::query()->findOrFail($data['offer_id']), $lines, [
|
|
'grant_kind' => $data['grant_kind'],
|
|
'price_cents' => $data['price_cents'] === null || $data['price_cents'] === '' ? null : (int) $data['price_cents'],
|
|
'holder_kind' => 'person',
|
|
]);
|
|
}, 'Die Angebotsfassung ist aktiv.');
|
|
}),
|
|
];
|
|
}
|
|
}
|