Ein eigenständig vermarktetes Paket (offers.standalone_bundle) erhält /pakete/{id}
mit seinem tatsächlichen Umfang, Shop-Kachel und Suche. Die Angebotsmaske kennt
Nutzerkreis und Anzahl Trainer. Zwei Bestandstests (CartTest, Vergleich der
Rechte und Erwerbsarten) legen die Vergleichsangebote jetzt als Einzellizenz an,
weil der Nutzerkreis zum identischen Umfang gehört; ihre Aussagen bleiben.
Eine Rechnungs- oder Stripe-Bestellung lässt sich nicht mit der Testzahlung bestätigen.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
271 lines
9.1 KiB
PHP
271 lines
9.1 KiB
PHP
<?php
|
||
|
||
namespace App\Domain\Commerce;
|
||
|
||
use App\Domain\Catalog\Bundle;
|
||
use App\Domain\Catalog\Composition;
|
||
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\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) {}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function forComposition(Composition $composition): array
|
||
{
|
||
$options = [];
|
||
|
||
foreach (Offer::query()->where('composition_id', $composition->id)->orderBy('id')->get() as $offer) {
|
||
$version = $this->offers->purchasableVersion($offer);
|
||
|
||
if ($version !== null) {
|
||
$options[] = $this->describe($offer, $version);
|
||
}
|
||
}
|
||
|
||
return $options;
|
||
}
|
||
|
||
/**
|
||
* Kaufbare Optionen der eigenen Produktseite eines Pakets: Angebote mit eigener Paketseite, deren aktive Fassung
|
||
* dieses Paket verkauft.
|
||
*
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function forBundle(Bundle $bundle): array
|
||
{
|
||
$options = [];
|
||
|
||
foreach (Offer::query()->where('standalone_bundle', true)->orderBy('id')->get() as $offer) {
|
||
$version = $this->offers->purchasableVersion($offer);
|
||
|
||
if ($version !== null && $this->bundleOf($version)?->id === $bundle->id) {
|
||
$options[] = $this->describe($offer, $version);
|
||
}
|
||
}
|
||
|
||
return $options;
|
||
}
|
||
|
||
/**
|
||
* 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;
|
||
}
|
||
|
||
/** Gruppenschlüssel der Alternativen: Produktseite eines Programms oder eigene Paketseite. */
|
||
public function groupKey(Offer $offer): ?string
|
||
{
|
||
if ($offer->composition_id !== null) {
|
||
return 'c:'.$offer->composition_id;
|
||
}
|
||
|
||
if ($offer->standalone_bundle) {
|
||
$version = $this->offers->purchasableVersion($offer) ?? $offer->versions()->orderByDesc('number')->first();
|
||
$bundle = $version === null ? null : $this->bundleOf($version);
|
||
|
||
return $bundle === null ? null : 'b:'.$bundle->id;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/** Adresse der Produktseite, auf der die Option angeboten wird, sonst null. */
|
||
public function pageUrl(Offer $offer): ?string
|
||
{
|
||
if ($offer->composition_id !== null) {
|
||
return route('public.program', $offer->composition_id);
|
||
}
|
||
|
||
$key = $this->groupKey($offer);
|
||
|
||
return $key !== null && str_starts_with($key, 'b:') ? route('public.package', (int) substr($key, 2)) : null;
|
||
}
|
||
|
||
/**
|
||
* Pakete mit eigener Produktseite und kaufbarer Option: Paketkennung => günstigster Preis und Zahl der Optionen.
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
public function bundleSummaries(): array
|
||
{
|
||
$summary = [];
|
||
|
||
foreach (Offer::query()->where('standalone_bundle', true)->orderBy('id')->get() as $offer) {
|
||
$version = $this->offers->purchasableVersion($offer);
|
||
$bundle = $version === null ? null : $this->bundleOf($version);
|
||
|
||
if ($bundle === null) {
|
||
continue;
|
||
}
|
||
|
||
$current = $summary[$bundle->id] ?? ['from_cents' => PHP_INT_MAX, 'count' => 0];
|
||
$summary[$bundle->id] = [
|
||
'from_cents' => min($current['from_cents'], (int) $version->price_cents),
|
||
'count' => $current['count'] + 1,
|
||
];
|
||
}
|
||
|
||
return $summary;
|
||
}
|
||
|
||
/**
|
||
* 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);
|
||
}
|
||
|
||
/**
|
||
* Zeilen für Shop-Suche und Kacheln: je Komposition die günstigste Option.
|
||
*
|
||
* @return array<int, array{from_cents: int, count: int}>
|
||
*/
|
||
public function summaries(): array
|
||
{
|
||
$summary = [];
|
||
|
||
foreach (Offer::query()->whereNotNull('composition_id')->orderBy('id')->get() as $offer) {
|
||
$version = $this->offers->purchasableVersion($offer);
|
||
|
||
if ($version === null) {
|
||
continue;
|
||
}
|
||
|
||
$current = $summary[$offer->composition_id] ?? ['from_cents' => PHP_INT_MAX, 'count' => 0];
|
||
$summary[$offer->composition_id] = [
|
||
'from_cents' => min($current['from_cents'], (int) $version->price_cents),
|
||
'count' => $current['count'] + 1,
|
||
];
|
||
}
|
||
|
||
return $summary;
|
||
}
|
||
|
||
/**
|
||
* @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 '';
|
||
}
|
||
}
|