76 lines
3.4 KiB
PHP
76 lines
3.4 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\ViewRecord;
|
|
use Filament\Schemas\Components\Utilities\Get;
|
|
|
|
class ViewOffer extends ViewRecord
|
|
{
|
|
use ReportsDomainRules;
|
|
|
|
protected static string $resource = OfferResource::class;
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('activate')->label('Fassung aktivieren')->schema([
|
|
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()
|
|
->helperText('Nur bei einem Kauf. 500 bedeutet 5,00 €.'),
|
|
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 {
|
|
/** @var Offer $offer */
|
|
$offer = $this->getRecord();
|
|
$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, $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.');
|
|
$this->getRecord()->unsetRelations();
|
|
$this->getRecord()->refresh();
|
|
}),
|
|
];
|
|
}
|
|
}
|