Die Auswahl nennt je Eintrag, ob der Shop die Seite zeigen kann (Programm mit Anzeigefreigabe), und die Fehlermeldung zur eigenen Paketseite nennt den Weg über Pakete oder ein Programm. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
338 lines
17 KiB
PHP
338 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\Bundle;
|
|
use App\Domain\Catalog\BundleService;
|
|
use App\Domain\Catalog\BundleStatus;
|
|
use App\Domain\Catalog\BundleVersion;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
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\ReleaseStatus;
|
|
use App\Domain\Catalog\StatusValue;
|
|
use App\Domain\Cms\Catalog\PublicCatalogReader;
|
|
use App\Domain\Commerce\Money;
|
|
use App\Domain\Commerce\ProductOptions;
|
|
use App\Filament\Resources\LicenseArticles\LicenseArticleResource;
|
|
use App\Filament\Resources\Offers\OfferResource;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Voraussetzungen bis zur Veröffentlichung für Lizenzartikel, Paket und Angebot in einer gemeinsamen Form
|
|
* (Verwaltungsstandard 14, Abschnitt 9: eine Komponente für gleiche Aufgaben). Die Klasse liest nur; sie ändert keine
|
|
* fachliche Regel. Jeder Schritt nennt, was fehlt, und bietet den Weg dorthin an.
|
|
*
|
|
* Schritt: ['titel', 'text', 'stand' => erledigt|offen|optional, 'aktion' => ['label', 'wire'|'href']|null]
|
|
* Ergebnis: ['schritte', 'bereit' (alle Pflichtschritte erledigt), 'naechster' (Text des ersten offenen Pflichtschritts)].
|
|
*/
|
|
class PublicationReadiness
|
|
{
|
|
public function __construct(
|
|
private readonly BundleService $bundles,
|
|
private readonly OfferService $offers,
|
|
private readonly PublicCatalogReader $catalog,
|
|
) {}
|
|
|
|
/** Ein Artikel ist verwendbar, wenn sein aktueller Stand veröffentlicht ist. */
|
|
public function articleUsable(LicenseArticle $article): bool
|
|
{
|
|
$release = $article->currentRelease;
|
|
|
|
return $release instanceof ArticleRelease && StatusValue::of($release->status) === ReleaseStatus::Veroeffentlicht->value;
|
|
}
|
|
|
|
/** Anzeige in Auswahllisten: Name, Kennung und Stand statt der bloßen Kennung. */
|
|
public function articleLabel(LicenseArticle $article): string
|
|
{
|
|
$title = $this->articleName($article);
|
|
$name = $title !== '' ? $title.' ('.$article->code.')' : (string) $article->code;
|
|
$release = $article->currentRelease;
|
|
|
|
if ($this->articleUsable($article)) {
|
|
return $name.' · Stand '.$release->number;
|
|
}
|
|
|
|
return $name.' · '.($release === null ? 'noch ohne Stand, nicht verwendbar' : 'Stand gesperrt, nicht verwendbar');
|
|
}
|
|
|
|
/** Name wie in der Redaktion: eigener Name oder Name des Inhalts, sonst der Arbeitstitel; leer ohne beides. */
|
|
public function articleName(LicenseArticle $article): string
|
|
{
|
|
return app(ProductOptions::class)->articleName($article);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public function articleOptions(): array
|
|
{
|
|
$options = [];
|
|
|
|
foreach (LicenseArticle::query()->with('currentRelease')->orderBy('code')->get() as $article) {
|
|
$options[$article->id] = $this->articleLabel($article);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Veröffentlichte Paketfassungen mit ihren Artikeln, damit die Auswahl zeigt, was verkauft wird.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function bundleVersionOptions(): array
|
|
{
|
|
$options = [];
|
|
|
|
foreach (BundleVersion::query()->where('status', BundleStatus::Veroeffentlicht)->with('bundle')->orderBy('id')->get() as $version) {
|
|
try {
|
|
$codes = LicenseArticle::query()->whereIn('id', $this->bundles->articleIds($version))->orderBy('code')->pluck('code')->all();
|
|
} catch (DomainRuleException) {
|
|
$codes = [];
|
|
}
|
|
|
|
$options[$version->id] = $version->bundle->name.' · Fassung '.$version->number.' ('.($codes === [] ? 'ohne Artikel' : implode(', ', $codes)).')';
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Artikel einer Auswahl, die keinen verwendbaren Stand haben.
|
|
*
|
|
* @param list<int> $articleIds
|
|
* @return list<LicenseArticle>
|
|
*/
|
|
public function unusableArticles(array $articleIds): array
|
|
{
|
|
return LicenseArticle::query()->with('currentRelease')->whereIn('id', array_unique($articleIds))->orderBy('code')->get()
|
|
->reject(fn (LicenseArticle $article): bool => $this->articleUsable($article))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @param list<LicenseArticle> $articles
|
|
*/
|
|
public function unusableMessage(array $articles, string $zweck): string
|
|
{
|
|
$namen = implode(', ', array_map(fn (LicenseArticle $article): string => '„'.($this->articleName($article) ?: $article->code).'“ ('.$article->code.')', $articles));
|
|
|
|
return (count($articles) === 1 ? 'Der Lizenzartikel ' : 'Die Lizenzartikel ').$namen
|
|
.(count($articles) === 1 ? ' hat' : ' haben')
|
|
.' noch keinen veröffentlichten Stand und vergibt'.(count($articles) === 1 ? '' : ' keine').' keine Inhalte. '
|
|
.'Lege zuerst am Artikel mit „Inhalt festlegen“ den Inhalt fest, bevor du '.$zweck.'.';
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function forArticle(LicenseArticle $article): array
|
|
{
|
|
$article->loadMissing('currentRelease');
|
|
$usable = $this->articleUsable($article);
|
|
$release = $article->currentRelease;
|
|
$inUse = $this->articleInUse($article);
|
|
|
|
return $this->result([
|
|
[
|
|
'titel' => 'Inhalt festlegen',
|
|
'text' => $usable
|
|
? 'Stand '.$release->number.' ist veröffentlicht und bestimmt, welche Inhalte der Artikel lizenziert.'
|
|
: 'Der Artikel lizenziert noch nichts. Wähle den freigegebenen Inhalt (Medium, Modul oder Programm), den er umfassen soll.',
|
|
'stand' => $usable ? 'erledigt' : 'offen',
|
|
'aktion' => $usable ? null : ['label' => 'Inhalt festlegen', 'wire' => "mountAction('release')"],
|
|
],
|
|
[
|
|
'titel' => 'Name und Beschreibung',
|
|
'text' => $this->articleName($article) !== ''
|
|
? 'Der Name „'.$this->articleName($article).'“ erscheint in Paketen, Angeboten, im Shop und in der Bibliothek.'
|
|
: 'Ohne Name erscheint die Kennung „'.$article->code.'“. Name, Teaser und Beschreibung pflegst du in der Redaktion.',
|
|
'stand' => $this->articleName($article) !== '' ? 'erledigt' : 'optional',
|
|
'aktion' => $this->articleName($article) !== '' ? null : ['label' => 'Redaktion bearbeiten', 'wire' => "openRedaktion('texte')"],
|
|
],
|
|
[
|
|
'titel' => 'In einem Paket oder Angebot verwenden',
|
|
'text' => $inUse
|
|
? 'Der Artikel ist in einem Paket oder Angebot enthalten.'
|
|
: 'Verkauft wird der Artikel erst über ein Angebot, allein oder in einem Paket.',
|
|
'stand' => $inUse ? 'erledigt' : 'offen',
|
|
'aktion' => $inUse ? null : ['label' => 'Angebote öffnen', 'href' => OfferResource::getUrl('index')],
|
|
'pflicht' => false,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function forBundle(Bundle $bundle): array
|
|
{
|
|
$published = $bundle->versions()->where('status', BundleStatus::Veroeffentlicht)->orderByDesc('number')->first();
|
|
$missing = [];
|
|
|
|
if ($published instanceof BundleVersion) {
|
|
try {
|
|
$missing = $this->unusableArticles($this->bundles->articleIds($published));
|
|
} catch (DomainRuleException) {
|
|
$missing = [];
|
|
}
|
|
}
|
|
|
|
$offered = $published instanceof BundleVersion && $this->bundleOffered($bundle);
|
|
|
|
return $this->result([
|
|
[
|
|
'titel' => 'Bestandteile festlegen',
|
|
'text' => $published instanceof BundleVersion
|
|
? 'Fassung '.$published->number.' ist veröffentlicht und unveränderlich.'
|
|
: 'Wähle die Lizenzartikel und Pakete, die zusammen verkauft werden, und veröffentliche die Fassung.',
|
|
'stand' => $published instanceof BundleVersion ? 'erledigt' : 'offen',
|
|
'aktion' => $published instanceof BundleVersion ? null : ['label' => 'Bestandteile festlegen', 'wire' => "mountAction('publish')"],
|
|
],
|
|
[
|
|
'titel' => 'Jeder enthaltene Artikel hat einen Inhalt',
|
|
'text' => ! $published instanceof BundleVersion
|
|
? 'Wird geprüft, sobald die Bestandteile feststehen.'
|
|
: ($missing === []
|
|
? 'Alle enthaltenen Artikel haben einen veröffentlichten Stand.'
|
|
: 'Ohne Stand und damit ohne Inhalt: '.implode(', ', array_map(fn (LicenseArticle $article): string => (string) $article->code, $missing)).'. Ein Angebot lässt sich erst aktivieren, wenn jeder Artikel einen Stand hat.'),
|
|
'stand' => $published instanceof BundleVersion && $missing === [] ? 'erledigt' : 'offen',
|
|
'aktion' => $missing === [] ? null : ['label' => 'Artikel „'.$missing[0]->code.'“ öffnen', 'href' => LicenseArticleResource::getUrl('view', ['record' => $missing[0]])],
|
|
],
|
|
[
|
|
'titel' => 'Angebot mit Preis und Rechten',
|
|
'text' => $offered
|
|
? 'Ein aktives Angebot verkauft dieses Paket.'
|
|
: 'Verkauft wird das Paket erst über ein Angebot. Dort wählst du die Paketfassung, die Rechte und den Preis.',
|
|
'stand' => $offered ? 'erledigt' : 'offen',
|
|
'aktion' => $offered ? null : ['label' => 'Angebote öffnen', 'href' => OfferResource::getUrl('index')],
|
|
'pflicht' => false,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function forOffer(Offer $offer): array
|
|
{
|
|
$active = $offer->versions()->where('status', 'aktiv')->orderByDesc('number')->first();
|
|
$hasActive = $active instanceof OfferVersion;
|
|
$articleIds = $hasActive ? $active->resolvedScopes()->pluck('license_article_id')->map(fn ($id): int => (int) $id)->all() : [];
|
|
$missing = $this->unusableArticles($articleIds);
|
|
$buyable = $hasActive && $this->offers->purchasableVersion($offer) !== null;
|
|
$page = $offer->composition;
|
|
$own = (bool) $offer->standalone_bundle;
|
|
$shown = $own || ($page !== null && $this->catalog->shownVersion($page) !== null);
|
|
$pageName = $page !== null ? (string) ($page->display_title ?: $page->designation) : '';
|
|
$activate = ['label' => $hasActive ? 'Geänderte Fassung aktivieren' : 'Angebot konfigurieren', 'wire' => "mountAction('activate')"];
|
|
|
|
return $this->result([
|
|
[
|
|
'titel' => 'Artikel oder Paket, Rechte, Preis und Vergabe festlegen',
|
|
'text' => $hasActive
|
|
? 'Fassung '.$active->number.' ist aktiv.'
|
|
: 'Wähle, was verkauft wird (Lizenzartikel oder Paketfassung), welche Nutzungsrechte dazugehören, für wen es gilt und was es kostet. Erst „Fassung aktivieren“ macht das Angebot gültig.',
|
|
'stand' => $hasActive ? 'erledigt' : 'offen',
|
|
'aktion' => $hasActive ? null : $activate,
|
|
],
|
|
[
|
|
'titel' => 'Inhalt vorhanden',
|
|
'text' => ! $hasActive
|
|
? 'Wird geprüft, sobald die Fassung aktiv ist.'
|
|
: ($missing === []
|
|
? 'Jeder enthaltene Lizenzartikel hat einen veröffentlichten Stand.'
|
|
: 'Ohne Stand und damit ohne Inhalt: '.implode(', ', array_map(fn (LicenseArticle $article): string => (string) $article->code, $missing)).'. Käufer würden nichts erhalten.'),
|
|
'stand' => $hasActive && $missing === [] ? 'erledigt' : 'offen',
|
|
'aktion' => $missing === [] ? null : ['label' => 'Artikel „'.$missing[0]->code.'“ öffnen', 'href' => LicenseArticleResource::getUrl('view', ['record' => $missing[0]])],
|
|
],
|
|
[
|
|
'titel' => 'Als Kauf mit Preis angeboten',
|
|
'text' => $buyable
|
|
? 'Die aktive Fassung ist ein Kauf zu '.Money::format((int) $active->price_cents).'.'
|
|
: ($hasActive ? 'Die aktive Fassung ist kein Kauf mit Preis (Vergabe: '.CatalogLabels::vergabe($active).'). Im Shop erscheinen nur Käufe.' : 'Wird mit der Fassung festgelegt.'),
|
|
'stand' => $buyable ? 'erledigt' : 'offen',
|
|
'aktion' => $buyable ? null : ($hasActive ? $activate : null),
|
|
],
|
|
[
|
|
'titel' => 'Nutzerkreis festlegen',
|
|
'text' => ! $hasActive
|
|
? 'Wird mit der Fassung festgelegt: Einzellizenz (ein Trainer) oder Mehrfachlizenz (Zahl der Trainer).'
|
|
: ($active->licence_scope !== null
|
|
? LicenceScope::label($active->licence_scope, $active->trainer_count).'. Der Käufer sieht das auf der Produktseite und in der Bestellung.'
|
|
: 'Diese Fassung hat keinen festgelegten Nutzerkreis. Käufer sehen „Nutzerkreis nicht festgelegt“. Lege ihn mit „Geänderte Fassung aktivieren“ fest.'),
|
|
'stand' => $hasActive && $active->licence_scope !== null ? 'erledigt' : 'offen',
|
|
'aktion' => $hasActive && $active->licence_scope === null ? $activate : null,
|
|
'pflicht' => false,
|
|
],
|
|
[
|
|
'titel' => 'Produktseite zuordnen',
|
|
'text' => $own
|
|
? 'Eigene Paketseite: Sie zeigt den Umfang des Pakets. Optionen desselben Pakets sind Alternativen im Warenkorb.'
|
|
: ($page !== null
|
|
? 'Zugeordnet: „'.$pageName.'“. Optionen derselben Produktseite sind Alternativen im Warenkorb.'
|
|
: 'Der Shop zeigt ein Angebot auf der Seite eines Programms. Wähle in der Maske die Produktseite. Ein Paket (Katalog → Pakete), das für sich verkauft wird, erhält dort „Eigene Paketseite“ und zeigt seinen Umfang; ein Lizenzartikel braucht dagegen ein Programm, das unter „Programme“ angelegt und zur Anzeige freigegeben ist. Ein Modul kann keine Produktseite sein.'),
|
|
'stand' => $own || $page !== null ? 'erledigt' : 'offen',
|
|
'aktion' => $own || $page !== null ? null : $activate,
|
|
],
|
|
[
|
|
'titel' => 'Produktseite öffentlich',
|
|
'text' => ! $own && $page === null
|
|
? 'Wird geprüft, sobald eine Produktseite zugeordnet ist.'
|
|
: ($own
|
|
? 'Die eigene Paketseite ist öffentlich, sobald die aktive Fassung ein Kauf mit Preis ist.'
|
|
: ($shown
|
|
? 'Das Programm ist zur Anzeige freigegeben. Das Angebot erscheint in der Shop-Suche und auf der Produktseite.'
|
|
: 'Das Programm „'.$pageName.'“ ist nicht zur Anzeige freigegeben oder kein Programm. Das Angebot ist dort nicht auffindbar.')),
|
|
'stand' => $shown ? 'erledigt' : 'offen',
|
|
'aktion' => $page === null || $shown ? null : ['label' => 'Programm öffnen', 'href' => EditorialCatalog::editUrl($page)],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string, mixed>> $schritte
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function result(array $schritte): array
|
|
{
|
|
$schritte = array_map(fn (array $schritt): array => ['pflicht' => true, 'aktion' => null, ...$schritt], $schritte);
|
|
$naechster = null;
|
|
|
|
foreach ($schritte as $schritt) {
|
|
if ($schritt['pflicht'] && $schritt['stand'] === 'offen') {
|
|
$naechster = $schritt;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'schritte' => $schritte,
|
|
'bereit' => $naechster === null,
|
|
'naechster' => $naechster,
|
|
];
|
|
}
|
|
|
|
private function articleInUse(LicenseArticle $article): bool
|
|
{
|
|
return DB::table('offer_lines')->where('license_article_id', $article->id)->exists()
|
|
|| DB::table('bundle_items')->where('license_article_id', $article->id)->exists();
|
|
}
|
|
|
|
private function bundleOffered(Bundle $bundle): bool
|
|
{
|
|
return DB::table('offer_lines')
|
|
->join('offer_versions', 'offer_versions.id', '=', 'offer_lines.offer_version_id')
|
|
->join('bundle_versions', 'bundle_versions.id', '=', 'offer_lines.bundle_version_id')
|
|
->where('bundle_versions.bundle_id', $bundle->id)
|
|
->where('offer_versions.status', 'aktiv')
|
|
->exists();
|
|
}
|
|
}
|