$perOffer Kennung der Option => Rabatt in Cent */ public function __construct( public readonly ?DiscountCode $code, public readonly string $state, public readonly ?string $message = null, public readonly array $perOffer = [], ) {} public function applied(): bool { return $this->state === self::OK && $this->totalCents() > 0; } public function totalCents(): int { return $this->state === self::OK ? array_sum($this->perOffer) : 0; } public function forOffer(int $offerId): int { return $this->state === self::OK ? (int) ($this->perOffer[$offerId] ?? 0) : 0; } /** Für die Bestellung festgehaltene Angaben zum Zeitpunkt der Bestellung. */ public function snapshot(): array { return [ 'code' => $this->code?->code, 'label' => $this->code?->label, 'kind' => $this->code?->kind, 'percent' => $this->code?->percent, 'amount_cents' => $this->code?->amount_cents, 'scope' => $this->code?->scope, 'eligibility' => $this->code?->eligibility, 'discount_cents' => $this->totalCents(), ]; } /** Teil der Prüfsumme der Bestellübersicht. */ public function digestPart(): string { $parts = []; $perOffer = $this->perOffer; ksort($perOffer); foreach ($perOffer as $offerId => $cents) { $parts[] = $offerId.'='.$cents; } return ($this->code?->id ?? 0).':'.$this->state.':'.implode(',', $parts); } }