- Regel je Angebot auf Abruf: alle Mitglieder, nur freigeschaltete Konten, wer einen Artikel besitzt - Freigaben je Angebot und Konto (offer_releases), nie geloescht, nur entzogen; Pflege im Angebot - Pruefung serverseitig in GrantIssuer::claim; Angebote bei Registrierung bleiben regelfrei - Mitgliederseite Angebote: Neu fuer dich (neue Staende besessener Artikel), Zum Uebernehmen, Shop - Migration 2026_12_01_110000, ADR-56, DEPLOYMENT.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
616 lines
34 KiB
PHP
616 lines
34 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Resources\Offers\Pages;
|
||
|
||
use App\Domain\Catalog\Bundle;
|
||
use App\Domain\Catalog\BundleService;
|
||
use App\Domain\Catalog\BundleVersion;
|
||
use App\Domain\Catalog\Composition;
|
||
use App\Domain\Catalog\DomainRuleException;
|
||
use App\Domain\Catalog\LicenceScope;
|
||
use App\Domain\Catalog\LicenseArticle;
|
||
use App\Domain\Catalog\Offer;
|
||
use App\Domain\Catalog\OfferRelease;
|
||
use App\Domain\Catalog\OfferService;
|
||
use App\Domain\Catalog\OfferStatus;
|
||
use App\Domain\Catalog\OfferVersion;
|
||
use App\Domain\Catalog\RightsProfileVersion;
|
||
use App\Domain\Catalog\StatusValue;
|
||
use App\Domain\Commerce\Money;
|
||
use App\Domain\Commerce\ProductOptions;
|
||
use App\Domain\Entitlement\OfferAccess;
|
||
use App\Filament\Pages\Zahlungsjournal;
|
||
use App\Filament\Resources\Offers\OfferResource;
|
||
use App\Filament\Support\CatalogLabels;
|
||
use App\Filament\Support\CatalogOptions;
|
||
use App\Filament\Support\PublicationReadiness;
|
||
use App\Filament\Support\ReportsDomainRules;
|
||
use App\Filament\Support\ShopAccess;
|
||
use App\Models\User;
|
||
use Filament\Actions\Action;
|
||
use Filament\Forms\Components\DatePicker;
|
||
use Filament\Forms\Components\Repeater;
|
||
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 Filament\Schemas\Components\View;
|
||
use Filament\Schemas\Schema;
|
||
use Filament\Support\Enums\Width;
|
||
use Illuminate\Contracts\Support\Htmlable;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Angebot im gemeinsamen Rahmen. Anlegen und Ändern nutzen dieselbe Maske (FACH-20): „Fassung aktivieren“ legt die
|
||
* nächste, danach unveränderliche Angebotsfassung an; bei einer Änderung ist die Maske mit der aktiven Fassung
|
||
* vorbelegt (FACH-08). Die Wirkung bleibt die bestehende von OfferService::activate.
|
||
*/
|
||
class ViewOffer extends ViewRecord
|
||
{
|
||
use ReportsDomainRules;
|
||
|
||
protected static string $resource = OfferResource::class;
|
||
|
||
public function getTitle(): string|Htmlable
|
||
{
|
||
return (string) $this->getRecord()->name;
|
||
}
|
||
|
||
public function getHeading(): string|Htmlable
|
||
{
|
||
return '';
|
||
}
|
||
|
||
public function getMaxContentWidth(): Width|string|null
|
||
{
|
||
return Width::Full;
|
||
}
|
||
|
||
protected function getHeaderActions(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
/**
|
||
* Der gemeinsame Objektkopf zeigt den Pfad; Filaments eigener Pfad entfällt wie bei Medien und Aufbau.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
public function getBreadcrumbs(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
public function infolist(Schema $schema): Schema
|
||
{
|
||
return $schema->components([
|
||
View::make('filament.catalog.objekt')->viewData(fn (): array => $this->angebotDaten())->columnSpanFull(),
|
||
])->columns(1);
|
||
}
|
||
|
||
public function activeVersion(): ?OfferVersion
|
||
{
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
|
||
return $offer->versions()
|
||
->where('status', OfferStatus::Aktiv)
|
||
->orderByDesc('number')
|
||
->with(['lines.article', 'lines.bundleVersion.bundle', 'lines.rights.profile'])
|
||
->first();
|
||
}
|
||
|
||
public function activateAction(): Action
|
||
{
|
||
return Action::make('activate')
|
||
->label(fn (): string => $this->activeVersion() !== null ? 'Geänderte Fassung aktivieren' : 'Fassung aktivieren')
|
||
->modalHeading(fn (): string => $this->activeVersion() !== null
|
||
? 'Option „'.$this->getRecord()->optionName().'“ ändern: Fassung '.$this->nextNumber().' aktivieren'
|
||
: 'Option „'.$this->getRecord()->optionName().'“: erste Fassung aktivieren')
|
||
->modalDescription(fn (): string => ($active = $this->activeVersion()) !== null
|
||
? 'Betroffen ist nur die Option „'.$this->getRecord()->optionName().'“'.$this->productPageSuffix().'. Die Maske ist mit Fassung '.$active->number.' vorbelegt. Erst „Fassung '.$this->nextNumber().' aktivieren“ schaltet die Änderung öffentlich (auch Bezeichnung und Produktseite, sie erzeugen ebenfalls eine neue Fassung und zeigen offenen Warenkörben „Bedingungen aktualisiert“): Fassung '.$active->number.' wird beendet und bleibt gespeichert, neue Käufe nutzen Fassung '.$this->nextNumber().'. Bereits begründete Berechtigungen und Bestellungen ändern sich nicht. Andere Optionen derselben Produktseite bleiben unverändert kaufbar. „Abbrechen“ verwirft die Eingaben.'
|
||
: 'Positionen, Vergabe, Eignung und Preis bilden die erste Fassung dieser Option. Erst das Aktivieren macht sie öffentlich; danach ist sie unveränderlich, Änderungen ergeben eine weitere Fassung.')
|
||
->modalSubmitActionLabel(fn (): string => 'Fassung '.$this->nextNumber().' aktivieren')
|
||
->modalWidth('4xl')
|
||
->fillForm(fn (): array => $this->formFromActive())
|
||
->schema([
|
||
TextInput::make('option_label')->label('Bezeichnung der Option')
|
||
->helperText('So heißt die Option auf der Produktseite, zum Beispiel „Einzellizenz für Privatpersonen“. Sie ist keine bindende Bedingung, wird aber wie die übrigen Angaben erst mit „Fassung aktivieren“ übernommen.')
|
||
->maxLength(120),
|
||
Select::make('holder_kind')->label('Geeignet für')->options([
|
||
'person' => 'Privatpersonen',
|
||
'organisation' => 'Organisationen und Vereine',
|
||
])->required()
|
||
->helperText('Wähle die Eignung passend zu den Rechten: Organisationslizenzen brauchen institutionelle Rechte. Das System erzwingt diese Kopplung nicht.'),
|
||
Select::make('licence_scope')->label('Nutzerkreis')
|
||
->options(LicenceScope::options())
|
||
->required()
|
||
->live()
|
||
->helperText('Wie viele Trainer die Inhalte einsetzen dürfen. Das ist unabhängig vom Lizenzinhaber (Person oder Verein), vom Nutzungszweck der Rechte und vom Preis; es wird aus keinem davon abgeleitet. Der Download bleibt zum Start beim bestellenden Konto. Der Wert wird mit der Bestellung festgehalten.'),
|
||
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)
|
||
->helperText('Höchstzahl der Trainer, die die Inhalte einsetzen dürfen, mindestens 2. Eine Einzellizenz gilt immer für genau einen Trainer.'),
|
||
Select::make('grant_kind')->label('Vergabe')->options([
|
||
'kauf' => 'Kauf',
|
||
'auf_abruf' => 'Auf Abruf',
|
||
'bei_registrierung' => 'Bei Registrierung',
|
||
])->required(),
|
||
TextInput::make('price_euro')->label('Preis in Euro')->suffix('€')
|
||
->helperText('Nur bei einem Kauf, zum Beispiel 49,00.'),
|
||
Repeater::make('lines')->label('Positionen')->schema([
|
||
Select::make('kind')->label('Ziel')->options([
|
||
'article' => 'Lizenzartikel',
|
||
'bundle' => 'Paketfassung',
|
||
])->required()->live(),
|
||
Select::make('article_id')->label('Artikel')->options(fn (): array => app(PublicationReadiness::class)->articleOptions())
|
||
->searchable()
|
||
->helperText('Nur Artikel mit veröffentlichtem Stand lassen sich verkaufen. Artikel ohne Stand sind gekennzeichnet.')
|
||
->visible(fn (Get $get): bool => $get('kind') === 'article'),
|
||
Select::make('bundle_version_id')->label('Paketfassung')->options(fn (): array => app(PublicationReadiness::class)->bundleVersionOptions())
|
||
->searchable()
|
||
->visible(fn (Get $get): bool => $get('kind') === 'bundle'),
|
||
Select::make('rights_id')->label('Rechte')->options(fn (): array => CatalogOptions::rights())->required(),
|
||
])->minItems(1)->addActionLabel('Position hinzufügen'),
|
||
])
|
||
->action(function (array $data): void {
|
||
$this->report(function () use ($data): string {
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
$lines = [];
|
||
|
||
foreach ($data['lines'] as $line) {
|
||
$target = $line['kind'] === 'bundle'
|
||
? BundleVersion::query()->findOrFail($line['bundle_version_id'])
|
||
: LicenseArticle::query()->findOrFail($line['article_id']);
|
||
$lines[] = [
|
||
'target' => $target,
|
||
'rights' => RightsProfileVersion::query()->findOrFail($line['rights_id']),
|
||
];
|
||
}
|
||
|
||
// Voraussetzung: Jeder Artikel der Positionen hat einen veröffentlichten Stand, sonst erhielten Käufer nichts.
|
||
$articleIds = [];
|
||
|
||
foreach ($lines as $line) {
|
||
$target = $line['target'];
|
||
array_push($articleIds, ...($target instanceof BundleVersion ? app(BundleService::class)->articleIds($target) : [$target->id]));
|
||
}
|
||
|
||
$unusable = app(PublicationReadiness::class)->unusableArticles($articleIds);
|
||
|
||
if ($unusable !== []) {
|
||
throw new DomainRuleException(app(PublicationReadiness::class)->unusableMessage($unusable, 'das Angebot aktivierst'));
|
||
}
|
||
|
||
// Bedingungen, die die Maske nicht zeigt, bleiben bei einer Änderung die der aktiven Fassung.
|
||
$active = $this->activeVersion();
|
||
$kept = $active === null ? [] : [
|
||
'term_rule' => $active->term_rule,
|
||
'tax_class' => $active->tax_class,
|
||
'dynamic' => (bool) $active->dynamic,
|
||
];
|
||
$price = Money::parse($data['price_euro'] ?? null);
|
||
|
||
if ($data['grant_kind'] === 'kauf' && ($price === null || $price <= 0)) {
|
||
throw new DomainRuleException('Ein Kauf braucht einen Preis über 0,00 €.');
|
||
}
|
||
|
||
$service = app(OfferService::class);
|
||
$version = DB::transaction(function () use ($service, $offer, $data, $lines, $kept, $price) {
|
||
$service->setOptionLabel($offer, $data['option_label'] ?? null);
|
||
|
||
return $service->activate($offer, $lines, [
|
||
...$kept,
|
||
'holder_kind' => $data['holder_kind'],
|
||
'licence_scope' => $data['licence_scope'] ?? null,
|
||
'trainer_count' => $data['trainer_count'] ?? null,
|
||
'grant_kind' => $data['grant_kind'],
|
||
'price_cents' => $price,
|
||
]);
|
||
});
|
||
|
||
$offer->unsetRelations();
|
||
$readiness = app(PublicationReadiness::class)->forOffer($offer->refresh());
|
||
|
||
return 'Fassung '.$version->number.' ist aktiv.'.($readiness['bereit']
|
||
? ' Die Option ist öffentlich kaufbar und im Shop auffindbar.'
|
||
: ' Noch nicht im Shop kaufbar. Nächster Schritt: '.$readiness['naechster']['titel'].'.');
|
||
}, 'Die Angebotsfassung ist aktiv.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Option aus dem Verkauf nehmen: nur die aktive Fassung dieser Option wird beendet.
|
||
*/
|
||
public function endAction(): Action
|
||
{
|
||
return Action::make('end')
|
||
->label('Aus dem Verkauf nehmen')
|
||
->color('danger')
|
||
->requiresConfirmation()
|
||
->modalHeading(fn (): string => 'Option „'.$this->getRecord()->optionName().'“ aus dem Verkauf nehmen?')
|
||
->modalDescription(fn (): string => 'Betroffen ist nur die Option „'.$this->getRecord()->optionName().'“'.$this->productPageSuffix().'. 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 derselben Produktseite bleiben kaufbar.')
|
||
->modalSubmitActionLabel('Aus dem Verkauf nehmen')
|
||
->visible(fn (): bool => $this->activeVersion() !== null)
|
||
->action(function (): void {
|
||
$this->report(function (): string {
|
||
$version = app(OfferService::class)->end($this->getRecord());
|
||
|
||
return 'Fassung '.$version->number.' ist beendet. Die Option ist nicht mehr kaufbar.';
|
||
}, 'Die Option ist aus dem Verkauf genommen.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
/** Ein Angebot auf Abruf: nur dafür gibt es Regeln, wer es übernehmen darf (ADR-56). */
|
||
private function isOnDemand(): bool
|
||
{
|
||
return $this->activeVersion()?->grant_kind === 'auf_abruf';
|
||
}
|
||
|
||
/**
|
||
* Wer das Angebot übernehmen darf: alle Mitglieder, nur freigeschaltete Konten oder wer einen Artikel besitzt.
|
||
*/
|
||
public function claimRuleAction(): Action
|
||
{
|
||
return Action::make('claimRule')
|
||
->label('Regel für den Abruf')
|
||
->modalHeading(fn (): string => 'Wer darf „'.$this->getRecord()->name.'“ abrufen?')
|
||
->modalDescription('Die Regel gilt für den kostenlosen Abruf in die Bibliothek und wird beim Abruf geprüft. Sie begrenzt nur den Abruf; sie vergibt nichts. Bereits übernommene Angebote bleiben in der Bibliothek.')
|
||
->modalSubmitActionLabel('Regel speichern')
|
||
->visible(fn (): bool => $this->isOnDemand())
|
||
->fillForm(fn (): array => [
|
||
'claim_access' => (string) ($this->getRecord()->claim_access ?: OfferAccess::ALL),
|
||
'claim_requires_article_id' => $this->getRecord()->claim_requires_article_id,
|
||
])
|
||
->schema([
|
||
Select::make('claim_access')->label('Wer darf abrufen')
|
||
->options(OfferAccess::OPTIONS)
|
||
->required()->live()->native(false)
|
||
->helperText('„Nur freigeschaltete Konten“: Du schaltest einzelne Konten auf dieser Seite frei und entziehst die Freigabe wieder. Sie gilt nur für dieses Angebot.'),
|
||
Select::make('claim_requires_article_id')->label('Artikel, den das Konto besitzen muss')
|
||
->options(fn (): array => LicenseArticle::query()->whereNull('archived_at')->orderBy('code')->get()
|
||
->mapWithKeys(fn (LicenseArticle $article): array => [$article->id => app(ProductOptions::class)->articleTitle($article)])->all())
|
||
->searchable()
|
||
->required(fn (Get $get): bool => $get('claim_access') === OfferAccess::OWNERS)
|
||
->visible(fn (Get $get): bool => $get('claim_access') === OfferAccess::OWNERS)
|
||
->helperText('Das Konto muss eine gültige Berechtigung auf diesen Artikel haben, zum Beispiel durch einen Kauf.'),
|
||
])
|
||
->action(function (array $data): void {
|
||
$this->report(function () use ($data): string {
|
||
app(OfferAccess::class)->setRule($this->getRecord(), (string) $data['claim_access'], filled($data['claim_requires_article_id'] ?? null) ? (int) $data['claim_requires_article_id'] : null);
|
||
|
||
return 'Die Regel für den Abruf ist gespeichert.';
|
||
}, 'Die Regel ist gespeichert.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
public function releaseAction(): Action
|
||
{
|
||
return Action::make('release')
|
||
->label('Konto freischalten')
|
||
->modalHeading(fn (): string => 'Konto für „'.$this->getRecord()->name.'“ freischalten')
|
||
->modalDescription('Das Konto darf genau dieses Angebot abrufen, solange Freigabe und Angebot gelten. Die Freigabe gilt nicht für andere Angebote und bleibt mit dem Entzug im Verlauf stehen.')
|
||
->modalSubmitActionLabel('Konto freischalten')
|
||
->visible(fn (): bool => $this->isOnDemand() && $this->getRecord()->claim_access === OfferAccess::RELEASED)
|
||
->schema([
|
||
Select::make('user_id')->label('Mitgliedskonto')->required()->searchable()
|
||
->getSearchResultsUsing(fn (string $search): array => $this->searchMembers($search))
|
||
->getOptionLabelUsing(fn ($value): ?string => ($user = User::query()->find($value)) === null ? null : $user->name.' ('.$user->email.')')
|
||
->helperText('Suche nach Name oder E-Mail-Adresse. Nur Konten mit Mitgliedsprofil.'),
|
||
DatePicker::make('valid_until')->label('Freigabe gültig bis einschließlich')->native(false)->displayFormat('d.m.Y')
|
||
->helperText('Leer: ohne Ende.'),
|
||
Textarea::make('reason')->label('Grundlage der Freigabe')->rows(2)->maxLength(500)
|
||
->helperText('Zum Beispiel „Teilnahme am Seminar am 12.11.2026“. Bleibt im Verlauf stehen.'),
|
||
])
|
||
->action(function (array $data): void {
|
||
$this->report(function () use ($data): string {
|
||
$member = User::query()->findOrFail($data['user_id']);
|
||
$until = filled($data['valid_until'] ?? null) ? Carbon::parse((string) $data['valid_until'], config('app.timezone'))->endOfDay() : null;
|
||
app(OfferAccess::class)->release($this->getRecord(), $member, auth('staff')->user() ?? auth()->user(), $until, $data['reason'] ?? null);
|
||
|
||
return 'Das Konto von '.$member->name.' ist für das Angebot freigeschaltet.';
|
||
}, 'Das Konto ist freigeschaltet.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
public function revokeAction(): Action
|
||
{
|
||
return Action::make('revoke')
|
||
->label('Freigabe entziehen')
|
||
->color('danger')
|
||
->requiresConfirmation()
|
||
->modalHeading('Freigabe entziehen?')
|
||
->modalDescription('Das Konto kann das Angebot danach nicht mehr abrufen. Bereits übernommene Angebote bleiben in der Bibliothek. Die Freigabe bleibt mit dem Entzug im Verlauf stehen.')
|
||
->modalSubmitActionLabel('Freigabe entziehen')
|
||
->schema([
|
||
Textarea::make('reason')->label('Grund')->rows(2)->maxLength(500),
|
||
])
|
||
->action(function (array $data, array $arguments): void {
|
||
$this->report(function () use ($data, $arguments): string {
|
||
$release = OfferRelease::query()->where('offer_id', $this->getRecord()->id)->findOrFail((int) ($arguments['release'] ?? 0));
|
||
app(OfferAccess::class)->revoke($release, auth('staff')->user() ?? auth()->user(), $data['reason'] ?? null);
|
||
|
||
return 'Die Freigabe ist entzogen.';
|
||
}, 'Die Freigabe ist entzogen.');
|
||
$this->getRecord()->unsetRelations();
|
||
$this->getRecord()->refresh();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @return array<int, string>
|
||
*/
|
||
private function searchMembers(string $search): array
|
||
{
|
||
$like = '%'.mb_strtolower(trim($search)).'%';
|
||
|
||
return User::query()
|
||
->whereNotNull('party_id')
|
||
->where(fn ($query) => $query->whereRaw('LOWER(name) LIKE ?', [$like])->orWhereRaw('LOWER(email) LIKE ?', [$like]))
|
||
->orderBy('name')
|
||
->limit(20)
|
||
->get()
|
||
->mapWithKeys(fn (User $user): array => [$user->id => $user->name.' ('.$user->email.')'])
|
||
->all();
|
||
}
|
||
|
||
/**
|
||
* Abschnitte „Wer darf abrufen“ und „Freigeschaltete Konten“ für ein Angebot auf Abruf.
|
||
*
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
private function abrufAbschnitte(Offer $offer): array
|
||
{
|
||
$date = fn ($value): string => $value?->format('d.m.Y') ?? '';
|
||
$access = (string) ($offer->claim_access ?: OfferAccess::ALL);
|
||
$article = $offer->claim_requires_article_id !== null ? LicenseArticle::query()->find($offer->claim_requires_article_id) : null;
|
||
$releases = $offer->releases()->with(['user', 'grantor'])->orderByDesc('id')->get();
|
||
$active = $releases->filter(fn (OfferRelease $release): bool => $release->revoked_at === null);
|
||
$past = $releases->filter(fn (OfferRelease $release): bool => $release->revoked_at !== null);
|
||
$sections = [[
|
||
'titel' => 'Wer darf abrufen',
|
||
'hilfe' => 'Diese Regel gilt für den kostenlosen Abruf in die Bibliothek (Mitgliederbereich → Angebote). Sie wird beim Abruf geprüft, nicht nur in der Anzeige.',
|
||
'zeilen' => [[
|
||
'text' => OfferAccess::OPTIONS[$access] ?? $access,
|
||
'neben' => $access === OfferAccess::OWNERS
|
||
? 'Erforderlich: '.($article !== null ? app(ProductOptions::class)->articleTitle($article) : 'ein Artikel fehlt (Regel prüfen)')
|
||
: ($access === OfferAccess::RELEASED ? 'Mit „Konto freischalten“ gibst du einzelne Konten frei.' : 'Jedes angemeldete Mitgliedskonto sieht das Angebot und kann es übernehmen.'),
|
||
]],
|
||
]];
|
||
|
||
if ($access === OfferAccess::RELEASED) {
|
||
$sections[] = [
|
||
'titel' => 'Freigeschaltete Konten',
|
||
'zusatz' => $active->count().' aktiv',
|
||
'hilfe' => 'Nur diese Konten sehen das Angebot im Mitgliederbereich und können es übernehmen.',
|
||
'zeilen' => $active->map(fn (OfferRelease $release): array => [
|
||
'text' => ($release->user?->name ?? 'Konto').' ('.($release->user?->email ?? '').')',
|
||
'neben' => 'freigeschaltet am '.$date($release->granted_at).($release->grantor ? ' von '.$release->grantor->name : '').($release->valid_until ? ', gültig bis einschließlich '.$date($release->valid_until) : '').(filled($release->reason) ? ' · '.$release->reason : ''),
|
||
'aktion' => ['label' => 'Entziehen', 'wire' => "mountAction('revoke', { release: ".$release->id.' })'],
|
||
])->values()->all(),
|
||
'leer' => ['titel' => 'Noch kein Konto freigeschaltet', 'text' => 'Mit „Konto freischalten“ gibst du ein Konto für dieses Angebot frei.'],
|
||
];
|
||
|
||
if ($past->isNotEmpty()) {
|
||
$sections[] = [
|
||
'titel' => 'Entzogene Freigaben',
|
||
'hilfe' => 'Der Verlauf bleibt erhalten.',
|
||
'zeilen' => $past->map(fn (OfferRelease $release): array => [
|
||
'text' => $release->user?->name ?? 'Konto',
|
||
'neben' => 'entzogen am '.$date($release->revoked_at).(filled($release->reason) ? ' · '.$release->reason : ''),
|
||
])->values()->all(),
|
||
];
|
||
}
|
||
}
|
||
|
||
return $sections;
|
||
}
|
||
|
||
/** „ der Produktseite „Titel““ oder leer, wenn die Option (noch) keine Produktseite hat. */
|
||
private function productPageSuffix(): string
|
||
{
|
||
$key = app(ProductOptions::class)->groupKey($this->getRecord());
|
||
|
||
if ($key === null) {
|
||
return '';
|
||
}
|
||
|
||
$id = (int) substr($key, 2);
|
||
$title = match ($key[0]) {
|
||
'a' => app(ProductOptions::class)->articleTitle(LicenseArticle::query()->find($id)),
|
||
'b' => (string) Bundle::query()->whereKey($id)->value('name'),
|
||
default => ($page = Composition::query()->find($id)) === null ? '' : (string) ($page->display_title ?: $page->designation),
|
||
};
|
||
|
||
return $title === '' ? '' : ' der Produktseite „'.$title.'“';
|
||
}
|
||
|
||
/**
|
||
* Vorbelegung aus der aktiven Fassung; ohne aktive Fassung eine leere Position.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function formFromActive(): array
|
||
{
|
||
$active = $this->activeVersion();
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
$option = ['option_label' => $offer->option_label];
|
||
|
||
if ($active === null) {
|
||
return [...$option, 'holder_kind' => 'person', 'licence_scope' => LicenceScope::SINGLE, 'grant_kind' => 'kauf', 'lines' => [['kind' => 'article']]];
|
||
}
|
||
|
||
return [
|
||
...$option,
|
||
'holder_kind' => $active->holder_kind,
|
||
'licence_scope' => $active->licence_scope,
|
||
'trainer_count' => $active->trainer_count,
|
||
'grant_kind' => $active->grant_kind,
|
||
'price_euro' => $active->price_cents === null ? null : number_format($active->price_cents / 100, 2, ',', ''),
|
||
'lines' => $active->lines->sortBy('position')->map(fn ($line): array => [
|
||
'kind' => $line->license_article_id !== null ? 'article' : 'bundle',
|
||
'article_id' => $line->license_article_id,
|
||
'bundle_version_id' => $line->bundle_version_id,
|
||
'rights_id' => $line->rights_profile_version_id,
|
||
])->values()->all(),
|
||
];
|
||
}
|
||
|
||
private function nextNumber(): int
|
||
{
|
||
return ((int) $this->getRecord()->versions()->max('number')) + 1;
|
||
}
|
||
|
||
/**
|
||
* Daten der gemeinsamen Katalog-Objektseite.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function angebotDaten(): array
|
||
{
|
||
/** @var Offer $offer */
|
||
$offer = $this->getRecord();
|
||
$active = $this->activeVersion();
|
||
$vergabe = $active !== null ? CatalogLabels::vergabe($active) : null;
|
||
|
||
return [
|
||
'kopf' => [
|
||
'brot' => [['label' => 'Angebote', 'href' => OfferResource::getUrl('index')], $offer->name],
|
||
'titel' => $offer->name,
|
||
'typ' => 'Angebot',
|
||
'status' => $active !== null
|
||
? ['text' => 'Fassung '.$active->number.' aktiv · '.$vergabe, 'ton' => 'gut']
|
||
: ['text' => 'Noch keine aktive Fassung. Ein Angebot ohne aktive Fassung kann niemand erwerben.', 'ton' => 'offen'],
|
||
'aktionen' => [
|
||
[
|
||
'label' => $active !== null ? 'Geänderte Fassung aktivieren' : 'Fassung aktivieren',
|
||
'wire' => "mountAction('activate')",
|
||
'art' => 'primary',
|
||
],
|
||
...($active !== null ? [[
|
||
'label' => 'Aus dem Verkauf nehmen',
|
||
'wire' => "mountAction('end')",
|
||
'art' => 'line',
|
||
]] : []),
|
||
...($this->isOnDemand() ? [[
|
||
'label' => 'Regel für den Abruf',
|
||
'wire' => "mountAction('claimRule')",
|
||
'art' => 'line',
|
||
]] : []),
|
||
...($this->isOnDemand() && $offer->claim_access === OfferAccess::RELEASED ? [[
|
||
'label' => 'Konto freischalten',
|
||
'wire' => "mountAction('release')",
|
||
'art' => 'line',
|
||
]] : []),
|
||
...(ShopAccess::allowed() ? [[
|
||
'label' => 'Zahlungen zu diesem Angebot',
|
||
'href' => Zahlungsjournal::getUrl(['angebot' => $offer->id, 'zeitraum' => 'jahr', 'betrieb' => 'alle']),
|
||
'art' => 'quiet',
|
||
]] : []),
|
||
],
|
||
],
|
||
'voraussetzungen' => app(PublicationReadiness::class)->forOffer($offer),
|
||
'haupt' => [
|
||
...($active !== null ? [$this->optionAbschnitt($offer, $active)] : []),
|
||
...($this->isOnDemand() ? $this->abrufAbschnitte($offer) : []),
|
||
[
|
||
'titel' => 'Positionen',
|
||
'zusatz' => $active !== null ? 'Fassung '.$active->number : null,
|
||
'hilfe' => $active !== null ? 'Vergabe: '.$vergabe.'. Diese Fassung ist unveränderlich; eine Änderung aktiviert die nächste Fassung.' : null,
|
||
'zeilen' => $active === null ? [] : $active->lines->sortBy('position')->map(fn ($line): array => [
|
||
'text' => $line->article !== null ? 'Lizenzartikel '.$line->article->code : 'Paket '.($line->bundleVersion?->bundle?->name ?? '–').' · Fassung '.($line->bundleVersion?->number ?? '–'),
|
||
'neben' => 'Rechte: '.($line->rights?->profile?->name ?? '–'),
|
||
])->values()->all(),
|
||
'leer' => ['titel' => 'Noch keine Positionen', 'text' => 'Mit „Fassung aktivieren“ legst du fest, welche Lizenzartikel oder Paketfassungen mit welchem Rechteprofil angeboten werden, wie vergeben wird und zu welchem Preis.'],
|
||
],
|
||
...($active !== null ? [[
|
||
'titel' => 'Aufgelöster Leistungsumfang',
|
||
'hilfe' => 'Diese Artikel-Rechte entstehen beim Erwerb dieser Fassung.',
|
||
'zeilen' => $this->leistungsumfang($active),
|
||
'leer' => ['titel' => 'Kein Leistungsumfang', 'text' => 'Diese Fassung hat keinen aufgelösten Leistungsumfang.'],
|
||
]] : []),
|
||
],
|
||
'seite' => [[
|
||
'titel' => 'Fassungen',
|
||
'zeilen' => $offer->versions()->orderByDesc('number')->get()->map(fn (OfferVersion $version): array => [
|
||
'text' => 'Fassung '.$version->number,
|
||
'zustand' => match (StatusValue::of($version->status)) {
|
||
'aktiv' => ['label' => 'Aktiv', 'ton' => 'gut'],
|
||
'beendet' => ['label' => 'Beendet', 'ton' => 'leise'],
|
||
default => ['label' => 'Entwurf', 'ton' => 'offen'],
|
||
},
|
||
'neben' => CatalogLabels::vergabe($version).($version->activated_at ? ' · aktiviert am '.$version->activated_at->format('d.m.Y') : ''),
|
||
])->all(),
|
||
'leer' => 'Noch keine Fassung.',
|
||
'hilfe' => 'Beendete Fassungen bleiben gespeichert; Erwerbe verweisen auf ihre Fassung. Ob ein Angebot künftig auch als Vorlage für ein weiteres dienen soll, ist fachlich noch offen (OFF-04).',
|
||
]],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Die Option auf einen Blick: Preis in Euro, Eignung, Rechte, zugrunde liegender Inhalt, Status und Produktseite.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function optionAbschnitt(Offer $offer, OfferVersion $active): array
|
||
{
|
||
$option = app(ProductOptions::class)->describe($offer, $active);
|
||
$inhalt = collect($option['scope'])->pluck('article')->unique()->implode(', ');
|
||
$rechte = collect($option['scope'])
|
||
->map(fn (array $item): string => $item['purpose'].($item['rights'] === [] ? '' : ': '.implode(', ', $item['rights'])))
|
||
->unique()
|
||
->implode('; ');
|
||
$key = app(ProductOptions::class)->groupKey($offer);
|
||
$seite = $key === null ? null : app(ProductOptions::class)->pageUrl($offer);
|
||
$kaufbar = app(OfferService::class)->purchasableVersion($offer) !== null;
|
||
|
||
return [
|
||
'titel' => 'Die Option auf einen Blick',
|
||
'zusatz' => 'Fassung '.$active->number,
|
||
'hilfe' => 'So erscheint die Option auf der öffentlichen Produktseite und im Warenkorb. Die Angaben dieser Fassung sind unveränderlich.',
|
||
'zeilen' => [
|
||
['text' => 'Preis: '.$option['price'], 'neben' => $active->grant_kind === 'kauf' ? 'Kauf mit Testzahlung' : CatalogLabels::vergabe($active)],
|
||
['text' => 'Geeignet für: '.$option['for'], 'neben' => $option['term']],
|
||
['text' => 'Nutzerkreis: '.$option['licence'], 'neben' => $option['licence_scope'] === null ? 'Mit „Geänderte Fassung aktivieren“ festlegen.' : 'Lizenzinhaber, Nutzerkreis und Downloadkonto sind getrennte Angaben.'],
|
||
['text' => 'Rechte: '.($rechte !== '' ? $rechte : 'keine'), 'neben' => null],
|
||
['text' => 'Zugrunde liegender Inhalt: '.($inhalt !== '' ? $inhalt : '–'), 'neben' => null],
|
||
['text' => 'Status: '.($kaufbar ? 'Öffentlich kaufbar' : 'Nicht als Kauf angeboten'), 'neben' => $kaufbar ? 'Aktive Fassung '.$active->number : null],
|
||
['text' => 'Produktseite: '.($kaufbar && $seite !== null ? 'öffentlich unter '.parse_url($seite, PHP_URL_PATH) : 'keine, solange die Option kein Kauf mit Preis ist'), 'neben' => $kaufbar ? 'Option „'.$offer->optionName().'“; Optionen desselben Artikels sind Alternativen im Warenkorb.' : null],
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Aufgelöster Leistungsumfang in lesbaren Fähigkeiten (dieselben Bezeichnungen wie beim Rechteprofil).
|
||
*
|
||
* @return list<array{text: string, neben: string}>
|
||
*/
|
||
private function leistungsumfang(OfferVersion $version): array
|
||
{
|
||
$zeilen = [];
|
||
|
||
foreach ($version->resolvedScopes()->with(['article', 'rights.profile'])->get() as $scope) {
|
||
$faehigkeiten = CatalogLabels::faehigkeiten($scope->rights?->capabilities);
|
||
$zeilen[] = [
|
||
'text' => 'Lizenzartikel '.$scope->article?->code,
|
||
'neben' => ($scope->rights?->profile?->name ?? '–').': '.($faehigkeiten === [] ? 'keine Fähigkeiten' : implode(', ', $faehigkeiten)),
|
||
];
|
||
}
|
||
|
||
return $zeilen;
|
||
}
|
||
}
|