offers->currentActiveVersion($offer); if (! in_array($version->grant_kind, ['auf_abruf', 'bei_registrierung'], true)) { throw new DomainRuleException('Dieses Angebot wird nicht frei vergeben.'); } if ($version->holder_kind !== 'person') { throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.'); } return $this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $offer->id); } public function grantRegistrationOffers(User $user): void { OfferVersion::query() ->where('status', OfferStatus::Aktiv) ->where('grant_kind', 'bei_registrierung') ->where('holder_kind', 'person') ->orderByDesc('number') ->get() ->unique('offer_id') ->each(function (OfferVersion $version) use ($user): void { $this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $version->offer_id); }); } public function grantManually(User $user, Offer $offer, User $actor, string $reason): Acquisition { $reason = trim($reason); if ($reason === '') { throw new DomainRuleException('Eine manuelle Vergabe braucht eine Begründung.'); } $version = $this->offers->currentActiveVersion($offer); if ($version->holder_kind !== 'person') { throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.'); } return DB::transaction(function () use ($user, $offer, $actor, $reason, $version): Acquisition { $operation = ManualGrant::query()->create([ 'actor_id' => $actor->id, 'party_id' => $user->party_id, 'offer_id' => $offer->id, 'reason' => $reason, ]); return $this->issue($user, $version, AcquisitionType::ManuelleVergabe, 'manual_grant', $operation->id); }); } /** * Freischaltung einer bezahlten Bestellposition aus deren festgehaltenem Umfang (Umfangskopie und Bedingungen der * Position, nicht aus der Angebotsfassung). Inhaber ist der Lizenzinhaber der Position, das auslösende Konto wird * am Erwerb festgehalten. Wiederholsicher: je Bestellposition genau ein Erwerb. */ public function issueFromOrderLine(OrderLine $line, Party $holder, User $actor): Acquisition { return DB::transaction(function () use ($line, $holder, $actor): Acquisition { $existing = Acquisition::query()->where([ 'party_id' => $holder->id, 'type' => AcquisitionType::Bestellposition, 'reference_type' => 'order_line', 'reference_id' => $line->id, ])->first(); if ($existing !== null) { return $existing->load('grants'); } $acquisition = Acquisition::query()->create([ 'party_id' => $holder->id, 'type' => AcquisitionType::Bestellposition, 'reference_type' => 'order_line', 'reference_id' => $line->id, 'acting_user_id' => $actor->id, 'conditions' => [ 'term_rule' => $line->term_rule, 'update_promise' => $line->update_promise, 'holder_kind' => $line->holder_kind, 'offer_version_id' => $line->offer_version_id, ], ]); $line->loadMissing('scopeItems.article'); foreach ($line->scopeItems as $item) { $acquisition->grants()->create([ 'license_article_id' => $item->license_article_id, 'rights_profile_version_id' => $item->rights_profile_version_id, 'rights_snapshot' => $item->rights_snapshot, 'release_at_grant_id' => $item->article?->current_release_id, 'status' => GrantStatus::Aktiv, 'valid_from' => now(), ]); } return $acquisition->load('grants'); }); } public function issue( User $user, OfferVersion $version, AcquisitionType $type, string $referenceType, int $referenceId, ): Acquisition { return DB::transaction(function () use ($user, $version, $type, $referenceType, $referenceId): Acquisition { $existing = Acquisition::query()->where([ 'party_id' => $user->party_id, 'type' => $type, 'reference_type' => $referenceType, 'reference_id' => $referenceId, ])->first(); if ($existing !== null) { return $existing->load('grants'); } $acquisition = Acquisition::query()->create([ 'party_id' => $user->party_id, 'type' => $type, 'reference_type' => $referenceType, 'reference_id' => $referenceId, 'conditions' => [ 'term_rule' => $version->term_rule, 'update_promise' => $version->update_promise, 'holder_kind' => $version->holder_kind, 'offer_version_id' => $version->id, ], ]); $version->load('resolvedScopes.rights', 'resolvedScopes.article'); foreach ($version->resolvedScopes as $scope) { $rights = $scope->rights; $acquisition->grants()->create([ 'license_article_id' => $scope->license_article_id, 'rights_profile_version_id' => $rights->id, 'rights_snapshot' => [ 'purpose' => $rights->purpose, 'capabilities' => $rights->capabilities, 'options' => $rights->options ?? [], ], 'release_at_grant_id' => $scope->article->current_release_id, 'status' => GrantStatus::Aktiv, 'valid_from' => now(), ]); } return $acquisition->load('grants'); }); } }