2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Domain\Services;
|
|
|
|
|
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Carbon\CarbonInterface;
|
|
|
|
|
use Carbon\CarbonPeriod;
|
|
|
|
|
use Carbon\WeekDay;
|
|
|
|
|
use Domain\Models\Entities\PickupExceptionOnDate;
|
|
|
|
|
use Domain\Models\Entities\Shop;
|
|
|
|
|
use Domain\Models\ValueObjects\EditablePickupExceptionForShop;
|
|
|
|
|
use Domain\Models\ValueObjects\PickupTimeSlot;
|
2026-11-15 21:39:33 +01:00
|
|
|
use Domain\Models\ValueObjects\PickupWindow;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Models\ValueObjects\ShopPickupOverride;
|
|
|
|
|
use Domain\Ports\Repositories\PickupExceptionRepository;
|
|
|
|
|
|
|
|
|
|
class PickupSchedulingService
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly PickupExceptionRepository $pickupExceptionRepository
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-02 16:35:02 +01:00
|
|
|
protected const WeekDay PICKUP_DAY = WeekDay::Friday;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-02 16:35:02 +01:00
|
|
|
protected const int CUTOFF_WEEK_OFFSET = 0;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-02 16:35:02 +01:00
|
|
|
protected const WeekDay CUTOFF_DAY = WeekDay::Monday;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-02 16:35:02 +01:00
|
|
|
protected const int CUTOFF_HOUR = 14;
|
|
|
|
|
|
|
|
|
|
protected const string CUTOFF_TIME_ZONE = 'Europe/Paris';
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
public function getPickupDay(): WeekDay
|
|
|
|
|
{
|
|
|
|
|
return self::PICKUP_DAY;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-02 16:35:02 +01:00
|
|
|
public function getCutoffTimezone(): string
|
|
|
|
|
{
|
|
|
|
|
return self::CUTOFF_TIME_ZONE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function currentCutoff(): CarbonInterface
|
|
|
|
|
{
|
|
|
|
|
$nextPickup = $this->nextPickup();
|
|
|
|
|
|
|
|
|
|
return $this->getDefaultDeadline($nextPickup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function nextPickup(): CarbonInterface
|
|
|
|
|
{
|
2026-11-16 01:58:44 +01:00
|
|
|
$systemTz = $this->getNow()->getTimezone();
|
|
|
|
|
$nowInParis = $this->getNow()->setTimezone(self::CUTOFF_TIME_ZONE);
|
|
|
|
|
$monday = $nowInParis->startOfWeek(CarbonInterface::MONDAY);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 01:58:44 +01:00
|
|
|
$thisWeekPickup = $monday
|
|
|
|
|
->addDays(self::PICKUP_DAY->value - 1)
|
|
|
|
|
->setTime(self::CUTOFF_HOUR, 0);
|
|
|
|
|
|
|
|
|
|
$thisWeekDeadline = $this->getDefaultDeadline($thisWeekPickup)->setTimezone(self::CUTOFF_TIME_ZONE);
|
|
|
|
|
|
|
|
|
|
$targetPickup = $nowInParis->lessThanOrEqualTo($thisWeekDeadline)
|
|
|
|
|
? $thisWeekPickup
|
|
|
|
|
: $thisWeekPickup->addWeek();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 01:58:44 +01:00
|
|
|
return $targetPickup->setTimezone($systemTz);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPeriodFromPickUpDay(): CarbonPeriod
|
|
|
|
|
{
|
|
|
|
|
$nextPickup = $this->nextPickup();
|
|
|
|
|
|
|
|
|
|
return $this->getPeriodForPickupDate($nextPickup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPeriodForPickupDate(CarbonInterface $pickupDate): CarbonPeriod
|
|
|
|
|
{
|
2026-11-16 01:58:44 +01:00
|
|
|
$systemTz = $this->getNow()->getTimezone();
|
|
|
|
|
$pickupInCutoff = CarbonImmutable::createFromInterface($pickupDate)->setTimezone(self::CUTOFF_TIME_ZONE);
|
|
|
|
|
$defaultDeadLine = $this->getDefaultDeadline($pickupInCutoff);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 01:58:44 +01:00
|
|
|
return CarbonPeriod::create(
|
|
|
|
|
$defaultDeadLine,
|
|
|
|
|
$pickupInCutoff->endOfDay()->setTimezone($systemTz),
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return \Domain\Models\ValueObjects\EditablePickupExceptionForShop[] */
|
|
|
|
|
public function getEditablePickupExceptionsFor(Shop $shop): array
|
|
|
|
|
{
|
|
|
|
|
$allExceptions = $this->pickupExceptionRepository->getAllExceptions();
|
|
|
|
|
$shopExceptions = $this->pickupExceptionRepository->getExceptionsAssociatedWithShop($shop->id());
|
|
|
|
|
|
|
|
|
|
return array_map(function (PickupExceptionOnDate $pickupException) use ($shopExceptions) {
|
|
|
|
|
$associated = null;
|
|
|
|
|
foreach ($shopExceptions as $shopEx) {
|
2026-10-29 18:21:22 +01:00
|
|
|
if ($shopEx->exception->id->equals($pickupException->id)) {
|
2026-06-14 21:32:07 +02:00
|
|
|
$associated = $shopEx;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 20:35:13 +01:00
|
|
|
$isChecked = $associated !== null;
|
|
|
|
|
$isPickupDay = $pickupException->isPickupDay(self::PICKUP_DAY);
|
2026-11-16 01:58:44 +01:00
|
|
|
$defaultDeadline = $this->getDefaultDeadline(
|
|
|
|
|
CarbonImmutable::createFromFormat('!Y-m-d', $pickupException->date->format('Y-m-d'), self::CUTOFF_TIME_ZONE)
|
|
|
|
|
?? $pickupException->date
|
|
|
|
|
);
|
2026-10-29 20:35:13 +01:00
|
|
|
$isBlocked = $associated !== null && ! $associated->hasReplacement();
|
|
|
|
|
$isPastDeadline = $associated !== null && $associated->isDeadlinePassed($this->getNow(), $defaultDeadline);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
return new EditablePickupExceptionForShop(
|
|
|
|
|
$pickupException,
|
|
|
|
|
$isPickupDay,
|
2026-10-29 20:35:13 +01:00
|
|
|
$isChecked && $isPickupDay && $isBlocked,
|
|
|
|
|
$isChecked && $isPickupDay && $isPastDeadline
|
2026-06-14 21:32:07 +02:00
|
|
|
);
|
|
|
|
|
}, $allExceptions);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
public function getPickupWindowFor(Shop $shop, CarbonInterface $targetPickupDay): ?PickupWindow
|
|
|
|
|
{
|
2026-11-16 01:58:44 +01:00
|
|
|
$todayDate = $this->getNow()->setTimezone(self::CUTOFF_TIME_ZONE)->startOfDay();
|
2026-11-15 21:39:33 +01:00
|
|
|
$targetDate = CarbonImmutable::createFromInterface($targetPickupDay)
|
|
|
|
|
->setTimezone(self::CUTOFF_TIME_ZONE)
|
|
|
|
|
->startOfDay();
|
|
|
|
|
|
|
|
|
|
if ($targetDate->lessThanOrEqualTo($todayDate)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coveredPeriod = CarbonPeriod::create($todayDate, $targetDate);
|
|
|
|
|
$pickupExceptionList = $this->pickupExceptionRepository->getExceptionsForShop($shop->id(), $coveredPeriod);
|
|
|
|
|
|
|
|
|
|
return $this->resolveWindowForDate($targetDate, $pickupExceptionList);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Domain\Models\ValueObjects\PickupTimeSlot[] */
|
|
|
|
|
public function getTimeSlotsFor(Shop $shop, \DateTimeInterface $until): array
|
|
|
|
|
{
|
2026-11-16 01:58:44 +01:00
|
|
|
$todayDate = $this->getNow()->setTimezone(self::CUTOFF_TIME_ZONE)->startOfDay();
|
2026-11-02 16:35:02 +01:00
|
|
|
$untilDayDate = CarbonImmutable::createFromInterface($until)->setTimezone(self::CUTOFF_TIME_ZONE)->startOfDay();
|
2026-06-14 21:32:07 +02:00
|
|
|
$coveredPeriod = CarbonPeriod::create($todayDate, $untilDayDate);
|
|
|
|
|
|
|
|
|
|
if ($untilDayDate->lessThanOrEqualTo($todayDate)) {
|
|
|
|
|
throw new \InvalidArgumentException('Cannot get time slots for past days');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pickupExceptionList = $this->pickupExceptionRepository->getExceptionsForShop($shop->id(), $coveredPeriod);
|
|
|
|
|
|
|
|
|
|
$filteredDates = array_filter(
|
|
|
|
|
$coveredPeriod->toArray(),
|
|
|
|
|
fn (CarbonInterface $date) => $date->isDayOfWeek(self::PICKUP_DAY->value)
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
$mappedSlots = array_map(
|
|
|
|
|
fn (CarbonInterface $date): ?PickupTimeSlot => $this->resolveWindowForDate($date, $pickupExceptionList)?->timeSlot,
|
|
|
|
|
$filteredDates,
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
$validSlots = array_filter($mappedSlots);
|
|
|
|
|
usort($validSlots, fn (PickupTimeSlot $a, PickupTimeSlot $b) => $a->getAt() <=> $b->getAt());
|
|
|
|
|
|
|
|
|
|
return $validSlots;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
/**
|
|
|
|
|
* @param \Domain\Models\ValueObjects\ShopPickupOverride[] $pickupExceptionList
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveWindowForDate(CarbonInterface $pickupDayDate, array $pickupExceptionList): ?PickupWindow
|
|
|
|
|
{
|
|
|
|
|
$date = CarbonImmutable::createFromInterface($pickupDayDate);
|
|
|
|
|
|
|
|
|
|
$shopPickupExceptionForTheDate = null;
|
|
|
|
|
foreach ($pickupExceptionList as $ex) {
|
2026-11-16 01:58:44 +01:00
|
|
|
if ($date->format('Y-m-d') === $ex->exception->date->format('Y-m-d')) {
|
2026-11-15 21:39:33 +01:00
|
|
|
$shopPickupExceptionForTheDate = $ex;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $shopPickupExceptionForTheDate !== null
|
|
|
|
|
? $this->resolveWindowForException($shopPickupExceptionForTheDate)
|
|
|
|
|
: $this->resolveWindowForPickupDay($date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function resolveWindowForPickupDay(CarbonInterface $pickupDayDate): ?PickupWindow
|
2026-06-14 21:32:07 +02:00
|
|
|
{
|
|
|
|
|
$defaultDeadline = $this->getDefaultDeadline($pickupDayDate);
|
|
|
|
|
|
|
|
|
|
// ? Have we reached the default standard deadline ?
|
|
|
|
|
return $this->isBefore($defaultDeadline)
|
|
|
|
|
// * Nominal pipeline: timeslot matches standard pickup day
|
2026-11-15 21:39:33 +01:00
|
|
|
? new PickupWindow($this->createTimeSlot($pickupDayDate), $defaultDeadline)
|
2026-06-14 21:32:07 +02:00
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
protected function resolveWindowForException(ShopPickupOverride $shopPickupException): ?PickupWindow
|
2026-06-14 21:32:07 +02:00
|
|
|
{
|
2026-10-29 20:35:13 +01:00
|
|
|
if (! $shopPickupException->hasReplacement() || $shopPickupException->replacementDate === null) {
|
2026-06-14 21:32:07 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 01:58:44 +01:00
|
|
|
$exceptionDate = CarbonImmutable::createFromFormat(
|
|
|
|
|
'!Y-m-d',
|
|
|
|
|
$shopPickupException->exception->date->format('Y-m-d'),
|
|
|
|
|
self::CUTOFF_TIME_ZONE
|
|
|
|
|
) ?? CarbonImmutable::createFromInterface($shopPickupException->exception->date);
|
|
|
|
|
|
|
|
|
|
$defaultDeadline = $this->getDefaultDeadline($exceptionDate);
|
2026-10-29 20:35:13 +01:00
|
|
|
$deadline = $shopPickupException->effectiveDeadline($defaultDeadline);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
return $this->isBefore($deadline)
|
2026-11-15 21:39:33 +01:00
|
|
|
? new PickupWindow($this->createTimeSlot($shopPickupException->replacementDate->toImmutable()), $deadline)
|
2026-06-14 21:32:07 +02:00
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 21:39:33 +01:00
|
|
|
protected function getTimeSlotForPickupDay(CarbonInterface $pickupDayDate): ?PickupTimeSlot
|
|
|
|
|
{
|
|
|
|
|
return $this->resolveWindowForPickupDay($pickupDayDate)?->timeSlot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getTimeSlotForException(ShopPickupOverride $shopPickupException): ?PickupTimeSlot
|
|
|
|
|
{
|
|
|
|
|
return $this->resolveWindowForException($shopPickupException)?->timeSlot;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
protected function createTimeSlot(CarbonInterface $forDate): PickupTimeSlot
|
|
|
|
|
{
|
|
|
|
|
return new PickupTimeSlot(
|
|
|
|
|
$forDate->startOfDay(),
|
|
|
|
|
\DateInterval::createFromDateString('23 hours 59 minutes 59 seconds')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getDefaultDeadline(CarbonInterface $pickupDate): CarbonImmutable
|
|
|
|
|
{
|
2026-11-02 16:35:02 +01:00
|
|
|
$systemTz = $this->getNow()->getTimezone();
|
2026-06-14 21:32:07 +02:00
|
|
|
$deadline = CarbonImmutable::createFromInterface($pickupDate)
|
2026-11-02 16:35:02 +01:00
|
|
|
->setTimezone(self::CUTOFF_TIME_ZONE)
|
2026-06-14 21:32:07 +02:00
|
|
|
->startOfWeek(CarbonInterface::MONDAY)
|
|
|
|
|
->addDays(self::CUTOFF_DAY->value - 1)
|
|
|
|
|
->addHours(self::CUTOFF_HOUR);
|
2026-11-02 16:35:02 +01:00
|
|
|
$deadline = $deadline->setTimezone($systemTz);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-10-28 08:53:34 +01:00
|
|
|
return $this->getCutoffWeekOffset() !== 0
|
|
|
|
|
? $deadline->addWeeks($this->getCutoffWeekOffset())
|
2026-06-14 21:32:07 +02:00
|
|
|
: $deadline;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 08:53:34 +01:00
|
|
|
private function getCutoffWeekOffset(): int
|
|
|
|
|
{
|
|
|
|
|
return self::CUTOFF_WEEK_OFFSET;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
protected function isBefore(CarbonInterface $date): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->getNow()->lessThanOrEqualTo($date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getNow(): CarbonImmutable
|
|
|
|
|
{
|
2026-11-15 21:39:33 +01:00
|
|
|
return CarbonImmutable::now();
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
}
|