- 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.
245 lines
7.3 KiB
PHP
245 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce;
|
|
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\OfferVersion;
|
|
use App\Domain\Identity\Party;
|
|
use App\Domain\Identity\PartyType;
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* Sitzungsgebundener Mehrangebotswarenkorb (Etappe 1). Er bleibt in derselben Browsersitzung über Anmeldung,
|
|
* Registrierung und E-Mail-Bestätigung erhalten. Je Option gibt es genau eine Position. Optionen derselben Produktseite
|
|
* (gleiche Komposition) sind Alternativen und werden nur nach ausdrücklichem Wechsel ersetzt.
|
|
* Der Warenkorb reserviert weder Preis noch Konditionen; er merkt sich, was der Käufer gesehen hat.
|
|
*/
|
|
class Cart
|
|
{
|
|
public const KEY = 'shop.cart';
|
|
|
|
public const ADDED = 'added';
|
|
|
|
public const PRESENT = 'present';
|
|
|
|
public const ALTERNATIVE = 'alternative';
|
|
|
|
public const UNAVAILABLE = 'unavailable';
|
|
|
|
public function __construct(
|
|
private readonly OwnershipCheck $ownership,
|
|
private readonly OrderHolders $holders,
|
|
private readonly OfferService $offers,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<int, array{version_id: int, price_cents: int}>
|
|
*/
|
|
public function items(): array
|
|
{
|
|
return (array) session()->get(self::KEY, []);
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return count($this->items());
|
|
}
|
|
|
|
public function has(Offer $offer): bool
|
|
{
|
|
return isset($this->items()[$offer->id]);
|
|
}
|
|
|
|
/**
|
|
* Die bereits enthaltene Alternative derselben Produktseite, falls vorhanden.
|
|
*/
|
|
public function alternativeTo(Offer $offer): ?Offer
|
|
{
|
|
if ($offer->composition_id === null) {
|
|
return null;
|
|
}
|
|
|
|
$ids = array_diff(array_keys($this->items()), [$offer->id]);
|
|
|
|
return $ids === [] ? null : Offer::query()
|
|
->whereIn('id', $ids)
|
|
->where('composition_id', $offer->composition_id)
|
|
->first();
|
|
}
|
|
|
|
public function add(Offer $offer): string
|
|
{
|
|
$version = $this->purchasable($offer);
|
|
|
|
if ($version === null) {
|
|
return self::UNAVAILABLE;
|
|
}
|
|
|
|
if ($this->has($offer)) {
|
|
return self::PRESENT;
|
|
}
|
|
|
|
if ($this->alternativeTo($offer) !== null) {
|
|
return self::ALTERNATIVE;
|
|
}
|
|
|
|
$this->put($offer, $version);
|
|
|
|
return self::ADDED;
|
|
}
|
|
|
|
/**
|
|
* Ausdrücklicher Wechsel: die Alternative wird durch die gewählte Option ersetzt.
|
|
*/
|
|
public function switchTo(Offer $offer): bool
|
|
{
|
|
$version = $this->purchasable($offer);
|
|
|
|
if ($version === null) {
|
|
return false;
|
|
}
|
|
|
|
$alternative = $this->alternativeTo($offer);
|
|
|
|
if ($alternative !== null) {
|
|
$this->remove($alternative);
|
|
}
|
|
|
|
$this->put($offer, $version);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function remove(Offer $offer): void
|
|
{
|
|
$items = $this->items();
|
|
unset($items[$offer->id]);
|
|
session()->put(self::KEY, $items);
|
|
}
|
|
|
|
/**
|
|
* Der Käufer übernimmt Preis und Fassung, die der Shop jetzt zeigt.
|
|
*/
|
|
public function acceptCurrent(Offer $offer): void
|
|
{
|
|
$version = $this->purchasable($offer);
|
|
|
|
if ($version !== null && $this->has($offer)) {
|
|
$this->put($offer, $version);
|
|
}
|
|
}
|
|
|
|
public function clear(): void
|
|
{
|
|
session()->forget(self::KEY);
|
|
}
|
|
|
|
/**
|
|
* Serverseitige Prüfung: aktuelle Fassung, Preis, Kaufbarkeit, passende Inhaberart, vorhandene Rechte.
|
|
*/
|
|
public function review(?User $user = null, ?int $holderPartyId = null): CartReview
|
|
{
|
|
$offers = Offer::query()
|
|
->whereIn('id', array_keys($this->items()))
|
|
->with('composition')
|
|
->get()
|
|
->keyBy('id');
|
|
$versions = [];
|
|
|
|
foreach ($this->items() as $offerId => $item) {
|
|
$offer = $offers->get($offerId);
|
|
|
|
if ($offer !== null) {
|
|
$versions[$offerId] = $this->purchasable($offer);
|
|
}
|
|
}
|
|
|
|
$holder = $user === null ? null : $this->holderFor($user, $holderPartyId, $versions);
|
|
$lines = [];
|
|
|
|
foreach ($this->items() as $offerId => $item) {
|
|
$offer = $offers->get($offerId);
|
|
|
|
if ($offer === null) {
|
|
continue;
|
|
}
|
|
|
|
$version = $versions[$offerId] ?? null;
|
|
$seen = (int) $item['price_cents'];
|
|
|
|
if ($version === null) {
|
|
$lines[] = new CartLine($offer, null, CartLine::WITHDRAWN, $seen, null,
|
|
'Diese Option ist nicht mehr erhältlich. Entferne sie, um mit den übrigen Positionen fortzufahren.');
|
|
|
|
continue;
|
|
}
|
|
|
|
$current = (int) $version->price_cents;
|
|
|
|
if ($holder !== null && ! $this->suitsHolder($version, $holder)) {
|
|
$lines[] = new CartLine($offer, $version, CartLine::WRONG_HOLDER, $seen, $current,
|
|
$version->holder_kind === 'organisation'
|
|
? 'Diese Option ist für Organisationen. Bestelle als Verein oder entferne sie.'
|
|
: 'Diese Option ist für Privatpersonen. Bestelle als Privatperson oder entferne sie.');
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($holder !== null && $this->ownership->covers($holder, $version)) {
|
|
$lines[] = new CartLine($offer, $version, CartLine::OWNED, $seen, $current);
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($current !== $seen) {
|
|
$lines[] = new CartLine($offer, $version, CartLine::PRICE_CHANGED, $seen, $current);
|
|
} elseif ((int) $item['version_id'] !== $version->id) {
|
|
$lines[] = new CartLine($offer, $version, CartLine::TERMS_CHANGED, $seen, $current,
|
|
'Die Bedingungen dieser Option wurden aktualisiert. Bitte prüfe die Übersicht.');
|
|
} else {
|
|
$lines[] = new CartLine($offer, $version, CartLine::OK, $seen, $current);
|
|
}
|
|
}
|
|
|
|
return new CartReview($lines, $holder);
|
|
}
|
|
|
|
/**
|
|
* Die aktive, als Kauf angebotene Fassung der Option; sonst null (nicht kaufbar oder zurückgezogen).
|
|
*/
|
|
public function purchasable(Offer $offer): ?OfferVersion
|
|
{
|
|
return $this->offers->purchasableVersion($offer);
|
|
}
|
|
|
|
private function put(Offer $offer, OfferVersion $version): void
|
|
{
|
|
$items = $this->items();
|
|
$items[$offer->id] = ['version_id' => $version->id, 'price_cents' => (int) $version->price_cents];
|
|
session()->put(self::KEY, $items);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, OfferVersion|null> $versions
|
|
*/
|
|
private function holderFor(User $user, ?int $partyId, array $versions): ?Party
|
|
{
|
|
$onlyOrganisations = $versions !== [] && collect($versions)
|
|
->filter()
|
|
->every(fn (OfferVersion $version): bool => $version->holder_kind === 'organisation');
|
|
|
|
try {
|
|
return $this->holders->resolve($user, $partyId, $onlyOrganisations);
|
|
} catch (DomainRuleException) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private function suitsHolder(OfferVersion $version, Party $holder): bool
|
|
{
|
|
return ($version->holder_kind === 'organisation') === ($holder->type === PartyType::Organisation);
|
|
}
|
|
}
|