diff --git a/app/Domain/Catalog/OfferService.php b/app/Domain/Catalog/OfferService.php index ecc3b57..8396eb6 100644 --- a/app/Domain/Catalog/OfferService.php +++ b/app/Domain/Catalog/OfferService.php @@ -163,6 +163,64 @@ public function activate(Offer $offer, array $lines, array $attributes = []): Of }); } + /** + * Bietet einen Lizenzartikel als Kauf an: legt die Option (Angebot) an und aktiviert ihre erste Fassung in einem + * Schritt. Besteht eine Prüfung nicht (Stand, Preis, Rechte), bleibt kein leeres Angebot zurück. + * + * @param array $attributes holder_kind, licence_scope, trainer_count, price_cents + */ + public function offerArticle(LicenseArticle $article, RightsProfileVersion $rights, array $attributes, string $name, ?string $label = null): OfferVersion + { + return DB::transaction(function () use ($article, $rights, $attributes, $name, $label): OfferVersion { + $offer = $this->create($name); + $this->setOptionLabel($offer, $label); + + return $this->activate($offer, [['target' => $article, 'rights' => $rights]], [...$attributes, 'grant_kind' => 'kauf']); + }); + } + + /** + * Ändert eine Option eines Artikels: Die nächste Fassung wird aktiv, die bisherige bleibt gespeichert. Bedingungen, + * die die Maske nicht zeigt (Laufzeit, Steuerklasse), bleiben die der letzten Fassung. Die Option muss den Artikel + * als einzige Position verkaufen. + * + * @param array $attributes holder_kind, licence_scope, trainer_count, price_cents + */ + public function reviseArticleOption(Offer $offer, LicenseArticle $article, RightsProfileVersion $rights, array $attributes, ?string $label = null): OfferVersion + { + return DB::transaction(function () use ($offer, $article, $rights, $attributes, $label): OfferVersion { + $latest = $offer->versions()->with('lines')->orderByDesc('number')->first(); + + if ($latest === null || $latest->lines->count() !== 1 || (int) $latest->lines->first()->license_article_id !== $article->id) { + throw new DomainRuleException('Diese Option gehört nicht zu diesem Lizenzartikel.'); + } + + $this->setOptionLabel($offer, $label); + + return $this->activate($offer, [['target' => $article, 'rights' => $rights]], [ + 'term_rule' => $latest->term_rule, + 'tax_class' => $latest->tax_class, + 'dynamic' => (bool) $latest->dynamic, + ...$attributes, + 'grant_kind' => 'kauf', + ]); + }); + } + + /** + * Vorschlag für die Bezeichnung einer Option, wenn keine eingegeben wird: Nutzerkreis und Eignung. + */ + public static function defaultOptionLabel(?string $holderKind, ?string $licenceScope, mixed $trainers): string + { + $scope = match ($licenceScope) { + LicenceScope::MULTI => 'Mehrfachlizenz ('.(int) $trainers.' Trainer)', + LicenceScope::SINGLE => 'Einzellizenz', + default => 'Lizenz', + }; + + return $scope.' für '.($holderKind === 'organisation' ? 'Organisationen und Vereine' : 'Privatpersonen'); + } + /** * Zieht die Option aus dem Verkauf zurück: Die aktive Fassung wird beendet und bleibt gespeichert. Bereits * begründete Berechtigungen und Bestellungen ändern sich nicht; offene Warenkörbe zeigen die Position als diff --git a/app/Domain/Commerce/ProductOptions.php b/app/Domain/Commerce/ProductOptions.php index 5cf90d0..b59ebe0 100644 --- a/app/Domain/Commerce/ProductOptions.php +++ b/app/Domain/Commerce/ProductOptions.php @@ -33,6 +33,23 @@ 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 + */ + 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. * diff --git a/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php b/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php index 04dbe8a..6983578 100644 --- a/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php +++ b/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php @@ -13,20 +13,32 @@ use App\Domain\Catalog\IdentityDimension; use App\Domain\Catalog\IdentityDimensionValue; use App\Domain\Catalog\IdentityProfile; +use App\Domain\Catalog\LicenceScope; use App\Domain\Catalog\LicenseArticle; +use App\Domain\Catalog\Offer; +use App\Domain\Catalog\OfferService; +use App\Domain\Catalog\OfferStatus; +use App\Domain\Catalog\RightsProfileVersion; +use App\Domain\Catalog\StatusValue; +use App\Domain\Commerce\Money; +use App\Domain\Commerce\ProductOptions; use App\Filament\Resources\ContentElements\ContentElementResource; use App\Filament\Resources\LicenseArticles\LicenseArticleResource; +use App\Filament\Resources\Offers\OfferResource; use App\Filament\Support\AssignsPillars; +use App\Filament\Support\CatalogLabels; use App\Filament\Support\CatalogOptions; use App\Filament\Support\EditorialCatalog; use App\Filament\Support\ManagesEditorialSurface; use App\Filament\Support\ManagesRichFields; +use App\Filament\Support\PublicationReadiness; use App\Filament\Support\ReportsDomainRules; use Filament\Actions\Action; use Filament\Forms\Components\Select; use Filament\Forms\Components\Textarea; use Filament\Forms\Components\TextInput; use Filament\Resources\Pages\ViewRecord; +use Filament\Schemas\Components\Utilities\Get; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Database\Eloquent\Model; @@ -409,6 +421,201 @@ public function withdrawAction(): Action }); } + /** + * Die Optionen dieses Artikels im Shop: Bezeichnung, Preis, Rechte, Nutzerkreis und Zustand je Option. Gelesen wird + * die aktive, sonst die letzte Fassung. + * + * @return list> + */ + public function optionRows(): array + { + /** @var LicenseArticle $article */ + $article = $this->getRecord(); + $options = app(ProductOptions::class); + $offers = app(OfferService::class); + $rows = []; + + foreach ($options->offersOfArticle($article) as $offer) { + $version = $offers->purchasableVersion($offer) + ?? $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first() + ?? $offer->versions()->orderByDesc('number')->first(); + + if ($version === null) { + continue; + } + + $version->loadMissing('lines.rights.profile'); + $active = StatusValue::of($version->status) === OfferStatus::Aktiv->value; + $buyable = $active && $offers->purchasableVersion($offer) !== null; + $rows[] = [ + 'offer' => $offer->id, + 'name' => $offer->optionName(), + 'price' => $version->price_cents === null ? 'kein Preis' : Money::format((int) $version->price_cents), + 'licence' => LicenceScope::label($version->licence_scope, $version->trainer_count), + 'for' => $version->holder_kind === 'organisation' ? 'Organisationen und Vereine' : 'Privatpersonen', + 'rights' => (string) ($version->lines->first()?->rights?->profile?->name ?? '–'), + 'number' => $version->number, + 'active' => $active, + 'buyable' => $buyable, + 'zustand' => match (true) { + $buyable => ['label' => 'Im Shop', 'ton' => 'gut'], + $active => ['label' => CatalogLabels::vergabe($version).' · nicht im Shop', 'ton' => 'neutral'], + default => ['label' => 'Nicht im Verkauf', 'ton' => 'leise'], + }, + 'page' => $buyable ? parse_url((string) $options->pageUrl($offer), PHP_URL_PATH) : null, + 'url' => OfferResource::getUrl('view', ['record' => $offer]), + ]; + } + + return $rows; + } + + /** Ein Artikel lässt sich anbieten, wenn sein aktueller Stand veröffentlicht ist. */ + public function canOffer(): bool + { + return app(PublicationReadiness::class)->articleUsable($this->getRecord()); + } + + /** + * Option hinzufügen oder ändern: Rechteprofil, Preis, Nutzerkreis und Eignung. Der Artikel steht fest, es gibt keine + * Auswahl von Artikel oder Inhalt. Mit dem Argument `offer` ändert die Aktion eine bestehende Option (nächste Fassung). + */ + public function optionAction(): Action + { + return Action::make('option') + ->modalWidth('2xl') + ->modalHeading(fn (array $arguments): string => isset($arguments['offer']) + ? 'Option „'.$this->optionOf($arguments)->optionName().'“ ändern' + : 'Option hinzufügen: so wird der Artikel verkauft') + ->modalDescription(fn (array $arguments): string => isset($arguments['offer']) + ? 'Die Maske ist mit der letzten Fassung vorbelegt. Erst „Speichern und anbieten“ schaltet die Änderung öffentlich: Die bisherige Fassung wird beendet und bleibt gespeichert, neue Käufe nutzen die neue. Bereits begründete Berechtigungen und Bestellungen ändern sich nicht. „Abbrechen“ verwirft die Eingaben.' + : 'Eine Option ist eine Lizenz mit Rechteprofil und Preis. Der Artikel kann mehrere Optionen haben, zum Beispiel für Privatpersonen und für Vereine. Sie erscheinen gemeinsam auf der Produktseite des Artikels. „Abbrechen“ verwirft die Eingaben.') + ->modalSubmitActionLabel('Speichern und anbieten') + ->fillForm(fn (array $arguments): array => $this->optionForm($arguments)) + ->schema([ + Select::make('rights_id')->label('Rechteprofil') + ->options(fn (): array => CatalogOptions::rights()) + ->required() + ->helperText('Was Käufer mit dem Artikel tun dürfen (ansehen, herunterladen, Nutzungszweck).'), + TextInput::make('price_euro')->label('Preis in Euro')->suffix('€')->required() + ->helperText('Zum Beispiel 49,00. Ein Kauf braucht einen Preis über 0,00 €.'), + Select::make('holder_kind')->label('Geeignet für')->options([ + 'person' => 'Privatpersonen', + 'organisation' => 'Organisationen und Vereine', + ])->required()->live() + ->helperText('Wähle die Eignung passend zum Rechteprofil: Organisationslizenzen brauchen institutionelle Rechte.'), + Select::make('licence_scope')->label('Nutzerkreis') + ->options(LicenceScope::options()) + ->required()->live() + ->helperText('Wie viele Trainer die Inhalte einsetzen dürfen. Der Download bleibt beim bestellenden Konto.'), + TextInput::make('trainer_count')->label('Anzahl Trainer') + ->numeric()->minValue(2)->maxValue(LicenceScope::MAX_TRAINERS)->integer() + ->required(fn (Get $get): bool => $get('licence_scope') === LicenceScope::MULTI) + ->visible(fn (Get $get): bool => $get('licence_scope') === LicenceScope::MULTI) + ->live(), + TextInput::make('option_label')->label('Bezeichnung der Option') + ->maxLength(120) + ->placeholder(fn (Get $get): string => OfferService::defaultOptionLabel($get('holder_kind'), $get('licence_scope'), $get('trainer_count'))) + ->helperText('So heißt die Option auf der Produktseite. Leer lassen übernimmt den grauen Vorschlag.'), + ]) + ->action(function (array $data, array $arguments): void { + $this->report(function () use ($data, $arguments): string { + /** @var LicenseArticle $article */ + $article = $this->getRecord(); + $rights = RightsProfileVersion::query()->findOrFail($data['rights_id']); + $price = Money::parse($data['price_euro'] ?? null); + $label = filled($data['option_label'] ?? null) + ? trim((string) $data['option_label']) + : OfferService::defaultOptionLabel($data['holder_kind'], $data['licence_scope'], $data['trainer_count'] ?? null); + $attributes = [ + 'holder_kind' => $data['holder_kind'], + 'licence_scope' => $data['licence_scope'], + 'trainer_count' => $data['trainer_count'] ?? null, + 'price_cents' => $price, + ]; + $service = app(OfferService::class); + + if (isset($arguments['offer'])) { + $offer = $this->optionOf($arguments); + $version = $service->reviseArticleOption($offer, $article, $rights, $attributes, $label); + } else { + $name = app(ProductOptions::class)->articleTitle($article).': '.$label; + $version = $service->offerArticle($article, $rights, $attributes, $name, $label); + $offer = $version->offer; + } + + $page = app(ProductOptions::class)->pageUrl($offer); + + return 'Die Option „'.$offer->optionName().'“ ist als Fassung '.$version->number.' aktiv und im Shop kaufbar' + .($page !== null ? ' unter '.parse_url($page, PHP_URL_PATH) : '').'.'; + }, 'Die Option ist aktiv.'); + $this->refreshRecord(); + }); + } + + /** Eine Option aus dem Verkauf nehmen: nur die aktive Fassung dieser Option wird beendet. */ + public function optionEndAction(): Action + { + return Action::make('optionEnd') + ->color('danger') + ->requiresConfirmation() + ->modalHeading(fn (array $arguments): string => 'Option „'.$this->optionOf($arguments)->optionName().'“ aus dem Verkauf nehmen?') + ->modalDescription('Die aktive Fassung wird beendet und bleibt gespeichert. Die Option ist danach nicht mehr kaufbar; in vorhandenen Warenkörben bleibt sie mit Hinweis sichtbar und blockiert den Abschluss, bis der Käufer sie entfernt. Bestehende Berechtigungen und Bestellungen ändern sich nicht. Andere Optionen des Artikels bleiben kaufbar.') + ->modalSubmitActionLabel('Aus dem Verkauf nehmen') + ->action(function (array $arguments): void { + $this->report(function () use ($arguments): string { + $offer = $this->optionOf($arguments); + $version = app(OfferService::class)->end($offer); + + return 'Die Option „'.$offer->optionName().'“ ist aus dem Verkauf genommen (Fassung '.$version->number.' beendet).'; + }, 'Die Option ist aus dem Verkauf genommen.'); + $this->refreshRecord(); + }); + } + + /** + * Die Option aus den Argumenten der Aktion, nur wenn sie zu diesem Artikel gehört. + * + * @param array $arguments + */ + private function optionOf(array $arguments): Offer + { + /** @var LicenseArticle $article */ + $article = $this->getRecord(); + $offer = collect(app(ProductOptions::class)->offersOfArticle($article))->first(fn (Offer $offer): bool => $offer->id === (int) ($arguments['offer'] ?? 0)); + + return $offer ?? abort(404); + } + + /** + * @param array $arguments + * @return array + */ + private function optionForm(array $arguments): array + { + if (! isset($arguments['offer'])) { + $rights = array_keys(CatalogOptions::rights()); + + return [ + 'holder_kind' => 'person', + 'licence_scope' => LicenceScope::SINGLE, + 'rights_id' => count($rights) === 1 ? $rights[0] : null, + ]; + } + + $offer = $this->optionOf($arguments); + $version = $offer->versions()->with('lines')->orderByDesc('number')->firstOrFail(); + + return [ + 'rights_id' => $version->lines->first()?->rights_profile_version_id, + 'price_euro' => $version->price_cents === null ? null : number_format($version->price_cents / 100, 2, ',', ''), + 'holder_kind' => $version->holder_kind, + 'licence_scope' => $version->licence_scope, + 'trainer_count' => $version->trainer_count, + 'option_label' => $offer->option_label, + ]; + } + private function isFirstStand(): bool { return ! $this->getRecord()->releases()->exists(); diff --git a/app/Filament/Resources/Offers/Pages/ListOffers.php b/app/Filament/Resources/Offers/Pages/ListOffers.php index b4b58ab..2cdc7b6 100644 --- a/app/Filament/Resources/Offers/Pages/ListOffers.php +++ b/app/Filament/Resources/Offers/Pages/ListOffers.php @@ -24,7 +24,7 @@ class ListOffers extends ListRecords public function getSubheading(): string|Htmlable|null { - return 'Ein Angebot verbindet Artikel oder eine Paketfassung mit einem Rechteprofil. Die aufgelösten Berechtigungen stehen auf der Angebotsseite.'; + return 'Ein Angebot ist eine Option zum Kauf: ein Artikel oder eine Paketfassung mit Rechteprofil und Preis. Optionen eines einzelnen Artikels legst du am Lizenzartikel an („Im Shop verkaufen“); hier stehen alle Angebote, auch die der Pakete.'; } protected function getHeaderActions(): array diff --git a/app/Filament/Resources/Offers/Pages/ViewOffer.php b/app/Filament/Resources/Offers/Pages/ViewOffer.php index 3dce5a4..09e7130 100644 --- a/app/Filament/Resources/Offers/Pages/ViewOffer.php +++ b/app/Filament/Resources/Offers/Pages/ViewOffer.php @@ -6,7 +6,6 @@ use App\Domain\Catalog\BundleService; use App\Domain\Catalog\BundleVersion; use App\Domain\Catalog\Composition; -use App\Domain\Catalog\CompositionVersion; use App\Domain\Catalog\DomainRuleException; use App\Domain\Catalog\LicenceScope; use App\Domain\Catalog\LicenseArticle; @@ -136,18 +135,6 @@ public function activateAction(): Action ])->required(), TextInput::make('price_euro')->label('Preis in Euro')->suffix('€') ->helperText('Nur bei einem Kauf, zum Beispiel 49,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', @@ -217,7 +204,6 @@ public function activateAction(): Action 'trainer_count' => $data['trainer_count'] ?? null, 'grant_kind' => $data['grant_kind'], 'price_cents' => $price, - 'composition_version_id' => filled($data['composition_version_id'] ?? null) ? (int) $data['composition_version_id'] : null, ]); }); diff --git a/app/Filament/Support/PublicationReadiness.php b/app/Filament/Support/PublicationReadiness.php index 3c64e09..1d784e5 100644 --- a/app/Filament/Support/PublicationReadiness.php +++ b/app/Filament/Support/PublicationReadiness.php @@ -155,12 +155,14 @@ public function forArticle(LicenseArticle $article): array 'aktion' => $this->articleName($article) !== '' ? null : ['label' => 'Redaktion bearbeiten', 'wire' => "openRedaktion('texte')"], ], [ - 'titel' => 'In einem Paket oder Angebot verwenden', + 'titel' => 'Im Shop verkaufen', 'text' => $inUse ? 'Der Artikel ist in einem Paket oder Angebot enthalten.' - : 'Verkauft wird der Artikel erst über ein Angebot, allein oder in einem Paket.', + : 'Verkauft wird der Artikel über eine Option mit Rechteprofil und Preis, allein oder in einem Paket.', 'stand' => $inUse ? 'erledigt' : 'offen', - 'aktion' => $inUse ? null : ['label' => 'Angebote öffnen', 'href' => OfferResource::getUrl('index')], + 'aktion' => $inUse ? null : ($usable + ? ['label' => 'Option hinzufügen', 'wire' => "mountAction('option')"] + : ['label' => 'Angebote öffnen', 'href' => OfferResource::getUrl('index')]), 'pflicht' => false, ], ]); diff --git a/resources/views/filament/editorial/license-desk.blade.php b/resources/views/filament/editorial/license-desk.blade.php index 6b89516..1f07bc1 100644 --- a/resources/views/filament/editorial/license-desk.blade.php +++ b/resources/views/filament/editorial/license-desk.blade.php @@ -40,6 +40,7 @@ @include('filament.verwaltung.abgang', ['offen' => $offen])
+

Präsentation des Artikels

@@ -66,6 +67,46 @@
@endif
+ +
+
+

Im Shop verkaufen

+ Jede Option ist eine Lizenz mit Rechteprofil und Preis. Alle Optionen erscheinen auf der Produktseite dieses Artikels. +
+ @if (! $this->canOffer()) +

Zuerst den Inhalt festlegen: Ohne veröffentlichten Stand lizenziert der Artikel nichts und lässt sich nicht verkaufen.

+ @else + @php($optionen = $this->optionRows()) + @if ($optionen === []) +
+ Noch keine Option +

Lege fest, mit welchem Rechteprofil und zu welchem Preis der Artikel verkauft wird. Mehrere Optionen, etwa für Privatpersonen und für Vereine, sind möglich.

+
+ @else +
    + @foreach ($optionen as $option) +
  • + {{ $option['name'] }} + {{ $option['zustand']['label'] }} + {{ $option['price'] }} · {{ $option['licence'] }} · {{ $option['for'] }} · Rechte: {{ $option['rights'] }} · Fassung {{ $option['number'] }} + + + @if ($option['active']) + + @endif + @if ($option['page']) + Produktseite ansehen + @endif + Angebot öffnen + +
  • + @endforeach +
+ @endif +
+ @endif +
+