Merge remote-tracking branch 'origin/develop' into feature/merkmale-symbol-seite
This commit is contained in:
commit
fbe4226347
32 files changed
+1640
-43
No files matched your search
@@ -13,6 +13,8 @@ class Offer extends Model
|
||||
'composition_id',
|
||||
'option_label',
|
||||
'standalone_bundle',
|
||||
'claim_access',
|
||||
'claim_requires_article_id',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
@@ -31,6 +33,12 @@ public function composition(): BelongsTo
|
||||
return $this->belongsTo(Composition::class);
|
||||
}
|
||||
|
||||
/** Freigaben einzelner Konten für den Abruf (nur bei Vergabe „auf Abruf“ mit der Regel „freigeschaltete Konten“). */
|
||||
public function releases(): HasMany
|
||||
{
|
||||
return $this->hasMany(OfferRelease::class);
|
||||
}
|
||||
|
||||
/** Bezeichnung der Option auf der Produktseite. */
|
||||
public function optionName(): string
|
||||
{
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Freigabe eines Kontos für ein Angebot „auf Abruf“ (ADR-56). Gilt je Angebot und Konto, wird nie gelöscht, nur entzogen.
|
||||
*/
|
||||
class OfferRelease extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'offer_id',
|
||||
'user_id',
|
||||
'granted_by',
|
||||
'granted_at',
|
||||
'valid_until',
|
||||
'revoked_by',
|
||||
'revoked_at',
|
||||
'reason',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'granted_at' => 'datetime',
|
||||
'valid_until' => 'datetime',
|
||||
'revoked_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function offer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Offer::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function grantor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'granted_by');
|
||||
}
|
||||
|
||||
public function isActiveAt(CarbonInterface $now): bool
|
||||
{
|
||||
return $this->revoked_at === null
|
||||
&& $this->granted_at->lessThanOrEqualTo($now)
|
||||
&& ($this->valid_until === null || $this->valid_until->greaterThan($now));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Types;
|
||||
|
||||
use App\Domain\Catalog\EditorialContentKind;
|
||||
use App\Domain\Catalog\Pillar;
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
use App\Domain\Commerce\Shop;
|
||||
use App\Domain\Commerce\ShopCatalog;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
|
||||
/**
|
||||
* Shop-Produkte (ADR-55): die kaufbaren Produkte als Kacheln, aus derselben Datenquelle wie die Shopseite (ShopCatalog).
|
||||
*
|
||||
* Gespeichert werden nur Darstellungswahl und Filter (Säule, Materialart); Titel, Bild, Preis und Marker kommen zur
|
||||
* Laufzeit aus den Fachdaten. Das Widget zeigt nichts, solange der Shop nicht sichtbar ist (Schalter „Shop sichtbar“).
|
||||
* Eine Kachel begründet keine Berechtigung. Anders als Programm- und Modulkarten trägt es Preise und führt zur Produktseite.
|
||||
*/
|
||||
class ShopProductsWidget extends WidgetType
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'shop_products';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Shop-Produkte';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Die kaufbaren Produkte des Shops als Kacheln mit Bild, Marker und Preis; optional gefiltert und sortiert.';
|
||||
}
|
||||
|
||||
public function supportedUnits(): array
|
||||
{
|
||||
return [2, 3, 4];
|
||||
}
|
||||
|
||||
public function minUnits(): int
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
public function preferredUnits(): int
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
public function narrowBehavior(): string
|
||||
{
|
||||
return 'Die Kacheln stehen untereinander; ab etwa 17 rem Kachelbreite mehrere nebeneinander.';
|
||||
}
|
||||
|
||||
public function stages(): array
|
||||
{
|
||||
return [
|
||||
'narrow' => 'eine Kachel je Zeile',
|
||||
'wide' => 'mehrere Kacheln nebeneinander ab 17 rem Kachelbreite',
|
||||
];
|
||||
}
|
||||
|
||||
public function icon(): string
|
||||
{
|
||||
return 'heroicon-o-shopping-bag';
|
||||
}
|
||||
|
||||
public function schematic(): array
|
||||
{
|
||||
return [[2, 4, 17, 28, 'line'], [21, 4, 17, 28, 'line'], [40, 4, 17, 28, 'line'], [4, 6, 13, 10, 'image'], [23, 6, 13, 10, 'image'], [42, 6, 13, 10, 'image'], [4, 19, 11, 3, 'ink'], [23, 19, 11, 3, 'ink'], [42, 19, 11, 3, 'ink'], [4, 26, 7, 3, 'accent'], [23, 26, 7, 3, 'accent'], [42, 26, 7, 3, 'accent']];
|
||||
}
|
||||
|
||||
public function variants(): array
|
||||
{
|
||||
return [
|
||||
'kacheln' => 'Kacheln mit Bild',
|
||||
];
|
||||
}
|
||||
|
||||
public function settings(): array
|
||||
{
|
||||
return [
|
||||
'sort' => ['name' => 'Alphabetisch', 'neu' => 'Neueste zuerst', 'preis' => 'Preis aufsteigend'],
|
||||
'limit' => ['alle' => 'Alle Produkte', 'n3' => '3 Produkte', 'n6' => '6 Produkte', 'n9' => '9 Produkte', 'n12' => '12 Produkte', 'n24' => '24 Produkte'],
|
||||
'search' => ['aus' => 'Ohne Suchfeld', 'an' => 'Mit Suchfeld'],
|
||||
];
|
||||
}
|
||||
|
||||
public function settingLabels(): array
|
||||
{
|
||||
return [
|
||||
'sort' => ['Reihenfolge', 'Ohne Wahl gilt die Reihenfolge der Shopseite: Altbestand, Artikel, Pakete.'],
|
||||
'limit' => ['Anzahl', 'Wie viele Produkte höchstens erscheinen. Ohne Wahl erscheinen alle.'],
|
||||
'search' => ['Suchfeld', 'Ein Suchfeld über den Kacheln. Es wirkt auf der Shopseite; auf anderen Seiten führt die Suche in den Shop.'],
|
||||
];
|
||||
}
|
||||
|
||||
public function settingGroups(): array
|
||||
{
|
||||
return ['Auswahl' => ['sort', 'limit', 'search']];
|
||||
}
|
||||
|
||||
public function dataRules(): array
|
||||
{
|
||||
return [
|
||||
'heading' => ['nullable', 'string', 'max:160'],
|
||||
'intro' => ['nullable', 'string', 'max:400'],
|
||||
'pillar' => ['nullable', 'integer'],
|
||||
'kind' => ['nullable', 'integer'],
|
||||
];
|
||||
}
|
||||
|
||||
public function dataFields(): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('heading')->label('Abschnittsüberschrift (optional)')->maxLength(160),
|
||||
Textarea::make('intro')->label('Einleitung (optional)')->rows(2)->maxLength(400),
|
||||
Select::make('pillar')
|
||||
->label('Nur Produkte einer Säule (optional)')
|
||||
->helperText('Nur veröffentlichte Säulen sind wählbar. Ohne Wahl erscheinen alle Produkte.')
|
||||
->options(fn (): array => Pillar::query()->published()->pluck('name', 'id')->all())
|
||||
->placeholder('Alle Säulen')
|
||||
->native(false),
|
||||
Select::make('kind')
|
||||
->label('Nur eine Materialart (optional)')
|
||||
->options(fn (): array => EditorialContentKind::query()->where('is_active', true)->orderBy('sort_order')->get()
|
||||
->filter(fn (EditorialContentKind $kind): bool => in_array('lizenzartikel', (array) ($kind->contexts ?? []), true))
|
||||
->pluck('label', 'id')->all())
|
||||
->placeholder('Alle Materialarten')
|
||||
->native(false),
|
||||
];
|
||||
}
|
||||
|
||||
public function clean(array $data, array $refs, WidgetErrors $errors): array
|
||||
{
|
||||
foreach (['heading', 'intro'] as $key) {
|
||||
$data[$key] = filled($data[$key] ?? null) ? trim((string) $data[$key]) : null;
|
||||
}
|
||||
|
||||
$pillar = filled($data['pillar'] ?? null) ? (int) $data['pillar'] : null;
|
||||
|
||||
if ($pillar !== null && ! Pillar::query()->published()->whereKey($pillar)->exists()) {
|
||||
$errors->add('data.pillar', 'Diese Säule gibt es nicht oder sie ist nicht veröffentlicht.');
|
||||
$pillar = null;
|
||||
}
|
||||
|
||||
$kind = filled($data['kind'] ?? null) ? (int) $data['kind'] : null;
|
||||
|
||||
if ($kind !== null && ! EditorialContentKind::query()->whereKey($kind)->exists()) {
|
||||
$errors->add('data.kind', 'Diese Materialart gibt es nicht.');
|
||||
$kind = null;
|
||||
}
|
||||
|
||||
$data['pillar'] = $pillar;
|
||||
$data['kind'] = $kind;
|
||||
|
||||
return [$data, $refs];
|
||||
}
|
||||
|
||||
public function resolve(array $widget, RenderContext $context): array
|
||||
{
|
||||
if (! Shop::visible()) {
|
||||
return ['cards' => [], 'term' => '', 'shopVisible' => false];
|
||||
}
|
||||
|
||||
$settings = is_array($widget['settings'] ?? null) ? $widget['settings'] : [];
|
||||
$search = ($settings['search'] ?? 'aus') === 'an';
|
||||
$term = $search && ! $context->preview ? trim((string) request()->query('suche', '')) : '';
|
||||
$limit = preg_match('/^n(\d+)$/', (string) ($settings['limit'] ?? ''), $match) === 1 ? (int) $match[1] : null;
|
||||
|
||||
return [
|
||||
'cards' => app(ShopCatalog::class)->cards($term, [
|
||||
'pillar' => $widget['data']['pillar'] ?? null,
|
||||
'kind' => $widget['data']['kind'] ?? null,
|
||||
'sort' => $settings['sort'] ?? null,
|
||||
'limit' => $limit,
|
||||
]),
|
||||
'term' => $term,
|
||||
'shopVisible' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function summary(array $widget): string
|
||||
{
|
||||
$heading = $widget['data']['heading'] ?? null;
|
||||
|
||||
return trim(($heading ? $heading.' · ' : '').'Shop-Produkte');
|
||||
}
|
||||
}
|
||||
@@ -34,12 +34,16 @@ public function __construct(
|
||||
private readonly ShopMarkers $markers,
|
||||
) {}
|
||||
|
||||
public const SORTS = ['name' => 'Alphabetisch', 'neu' => 'Neueste zuerst', 'preis' => 'Preis aufsteigend'];
|
||||
|
||||
/**
|
||||
* @param array{pillar?: int|null, kind?: int|null, sort?: string|null, limit?: int|null} $options Filter nach Säule
|
||||
* und Materialart, Sortierung (name, neu, preis) und Höchstzahl
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function cards(string $term = ''): array
|
||||
public function cards(string $term = '', array $options = []): array
|
||||
{
|
||||
$term = mb_strtolower(trim($term));
|
||||
$term = mb_strtolower(mb_substr(trim($term), 0, 100));
|
||||
$cards = [];
|
||||
|
||||
// Altbestand: Angebote mit mehreren Positionen auf einer freigegebenen Programmseite.
|
||||
@@ -54,6 +58,8 @@ public function cards(string $term = ''): array
|
||||
$teaser = (string) ($composition->teaser ?: FormattedText::plain($composition->summary));
|
||||
$pillars = $this->pillarNames($composition->pillars);
|
||||
$card = $this->card('c:'.$composition->id, 'programm', route('public.program', $composition), $title, $teaser, $summaries[$composition->id], 'Programm', null, $pillars, [], $this->image($composition));
|
||||
$card['pillar_ids'] = $composition->pillars->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
||||
$card['kind_id'] = $composition->editorial_content_kind_id !== null ? (int) $composition->editorial_content_kind_id : null;
|
||||
|
||||
if ($this->matches($term, $title.' '.$teaser.' '.FormattedText::plain($composition->summary).' '.$composition->search_terms.' '.implode(' ', $pillars))) {
|
||||
$cards[] = $card;
|
||||
@@ -74,6 +80,9 @@ public function cards(string $term = ''): array
|
||||
'a:'.$article->id, 'artikel', route('shop.article', $article), $title, $teaser, $articleSummaries[$article->id],
|
||||
(string) ($kind?->label ?? ''), $this->symbol($kind?->symbol), $pillars, $this->markers->visible($article, $offerIds), $this->image($article),
|
||||
);
|
||||
$card['pillar_ids'] = $article->pillars->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
||||
$card['kind_id'] = $kind !== null ? (int) $kind->id : null;
|
||||
$card['since'] = $this->markers->firstOffered($offerIds)?->getTimestamp() ?? 0;
|
||||
|
||||
if ($this->matches($term, $title.' '.$teaser.' '.$profile->value('summary').' '.$profile->searchTerms.' '.($kind?->label ?? '').' '.implode(' ', $pillars))) {
|
||||
$cards[] = $card;
|
||||
@@ -94,7 +103,42 @@ public function cards(string $term = ''): array
|
||||
}
|
||||
}
|
||||
|
||||
return $cards;
|
||||
return $this->arrange($cards, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter, Sortierung und Höchstzahl der Kacheln (Widget und Shopseite). Ohne Angabe bleibt die Reihenfolge der
|
||||
* Datenquelle.
|
||||
*
|
||||
* @param list<array<string, mixed>> $cards
|
||||
* @param array<string, mixed> $options
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function arrange(array $cards, array $options): array
|
||||
{
|
||||
$pillar = isset($options['pillar']) ? (int) $options['pillar'] : 0;
|
||||
$kind = isset($options['kind']) ? (int) $options['kind'] : 0;
|
||||
|
||||
if ($pillar > 0) {
|
||||
$cards = array_filter($cards, fn (array $card): bool => in_array($pillar, $card['pillar_ids'] ?? [], true));
|
||||
}
|
||||
|
||||
if ($kind > 0) {
|
||||
$cards = array_filter($cards, fn (array $card): bool => ($card['kind_id'] ?? null) === $kind);
|
||||
}
|
||||
|
||||
$cards = array_values($cards);
|
||||
|
||||
match ($options['sort'] ?? null) {
|
||||
'name' => usort($cards, fn (array $a, array $b): int => strcmp(mb_strtolower($a['title']), mb_strtolower($b['title']))),
|
||||
'neu' => usort($cards, fn (array $a, array $b): int => [$b['since'] ?? 0, mb_strtolower($a['title'])] <=> [$a['since'] ?? 0, mb_strtolower($b['title'])]),
|
||||
'preis' => usort($cards, fn (array $a, array $b): int => [$a['from_cents'], mb_strtolower($a['title'])] <=> [$b['from_cents'], mb_strtolower($b['title'])]),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$limit = isset($options['limit']) ? (int) $options['limit'] : 0;
|
||||
|
||||
return $limit > 0 ? array_slice($cards, 0, $limit) : $cards;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,6 +164,9 @@ private function card(string $key, string $kind, string $url, string $title, str
|
||||
'symbol' => $symbol,
|
||||
'pillars' => $pillars,
|
||||
'markers' => $markers,
|
||||
'pillar_ids' => [],
|
||||
'kind_id' => null,
|
||||
'since' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public function visible(LicenseArticle $article, array $offerIds, ?Carbon $now =
|
||||
}
|
||||
|
||||
/** Zeitpunkt der ersten Fassung eines Angebots als Kauf. */
|
||||
private function firstOffered(array $offerIds): ?Carbon
|
||||
public function firstOffered(array $offerIds): ?Carbon
|
||||
{
|
||||
if ($offerIds === []) {
|
||||
return null;
|
||||
|
||||
@@ -28,6 +28,11 @@ public function claim(User $user, Offer $offer): Acquisition
|
||||
throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.');
|
||||
}
|
||||
|
||||
// Wer ein Angebot auf Abruf übernehmen darf, bestimmt seine Regel (ADR-56); „bei Registrierung“ kennt keine.
|
||||
if ($version->grant_kind === 'auf_abruf') {
|
||||
app(OfferAccess::class)->assertMayClaim($user, $offer);
|
||||
}
|
||||
|
||||
return $this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $offer->id);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Entitlement;
|
||||
|
||||
use App\Domain\Catalog\ArticleRelease;
|
||||
use App\Domain\Commerce\ProductOptions;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Neue Stände zu Artikeln, die das Konto bereits besitzt („Neu für dich“, ADR-56).
|
||||
*
|
||||
* Nach der Updatepolitik (ADR-09, OPEN-01) stehen alle später veröffentlichten Stände desselben Artikels dem Inhaber
|
||||
* ohne Aufpreis zu, solange die Lizenz gilt. Sie erscheinen von selbst in der Bibliothek; diese Liste macht sie nur
|
||||
* sichtbar. Es wird nichts vergeben und nichts übernommen.
|
||||
*/
|
||||
class NewStands
|
||||
{
|
||||
/** So lange (in Tagen) nach der Veröffentlichung erscheint ein neuer Stand in der Liste. */
|
||||
public const DAYS = 90;
|
||||
|
||||
/**
|
||||
* @return Collection<int, array{grant: Grant, title: string, number: int, since: CarbonInterface}>
|
||||
*/
|
||||
public function forUser(User $user): Collection
|
||||
{
|
||||
if ($user->party_id === null) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$limit = now()->subDays(self::DAYS);
|
||||
$titles = app(ProductOptions::class);
|
||||
|
||||
return Grant::query()
|
||||
->with(['article.currentRelease', 'releaseAtGrant'])
|
||||
->where('status', GrantStatus::Aktiv)
|
||||
->where(fn ($query) => $query->whereNull('valid_until')->orWhere('valid_until', '>', now()))
|
||||
->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id))
|
||||
->whereHas('article', fn ($query) => $query->whereNull('archived_at'))
|
||||
->orderByDesc('id')
|
||||
->get()
|
||||
->filter(function (Grant $grant) use ($limit): bool {
|
||||
$current = $grant->article?->currentRelease;
|
||||
$had = $grant->releaseAtGrant;
|
||||
|
||||
return $current instanceof ArticleRelease
|
||||
&& $had instanceof ArticleRelease
|
||||
&& (int) $current->number > (int) $had->number
|
||||
&& $current->created_at !== null
|
||||
&& $current->created_at->greaterThanOrEqualTo($limit);
|
||||
})
|
||||
->unique('license_article_id')
|
||||
->map(fn (Grant $grant): array => [
|
||||
'grant' => $grant,
|
||||
'title' => $titles->articleTitle($grant->article),
|
||||
'number' => (int) $grant->article->currentRelease->number,
|
||||
'since' => $grant->article->currentRelease->created_at,
|
||||
])
|
||||
->values();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Entitlement;
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
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\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Wer ein Angebot „auf Abruf“ in die Bibliothek übernehmen darf (ADR-56). Drei Regeln, serverseitig beim Abruf geprüft:
|
||||
* alle angemeldeten Mitglieder, nur ausdrücklich freigeschaltete Konten (Freigabe je Angebot und Konto, wie bei
|
||||
* Rabattcodes, nie gelöscht, nur entzogen) oder Mitglieder, die einen bestimmten anderen Artikel besitzen.
|
||||
*
|
||||
* Die Regel gilt nur für Vergaben „auf Abruf“. „Bei Registrierung“ geht automatisch an neue Konten; Kauf ist Sache der
|
||||
* Kasse. Die Regel begründet keinen Zugang, sie begrenzt nur den freien Abruf.
|
||||
*/
|
||||
class OfferAccess
|
||||
{
|
||||
public const ALL = 'alle';
|
||||
|
||||
public const RELEASED = 'konten';
|
||||
|
||||
public const OWNERS = 'besitz';
|
||||
|
||||
public const OPTIONS = [
|
||||
self::ALL => 'Alle angemeldeten Mitglieder',
|
||||
self::RELEASED => 'Nur freigeschaltete Konten',
|
||||
self::OWNERS => 'Mitglieder, die einen bestimmten Artikel besitzen',
|
||||
];
|
||||
|
||||
public function __construct(private readonly OfferService $offers) {}
|
||||
|
||||
/**
|
||||
* Die Regel eines Angebots festlegen. Der Artikel ist nur bei „besitzt einen Artikel“ gesetzt.
|
||||
*/
|
||||
public function setRule(Offer $offer, string $access, ?int $articleId = null): void
|
||||
{
|
||||
if (! array_key_exists($access, self::OPTIONS)) {
|
||||
throw new DomainRuleException('Diese Regel für den Abruf gibt es nicht.');
|
||||
}
|
||||
|
||||
if ($access === self::OWNERS) {
|
||||
$article = $articleId === null ? null : LicenseArticle::query()->whereNull('archived_at')->find($articleId);
|
||||
|
||||
if ($article === null) {
|
||||
throw new DomainRuleException('Für diese Regel braucht es einen vorhandenen, nicht archivierten Lizenzartikel.');
|
||||
}
|
||||
} else {
|
||||
$articleId = null;
|
||||
}
|
||||
|
||||
$offer->claim_access = $access;
|
||||
$offer->claim_requires_article_id = $articleId;
|
||||
$offer->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Warum das Konto das Angebot nicht abrufen darf; null, wenn es das darf. Regeln gelten nur für Vergaben „auf Abruf“.
|
||||
*/
|
||||
public function refusal(User $user, Offer $offer, ?CarbonInterface $now = null): ?string
|
||||
{
|
||||
$now ??= now();
|
||||
$version = $this->activeVersion($offer);
|
||||
|
||||
if ($version === null || ! in_array($version->grant_kind, ['auf_abruf', 'bei_registrierung'], true) || $version->holder_kind !== 'person') {
|
||||
return 'Dieses Angebot wird nicht frei vergeben.';
|
||||
}
|
||||
|
||||
if ($user->party_id === null) {
|
||||
return 'Nur ein Mitgliedskonto kann ein Angebot abrufen.';
|
||||
}
|
||||
|
||||
// „Bei Registrierung“ geht automatisch an neue Konten und kennt keine Regel.
|
||||
if ($version->grant_kind !== 'auf_abruf') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return match ((string) $offer->claim_access) {
|
||||
self::RELEASED => $this->activeRelease($offer, $user, $now) === null ? 'Dieses Angebot ist für dein Konto nicht freigeschaltet.' : null,
|
||||
self::OWNERS => $offer->claim_requires_article_id !== null && $this->owns($user, (int) $offer->claim_requires_article_id, $now) ? null : 'Dieses Angebot steht für dein Konto nicht zur Verfügung.',
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function mayClaim(User $user, Offer $offer, ?CarbonInterface $now = null): bool
|
||||
{
|
||||
return $this->refusal($user, $offer, $now) === null;
|
||||
}
|
||||
|
||||
public function assertMayClaim(User $user, Offer $offer): void
|
||||
{
|
||||
$reason = $this->refusal($user, $offer);
|
||||
|
||||
if ($reason !== null) {
|
||||
throw new DomainRuleException($reason);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Angebote auf Abruf, die das Konto jetzt übernehmen darf und noch nicht übernommen hat.
|
||||
*
|
||||
* @return Collection<int, Offer>
|
||||
*/
|
||||
public function claimable(User $user): Collection
|
||||
{
|
||||
if ($user->party_id === null) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$taken = Acquisition::query()
|
||||
->where('party_id', $user->party_id)
|
||||
->where('type', AcquisitionType::FreierAbruf)
|
||||
->where('reference_type', 'offer')
|
||||
->pluck('reference_id')
|
||||
->all();
|
||||
|
||||
return Offer::query()
|
||||
->whereHas('versions', fn ($query) => $query->where('status', OfferStatus::Aktiv)->where('holder_kind', 'person')->whereIn('grant_kind', ['auf_abruf', 'bei_registrierung']))
|
||||
->whereNotIn('id', $taken)
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->filter(fn (Offer $offer): bool => $this->mayClaim($user, $offer))
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein Konto für ein Angebot freischalten (Regel „nur freigeschaltete Konten“).
|
||||
*/
|
||||
public function release(Offer $offer, User $member, ?User $actor, ?CarbonInterface $until = null, ?string $reason = null): OfferRelease
|
||||
{
|
||||
if ($offer->claim_access !== self::RELEASED) {
|
||||
throw new DomainRuleException('Freigaben gibt es nur bei der Regel „nur freigeschaltete Konten“.');
|
||||
}
|
||||
|
||||
if ($member->party_id === null) {
|
||||
throw new DomainRuleException('Nur ein Mitgliedskonto kann für ein Angebot freigeschaltet werden.');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($offer, $member, $actor, $until, $reason): OfferRelease {
|
||||
Offer::query()->whereKey($offer->id)->lockForUpdate()->firstOrFail();
|
||||
|
||||
if ($this->activeRelease($offer, $member) !== null) {
|
||||
throw new DomainRuleException('Dieses Konto ist für das Angebot bereits freigeschaltet.');
|
||||
}
|
||||
|
||||
if ($until !== null && $until->lessThanOrEqualTo(now())) {
|
||||
throw new DomainRuleException('Das Ende der Freigabe liegt in der Vergangenheit.');
|
||||
}
|
||||
|
||||
return OfferRelease::query()->create([
|
||||
'offer_id' => $offer->id,
|
||||
'user_id' => $member->id,
|
||||
'granted_by' => $actor?->id,
|
||||
'granted_at' => now(),
|
||||
'valid_until' => $until,
|
||||
'reason' => filled($reason) ? trim((string) $reason) : null,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Entzieht die Freigabe. Sie bleibt mit Zeitpunkt, Person und Begründung gespeichert; bereits übernommene Angebote
|
||||
* bleiben in der Bibliothek.
|
||||
*/
|
||||
public function revoke(OfferRelease $release, ?User $actor, ?string $reason = null): OfferRelease
|
||||
{
|
||||
return DB::transaction(function () use ($release, $actor, $reason): OfferRelease {
|
||||
$release = OfferRelease::query()->lockForUpdate()->findOrFail($release->id);
|
||||
|
||||
if ($release->revoked_at !== null) {
|
||||
throw new DomainRuleException('Diese Freigabe ist bereits entzogen.');
|
||||
}
|
||||
|
||||
$release->forceFill([
|
||||
'revoked_at' => now(),
|
||||
'revoked_by' => $actor?->id,
|
||||
'reason' => filled($reason) ? trim((string) $reason) : $release->reason,
|
||||
])->save();
|
||||
|
||||
return $release;
|
||||
});
|
||||
}
|
||||
|
||||
public function activeRelease(Offer $offer, User $member, ?CarbonInterface $now = null): ?OfferRelease
|
||||
{
|
||||
$now ??= now();
|
||||
|
||||
return OfferRelease::query()
|
||||
->where('offer_id', $offer->id)
|
||||
->where('user_id', $member->id)
|
||||
->whereNull('revoked_at')
|
||||
->where('granted_at', '<=', $now)
|
||||
->where(fn ($query) => $query->whereNull('valid_until')->orWhere('valid_until', '>', $now))
|
||||
->first();
|
||||
}
|
||||
|
||||
/** Das Konto besitzt den Artikel, wenn seine Person eine aktive, gültige Berechtigung darauf hat. */
|
||||
public function owns(User $user, int $articleId, ?CarbonInterface $now = null): bool
|
||||
{
|
||||
$now ??= now();
|
||||
|
||||
if ($user->party_id === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Grant::query()
|
||||
->where('license_article_id', $articleId)
|
||||
->where('status', GrantStatus::Aktiv)
|
||||
->where(fn ($query) => $query->whereNull('valid_from')->orWhere('valid_from', '<=', $now))
|
||||
->where(fn ($query) => $query->whereNull('valid_until')->orWhere('valid_until', '>', $now))
|
||||
->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id))
|
||||
->exists();
|
||||
}
|
||||
|
||||
private function activeVersion(Offer $offer): ?OfferVersion
|
||||
{
|
||||
return $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user