diff --git a/app/Domain/Catalog/ArticleRelease.php b/app/Domain/Catalog/ArticleRelease.php new file mode 100644 index 0000000..4825b42 --- /dev/null +++ b/app/Domain/Catalog/ArticleRelease.php @@ -0,0 +1,78 @@ + 'boolean', + 'status' => ReleaseStatus::class, + ]; + } + + protected static function booted(): void + { + static::updating(function (ArticleRelease $release): void { + $original = StatusValue::of($release->getOriginal('status')); + + if (! in_array($original, [ + ReleaseStatus::Veroeffentlicht->value, + ReleaseStatus::AbgeloestVerfuegbar->value, + ], true)) { + return; + } + + $dirty = array_diff(array_keys($release->getDirty()), ['status', 'updated_at']); + + if ($dirty !== []) { + throw new DomainRuleException('Ein veröffentlichter Artikel-Stand ist unveränderlich.'); + } + + $next = StatusValue::of($release->status); + + if ($original === ReleaseStatus::Veroeffentlicht->value && $next !== ReleaseStatus::AbgeloestVerfuegbar->value) { + throw new DomainRuleException('Ein veröffentlichter Artikel-Stand kann nur abgelöst werden.'); + } + }); + } + + public function article(): BelongsTo + { + return $this->belongsTo(LicenseArticle::class, 'license_article_id'); + } + + public function contents(): HasMany + { + return $this->hasMany(ReleaseContent::class)->orderBy('position'); + } + + public function rootElementVersion(): BelongsTo + { + return $this->belongsTo(ContentElementVersion::class, 'root_element_version_id'); + } + + public function rootCompositionVersion(): BelongsTo + { + return $this->belongsTo(CompositionVersion::class, 'root_composition_version_id'); + } +} diff --git a/app/Domain/Catalog/ArticleService.php b/app/Domain/Catalog/ArticleService.php new file mode 100644 index 0000000..20f00a7 --- /dev/null +++ b/app/Domain/Catalog/ArticleService.php @@ -0,0 +1,83 @@ +create([ + 'code' => $code, + 'product_line' => $productLine !== null && trim($productLine) !== '' ? trim($productLine) : null, + ]); + } + + public function publishRelease( + LicenseArticle $article, + ContentElementVersion|CompositionVersion $root, + string $classification, + ?string $changeNote = null, + ): ArticleRelease { + $classification = trim($classification); + + if ($classification === '') { + throw new DomainRuleException('Ein neuer Stand braucht eine Einordnung, warum er zum selben Artikel gehört.'); + } + + if ($root->status !== ContentVersionStatus::Bereit) { + throw new DomainRuleException('Ein Artikel-Stand kann nur eine bereitgestellte Fassung festschreiben.'); + } + + $rows = $this->resolver->expand($root); + + if ($rows === []) { + throw new DomainRuleException('Ein Artikel-Stand braucht mindestens eine Inhaltsfassung.'); + } + + return DB::transaction(function () use ($article, $root, $classification, $changeNote, $rows): ArticleRelease { + $release = $article->releases()->create([ + 'number' => ((int) $article->releases()->max('number')) + 1, + 'classification_note' => $classification, + 'status' => ReleaseStatus::Entwurf, + 'change_note' => $changeNote, + 'root_element_version_id' => $root instanceof ContentElementVersion ? $root->id : null, + 'root_composition_version_id' => $root instanceof CompositionVersion ? $root->id : null, + ]); + + foreach ($rows as $index => $row) { + $release->contents()->create([ + 'position' => $index + 1, + 'content_element_version_id' => $row['version_id'], + 'role' => $row['role'] !== '' ? $row['role'] : $row['path'], + 'path' => $row['path'], + ]); + } + + ArticleRelease::query() + ->where('license_article_id', $article->id) + ->where('status', ReleaseStatus::Veroeffentlicht) + ->whereKeyNot($release->id) + ->each(function (ArticleRelease $previous): void { + $previous->status = ReleaseStatus::AbgeloestVerfuegbar; + $previous->save(); + }); + + $release->status = ReleaseStatus::Veroeffentlicht; + $release->save(); + $article->current_release_id = $release->id; + $article->save(); + + return $release->load('contents'); + }); + } +} diff --git a/app/Domain/Catalog/Bundle.php b/app/Domain/Catalog/Bundle.php new file mode 100644 index 0000000..ba61e91 --- /dev/null +++ b/app/Domain/Catalog/Bundle.php @@ -0,0 +1,26 @@ + 'datetime', + ]; + } + + public function versions(): HasMany + { + return $this->hasMany(BundleVersion::class); + } +} diff --git a/app/Domain/Catalog/BundleItem.php b/app/Domain/Catalog/BundleItem.php new file mode 100644 index 0000000..c6e6b63 --- /dev/null +++ b/app/Domain/Catalog/BundleItem.php @@ -0,0 +1,46 @@ +whereKey($item->bundle_version_id)->value('status'); + + if (StatusValue::of($status) === BundleStatus::Veroeffentlicht->value) { + throw new DomainRuleException('Eine veröffentlichte Paketfassung nimmt keine weiteren Bestandteile auf.'); + } + }); + + static::updating(function (): void { + throw new DomainRuleException('Bestandteile einer Paketfassung sind unveränderlich.'); + }); + } + + public function bundleVersion(): BelongsTo + { + return $this->belongsTo(BundleVersion::class); + } + + public function article(): BelongsTo + { + return $this->belongsTo(LicenseArticle::class, 'license_article_id'); + } + + public function nestedBundle(): BelongsTo + { + return $this->belongsTo(Bundle::class, 'nested_bundle_id'); + } +} diff --git a/app/Domain/Catalog/BundleService.php b/app/Domain/Catalog/BundleService.php new file mode 100644 index 0000000..4289f7a --- /dev/null +++ b/app/Domain/Catalog/BundleService.php @@ -0,0 +1,137 @@ +create([ + 'name' => $name, + ]); + } + + /** + * @param list $items + */ + public function publish(Bundle $bundle, array $items): BundleVersion + { + if ($items === []) { + throw new DomainRuleException('Ein Paket braucht mindestens einen Bestandteil.'); + } + + $this->assertItems($items); + $this->collectArticleIds($bundle->id, $items); + + return DB::transaction(function () use ($bundle, $items): BundleVersion { + $version = $bundle->versions()->create([ + 'number' => ((int) $bundle->versions()->max('number')) + 1, + 'status' => BundleStatus::Entwurf, + ]); + + foreach (array_values($items) as $index => $item) { + $version->items()->create([ + 'position' => $index + 1, + 'license_article_id' => $item instanceof LicenseArticle ? $item->id : null, + 'nested_bundle_id' => $item instanceof Bundle ? $item->id : null, + ]); + } + + $version->status = BundleStatus::Veroeffentlicht; + $version->save(); + + return $version->load('items'); + }); + } + + /** + * @return list + */ + public function articleIds(BundleVersion $version): array + { + return $this->collectArticleIds($version->bundle_id, $this->targetsOf($version)); + } + + /** + * @param list $items + */ + private function assertItems(array $items): void + { + foreach ($items as $item) { + if (! $item instanceof LicenseArticle && ! $item instanceof Bundle) { + throw new DomainRuleException('Ein Paket enthält nur Lizenzartikel oder andere Pakete.'); + } + } + } + + /** + * @param list $items + * @param list $stack + * @return list + */ + private function collectArticleIds(int $bundleId, array $items, array $stack = []): array + { + if (in_array($bundleId, $stack, true)) { + throw new DomainRuleException('Pakete dürfen keinen Zyklus bilden.'); + } + + $stack[] = $bundleId; + $articleIds = []; + + foreach ($items as $item) { + if ($item instanceof LicenseArticle) { + $articleIds[$item->id] = $item->id; + + continue; + } + + $published = $this->publishedVersion($item); + + if ($published === null) { + throw new DomainRuleException('Ein eingebettetes Paket braucht eine veröffentlichte Fassung.'); + } + + foreach ($this->collectArticleIds($item->id, $this->targetsOf($published), $stack) as $articleId) { + $articleIds[$articleId] = $articleId; + } + } + + return array_values($articleIds); + } + + private function publishedVersion(Bundle $bundle): ?BundleVersion + { + return $bundle->versions() + ->where('status', BundleStatus::Veroeffentlicht) + ->orderByDesc('number') + ->first(); + } + + /** + * @return list + */ + private function targetsOf(BundleVersion $version): array + { + $targets = []; + + foreach ($version->items()->orderBy('position')->get() as $item) { + $target = $item->article ?? $item->nestedBundle; + + if ($target === null) { + throw new DomainRuleException('Ein Paketbestandteil braucht genau ein Ziel.'); + } + + $targets[] = $target; + } + + return $targets; + } +} diff --git a/app/Domain/Catalog/BundleStatus.php b/app/Domain/Catalog/BundleStatus.php new file mode 100644 index 0000000..670004c --- /dev/null +++ b/app/Domain/Catalog/BundleStatus.php @@ -0,0 +1,9 @@ + BundleStatus::class, + ]; + } + + protected static function booted(): void + { + static::updating(function (BundleVersion $version): void { + if (StatusValue::of($version->getOriginal('status')) === BundleStatus::Veroeffentlicht->value) { + throw new DomainRuleException('Eine veröffentlichte Paketfassung ist unveränderlich.'); + } + }); + } + + public function bundle(): BelongsTo + { + return $this->belongsTo(Bundle::class); + } + + public function items(): HasMany + { + return $this->hasMany(BundleItem::class)->orderBy('position'); + } +} diff --git a/app/Domain/Catalog/CompatibilityAttestation.php b/app/Domain/Catalog/CompatibilityAttestation.php new file mode 100644 index 0000000..ebdb261 --- /dev/null +++ b/app/Domain/Catalog/CompatibilityAttestation.php @@ -0,0 +1,37 @@ + 'datetime', + ]; + } + + public function compositionVersion(): BelongsTo + { + return $this->belongsTo(CompositionVersion::class); + } + + public function confirmer(): BelongsTo + { + return $this->belongsTo(User::class, 'confirmed_by'); + } +} diff --git a/app/Domain/Catalog/Composition.php b/app/Domain/Catalog/Composition.php new file mode 100644 index 0000000..25ef2d5 --- /dev/null +++ b/app/Domain/Catalog/Composition.php @@ -0,0 +1,26 @@ + 'datetime', + ]; + } + + public function versions(): HasMany + { + return $this->hasMany(CompositionVersion::class); + } +} diff --git a/app/Domain/Catalog/CompositionMember.php b/app/Domain/Catalog/CompositionMember.php new file mode 100644 index 0000000..c12f9ae --- /dev/null +++ b/app/Domain/Catalog/CompositionMember.php @@ -0,0 +1,47 @@ +whereKey($member->composition_version_id)->value('status'); + + if (StatusValue::of($status) === ContentVersionStatus::Bereit->value) { + throw new DomainRuleException('Eine bestätigte Kompositionsfassung nimmt keine weiteren Mitglieder auf.'); + } + }); + + static::updating(function (): void { + throw new DomainRuleException('Mitglieder einer Komposition werden nicht verändert, sondern durch eine neue Fassung ersetzt.'); + }); + } + + public function compositionVersion(): BelongsTo + { + return $this->belongsTo(CompositionVersion::class); + } + + public function contentElementVersion(): BelongsTo + { + return $this->belongsTo(ContentElementVersion::class); + } + + public function nestedCompositionVersion(): BelongsTo + { + return $this->belongsTo(CompositionVersion::class, 'nested_composition_version_id'); + } +} diff --git a/app/Domain/Catalog/CompositionVersion.php b/app/Domain/Catalog/CompositionVersion.php new file mode 100644 index 0000000..f79a615 --- /dev/null +++ b/app/Domain/Catalog/CompositionVersion.php @@ -0,0 +1,49 @@ + ContentVersionStatus::class, + ]; + } + + protected static function booted(): void + { + static::updating(function (CompositionVersion $version): void { + if (StatusValue::of($version->getOriginal('status')) === ContentVersionStatus::Bereit->value) { + throw new DomainRuleException('Eine bestätigte Kompositionsfassung ist unveränderlich.'); + } + }); + } + + public function composition(): BelongsTo + { + return $this->belongsTo(Composition::class); + } + + public function members(): HasMany + { + return $this->hasMany(CompositionMember::class)->orderBy('position'); + } + + public function attestations(): HasMany + { + return $this->hasMany(CompatibilityAttestation::class); + } +} diff --git a/app/Domain/Catalog/ContentElement.php b/app/Domain/Catalog/ContentElement.php new file mode 100644 index 0000000..81dfd42 --- /dev/null +++ b/app/Domain/Catalog/ContentElement.php @@ -0,0 +1,28 @@ + 'datetime', + ]; + } + + public function versions(): HasMany + { + return $this->hasMany(ContentElementVersion::class); + } +} diff --git a/app/Domain/Catalog/ContentElementVersion.php b/app/Domain/Catalog/ContentElementVersion.php new file mode 100644 index 0000000..0878a63 --- /dev/null +++ b/app/Domain/Catalog/ContentElementVersion.php @@ -0,0 +1,44 @@ + ContentVersionStatus::class, + 'metadata' => 'array', + ]; + } + + protected static function booted(): void + { + static::updating(function (ContentElementVersion $version): void { + if (in_array(StatusValue::of($version->getOriginal('status')), [ + ContentVersionStatus::Bereit->value, + ContentVersionStatus::Gesperrt->value, + ], true)) { + throw new DomainRuleException('Eine bereitgestellte Inhaltsfassung ist unveränderlich.'); + } + }); + } + + public function element(): BelongsTo + { + return $this->belongsTo(ContentElement::class, 'content_element_id'); + } +} diff --git a/app/Domain/Catalog/ContentResolver.php b/app/Domain/Catalog/ContentResolver.php new file mode 100644 index 0000000..4ed1470 --- /dev/null +++ b/app/Domain/Catalog/ContentResolver.php @@ -0,0 +1,61 @@ + + */ + public function expand(ContentElementVersion|CompositionVersion $node, string $path = '', array $stack = []): array + { + if ($node instanceof ContentElementVersion) { + $node->loadMissing('element'); + + return [[ + 'version_id' => $node->id, + 'role' => $path, + 'path' => $path !== '' ? $path : $node->element->working_title, + ]]; + } + + if (in_array($node->composition_id, $stack, true)) { + throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.'); + } + + $stack[] = $node->composition_id; + $rows = []; + + foreach ($node->members()->orderBy('position')->get() as $member) { + $segment = $path === '' ? $member->role : $path.' / '.$member->role; + + if ($member->content_element_version_id !== null) { + $rows[] = [ + 'version_id' => $member->content_element_version_id, + 'role' => $member->role, + 'path' => $segment, + ]; + + continue; + } + + $nested = $member->nestedCompositionVersion; + + if ($nested === null) { + throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.'); + } + + $rows = array_merge($rows, $this->expand($nested, $segment, $stack)); + } + + return $rows; + } + + public function resolvedHash(ContentElementVersion|CompositionVersion $node): string + { + $ids = array_values(array_unique(array_column($this->expand($node), 'version_id'))); + sort($ids); + + return hash('sha256', json_encode($ids, JSON_THROW_ON_ERROR)); + } +} diff --git a/app/Domain/Catalog/ContentService.php b/app/Domain/Catalog/ContentService.php new file mode 100644 index 0000000..ec6da25 --- /dev/null +++ b/app/Domain/Catalog/ContentService.php @@ -0,0 +1,196 @@ +create([ + 'kind' => $this->required($kind, 'Die Art des Inhaltselements fehlt.'), + 'output_class' => $this->required($outputClass, 'Die Ausgabeklasse fehlt.'), + 'working_title' => $this->required($workingTitle, 'Der Arbeitstitel fehlt.'), + ]); + } + + public function publishElementVersion( + ContentElement $element, + ?string $body = null, + array $metadata = [], + ?string $changeNote = null, + ): ContentElementVersion { + if (($body === null || $body === '') && $metadata === []) { + throw new DomainRuleException('Eine Inhaltsfassung braucht einen Inhalt.'); + } + + $number = ((int) $element->versions()->max('number')) + 1; + + return $element->versions()->create([ + 'number' => $number, + 'status' => ContentVersionStatus::Bereit, + 'content_hash' => hash('sha256', json_encode([ + 'body' => $body, + 'metadata' => $metadata, + ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)), + 'body' => $body, + 'metadata' => $metadata === [] ? null : $metadata, + 'change_note' => $changeNote, + ]); + } + + /** + * @param list $members + */ + public function publishComposition( + Composition $composition, + array $members, + ?User $confirmedBy = null, + ?string $note = null, + ): CompositionVersion { + if ($members === []) { + throw new DomainRuleException('Eine Komposition braucht mindestens ein Mitglied.'); + } + + $this->assertAcyclic($composition->id, $members); + + foreach ($members as $member) { + $this->assertReadyMember($member['target']); + } + + return DB::transaction(function () use ($composition, $members, $confirmedBy, $note): CompositionVersion { + $number = ((int) $composition->versions()->max('number')) + 1; + $version = $composition->versions()->create([ + 'number' => $number, + 'status' => ContentVersionStatus::Entwurf, + 'members_hash' => $this->membersHash($members), + 'resolved_hash' => 'pending', + ]); + + foreach (array_values($members) as $index => $member) { + $target = $member['target']; + $version->members()->create([ + 'position' => $index + 1, + 'role' => $this->required($member['role'], 'Die Rolle eines Mitglieds fehlt.'), + 'content_element_version_id' => $target instanceof ContentElementVersion ? $target->id : null, + 'nested_composition_version_id' => $target instanceof CompositionVersion ? $target->id : null, + ]); + } + + $resolvedHash = $this->resolver->resolvedHash($version); + $version->attestations()->create([ + 'resolved_hash' => $resolvedHash, + 'confirmed_by' => $confirmedBy?->id, + 'confirmed_at' => now(), + 'note' => $note, + ]); + $version->resolved_hash = $resolvedHash; + $version->status = ContentVersionStatus::Bereit; + $version->save(); + + return $version->load('members'); + }); + } + + public function createComposition(string $designation): Composition + { + return Composition::query()->create([ + 'designation' => $this->required($designation, 'Die Bezeichnung der Komposition fehlt.'), + ]); + } + + /** + * @return list + */ + public function containingCompositionIds(ContentElementVersion $version): array + { + $ids = []; + + foreach (CompositionVersion::query()->where('status', ContentVersionStatus::Bereit)->get() as $compositionVersion) { + $contained = array_column($this->resolver->expand($compositionVersion), 'version_id'); + + if (in_array($version->id, $contained, true)) { + $ids[] = $compositionVersion->id; + } + } + + return $ids; + } + + /** + * @param list $members + * @param list $stack + */ + private function assertAcyclic(int $compositionId, array $members, array $stack = []): void + { + if (in_array($compositionId, $stack, true)) { + throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.'); + } + + $stack[] = $compositionId; + + foreach ($members as $member) { + $target = $member['target']; + + if (! $target instanceof CompositionVersion) { + continue; + } + + $nested = []; + + foreach ($target->members()->orderBy('position')->get() as $row) { + $child = $row->contentElementVersion ?? $row->nestedCompositionVersion; + + if ($child === null) { + throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.'); + } + + $nested[] = ['role' => $row->role, 'target' => $child]; + } + + $this->assertAcyclic($target->composition_id, $nested, $stack); + } + } + + private function assertReadyMember(ContentElementVersion|CompositionVersion $target): void + { + if ($target->status !== ContentVersionStatus::Bereit) { + throw new DomainRuleException('Eine Komposition darf nur bereitgestellte Fassungen enthalten.'); + } + } + + /** + * @param list $members + */ + private function membersHash(array $members): string + { + $list = []; + + foreach (array_values($members) as $index => $member) { + $target = $member['target']; + $list[] = [ + 'position' => $index + 1, + 'role' => $member['role'], + 'element_version_id' => $target instanceof ContentElementVersion ? $target->id : null, + 'composition_version_id' => $target instanceof CompositionVersion ? $target->id : null, + ]; + } + + return hash('sha256', json_encode($list, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)); + } + + private function required(string $value, string $message): string + { + $value = trim($value); + + if ($value === '') { + throw new DomainRuleException($message); + } + + return $value; + } +} diff --git a/app/Domain/Catalog/ContentVersionStatus.php b/app/Domain/Catalog/ContentVersionStatus.php new file mode 100644 index 0000000..e61b995 --- /dev/null +++ b/app/Domain/Catalog/ContentVersionStatus.php @@ -0,0 +1,10 @@ + 'datetime', + ]; + } + + public function releases(): HasMany + { + return $this->hasMany(ArticleRelease::class); + } + + public function currentRelease(): BelongsTo + { + return $this->belongsTo(ArticleRelease::class, 'current_release_id'); + } + + public function successorOf(): BelongsTo + { + return $this->belongsTo(self::class, 'successor_of_id'); + } +} diff --git a/app/Domain/Catalog/Offer.php b/app/Domain/Catalog/Offer.php new file mode 100644 index 0000000..41f38d8 --- /dev/null +++ b/app/Domain/Catalog/Offer.php @@ -0,0 +1,18 @@ +hasMany(OfferVersion::class); + } +} diff --git a/app/Domain/Catalog/OfferLine.php b/app/Domain/Catalog/OfferLine.php new file mode 100644 index 0000000..0af23e6 --- /dev/null +++ b/app/Domain/Catalog/OfferLine.php @@ -0,0 +1,55 @@ +whereKey($line->offer_version_id)->value('status'); + + if (in_array(StatusValue::of($status), [ + OfferStatus::Aktiv->value, + OfferStatus::Beendet->value, + ], true)) { + throw new DomainRuleException('Eine aktivierte Angebotsfassung nimmt keine weiteren Positionen auf.'); + } + }); + + static::updating(function (): void { + throw new DomainRuleException('Positionen einer Angebotsfassung sind unveränderlich.'); + }); + } + + public function offerVersion(): BelongsTo + { + return $this->belongsTo(OfferVersion::class); + } + + public function article(): BelongsTo + { + return $this->belongsTo(LicenseArticle::class, 'license_article_id'); + } + + public function bundleVersion(): BelongsTo + { + return $this->belongsTo(BundleVersion::class); + } + + public function rights(): BelongsTo + { + return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id'); + } +} diff --git a/app/Domain/Catalog/OfferService.php b/app/Domain/Catalog/OfferService.php new file mode 100644 index 0000000..44fdc8f --- /dev/null +++ b/app/Domain/Catalog/OfferService.php @@ -0,0 +1,182 @@ + $capabilities + * @param array $options + */ + public function createRightsProfile( + string $name, + string $purpose, + array $capabilities, + array $options = [], + ): RightsProfileVersion { + $purpose = trim($purpose); + + if (! in_array($purpose, ['ehrenamtlich', 'kommerziell', 'institutionell'], true)) { + throw new DomainRuleException('Der Nutzungszweck eines Rechteprofils ist ehrenamtlich, kommerziell oder institutionell.'); + } + + if ($capabilities === []) { + throw new DomainRuleException('Ein Rechteprofil braucht mindestens eine Fähigkeit.'); + } + + $profile = RightsProfile::query()->create([ + 'name' => trim($name) !== '' ? trim($name) : throw new DomainRuleException('Der Name des Rechteprofils fehlt.'), + ]); + + return $profile->versions()->create([ + 'number' => 1, + 'purpose' => $purpose, + 'capabilities' => $capabilities, + 'options' => $options, + ]); + } + + public function create(string $name): Offer + { + $name = trim($name); + + if ($name === '') { + throw new DomainRuleException('Der Name des Angebots fehlt.'); + } + + return Offer::query()->create([ + 'name' => $name, + ]); + } + + /** + * @param list $lines + * @param array $attributes + */ + public function activate(Offer $offer, array $lines, array $attributes = []): OfferVersion + { + if ($lines === []) { + throw new DomainRuleException('Eine Angebotsfassung braucht mindestens eine Position.'); + } + + $attributes = array_merge([ + 'price_cents' => null, + 'tax_class' => null, + 'term_rule' => 'dauerhaft', + 'update_promise' => 'alle_freigegebenen_fassungen', + 'dynamic' => false, + 'grant_kind' => 'kauf', + 'holder_kind' => 'person', + ], $attributes); + + $this->assertAttributes($attributes); + $scope = $this->scopeFor($lines); + + return DB::transaction(function () use ($offer, $lines, $attributes, $scope): OfferVersion { + $version = $offer->versions()->create([ + 'number' => ((int) $offer->versions()->max('number')) + 1, + 'status' => OfferStatus::Entwurf, + 'price_cents' => $attributes['price_cents'], + 'tax_class' => $attributes['tax_class'], + 'term_rule' => $attributes['term_rule'], + 'update_promise' => $attributes['update_promise'], + 'dynamic' => $attributes['dynamic'], + 'grant_kind' => $attributes['grant_kind'], + 'holder_kind' => $attributes['holder_kind'], + ]); + + foreach (array_values($lines) as $index => $line) { + $target = $line['target']; + $version->lines()->create([ + 'position' => $index + 1, + 'license_article_id' => $target instanceof LicenseArticle ? $target->id : null, + 'bundle_version_id' => $target instanceof BundleVersion ? $target->id : null, + 'rights_profile_version_id' => $line['rights']->id, + ]); + } + + foreach ($scope as $entry) { + $version->resolvedScopes()->create($entry); + } + + RightsProfileVersion::query() + ->whereIn('id', array_column($scope, 'rights_profile_version_id')) + ->whereNull('locked_at') + ->update(['locked_at' => now()]); + + $version->status = OfferStatus::Aktiv; + $version->activated_at = now(); + $version->save(); + + return $version->load('resolvedScopes'); + }); + } + + /** + * @return list + */ + public function scopeArticleIds(OfferVersion $version): array + { + return $version->resolvedScopes()->orderBy('id')->pluck('license_article_id')->all(); + } + + /** + * @param array $attributes + */ + private function assertAttributes(array $attributes): void + { + if (! in_array($attributes['term_rule'], ['dauerhaft', 'befristet', 'solange_angebot_aktiv', 'periodisch'], true)) { + throw new DomainRuleException('Die Laufzeitregel der Angebotsfassung ist unbekannt.'); + } + + if (! in_array($attributes['grant_kind'], ['kauf', 'bei_registrierung', 'auf_abruf'], true)) { + throw new DomainRuleException('Die Vergabeart der Angebotsfassung ist unbekannt.'); + } + + if (! in_array($attributes['holder_kind'], ['person', 'organisation'], true)) { + throw new DomainRuleException('Die Inhaberart der Angebotsfassung ist Person oder Organisation.'); + } + + if ($attributes['update_promise'] !== 'alle_freigegebenen_fassungen') { + throw new DomainRuleException('Die Updatezusage umfasst alle später freigegebenen Fassungen des Artikels.'); + } + } + + /** + * @param list $lines + * @return list + */ + private function scopeFor(array $lines): array + { + $scope = []; + + foreach ($lines as $line) { + $target = $line['target']; + $rights = $line['rights']; + + if ($target instanceof BundleVersion && $target->status !== BundleStatus::Veroeffentlicht) { + throw new DomainRuleException('Eine Angebotsfassung enthält nur veröffentlichte Paketfassungen.'); + } + + $articleIds = match (true) { + $target instanceof LicenseArticle => [$target->id], + $target instanceof BundleVersion => $this->bundles->articleIds($target), + default => throw new DomainRuleException('Eine Angebotsposition zeigt auf einen Lizenzartikel oder eine Paketfassung.'), + }; + + foreach ($articleIds as $articleId) { + $key = $articleId.'-'.$rights->id; + $scope[$key] = [ + 'license_article_id' => $articleId, + 'rights_profile_version_id' => $rights->id, + ]; + } + } + + return array_values($scope); + } +} diff --git a/app/Domain/Catalog/OfferStatus.php b/app/Domain/Catalog/OfferStatus.php new file mode 100644 index 0000000..65e7916 --- /dev/null +++ b/app/Domain/Catalog/OfferStatus.php @@ -0,0 +1,10 @@ + OfferStatus::class, + 'dynamic' => 'boolean', + 'activated_at' => 'datetime', + ]; + } + + protected static function booted(): void + { + static::updating(function (OfferVersion $version): void { + if (in_array(StatusValue::of($version->getOriginal('status')), [ + OfferStatus::Aktiv->value, + OfferStatus::Beendet->value, + ], true)) { + throw new DomainRuleException('Eine aktivierte Angebotsfassung ist unveränderlich.'); + } + }); + } + + public function offer(): BelongsTo + { + return $this->belongsTo(Offer::class); + } + + public function lines(): HasMany + { + return $this->hasMany(OfferLine::class)->orderBy('position'); + } + + public function resolvedScopes(): HasMany + { + return $this->hasMany(ResolvedScope::class); + } +} diff --git a/app/Domain/Catalog/ReleaseContent.php b/app/Domain/Catalog/ReleaseContent.php new file mode 100644 index 0000000..bdb3a4c --- /dev/null +++ b/app/Domain/Catalog/ReleaseContent.php @@ -0,0 +1,42 @@ +whereKey($content->article_release_id)->value('status')); + + if ($status !== ReleaseStatus::Entwurf->value) { + throw new DomainRuleException('Der Inhalt eines Artikel-Stands wird nur im Entwurf festgeschrieben.'); + } + }); + + static::updating(function (): void { + throw new DomainRuleException('Der festgeschriebene Inhalt eines Artikel-Stands ist unveränderlich.'); + }); + } + + public function release(): BelongsTo + { + return $this->belongsTo(ArticleRelease::class, 'article_release_id'); + } + + public function elementVersion(): BelongsTo + { + return $this->belongsTo(ContentElementVersion::class, 'content_element_version_id'); + } +} diff --git a/app/Domain/Catalog/ReleaseStatus.php b/app/Domain/Catalog/ReleaseStatus.php new file mode 100644 index 0000000..baaefc6 --- /dev/null +++ b/app/Domain/Catalog/ReleaseStatus.php @@ -0,0 +1,10 @@ +belongsTo(OfferVersion::class); + } + + public function article(): BelongsTo + { + return $this->belongsTo(LicenseArticle::class, 'license_article_id'); + } + + public function rights(): BelongsTo + { + return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id'); + } +} diff --git a/app/Domain/Catalog/RightsProfile.php b/app/Domain/Catalog/RightsProfile.php new file mode 100644 index 0000000..0b4a9db --- /dev/null +++ b/app/Domain/Catalog/RightsProfile.php @@ -0,0 +1,18 @@ +hasMany(RightsProfileVersion::class); + } +} diff --git a/app/Domain/Catalog/RightsProfileVersion.php b/app/Domain/Catalog/RightsProfileVersion.php new file mode 100644 index 0000000..7828615 --- /dev/null +++ b/app/Domain/Catalog/RightsProfileVersion.php @@ -0,0 +1,41 @@ + 'array', + 'options' => 'array', + 'locked_at' => 'datetime', + ]; + } + + protected static function booted(): void + { + static::updating(function (RightsProfileVersion $version): void { + if ($version->getOriginal('locked_at') !== null) { + throw new DomainRuleException('Eine verwendete Rechteprofil-Fassung ist unveränderlich.'); + } + }); + } + + public function profile(): BelongsTo + { + return $this->belongsTo(RightsProfile::class, 'rights_profile_id'); + } +} diff --git a/app/Domain/Catalog/StatusValue.php b/app/Domain/Catalog/StatusValue.php new file mode 100644 index 0000000..aee0342 --- /dev/null +++ b/app/Domain/Catalog/StatusValue.php @@ -0,0 +1,13 @@ +value : $status; + } +} diff --git a/app/Domain/Entitlement/Acquisition.php b/app/Domain/Entitlement/Acquisition.php new file mode 100644 index 0000000..aaae9cf --- /dev/null +++ b/app/Domain/Entitlement/Acquisition.php @@ -0,0 +1,41 @@ + AcquisitionType::class, + 'conditions' => 'array', + ]; + } + + public function party(): BelongsTo + { + return $this->belongsTo(Party::class); + } + + public function grants(): HasMany + { + return $this->hasMany(Grant::class); + } +} diff --git a/app/Domain/Entitlement/AcquisitionType.php b/app/Domain/Entitlement/AcquisitionType.php new file mode 100644 index 0000000..0726618 --- /dev/null +++ b/app/Domain/Entitlement/AcquisitionType.php @@ -0,0 +1,11 @@ + 'array', + 'valid_from' => 'datetime', + 'valid_until' => 'datetime', + 'status' => GrantStatus::class, + ]; + } + + public function acquisition(): BelongsTo + { + return $this->belongsTo(Acquisition::class); + } + + public function article(): BelongsTo + { + return $this->belongsTo(LicenseArticle::class, 'license_article_id'); + } + + public function rights(): BelongsTo + { + return $this->belongsTo(RightsProfileVersion::class, 'rights_profile_version_id'); + } + + public function releaseAtGrant(): BelongsTo + { + return $this->belongsTo(ArticleRelease::class, 'release_at_grant_id'); + } +} diff --git a/app/Domain/Entitlement/GrantStatus.php b/app/Domain/Entitlement/GrantStatus.php new file mode 100644 index 0000000..1a45005 --- /dev/null +++ b/app/Domain/Entitlement/GrantStatus.php @@ -0,0 +1,11 @@ +id(); + $table->string('kind'); + $table->string('output_class'); + $table->string('working_title'); + $table->timestamp('archived_at')->nullable(); + $table->timestamps(); + }); + + Schema::create('content_element_versions', function (Blueprint $table) { + $table->id(); + $table->foreignId('content_element_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('status'); + $table->string('content_hash'); + $table->text('body')->nullable(); + $table->json('metadata')->nullable(); + $table->text('change_note')->nullable(); + $table->timestamps(); + + $table->unique(['content_element_id', 'number']); + }); + + Schema::create('compositions', function (Blueprint $table) { + $table->id(); + $table->string('designation'); + $table->timestamp('archived_at')->nullable(); + $table->timestamps(); + }); + + Schema::create('composition_versions', function (Blueprint $table) { + $table->id(); + $table->foreignId('composition_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('status'); + $table->string('members_hash'); + $table->string('resolved_hash'); + $table->timestamps(); + + $table->unique(['composition_id', 'number']); + }); + + Schema::create('composition_members', function (Blueprint $table) { + $table->id(); + $table->foreignId('composition_version_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('position'); + $table->string('role'); + $table->foreignId('content_element_version_id')->nullable()->constrained()->restrictOnDelete(); + $table->foreignId('nested_composition_version_id')->nullable()->constrained('composition_versions')->restrictOnDelete(); + $table->timestamps(); + + $table->unique(['composition_version_id', 'position']); + }); + + Schema::create('compatibility_attestations', function (Blueprint $table) { + $table->id(); + $table->foreignId('composition_version_id')->constrained()->cascadeOnDelete(); + $table->string('resolved_hash'); + $table->foreignId('confirmed_by')->nullable()->constrained('users')->nullOnDelete(); + $table->timestamp('confirmed_at'); + $table->text('note')->nullable(); + }); + + Schema::create('license_articles', function (Blueprint $table) { + $table->id(); + $table->string('code')->unique(); + $table->string('product_line')->nullable(); + $table->foreignId('successor_of_id')->nullable()->constrained('license_articles')->nullOnDelete(); + $table->timestamp('archived_at')->nullable(); + $table->timestamps(); + }); + + Schema::create('article_releases', function (Blueprint $table) { + $table->id(); + $table->foreignId('license_article_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('change_kind')->nullable(); + $table->boolean('important')->default(false); + $table->text('classification_note'); + $table->string('status'); + $table->text('block_reason')->nullable(); + $table->foreignId('fallback_release_id')->nullable()->constrained('article_releases')->nullOnDelete(); + $table->text('change_note')->nullable(); + $table->foreignId('root_element_version_id')->nullable()->constrained('content_element_versions')->restrictOnDelete(); + $table->foreignId('root_composition_version_id')->nullable()->constrained('composition_versions')->restrictOnDelete(); + $table->timestamps(); + + $table->unique(['license_article_id', 'number']); + }); + + Schema::create('release_contents', function (Blueprint $table) { + $table->id(); + $table->foreignId('article_release_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('position'); + $table->foreignId('content_element_version_id')->constrained()->restrictOnDelete(); + $table->string('role'); + $table->string('path'); + $table->timestamps(); + + $table->unique(['article_release_id', 'position']); + }); + + Schema::table('license_articles', function (Blueprint $table) { + $table->foreignId('current_release_id')->nullable()->constrained('article_releases')->nullOnDelete(); + }); + + Schema::create('bundles', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->timestamp('archived_at')->nullable(); + $table->timestamps(); + }); + + Schema::create('bundle_versions', function (Blueprint $table) { + $table->id(); + $table->foreignId('bundle_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('status'); + $table->timestamps(); + + $table->unique(['bundle_id', 'number']); + }); + + Schema::create('bundle_items', function (Blueprint $table) { + $table->id(); + $table->foreignId('bundle_version_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('position'); + $table->foreignId('license_article_id')->nullable()->constrained()->restrictOnDelete(); + $table->foreignId('nested_bundle_id')->nullable()->constrained('bundles')->restrictOnDelete(); + $table->timestamps(); + + $table->unique(['bundle_version_id', 'position']); + }); + + Schema::create('rights_profiles', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->timestamps(); + }); + + Schema::create('rights_profile_versions', function (Blueprint $table) { + $table->id(); + $table->foreignId('rights_profile_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('purpose'); + $table->json('capabilities'); + $table->json('options'); + $table->timestamp('locked_at')->nullable(); + $table->timestamps(); + + $table->unique(['rights_profile_id', 'number']); + }); + + Schema::create('offers', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->timestamps(); + }); + + Schema::create('offer_versions', function (Blueprint $table) { + $table->id(); + $table->foreignId('offer_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('number'); + $table->string('status'); + $table->unsignedInteger('price_cents')->nullable(); + $table->string('tax_class')->nullable(); + $table->string('term_rule'); + $table->string('update_promise'); + $table->boolean('dynamic')->default(false); + $table->string('grant_kind'); + $table->string('holder_kind'); + $table->timestamp('activated_at')->nullable(); + $table->timestamps(); + + $table->unique(['offer_id', 'number']); + }); + + Schema::create('offer_lines', function (Blueprint $table) { + $table->id(); + $table->foreignId('offer_version_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('position'); + $table->foreignId('license_article_id')->nullable()->constrained()->restrictOnDelete(); + $table->foreignId('bundle_version_id')->nullable()->constrained()->restrictOnDelete(); + $table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete(); + $table->timestamps(); + + $table->unique(['offer_version_id', 'position']); + }); + + Schema::create('resolved_scopes', function (Blueprint $table) { + $table->id(); + $table->foreignId('offer_version_id')->constrained()->cascadeOnDelete(); + $table->foreignId('license_article_id')->constrained()->restrictOnDelete(); + $table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete(); + $table->timestamps(); + + $table->unique(['offer_version_id', 'license_article_id', 'rights_profile_version_id'], 'resolved_scopes_unique'); + }); + + Schema::create('acquisitions', function (Blueprint $table) { + $table->id(); + $table->foreignId('party_id')->constrained()->restrictOnDelete(); + $table->string('type'); + $table->string('reference_type'); + $table->unsignedBigInteger('reference_id'); + $table->json('conditions')->nullable(); + $table->timestamps(); + + $table->unique(['party_id', 'type', 'reference_type', 'reference_id'], 'acquisitions_holder_source_unique'); + }); + + Schema::create('grants', function (Blueprint $table) { + $table->id(); + $table->foreignId('acquisition_id')->constrained()->cascadeOnDelete(); + $table->foreignId('license_article_id')->constrained()->restrictOnDelete(); + $table->foreignId('rights_profile_version_id')->constrained()->restrictOnDelete(); + $table->json('rights_snapshot'); + $table->timestamp('valid_from')->nullable(); + $table->timestamp('valid_until')->nullable(); + $table->foreignId('release_at_grant_id')->nullable()->constrained('article_releases')->nullOnDelete(); + $table->string('status'); + $table->text('status_reason')->nullable(); + $table->timestamps(); + + $table->unique( + ['acquisition_id', 'license_article_id', 'rights_profile_version_id'], + 'grants_acquisition_article_rights_unique', + ); + }); + } + + public function down(): void + { + Schema::dropIfExists('grants'); + Schema::dropIfExists('acquisitions'); + Schema::dropIfExists('resolved_scopes'); + Schema::dropIfExists('offer_lines'); + Schema::dropIfExists('offer_versions'); + Schema::dropIfExists('offers'); + Schema::dropIfExists('rights_profile_versions'); + Schema::dropIfExists('rights_profiles'); + Schema::dropIfExists('bundle_items'); + Schema::dropIfExists('bundle_versions'); + Schema::dropIfExists('bundles'); + Schema::table('license_articles', function (Blueprint $table) { + $table->dropConstrainedForeignId('current_release_id'); + }); + Schema::dropIfExists('release_contents'); + Schema::dropIfExists('article_releases'); + Schema::dropIfExists('license_articles'); + Schema::dropIfExists('compatibility_attestations'); + Schema::dropIfExists('composition_members'); + Schema::dropIfExists('composition_versions'); + Schema::dropIfExists('compositions'); + Schema::dropIfExists('content_element_versions'); + Schema::dropIfExists('content_elements'); + } +}; diff --git a/tests/Feature/CatalogRulesTest.php b/tests/Feature/CatalogRulesTest.php new file mode 100644 index 0000000..9e1aa92 --- /dev/null +++ b/tests/Feature/CatalogRulesTest.php @@ -0,0 +1,278 @@ +element('Arbeitsheft', 'Seite 1'); + $release = app(ArticleService::class)->publishRelease( + $this->article('arbeitsheft'), + $version, + 'Eigenständiges Arbeitsheft, kein Bestandteil einer Übung.', + ); + + $this->assertSame([$version->id], $release->contents->pluck('content_element_version_id')->all()); + $this->assertSame($version->id, $release->root_element_version_id); + } + + public function test_b_composition_fixes_exact_member_versions(): void + { + $film = $this->element('Film', 'Aufnahme'); + $sheet = $this->element('Arbeitsblatt', 'Aufgaben'); + $composition = app(ContentService::class)->publishComposition( + app(ContentService::class)->createComposition('Übung'), + [ + ['role' => 'Film', 'target' => $film], + ['role' => 'Arbeitsblatt', 'target' => $sheet], + ], + ); + $release = app(ArticleService::class)->publishRelease( + $this->article('uebung'), + $composition, + 'Die Übung wird als ein Lizenzartikel geführt.', + ); + + $this->assertSame( + [$film->id, $sheet->id], + $release->contents->pluck('content_element_version_id')->all(), + ); + $this->assertNotNull($composition->attestations()->first()); + } + + public function test_c_one_element_version_can_be_reused(): void + { + $shared = $this->element('Merkblatt', 'Hinweise'); + $first = app(ContentService::class)->publishComposition( + app(ContentService::class)->createComposition('Übung A'), + [['role' => 'Merkblatt', 'target' => $shared]], + ); + $second = app(ContentService::class)->publishComposition( + app(ContentService::class)->createComposition('Übung B'), + [['role' => 'Merkblatt', 'target' => $shared]], + ); + + $this->assertEqualsCanonicalizing( + [$first->id, $second->id], + app(ContentService::class)->containingCompositionIds($shared), + ); + } + + public function test_d_nested_composition_resolves_inner_versions_and_rejects_cycles(): void + { + $sheet = $this->element('Arbeitsblatt', 'Aufgaben'); + $content = app(ContentService::class); + $inner = $content->publishComposition( + $content->createComposition('Materialsatz'), + [['role' => 'Arbeitsblatt', 'target' => $sheet]], + ); + $outer = $content->publishComposition( + $content->createComposition('Übung'), + [['role' => 'Materialsatz', 'target' => $inner]], + ); + $release = app(ArticleService::class)->publishRelease( + $this->article('uebung-mit-satz'), + $outer, + 'Verschachtelte Übung, derselbe Artikel.', + ); + + $this->assertSame(['Materialsatz / Arbeitsblatt'], $release->contents->pluck('path')->all()); + $this->assertSame([$sheet->id], $release->contents->pluck('content_element_version_id')->all()); + + $this->expectException(DomainRuleException::class); + $content->publishComposition( + $inner->composition, + [['role' => 'Übung', 'target' => $outer]], + ); + } + + public function test_e_one_article_can_belong_to_two_bundles(): void + { + $article = $this->publishedArticle('einzel', 'Blatt'); + $bundles = app(BundleService::class); + $first = $bundles->publish($bundles->create('Paket eins'), [$article]); + $second = $bundles->publish($bundles->create('Paket zwei'), [$article]); + + $this->assertSame([$article->id], $bundles->articleIds($first)); + $this->assertSame([$article->id], $bundles->articleIds($second)); + } + + public function test_f_nested_bundles_reject_cycles_and_deduplicate_articles(): void + { + $article = $this->publishedArticle('gemeinsam', 'Blatt'); + $bundles = app(BundleService::class); + $left = $bundles->create('Links'); + $right = $bundles->create('Rechts'); + $bundles->publish($left, [$article]); + $bundles->publish($right, [$article]); + $outer = $bundles->publish($bundles->create('Außen'), [$left, $right]); + + $this->assertSame([$article->id], $bundles->articleIds($outer)); + + $this->expectException(DomainRuleException::class); + $bundles->publish($left, [$outer->bundle]); + } + + public function test_g_a_new_element_version_does_not_change_a_published_composition(): void + { + $content = app(ContentService::class); + $element = $content->createElement('pdf', 'begleitmaterial', 'Anleitung'); + $first = $content->publishElementVersion($element, 'Fassung 1'); + $composition = $content->publishComposition( + $content->createComposition('Übung'), + [['role' => 'Anleitung', 'target' => $first]], + ); + $release = app(ArticleService::class)->publishRelease( + $this->article('anleitung'), + $composition, + 'Korrektur bleibt eine Fassung desselben Artikels.', + ); + + $second = $content->publishElementVersion($element->fresh(), 'Fassung 2'); + $composition->refresh()->load('members'); + + $this->assertSame($first->id, $composition->members->sole()->content_element_version_id); + $this->assertNotSame($second->id, $composition->members->sole()->content_element_version_id); + $this->assertSame( + [$first->id], + $release->fresh()->contents->pluck('content_element_version_id')->all(), + ); + + $this->expectException(DomainRuleException::class); + $first->body = 'stillschweigend geändert'; + $first->save(); + } + + public function test_h_a_bundle_change_does_not_change_an_activated_offer(): void + { + $first = $this->publishedArticle('grundlage', 'Grundlage'); + $second = $this->publishedArticle('erweiterung', 'Erweiterung'); + $bundles = app(BundleService::class); + $bundle = $bundles->create('Programm'); + $published = $bundles->publish($bundle, [$first]); + $rights = $this->rights(); + $offer = app(OfferService::class)->activate( + app(OfferService::class)->create('Programmangebot'), + [['target' => $published, 'rights' => $rights]], + ); + + $bundles->publish($bundle, [$first, $second]); + $offer->refresh(); + + $this->assertSame([$first->id], app(OfferService::class)->scopeArticleIds($offer)); + $this->assertSame([$first->id], $bundles->articleIds($published->fresh())); + $this->assertNotNull($rights->fresh()->locked_at); + + $this->expectException(DomainRuleException::class); + $offer->tax_class = 'unveraendert'; + $offer->save(); + } + + public function test_each_holder_gets_an_own_acquisition_and_one_grant_per_article(): void + { + $article = $this->publishedArticle('frei', 'Kostenloses Blatt'); + $rights = $this->rights(); + $offer = app(OfferService::class)->create('Kostenlos'); + app(OfferService::class)->activate($offer, [ + ['target' => $article, 'rights' => $rights], + ]); + + $acquisitions = []; + + foreach (['Mitglied Eins', 'Mitglied Zwei'] as $name) { + $party = Party::query()->create([ + 'type' => PartyType::Person, + 'display_name' => $name, + ]); + $acquisitions[] = Acquisition::query()->create([ + 'party_id' => $party->id, + 'type' => AcquisitionType::FreierAbruf, + 'reference_type' => 'offer', + 'reference_id' => $offer->id, + ]); + } + + $this->assertCount(2, $acquisitions); + + try { + Acquisition::query()->create([ + 'party_id' => $acquisitions[0]->party_id, + 'type' => AcquisitionType::FreierAbruf, + 'reference_type' => 'offer', + 'reference_id' => $offer->id, + ]); + $this->fail('Derselbe Inhaber darf dasselbe Angebot nicht ein zweites Mal abrufen.'); + } catch (QueryException) { + } + + Grant::query()->create([ + 'acquisition_id' => $acquisitions[0]->id, + 'license_article_id' => $article->id, + 'rights_profile_version_id' => $rights->id, + 'rights_snapshot' => $rights->capabilities, + 'release_at_grant_id' => $article->current_release_id, + 'status' => GrantStatus::Aktiv, + ]); + + $this->expectException(QueryException::class); + Grant::query()->create([ + 'acquisition_id' => $acquisitions[0]->id, + 'license_article_id' => $article->id, + 'rights_profile_version_id' => $rights->id, + 'rights_snapshot' => $rights->capabilities, + 'status' => GrantStatus::Aktiv, + ]); + } + + private function element(string $title, string $body): ContentElementVersion + { + $content = app(ContentService::class); + + return $content->publishElementVersion($content->createElement('text', 'text', $title), $body); + } + + private function article(string $code): LicenseArticle + { + return app(ArticleService::class)->create($code); + } + + private function publishedArticle(string $code, string $title): LicenseArticle + { + $article = $this->article($code); + app(ArticleService::class)->publishRelease( + $article, + $this->element($title, $title), + 'Eigenständiger Artikel.', + ); + + return $article->fresh(); + } + + private function rights(): RightsProfileVersion + { + return app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', [ + 'text' => ['lesen'], + ]); + } +}