- 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.
89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Cms\Catalog\PublicCatalogReader;
|
|
use App\Domain\Commerce\Cart;
|
|
use App\Domain\Commerce\Money;
|
|
use App\Domain\Commerce\OrderHolders;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
/**
|
|
* Öffentlicher Warenkorb (Etappe 1, nur Testzahlung). Er ist vor der Anmeldung nutzbar; die Prüfung der
|
|
* vorhandenen Rechte erfolgt, sobald ein Konto angemeldet ist.
|
|
*/
|
|
class CartController extends Controller
|
|
{
|
|
public function show(Request $request, Cart $cart, OrderHolders $holders): View
|
|
{
|
|
$user = $request->user();
|
|
$review = $cart->review($user, $request->integer('bestellen_als') ?: null);
|
|
|
|
return view('shop.cart', [
|
|
'review' => $review,
|
|
'holders' => $user === null ? collect() : $holders->for($user),
|
|
]);
|
|
}
|
|
|
|
public function add(Offer $offer, Cart $cart, PublicCatalogReader $catalog): RedirectResponse
|
|
{
|
|
$page = $offer->composition;
|
|
$result = $page !== null && $catalog->shownVersion($page) !== null
|
|
? $cart->add($offer)
|
|
: Cart::UNAVAILABLE;
|
|
$back = $this->productPage($offer);
|
|
|
|
return match ($result) {
|
|
Cart::ADDED => $back->with('status', 'Die Option liegt im Warenkorb.'),
|
|
Cart::PRESENT => $back->with('status', 'Diese Option liegt bereits im Warenkorb. Sie erscheint nur einmal.'),
|
|
Cart::ALTERNATIVE => $back->with('shop.switch', $offer->id),
|
|
default => $back->withErrors(['shop' => 'Diese Option ist gerade nicht erhältlich.']),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Ausdrücklicher Wechsel zwischen Alternativoptionen derselben Produktseite.
|
|
*/
|
|
public function switch(Offer $offer, Cart $cart): RedirectResponse
|
|
{
|
|
if (! $cart->switchTo($offer)) {
|
|
return $this->productPage($offer)->withErrors(['shop' => 'Diese Option ist gerade nicht erhältlich.']);
|
|
}
|
|
|
|
return redirect()->route('cart')->with('status', 'Die Option wurde gewechselt.');
|
|
}
|
|
|
|
public function remove(Offer $offer, Cart $cart): RedirectResponse
|
|
{
|
|
$cart->remove($offer);
|
|
|
|
return redirect()->route('cart')->with('status', 'Die Position ist entfernt.');
|
|
}
|
|
|
|
/**
|
|
* Der Käufer übernimmt den aktuell gezeigten Preis und die aktuellen Bedingungen einer Position.
|
|
*/
|
|
public function accept(Offer $offer, Cart $cart): RedirectResponse
|
|
{
|
|
$seen = $cart->items()[$offer->id]['price_cents'] ?? null;
|
|
$cart->acceptCurrent($offer);
|
|
$price = $cart->purchasable($offer)?->price_cents;
|
|
|
|
return redirect()->route('cart')->with('status', match (true) {
|
|
$price === null => 'Die Option ist nicht mehr erhältlich.',
|
|
$seen !== null && (int) $seen !== (int) $price => 'Der aktuelle Preis von '.Money::format($price).' ist übernommen.',
|
|
default => 'Die aktuellen Bedingungen sind übernommen.',
|
|
});
|
|
}
|
|
|
|
private function productPage(Offer $offer): RedirectResponse
|
|
{
|
|
return $offer->composition_id !== null
|
|
? redirect()->route('public.program', $offer->composition_id)
|
|
: redirect()->route('cart');
|
|
}
|
|
}
|