Domäne: Rabattcodes mit Geltungsbereich (Warenkorb oder gewählte Ziele) und Einlösungsberechtigung (allgemein oder freigeschaltete Konten) unabhängig voneinander; Prüfung bei Eingabe, in jeder Übersicht und unter Sperre vor Bestellabschluss (Prüfsumme der Übersicht enthält den Rabatt). Rabatt je Position, Zwischensumme und Zahlbetrag in der Bestellung; Stripe erhält den Preis nach Rabatt. Nutzerkreis (Einzel-/Mehrfachlizenz, Trainerzahl) an der Angebotsfassung, kopiert in Bestellposition und Erwerb, Teil der Prüfung "Bereits erworben". OfferService::activate lehnt Angebote ohne veröffentlichten Artikelstand ab. Kasse: Zustimmungen als Pflichtangabe mit Nachweis, "Zahlungspflichtig bestellen", Rechnungsweg für Organisationen, Erstattung über Stripe, Widerruf erklären. Produktivfreigabe SHOP_LIVE_RELEASE standardmäßig aus. Bestandstests geändert (fachlich überholt, Assertions nicht abgeschwächt): die Schaltfläche heißt "Zahlungspflichtig bestellen" (§ 312j BGB), die Summe heißt "Zu zahlen", der Vereinshinweis steht als Rollenliste; Bestellungen über die Kasse senden jetzt die Zustimmungen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce;
|
|
|
|
use App\Domain\Catalog\LicenceScope;
|
|
use App\Domain\Catalog\OfferVersion;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Domain\Identity\Party;
|
|
|
|
/**
|
|
* „Bereits erworben“ (FACH-59): Der vorgesehene Lizenzinhaber hat bereits eine gültige Lizenz mit demselben Umfang
|
|
* und denselben Nutzungsrechten. Maßgeblich ist genau ein Erwerb, der jeden Umfangsposten der Angebotsfassung mit
|
|
* gleichem Zweck, gleichen Fähigkeiten und gleichen Optionen trägt. Rechte mehrerer Erwerbe werden nicht zu einer
|
|
* neuen Lizenz kombiniert. Alle Erwerbsarten zählen (Kauf, freier Abruf, manuelle Vergabe), sofern aktiv und gültig.
|
|
*/
|
|
class OwnershipCheck
|
|
{
|
|
public function covers(Party $holder, OfferVersion $version): bool
|
|
{
|
|
$needed = $this->needed($version);
|
|
|
|
if ($needed === []) {
|
|
return false;
|
|
}
|
|
|
|
$now = now();
|
|
$byAcquisition = Grant::query()
|
|
->where('status', GrantStatus::Aktiv)
|
|
->whereHas('acquisition', fn ($query) => $query->where('party_id', $holder->id))
|
|
->with('acquisition')
|
|
->get()
|
|
->filter(fn (Grant $grant): bool => ($grant->valid_from === null || ! $grant->valid_from->greaterThan($now))
|
|
&& ($grant->valid_until === null || $grant->valid_until->greaterThan($now)))
|
|
->groupBy('acquisition_id');
|
|
|
|
$wanted = LicenceScope::key($version->licence_scope, $version->trainer_count);
|
|
|
|
foreach ($byAcquisition as $grants) {
|
|
$conditions = (array) ($grants->first()->acquisition?->conditions ?? []);
|
|
|
|
// Gleicher Umfang heißt auch gleicher Nutzerkreis: Eine Einzellizenz deckt keine Mehrfachlizenz ab und
|
|
// umgekehrt (der Nutzerkreis wird nicht aus Zweck oder Preis abgeleitet).
|
|
if (LicenceScope::key($conditions['licence_scope'] ?? null, isset($conditions['trainer_count']) ? (int) $conditions['trainer_count'] : null) !== $wanted) {
|
|
continue;
|
|
}
|
|
|
|
$have = [];
|
|
|
|
foreach ($grants as $grant) {
|
|
$have[$grant->license_article_id.'|'.$this->fingerprint($grant->rights_snapshot ?? [])] = true;
|
|
}
|
|
|
|
if (array_diff_key($needed, $have) === []) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, true>
|
|
*/
|
|
private function needed(OfferVersion $version): array
|
|
{
|
|
$needed = [];
|
|
$version->loadMissing('resolvedScopes.rights');
|
|
|
|
foreach ($version->resolvedScopes as $scope) {
|
|
$needed[$scope->license_article_id.'|'.$this->fingerprint([
|
|
'purpose' => $scope->rights->purpose,
|
|
'capabilities' => $scope->rights->capabilities,
|
|
'options' => $scope->rights->options ?? [],
|
|
])] = true;
|
|
}
|
|
|
|
return $needed;
|
|
}
|
|
|
|
/**
|
|
* Reihenfolge von Schlüsseln und Aktionen spielt keine Rolle.
|
|
*
|
|
* @param array<string, mixed> $rights
|
|
*/
|
|
private function fingerprint(array $rights): string
|
|
{
|
|
$capabilities = [];
|
|
|
|
foreach ((array) ($rights['capabilities'] ?? []) as $class => $actions) {
|
|
$actions = array_values(array_unique((array) $actions));
|
|
sort($actions);
|
|
$capabilities[(string) $class] = $actions;
|
|
}
|
|
|
|
ksort($capabilities);
|
|
$options = (array) ($rights['options'] ?? []);
|
|
ksort($options);
|
|
|
|
return md5(json_encode([
|
|
'purpose' => (string) ($rights['purpose'] ?? ''),
|
|
'capabilities' => $capabilities,
|
|
'options' => $options,
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
}
|