Paket 1: eine aktive Angebotsfassung und Bestellbezeichnung
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8cd382c357
commit
7820bececb
13 files changed
+529
-25
No files matched your search
@@ -8,6 +8,20 @@ class OfferService
|
||||
{
|
||||
public function __construct(private readonly BundleService $bundles) {}
|
||||
|
||||
public function currentActiveVersion(Offer $offer): OfferVersion
|
||||
{
|
||||
$version = $offer->versions()
|
||||
->where('status', OfferStatus::Aktiv)
|
||||
->orderByDesc('number')
|
||||
->first();
|
||||
|
||||
if ($version === null) {
|
||||
throw new DomainRuleException('Das Angebot hat keine aktive Fassung.');
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $capabilities
|
||||
* @param array<string, mixed> $options
|
||||
@@ -77,6 +91,13 @@ public function activate(Offer $offer, array $lines, array $attributes = []): Of
|
||||
$scope = $this->scopeFor($lines);
|
||||
|
||||
return DB::transaction(function () use ($offer, $lines, $attributes, $scope): OfferVersion {
|
||||
$offer = Offer::query()->lockForUpdate()->findOrFail($offer->id);
|
||||
|
||||
OfferVersion::query()
|
||||
->where('offer_id', $offer->id)
|
||||
->where('status', OfferStatus::Aktiv)
|
||||
->update(['status' => OfferStatus::Beendet->value]);
|
||||
|
||||
$version = $offer->versions()->create([
|
||||
'number' => ((int) $offer->versions()->max('number')) + 1,
|
||||
'status' => OfferStatus::Entwurf,
|
||||
|
||||
@@ -34,11 +34,24 @@ protected function casts(): array
|
||||
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.');
|
||||
$original = StatusValue::of($version->getOriginal('status'));
|
||||
$next = StatusValue::of($version->status);
|
||||
|
||||
if ($original === OfferStatus::Beendet->value) {
|
||||
throw new DomainRuleException('Eine beendete Angebotsfassung ist unveränderlich.');
|
||||
}
|
||||
|
||||
if ($original === OfferStatus::Aktiv->value) {
|
||||
if ($next !== OfferStatus::Beendet->value) {
|
||||
throw new DomainRuleException('Eine aktive Angebotsfassung lässt sich nur durch Beendigung schließen.');
|
||||
}
|
||||
|
||||
$dirty = $version->getDirty();
|
||||
unset($dirty['status'], $dirty['updated_at']);
|
||||
|
||||
if ($dirty !== []) {
|
||||
throw new DomainRuleException('Eine aktive Angebotsfassung lässt sich nur durch Beendigung schließen.');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Catalog\Offer;
|
||||
use App\Domain\Catalog\OfferStatus;
|
||||
use App\Domain\Catalog\OfferService;
|
||||
use App\Domain\Entitlement\Acquisition;
|
||||
use App\Domain\Entitlement\AcquisitionType;
|
||||
use App\Domain\Entitlement\GrantIssuer;
|
||||
@@ -17,7 +17,10 @@
|
||||
|
||||
class Checkout
|
||||
{
|
||||
public function __construct(private readonly GrantIssuer $grants) {}
|
||||
public function __construct(
|
||||
private readonly GrantIssuer $grants,
|
||||
private readonly OfferService $offers,
|
||||
) {}
|
||||
|
||||
public function place(User $user, Offer $offer): Order
|
||||
{
|
||||
@@ -25,9 +28,9 @@ public function place(User $user, Offer $offer): Order
|
||||
throw new DomainRuleException('Nur ein aktives Mitglied kann kaufen.');
|
||||
}
|
||||
|
||||
$version = $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first();
|
||||
$version = $this->offers->currentActiveVersion($offer);
|
||||
|
||||
if ($version === null || $version->grant_kind !== 'kauf' || $version->holder_kind !== 'person') {
|
||||
if ($version->grant_kind !== 'kauf' || $version->holder_kind !== 'person') {
|
||||
throw new DomainRuleException('Dieses Angebot kann nicht als persönlicher Kauf bestellt werden.');
|
||||
}
|
||||
|
||||
@@ -35,7 +38,7 @@ public function place(User $user, Offer $offer): Order
|
||||
throw new DomainRuleException('Der Kauf braucht einen Preis an der Angebotsfassung.');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($user, $version): Order {
|
||||
return DB::transaction(function () use ($user, $offer, $version): Order {
|
||||
$order = Order::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'party_id' => $user->party_id,
|
||||
@@ -44,6 +47,7 @@ public function place(User $user, Offer $offer): Order
|
||||
]);
|
||||
$line = $order->lines()->create([
|
||||
'offer_version_id' => $version->id,
|
||||
'offer_name' => $offer->name,
|
||||
'price_cents' => $version->price_cents,
|
||||
'term_rule' => $version->term_rule,
|
||||
'update_promise' => $version->update_promise,
|
||||
|
||||
@@ -12,6 +12,7 @@ class OrderLine extends Model
|
||||
protected $fillable = [
|
||||
'order_id',
|
||||
'offer_version_id',
|
||||
'offer_name',
|
||||
'price_cents',
|
||||
'term_rule',
|
||||
'update_promise',
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Catalog\Offer;
|
||||
use App\Domain\Catalog\OfferService;
|
||||
use App\Domain\Catalog\OfferStatus;
|
||||
use App\Domain\Catalog\OfferVersion;
|
||||
use App\Models\User;
|
||||
@@ -11,9 +12,11 @@
|
||||
|
||||
class GrantIssuer
|
||||
{
|
||||
public function __construct(private readonly OfferService $offers) {}
|
||||
|
||||
public function claim(User $user, Offer $offer): Acquisition
|
||||
{
|
||||
$version = $this->activeVersion($offer);
|
||||
$version = $this->offers->currentActiveVersion($offer);
|
||||
|
||||
if (! in_array($version->grant_kind, ['auf_abruf', 'bei_registrierung'], true)) {
|
||||
throw new DomainRuleException('Dieses Angebot wird nicht frei vergeben.');
|
||||
@@ -32,8 +35,9 @@ public function grantRegistrationOffers(User $user): void
|
||||
->where('status', OfferStatus::Aktiv)
|
||||
->where('grant_kind', 'bei_registrierung')
|
||||
->where('holder_kind', 'person')
|
||||
->with('offer')
|
||||
->orderByDesc('number')
|
||||
->get()
|
||||
->unique('offer_id')
|
||||
->each(function (OfferVersion $version) use ($user): void {
|
||||
$this->issue($user, $version, AcquisitionType::FreierAbruf, 'offer', $version->offer_id);
|
||||
});
|
||||
@@ -47,7 +51,7 @@ public function grantManually(User $user, Offer $offer, User $actor, string $rea
|
||||
throw new DomainRuleException('Eine manuelle Vergabe braucht eine Begründung.');
|
||||
}
|
||||
|
||||
$version = $this->activeVersion($offer);
|
||||
$version = $this->offers->currentActiveVersion($offer);
|
||||
|
||||
if ($version->holder_kind !== 'person') {
|
||||
throw new DomainRuleException('Dieses Angebot richtet sich nicht an Personen.');
|
||||
@@ -118,15 +122,4 @@ public function issue(
|
||||
return $acquisition->load('grants');
|
||||
});
|
||||
}
|
||||
|
||||
private function activeVersion(Offer $offer): OfferVersion
|
||||
{
|
||||
$version = $offer->versions()->where('status', OfferStatus::Aktiv)->orderByDesc('number')->first();
|
||||
|
||||
if ($version === null) {
|
||||
throw new DomainRuleException('Das Angebot hat keine aktive Fassung.');
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user