Ergänze das U1-Fachmodell für Inhalt, Paket und Angebot.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2035c46edd
commit
2ded4b7fad
35 files changed
+2115
No files matched your search
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class OfferService
|
||||
{
|
||||
public function __construct(private readonly BundleService $bundles) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $capabilities
|
||||
* @param array<string, mixed> $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<array{target: LicenseArticle|BundleVersion, rights: RightsProfileVersion}> $lines
|
||||
* @param array<string, mixed> $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<int>
|
||||
*/
|
||||
public function scopeArticleIds(OfferVersion $version): array
|
||||
{
|
||||
return $version->resolvedScopes()->orderBy('id')->pluck('license_article_id')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $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<array{target: LicenseArticle|BundleVersion, rights: RightsProfileVersion}> $lines
|
||||
* @return list<array{license_article_id: int, rights_profile_version_id: int}>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user