- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation. - Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing. - Add ShopProductPageTest to verify shop product listings, search functionality, and product page details. - Introduce BuildsShop trait for seeding test data related to shop functionality. - Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
95 lines
3.0 KiB
PHP
95 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce;
|
|
|
|
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))
|
|
->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');
|
|
|
|
foreach ($byAcquisition as $grants) {
|
|
$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));
|
|
}
|
|
}
|