Die Fähigkeiten im aufgelösten Leistungsumfang tragen dieselben Bezeichnungen wie beim Rechteprofil („Film ansehen“ statt „film: streamen“). Filaments eigener Pfad entfällt auf der Angebotsseite wie bei Medien und Aufbau. Der Art-Umschalter der Bestandsliste bricht nicht mehr um. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
263 lines
11 KiB
PHP
263 lines
11 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Resources\Offers\Pages;
|
||
|
||
use App\Domain\Catalog\BundleVersion;
|
||
use App\Domain\Catalog\CompositionVersion;
|
||
use App\Domain\Catalog\LicenseArticle;
|
||
use App\Domain\Catalog\Offer;
|
||
use App\Domain\Catalog\OfferService;
|
||
use App\Domain\Catalog\OfferStatus;
|
||
use App\Domain\Catalog\OfferVersion;
|
||
use App\Domain\Catalog\RightsProfileVersion;
|
||
use App\Domain\Catalog\StatusValue;
|
||
use App\Filament\Resources\Offers\OfferResource;
|
||
use App\Filament\Resources\RightsProfiles\Pages\ListRightsProfiles;
|
||
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;
|
||
use Filament\Schemas\Components\View;
|
||
use Filament\Schemas\Schema;
|
||
use Filament\Support\Enums\Width;
|
||
use Illuminate\Contracts\Support\Htmlable;
|
||
|
||
/**
|
||
* Angebot im gemeinsamen Rahmen. Anlegen und Ändern nutzen dieselbe Maske (FACH-20): „Fassung aktivieren“ legt die
|
||
* nächste, danach unveränderliche Angebotsfassung an; bei einer Änderung ist die Maske mit der aktiven Fassung
|
||
* vorbelegt (FACH-08). Die Wirkung bleibt die bestehende von OfferService::activate.
|
||
*/
|
||
class ViewOffer extends ViewRecord
|
||
{
|
||
use ReportsDomainRules;
|
||
|
||
protected static string $resource = OfferResource::class;
|
||
|
||
public function getTitle(): string|Htmlable
|
||
{
|
||
return (string) $this->getRecord()->name;
|
||
}
|
||
|
||
public function getHeading(): string|Htmlable
|
||
{
|
||
return '';
|
||
}
|
||
|
||
public function getMaxContentWidth(): Width|string|null
|
||
{
|
||
return Width::Full;
|
||
}
|
||
|
||
protected function getHeaderActions(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
/**
|
||
* Der gemeinsame Objektkopf zeigt den Pfad; Filaments eigener Pfad entfällt wie bei Medien und Aufbau.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
public function getBreadcrumbs(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
public function infolist(Schema $schema): Schema
|
||
{
|
||
return $schema->components([
|
||
View::make('filament.catalog.angebot')->viewData(fn (): array => $this->angebotDaten())->columnSpanFull(),
|
||
])->columns(1);
|
||
}
|
||
|
||
public function activeVersion(): ?OfferVersion
|
||
{
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
|
||
return $offer->versions()
|
||
->where('status', OfferStatus::Aktiv)
|
||
->orderByDesc('number')
|
||
->with(['lines.article', 'lines.bundleVersion.bundle', 'lines.rights.profile'])
|
||
->first();
|
||
}
|
||
|
||
public function activateAction(): Action
|
||
{
|
||
return Action::make('activate')
|
||
->label(fn (): string => $this->activeVersion() !== null ? 'Geänderte Fassung aktivieren' : 'Fassung aktivieren')
|
||
->modalHeading(fn (): string => $this->activeVersion() !== null
|
||
? 'Angebot ändern: Fassung '.$this->nextNumber().' aktivieren'
|
||
: 'Erste Fassung aktivieren')
|
||
->modalDescription(fn (): string => ($active = $this->activeVersion()) !== null
|
||
? 'Die Maske ist mit Fassung '.$active->number.' vorbelegt. Beim Aktivieren wird Fassung '.$active->number.' beendet und bleibt unverändert gespeichert; neue Erwerbe nutzen Fassung '.$this->nextNumber().'. Bereits begründete Berechtigungen ändern sich dadurch nicht.'
|
||
: 'Positionen, Vergabe und Preis bilden die erste Fassung. Nach dem Aktivieren ist sie unveränderlich; Änderungen ergeben eine weitere Fassung.')
|
||
->modalSubmitActionLabel(fn (): string => 'Fassung '.$this->nextNumber().' aktivieren')
|
||
->modalWidth('4xl')
|
||
->fillForm(fn (): array => $this->formFromActive())
|
||
->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 €.'),
|
||
Select::make('composition_version_id')->label('Programmfassung, die abgedeckt sein muss')
|
||
->options(function (): array {
|
||
return CompositionVersion::query()
|
||
->where('status', 'bereit')
|
||
->with('composition.editorialContentKind')
|
||
->orderByDesc('id')
|
||
->get()
|
||
->mapWithKeys(fn (CompositionVersion $version): array => [$version->id => CatalogOptions::compositionLabel($version)])
|
||
->all();
|
||
})
|
||
->searchable()
|
||
->helperText('Wenn gewählt, lässt sich die Fassung nur aktivieren, wenn jeder Bestandteil in den Artikeln enthalten ist.'),
|
||
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)->addActionLabel('Position hinzufügen'),
|
||
])
|
||
->action(function (array $data): void {
|
||
$this->report(function () use ($data): string {
|
||
/** @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']),
|
||
];
|
||
}
|
||
|
||
$version = 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',
|
||
'composition_version_id' => filled($data['composition_version_id'] ?? null) ? (int) $data['composition_version_id'] : null,
|
||
]);
|
||
|
||
return 'Fassung '.$version->number.' ist aktiv.';
|
||
}, 'Die Angebotsfassung ist aktiv.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Vorbelegung aus der aktiven Fassung; ohne aktive Fassung eine leere Position.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function formFromActive(): array
|
||
{
|
||
$active = $this->activeVersion();
|
||
|
||
if ($active === null) {
|
||
return ['grant_kind' => 'kauf', 'lines' => [['kind' => 'article']]];
|
||
}
|
||
|
||
return [
|
||
'grant_kind' => $active->grant_kind,
|
||
'price_cents' => $active->price_cents,
|
||
'lines' => $active->lines->sortBy('position')->map(fn ($line): array => [
|
||
'kind' => $line->license_article_id !== null ? 'article' : 'bundle',
|
||
'article_id' => $line->license_article_id,
|
||
'bundle_version_id' => $line->bundle_version_id,
|
||
'rights_id' => $line->rights_profile_version_id,
|
||
])->values()->all(),
|
||
];
|
||
}
|
||
|
||
private function nextNumber(): int
|
||
{
|
||
return ((int) $this->getRecord()->versions()->max('number')) + 1;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function angebotDaten(): array
|
||
{
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
$active = $this->activeVersion();
|
||
$versions = $offer->versions()->orderByDesc('number')->get();
|
||
|
||
return [
|
||
'offer' => $offer,
|
||
'active' => $active,
|
||
'grants' => $active !== null ? $this->leistungsumfang($active) : [],
|
||
'versions' => $versions->map(fn (OfferVersion $version): array => [
|
||
'number' => $version->number,
|
||
'status' => match (StatusValue::of($version->status)) {
|
||
'aktiv' => 'Aktiv',
|
||
'beendet' => 'Beendet',
|
||
default => 'Entwurf',
|
||
},
|
||
'ton' => match (StatusValue::of($version->status)) {
|
||
'aktiv' => 'gut',
|
||
'beendet' => 'leise',
|
||
default => 'offen',
|
||
},
|
||
'vergabe' => $this->vergabe($version),
|
||
'wann' => $version->activated_at?->format('d.m.Y'),
|
||
])->all(),
|
||
'vergabe' => $active !== null ? $this->vergabe($active) : null,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Aufgelöster Leistungsumfang in lesbaren Fähigkeiten (dieselben Bezeichnungen wie beim Rechteprofil).
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
private function leistungsumfang(OfferVersion $version): array
|
||
{
|
||
$zeilen = [];
|
||
|
||
foreach ($version->resolvedScopes()->with(['article', 'rights.profile'])->get() as $scope) {
|
||
$faehigkeiten = [];
|
||
|
||
foreach ($scope->rights?->capabilities ?? [] as $class => $actions) {
|
||
foreach ((array) $actions as $action) {
|
||
$faehigkeiten[] = ListRightsProfiles::FAEHIGKEITEN[$class.':'.$action] ?? $class.': '.$action;
|
||
}
|
||
}
|
||
|
||
$zeilen[] = 'Lizenzartikel '.$scope->article?->code.' · '.($scope->rights?->profile?->name ?? '–').' · '.($faehigkeiten === [] ? 'keine Fähigkeiten' : implode(', ', $faehigkeiten));
|
||
}
|
||
|
||
return $zeilen === [] ? ['Diese Fassung hat keinen aufgelösten Leistungsumfang.'] : $zeilen;
|
||
}
|
||
|
||
private function vergabe(OfferVersion $version): string
|
||
{
|
||
$kind = match ($version->grant_kind) {
|
||
'kauf' => 'Kauf',
|
||
'auf_abruf' => 'Auf Abruf',
|
||
'bei_registrierung' => 'Bei Registrierung',
|
||
default => (string) $version->grant_kind,
|
||
};
|
||
|
||
return $version->price_cents === null ? $kind : $kind.' · '.number_format($version->price_cents / 100, 2, ',', '.').' €';
|
||
}
|
||
}
|