411 lines
13 KiB
PHP
411 lines
13 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Support;
|
||
|
||
use App\Domain\Catalog\Bundle;
|
||
use App\Domain\Catalog\BundleService;
|
||
use App\Domain\Catalog\BundleStatus;
|
||
use App\Domain\Catalog\BundleVersion;
|
||
use App\Domain\Catalog\Composition;
|
||
use App\Domain\Catalog\CompositionMember;
|
||
use App\Domain\Catalog\ContentElement;
|
||
use App\Domain\Catalog\ContentVersionStatus;
|
||
use App\Domain\Catalog\DomainRuleException;
|
||
use App\Domain\Catalog\LicenseArticle;
|
||
use App\Domain\Catalog\Offer;
|
||
use App\Domain\Catalog\OfferStatus;
|
||
use App\Domain\Catalog\OfferVersion;
|
||
use App\Domain\Catalog\ReleaseContent;
|
||
use App\Domain\Catalog\ResolvedScope;
|
||
use App\Domain\Catalog\RightsProfile;
|
||
use App\Domain\Catalog\RightsProfileVersion;
|
||
use App\Domain\Catalog\StatusValue;
|
||
|
||
class CatalogReading
|
||
{
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function articleStand(LicenseArticle $article): array
|
||
{
|
||
$release = $article->currentRelease()->with(['contents.elementVersion.element'])->first();
|
||
|
||
if ($release === null) {
|
||
return ['Noch kein Stand veröffentlicht. Ein Lizenzartikel ohne Stand vergibt keine Inhalte.'];
|
||
}
|
||
|
||
$lines = [
|
||
'Stand '.$release->number.' · '.$this->status($release->status).' · '.$release->classification_note,
|
||
];
|
||
|
||
foreach ($release->contents as $content) {
|
||
$title = $content->elementVersion?->element?->working_title ?? 'Inhalt fehlt';
|
||
$lines[] = $content->path.' — '.$title;
|
||
}
|
||
|
||
if ($release->contents->isEmpty()) {
|
||
$lines[] = 'Dieser Stand hat keine festgeschriebenen Inhalte.';
|
||
}
|
||
|
||
return $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function articleContext(LicenseArticle $article): array
|
||
{
|
||
$lines = [];
|
||
|
||
foreach (BundleVersion::query()->where('status', BundleStatus::Veroeffentlicht)->with('bundle')->orderBy('id')->get() as $version) {
|
||
try {
|
||
$ids = app(BundleService::class)->articleIds($version);
|
||
} catch (DomainRuleException $exception) {
|
||
continue;
|
||
}
|
||
|
||
if (in_array($article->id, $ids, true)) {
|
||
$lines[] = 'Paket '.$version->bundle->name.' · Fassung '.$version->number;
|
||
}
|
||
}
|
||
|
||
$scopes = ResolvedScope::query()
|
||
->where('license_article_id', $article->id)
|
||
->with(['offerVersion.offer', 'rights.profile'])
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
foreach ($scopes as $scope) {
|
||
$version = $scope->offerVersion;
|
||
$lines[] = 'Angebot '.$version->offer->name
|
||
.' · Fassung '.$version->number
|
||
.' · '.$this->status($version->status)
|
||
.' · '.$this->grant($version)
|
||
.' · '.$scope->rights->profile->name
|
||
.' ('.$this->capabilities($scope->rights).')';
|
||
}
|
||
|
||
return $lines === []
|
||
? ['Dieser Artikel ist noch in keinem veröffentlichten Paket und in keiner Angebotsfassung aufgelöst.']
|
||
: $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function bundleParts(Bundle $bundle): array
|
||
{
|
||
$version = $this->publishedBundle($bundle);
|
||
|
||
if ($version === null) {
|
||
return ['Noch keine veröffentlichte Fassung. Bestandteile werden mit der Fassung festgeschrieben.'];
|
||
}
|
||
|
||
$lines = ['Fassung '.$version->number];
|
||
|
||
foreach ($version->items as $item) {
|
||
if ($item->article !== null) {
|
||
$lines[] = 'Lizenzartikel '.$item->article->code;
|
||
|
||
continue;
|
||
}
|
||
|
||
$lines[] = 'Paket '.($item->nestedBundle->name ?? 'fehlt');
|
||
}
|
||
|
||
$older = $bundle->versions()->where('status', BundleStatus::Veroeffentlicht)->count() - 1;
|
||
|
||
if ($older > 0) {
|
||
$lines[] = $older.' ältere Fassung'.($older === 1 ? '' : 'en').' bleibt unverändert.';
|
||
}
|
||
|
||
return $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function bundleArticles(Bundle $bundle): array
|
||
{
|
||
$version = $this->publishedBundle($bundle);
|
||
|
||
if ($version === null) {
|
||
return ['Ohne Fassung gibt es nichts aufzulösen.'];
|
||
}
|
||
|
||
try {
|
||
$ids = app(BundleService::class)->articleIds($version);
|
||
} catch (DomainRuleException $exception) {
|
||
return [$exception->getMessage()];
|
||
}
|
||
|
||
$codes = LicenseArticle::query()->whereIn('id', $ids)->orderBy('code')->pluck('code')->all();
|
||
|
||
return $codes === []
|
||
? ['Die Fassung löst keinen Lizenzartikel auf.']
|
||
: $codes;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function offerPositions(Offer $offer): array
|
||
{
|
||
$version = $this->activeOffer($offer);
|
||
|
||
if ($version === null) {
|
||
return ['Noch keine aktive Fassung. Ein Angebot ohne Fassung kann niemand erwerben.'];
|
||
}
|
||
|
||
$lines = [
|
||
'Fassung '.$version->number.' · '.$this->status($version->status).' · '.$this->grant($version),
|
||
];
|
||
|
||
foreach ($version->lines as $line) {
|
||
$target = $line->article !== null
|
||
? 'Lizenzartikel '.$line->article->code
|
||
: 'Paket '.($line->bundleVersion->bundle->name ?? 'fehlt').' · Fassung '.($line->bundleVersion->number ?? '–');
|
||
$lines[] = $target.' · Rechte '.$line->rights->profile->name;
|
||
}
|
||
|
||
return $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function offerGrants(Offer $offer): array
|
||
{
|
||
$version = $this->activeOffer($offer);
|
||
|
||
if ($version === null) {
|
||
return ['Der Leistungsumfang wird erst beim Aktivieren aufgelöst.'];
|
||
}
|
||
|
||
$lines = [];
|
||
|
||
foreach ($version->resolvedScopes as $scope) {
|
||
$lines[] = $scope->article->code.' · '.$scope->rights->profile->name.' · '.$this->capabilities($scope->rights);
|
||
}
|
||
|
||
return $lines === []
|
||
? ['Diese Fassung hat keinen aufgelösten Leistungsumfang.']
|
||
: $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function rights(RightsProfile $profile): array
|
||
{
|
||
$version = $profile->versions()->orderByDesc('number')->first();
|
||
|
||
if ($version === null) {
|
||
return ['Noch keine Fassung.'];
|
||
}
|
||
|
||
return [
|
||
'Fassung '.$version->number.' · '.$this->purpose($version->purpose),
|
||
$this->capabilities($version),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function elementVersions(ContentElement $element): array
|
||
{
|
||
$versions = $element->versions()->orderByDesc('number')->get();
|
||
|
||
if ($versions->isEmpty()) {
|
||
return ['Noch keine Fassung.'];
|
||
}
|
||
|
||
return $versions->map(function ($version): string {
|
||
$note = filled($version->change_note) ? ' · '.$version->change_note : '';
|
||
|
||
return 'Fassung '.$version->number.' · '.$this->status($version->status).$note;
|
||
})->all();
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function elementUsage(ContentElement $element): array
|
||
{
|
||
$versionIds = $element->versions()->pluck('id');
|
||
$lines = [];
|
||
|
||
$members = CompositionMember::query()
|
||
->whereIn('content_element_version_id', $versionIds)
|
||
->with(['compositionVersion.composition', 'contentElementVersion'])
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
foreach ($members as $member) {
|
||
$lines[] = 'Komposition '.$member->compositionVersion->composition->designation
|
||
.' · Fassung '.$member->compositionVersion->number
|
||
.' · Rolle '.$member->role;
|
||
}
|
||
|
||
$contents = ReleaseContent::query()
|
||
->whereIn('content_element_version_id', $versionIds)
|
||
->with(['release.article', 'elementVersion'])
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
foreach ($contents as $content) {
|
||
$lines[] = 'Lizenzartikel '.$content->release->article->code
|
||
.' · Stand '.$content->release->number
|
||
.' · '.$content->path;
|
||
}
|
||
|
||
return $lines === []
|
||
? ['Noch in keiner Komposition und in keinem Artikel-Stand verwendet.']
|
||
: $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function compositionMembers(Composition $composition): array
|
||
{
|
||
$version = $composition->versions()
|
||
->where('status', ContentVersionStatus::Bereit)
|
||
->orderByDesc('number')
|
||
->with(['members.contentElementVersion.element', 'members.nestedCompositionVersion.composition'])
|
||
->first();
|
||
|
||
if ($version === null) {
|
||
return ['Noch keine bestätigte Fassung.'];
|
||
}
|
||
|
||
$lines = ['Fassung '.$version->number];
|
||
|
||
foreach ($version->members as $member) {
|
||
if ($member->contentElementVersion !== null) {
|
||
$lines[] = $member->role.' — '.$member->contentElementVersion->element->working_title;
|
||
|
||
continue;
|
||
}
|
||
|
||
$nested = $member->nestedCompositionVersion;
|
||
$lines[] = $member->role.' — Komposition '.($nested->composition->designation ?? 'fehlt');
|
||
}
|
||
|
||
return $lines;
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
public function compositionUsage(Composition $composition): array
|
||
{
|
||
$versionIds = $composition->versions()->pluck('id');
|
||
$lines = [];
|
||
|
||
$parents = CompositionMember::query()
|
||
->whereIn('nested_composition_version_id', $versionIds)
|
||
->with('compositionVersion.composition')
|
||
->orderBy('id')
|
||
->get();
|
||
|
||
foreach ($parents as $member) {
|
||
$lines[] = 'Komposition '.$member->compositionVersion->composition->designation
|
||
.' · Fassung '.$member->compositionVersion->number
|
||
.' · Rolle '.$member->role;
|
||
}
|
||
|
||
$articles = LicenseArticle::query()
|
||
->whereHas('releases', function ($query) use ($versionIds): void {
|
||
$query->whereIn('root_composition_version_id', $versionIds);
|
||
})
|
||
->orderBy('code')
|
||
->pluck('code');
|
||
|
||
foreach ($articles as $code) {
|
||
$lines[] = 'Wurzel des Lizenzartikels '.$code;
|
||
}
|
||
|
||
return $lines === []
|
||
? ['Noch nicht in einer anderen Komposition und nicht als Wurzel eines Artikels verwendet.']
|
||
: $lines;
|
||
}
|
||
|
||
private function publishedBundle(Bundle $bundle): ?BundleVersion
|
||
{
|
||
return $bundle->versions()
|
||
->where('status', BundleStatus::Veroeffentlicht)
|
||
->orderByDesc('number')
|
||
->with(['items.article', 'items.nestedBundle'])
|
||
->first();
|
||
}
|
||
|
||
private function activeOffer(Offer $offer): ?OfferVersion
|
||
{
|
||
return $offer->versions()
|
||
->where('status', OfferStatus::Aktiv)
|
||
->orderByDesc('number')
|
||
->with([
|
||
'lines.article',
|
||
'lines.bundleVersion.bundle',
|
||
'lines.rights.profile',
|
||
'resolvedScopes.article',
|
||
'resolvedScopes.rights.profile',
|
||
])
|
||
->first();
|
||
}
|
||
|
||
private function grant(OfferVersion $version): string
|
||
{
|
||
$kind = match ($version->grant_kind) {
|
||
'kauf' => 'Kauf',
|
||
'auf_abruf' => 'Auf Abruf',
|
||
'bei_registrierung' => 'Bei Registrierung',
|
||
default => (string) $version->grant_kind,
|
||
};
|
||
|
||
if ($version->price_cents === null) {
|
||
return $kind;
|
||
}
|
||
|
||
return $kind.' · '.number_format($version->price_cents / 100, 2, ',', '.').' €';
|
||
}
|
||
|
||
private function capabilities(RightsProfileVersion $version): string
|
||
{
|
||
$parts = [];
|
||
|
||
foreach ($version->capabilities ?? [] as $class => $actions) {
|
||
$actions = is_array($actions) ? implode(', ', $actions) : (string) $actions;
|
||
$parts[] = $class.': '.$actions;
|
||
}
|
||
|
||
return $parts === [] ? 'keine Fähigkeiten' : implode('; ', $parts);
|
||
}
|
||
|
||
private function purpose(string $purpose): string
|
||
{
|
||
return match ($purpose) {
|
||
'ehrenamtlich' => 'Ehrenamtlich',
|
||
'kommerziell' => 'Kommerziell',
|
||
'institutionell' => 'Institutionell',
|
||
default => $purpose,
|
||
};
|
||
}
|
||
|
||
private function status(mixed $status): string
|
||
{
|
||
return match (StatusValue::of($status)) {
|
||
'veroeffentlicht' => 'veröffentlicht',
|
||
'abgeloest_verfuegbar' => 'abgelöst, weiterhin verfügbar',
|
||
'gesperrt' => 'gesperrt',
|
||
'bereit' => 'bereit',
|
||
'entwurf' => 'Entwurf',
|
||
'aktiv' => 'aktiv',
|
||
'beendet' => 'beendet',
|
||
default => StatusValue::of($status) ?? 'unbekannt',
|
||
};
|
||
}
|
||
}
|