Am Lizenzartikel gibt es den Abschnitt "Im Shop verkaufen": Er listet die Optionen mit Preis, Rechten, Nutzerkreis und Zustand. "Option hinzufuegen" fragt nur Rechteprofil, Preis, Eignung, Nutzerkreis und eine Bezeichnung ab; Artikel, Inhalt und Vergabe stehen fest. Aendern und Aus dem Verkauf nehmen laufen am selben Ort. OfferService::offerArticle legt Angebot und erste Fassung in einer Transaktion an, damit eine fehlgeschlagene Pruefung kein leeres Angebot hinterlaesst. reviseArticleOption aktiviert die naechste Fassung und lehnt Optionen anderer Artikel ab. Aus der Angebotsmaske entfaellt die Auswahl "Programmfassung, die abgedeckt sein muss"; die Pruefung bleibt im Dienst. Pakete und der Altbestand behalten die Angebotsmaske.
379 lines
13 KiB
PHP
379 lines
13 KiB
PHP
<?php
|
||
|
||
namespace App\Domain\Commerce;
|
||
|
||
use App\Domain\Catalog\Bundle;
|
||
use App\Domain\Catalog\Composition;
|
||
use App\Domain\Catalog\CompositionStructureReader;
|
||
use App\Domain\Catalog\EditorialProfileReader;
|
||
use App\Domain\Catalog\LicenceScope;
|
||
use App\Domain\Catalog\LicenseArticle;
|
||
use App\Domain\Catalog\Offer;
|
||
use App\Domain\Catalog\OfferService;
|
||
use App\Domain\Catalog\OfferVersion;
|
||
use App\Domain\Catalog\StructureNode;
|
||
use App\Filament\Support\CatalogLabels;
|
||
|
||
/**
|
||
* Kaufbare Lizenzoptionen einer Produktseite aus dem veröffentlichten Katalog: aktive Angebotsfassungen mit Preis,
|
||
* Eignung, Laufzeit, Umfang und Nutzungsrechten. Die Seite selbst (Titel, Beschreibung, Struktur) kommt aus der
|
||
* zur Anzeige freigegebenen Komposition; hier entsteht kein zweiter Text.
|
||
*/
|
||
class ProductOptions
|
||
{
|
||
public function __construct(private readonly OfferService $offers) {}
|
||
|
||
/**
|
||
* Optionen der Produktseite eines Lizenzartikels: Angebote, deren einzige Position dieser Artikel ist.
|
||
*
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function forArticle(LicenseArticle $article): array
|
||
{
|
||
return $this->optionsFor('a:'.$article->id);
|
||
}
|
||
|
||
/**
|
||
* Alle Optionen, die diesen Artikel als einzige Position verkaufen oder verkauften, auch beendete und solche ohne
|
||
* Preis (Verwaltung). Die öffentliche Seite zeigt davon nur die kaufbaren (`forArticle`).
|
||
*
|
||
* @return list<Offer>
|
||
*/
|
||
public function offersOfArticle(LicenseArticle $article): array
|
||
{
|
||
return Offer::query()
|
||
->whereHas('versions.lines', fn ($query) => $query->where('license_article_id', $article->id))
|
||
->orderBy('id')
|
||
->get()
|
||
->filter(fn (Offer $offer): bool => $this->groupKey($offer) === 'a:'.$article->id)
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
/**
|
||
* Kaufbare Optionen der Produktseite eines Pakets: Angebote, deren einzige Position eine Fassung dieses Pakets ist.
|
||
*
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function forBundle(Bundle $bundle): array
|
||
{
|
||
return $this->optionsFor('b:'.$bundle->id);
|
||
}
|
||
|
||
/**
|
||
* Altbestand: Angebote mit mehreren Positionen, die einer Programmseite zugeordnet wurden. Neue Angebote
|
||
* haben immer genau eine Position und damit eine Artikel- oder Paketseite.
|
||
*
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function forComposition(Composition $composition): array
|
||
{
|
||
return $this->optionsFor('c:'.$composition->id);
|
||
}
|
||
|
||
/**
|
||
* Der Artikel, den die Fassung als einzige Position verkauft, sonst null.
|
||
*/
|
||
public function articleOf(OfferVersion $version): ?LicenseArticle
|
||
{
|
||
$version->loadMissing('lines.article');
|
||
|
||
return $version->lines->count() === 1 ? $version->lines->first()->article : null;
|
||
}
|
||
|
||
/**
|
||
* Aufbau des Inhalts, den der aktuelle Stand des Artikels liefert (Rolle und Titel der Bestandteile). Ein Artikel
|
||
* mit einem einzelnen Medium als Wurzel hat keine Struktur. Dateien und Lehrtexte kommen hier nie vor.
|
||
*
|
||
* @return list<StructureNode>
|
||
*/
|
||
public function articleStructure(LicenseArticle $article): array
|
||
{
|
||
$version = $article->currentRelease?->rootCompositionVersion;
|
||
|
||
return $version === null ? [] : app(CompositionStructureReader::class)->tree($version);
|
||
}
|
||
|
||
/** Lernziel des Inhalts im aktuellen Stand des Artikels, wie es schon die öffentliche Programmseite zeigt. */
|
||
public function articleGoal(LicenseArticle $article): ?string
|
||
{
|
||
$goal = $article->currentRelease?->rootCompositionVersion?->learning_goal;
|
||
|
||
return filled($goal) ? (string) $goal : null;
|
||
}
|
||
|
||
/**
|
||
* Artikel mit kaufbarer Option, deren Inhalt diese Komposition ist: Die Programmseite verweist auf sie.
|
||
*
|
||
* @return list<LicenseArticle>
|
||
*/
|
||
public function articlesOf(Composition $composition): array
|
||
{
|
||
return LicenseArticle::query()
|
||
->whereNull('archived_at')
|
||
->whereHas('currentRelease.rootCompositionVersion', fn ($query) => $query->where('composition_id', $composition->id))
|
||
->orderBy('code')
|
||
->get()
|
||
->filter(fn (LicenseArticle $article): bool => $this->forArticle($article) !== [])
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
/**
|
||
* Das Paket, das die Fassung unmittelbar verkauft (erste Paketposition), sonst null.
|
||
*/
|
||
public function bundleOf(OfferVersion $version): ?Bundle
|
||
{
|
||
$version->loadMissing('lines.bundleVersion.bundle');
|
||
|
||
return $version->lines->first(fn ($line): bool => $line->bundleVersion !== null)?->bundleVersion?->bundle;
|
||
}
|
||
|
||
/**
|
||
* Schlüssel der Produktseite, auf der die Option angeboten wird: Artikelseite (`a:`), Paketseite (`b:`) oder, nur
|
||
* im Altbestand mit mehreren Positionen, die Programmseite (`c:`). Die Seite ergibt sich aus der Angebotsfassung
|
||
* und wird nirgends von Hand zugeordnet.
|
||
*/
|
||
public function groupKey(Offer $offer): ?string
|
||
{
|
||
$version = $this->offers->purchasableVersion($offer) ?? $offer->versions()->orderByDesc('number')->first();
|
||
|
||
return $this->keyFor($offer, $version);
|
||
}
|
||
|
||
private function keyFor(Offer $offer, ?OfferVersion $version): ?string
|
||
{
|
||
if ($version !== null && $version->lines()->count() === 1) {
|
||
$article = $this->articleOf($version);
|
||
|
||
if ($article !== null) {
|
||
return 'a:'.$article->id;
|
||
}
|
||
|
||
$bundle = $this->bundleOf($version);
|
||
|
||
if ($bundle !== null) {
|
||
return 'b:'.$bundle->id;
|
||
}
|
||
}
|
||
|
||
return $offer->composition_id !== null ? 'c:'.$offer->composition_id : null;
|
||
}
|
||
|
||
/** Adresse der Produktseite, auf der die Option angeboten wird, sonst null. */
|
||
public function pageUrl(Offer $offer): ?string
|
||
{
|
||
$key = $this->groupKey($offer);
|
||
|
||
if ($key === null) {
|
||
return null;
|
||
}
|
||
|
||
$id = (int) substr($key, 2);
|
||
|
||
return match ($key[0]) {
|
||
'a' => route('shop.article', $id),
|
||
'b' => route('public.package', $id),
|
||
default => route('public.program', $id),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* Kaufbare Optionen je Produktseite: Seitenschlüssel => Angebot und Fassung in der Reihenfolge der Angebote.
|
||
*
|
||
* @return array<string, list<array{offer: Offer, version: OfferVersion}>>
|
||
*/
|
||
private function purchasable(): array
|
||
{
|
||
$pages = [];
|
||
|
||
foreach (Offer::query()->orderBy('id')->get() as $offer) {
|
||
$version = $this->offers->purchasableVersion($offer);
|
||
$key = $version === null ? null : $this->keyFor($offer, $version);
|
||
|
||
if ($key !== null) {
|
||
$pages[$key][] = ['offer' => $offer, 'version' => $version];
|
||
}
|
||
}
|
||
|
||
return $pages;
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
private function optionsFor(string $key): array
|
||
{
|
||
return array_map(fn (array $row): array => $this->describe($row['offer'], $row['version']), $this->purchasable()[$key] ?? []);
|
||
}
|
||
|
||
/**
|
||
* Günstigster Preis und Zahl der Optionen je Seite einer Art ('a', 'b' oder 'c'): Kennung => Zusammenfassung.
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
private function summariesOf(string $kind): array
|
||
{
|
||
$summary = [];
|
||
|
||
foreach ($this->purchasable() as $key => $rows) {
|
||
if ($key[0] !== $kind) {
|
||
continue;
|
||
}
|
||
|
||
$summary[(int) substr($key, 2)] = [
|
||
'from_cents' => min(array_map(fn (array $row): int => (int) $row['version']->price_cents, $rows)),
|
||
'count' => count($rows),
|
||
];
|
||
}
|
||
|
||
return $summary;
|
||
}
|
||
|
||
/**
|
||
* Artikel mit kaufbarer Option: Artikelkennung => günstigster Preis und Zahl der Optionen.
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
public function articleSummaries(): array
|
||
{
|
||
return $this->summariesOf('a');
|
||
}
|
||
|
||
/**
|
||
* Pakete mit kaufbarer Option: Paketkennung => günstigster Preis und Zahl der Optionen.
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
public function bundleSummaries(): array
|
||
{
|
||
return $this->summariesOf('b');
|
||
}
|
||
|
||
/**
|
||
* Altbestand: Programmseiten mit kaufbaren Optionen (Angebote mit mehreren Positionen).
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
public function summaries(): array
|
||
{
|
||
return $this->summariesOf('c');
|
||
}
|
||
|
||
/**
|
||
* Der tatsächliche Umfang einer Paketseite: die Artikel aller Optionen ohne Doppelte, in der Reihenfolge der
|
||
* Optionen, mit Name und Kurzbeschreibung aus der Redaktion.
|
||
*
|
||
* @param list<array<string, mixed>> $options
|
||
* @return list<array{title: string, teaser: string}>
|
||
*/
|
||
public function bundleContents(array $options): array
|
||
{
|
||
$contents = [];
|
||
|
||
foreach ($options as $option) {
|
||
$option['version']->loadMissing('resolvedScopes.article');
|
||
|
||
foreach ($option['version']->resolvedScopes as $item) {
|
||
$article = $item->article;
|
||
|
||
if ($article === null || isset($contents[$article->id])) {
|
||
continue;
|
||
}
|
||
|
||
$profile = app(EditorialProfileReader::class)->read($article, 'detail');
|
||
$contents[$article->id] = [
|
||
'title' => $this->articleTitle($article),
|
||
'teaser' => trim((string) ($profile->value('teaser') ?? '')),
|
||
];
|
||
}
|
||
}
|
||
|
||
return array_values($contents);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function describe(Offer $offer, OfferVersion $version): array
|
||
{
|
||
$version->loadMissing('resolvedScopes.article', 'resolvedScopes.rights');
|
||
$scope = [];
|
||
|
||
foreach ($version->resolvedScopes as $item) {
|
||
$faehigkeiten = CatalogLabels::faehigkeiten($item->rights?->capabilities);
|
||
$scope[] = [
|
||
'article' => $this->articleTitle($item->article),
|
||
'purpose' => CatalogLabels::zweck($item->rights?->purpose),
|
||
'rights' => $faehigkeiten,
|
||
];
|
||
}
|
||
|
||
return [
|
||
'offer' => $offer,
|
||
'version' => $version,
|
||
'name' => $offer->optionName(),
|
||
'price_cents' => (int) $version->price_cents,
|
||
'price' => Money::format((int) $version->price_cents),
|
||
'for' => $version->holder_kind === 'organisation' ? 'Organisationen und Vereine' : 'Privatpersonen',
|
||
'holder_kind' => $version->holder_kind,
|
||
'term' => self::termLabel($version),
|
||
'licence' => LicenceScope::label($version->licence_scope, $version->trainer_count),
|
||
'licence_scope' => $version->licence_scope,
|
||
'trainer_count' => $version->trainer_count,
|
||
'licence_note' => LicenceScope::explanation($version->licence_scope, $version->trainer_count),
|
||
'scope' => $scope,
|
||
];
|
||
}
|
||
|
||
public static function termLabel(OfferVersion $version): string
|
||
{
|
||
return self::termRuleLabel($version->term_rule);
|
||
}
|
||
|
||
/** Laufzeitregel aus der Angebotsfassung oder aus den Bedingungen eines Erwerbs (gleiche Schlüssel). */
|
||
public static function termRuleLabel(?string $rule): string
|
||
{
|
||
return match ((string) $rule) {
|
||
'dauerhaft' => 'Dauerhaft nutzbar',
|
||
'befristet' => 'Befristet',
|
||
'solange_angebot_aktiv' => 'Solange das Angebot aktiv ist',
|
||
'periodisch' => 'Laufend, periodisch',
|
||
default => (string) $rule,
|
||
};
|
||
}
|
||
|
||
public function articleTitle(?LicenseArticle $article): string
|
||
{
|
||
if ($article === null) {
|
||
return '–';
|
||
}
|
||
|
||
return $this->articleName($article) ?: (string) $article->code;
|
||
}
|
||
|
||
/**
|
||
* Name wie in der Redaktion: eigener Name, sonst der Name des Inhalts (Anzeigename, Arbeitstitel oder
|
||
* Bezeichnung), sonst der eigene Arbeitstitel; leer, wenn nichts davon vorhanden ist.
|
||
*/
|
||
public function articleName(LicenseArticle $article): string
|
||
{
|
||
$profile = app(EditorialProfileReader::class)->read($article, 'detail');
|
||
$source = $profile->source;
|
||
$candidates = [
|
||
$profile->value('display_title'),
|
||
$article->working_title,
|
||
$source?->getAttribute('working_title'),
|
||
$source?->getAttribute('designation'),
|
||
];
|
||
|
||
foreach ($candidates as $candidate) {
|
||
if (filled($candidate)) {
|
||
return trim((string) $candidate);
|
||
}
|
||
}
|
||
|
||
return '';
|
||
}
|
||
}
|