169 lines
5.1 KiB
PHP
169 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Actions\Orders\ApplyPickTimeSlot;
|
|
use App\Actions\Orders\FinalizeOrder;
|
|
use App\Actions\Orders\InitializeOrderTunnel;
|
|
use App\Contracts\Executor;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\Orders\ConfirmOrderRequest;
|
|
use App\Http\Requests\Fo\Orders\PickTimeSlotRequest;
|
|
use App\Http\Requests\Fo\Orders\ValidateSummaryRequest;
|
|
use App\Models\Order;
|
|
use App\Queries\Orders\GetBasketSummaryQuery;
|
|
use App\Queries\Orders\GetBasketSummaryQueryInput;
|
|
use App\Queries\Orders\GetOrderConfirmationQuery;
|
|
use App\Queries\Orders\GetOrderConfirmationQueryInput;
|
|
use App\Queries\Orders\GetPickTimeSlotQuery;
|
|
use App\Queries\Orders\GetPickTimeSlotQueryInput;
|
|
use App\Queries\Orders\GetTunnelOrderConfirmedQuery;
|
|
use App\Queries\Orders\GetTunnelOrderConfirmedQueryInput;
|
|
use Domain\Enums\OrderTunnelStep;
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class TunnelController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService,
|
|
private readonly Executor $executor,
|
|
) {
|
|
}
|
|
|
|
// * -- STEP 1 -- *
|
|
|
|
public function basketSummary(GetBasketSummaryQuery $query): View
|
|
{
|
|
$this->authorize('canReviewBasketSummary', OrderTunnelStep::class);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetBasketSummaryQueryInput()
|
|
);
|
|
|
|
return view('fo.tunnel.basket-summary', [
|
|
'step' => $output->step,
|
|
'basket' => $output->basket,
|
|
]);
|
|
}
|
|
|
|
public function basketSummarySubmit(
|
|
ValidateSummaryRequest $request,
|
|
InitializeOrderTunnel $action
|
|
): RedirectResponse {
|
|
$this->authorize('canReviewBasketSummary', OrderTunnelStep::class);
|
|
|
|
$this->executor->execute(
|
|
$action,
|
|
$request->toInitializeOrderTunnelInput()
|
|
);
|
|
|
|
return to_route('fo.sales.pick-time-slot');
|
|
}
|
|
|
|
// * -- STEP 2 -- *
|
|
|
|
public function pickTimeSlot(GetPickTimeSlotQuery $query): View
|
|
{
|
|
$this->authorize('canPickTimeSlot', OrderTunnelStep::class);
|
|
|
|
$customer = $this->authService->getCustomerAsMandatory();
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetPickTimeSlotQueryInput($customer->getId())
|
|
);
|
|
|
|
return view('fo.tunnel.pick-time-slot', [
|
|
'step' => $output->step,
|
|
'shop' => $output->shop,
|
|
'mapUrl' => $output->mapUrl,
|
|
'timeSlots' => $output->timeSlots,
|
|
]);
|
|
}
|
|
|
|
public function pickTimeSlotSubmit(
|
|
PickTimeSlotRequest $request,
|
|
ApplyPickTimeSlot $action
|
|
): RedirectResponse {
|
|
$this->authorize('canPickTimeSlot', OrderTunnelStep::class);
|
|
|
|
$matchedSlot = $this->executor->execute(
|
|
$action,
|
|
$request->toApplyPickTimeSlotInput()
|
|
);
|
|
|
|
if ($matchedSlot === null) {
|
|
return back()->withErrors([
|
|
'timeslot' => trans('Invalid or unavailable time slot'),
|
|
])->withInput();
|
|
}
|
|
|
|
return to_route('fo.sales.confirmation');
|
|
}
|
|
|
|
// * -- STEP 3 -- *
|
|
|
|
public function confirmation(GetOrderConfirmationQuery $query): View
|
|
{
|
|
$this->authorize('canFinalizeOrder', OrderTunnelStep::class);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetOrderConfirmationQueryInput()
|
|
);
|
|
|
|
return view('fo.tunnel.confirmation', [
|
|
'step' => $output->step,
|
|
'basket' => $output->basket,
|
|
'builder' => $output->builder,
|
|
'shop' => $output->shop,
|
|
]);
|
|
}
|
|
|
|
public function confirmationSubmit(
|
|
ConfirmOrderRequest $request,
|
|
FinalizeOrder $action
|
|
): RedirectResponse {
|
|
$this->authorize('canFinalizeOrder', OrderTunnelStep::class);
|
|
|
|
try {
|
|
/** @var \Domain\Models\ValueObjects\OrderReference $orderReference */
|
|
$orderReference = $this->executor->execute(
|
|
$action,
|
|
$request->toFinalizeOrderInput()
|
|
);
|
|
} catch (WeeklyLimitExceededException | OrderLimitExceededException $e) {
|
|
return back()->with('message', $e->getMessage());
|
|
}
|
|
|
|
return to_route('fo.sales.confirmation.show-confirmed', (string) $orderReference);
|
|
}
|
|
|
|
// * -- CONFIRMATION -- *
|
|
|
|
public function confirmed(
|
|
Order $order,
|
|
GetTunnelOrderConfirmedQuery $query
|
|
): View {
|
|
$this->authorize('viewOwn', $order);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetTunnelOrderConfirmedQueryInput($order)
|
|
);
|
|
|
|
return view('fo.tunnel.confirmed', [
|
|
'step' => $output->step,
|
|
'order' => $output->order,
|
|
'shop' => $output->shop,
|
|
'mapUrl' => $output->mapUrl,
|
|
]);
|
|
}
|
|
}
|