AppointmentDomainService.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Services\Booking; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 8 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 9 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 10 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 11 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 12 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 13 | |
| 14 | /** |
| 15 | * Class AppointmentDomainService |
| 16 | * |
| 17 | * @package AmeliaBooking\Domain\Services\Booking |
| 18 | */ |
| 19 | class AppointmentDomainService |
| 20 | { |
| 21 | /** |
| 22 | * Returns an array with bookings statuses count for passed appointment |
| 23 | * |
| 24 | * @param Appointment $appointment |
| 25 | * |
| 26 | * @return array |
| 27 | * @throws InvalidArgumentException |
| 28 | */ |
| 29 | public function getBookingsStatusesCount($appointment) |
| 30 | { |
| 31 | $approvedBookings = 0; |
| 32 | $pendingBookings = 0; |
| 33 | $canceledBookings = 0; |
| 34 | $rejectedBookings = 0; |
| 35 | $noShowBookings = 0; |
| 36 | |
| 37 | foreach ((array)$appointment->getBookings()->keys() as $customerBookingKey) { |
| 38 | /** @var CustomerBooking $booking */ |
| 39 | $booking = $appointment->getBookings()->getItem($customerBookingKey); |
| 40 | |
| 41 | switch ($booking->getStatus()->getValue()) { |
| 42 | case BookingStatus::PENDING: |
| 43 | $pendingBookings += $booking->getPersons()->getValue(); |
| 44 | break; |
| 45 | case BookingStatus::CANCELED: |
| 46 | $canceledBookings += $booking->getPersons()->getValue(); |
| 47 | break; |
| 48 | case BookingStatus::REJECTED: |
| 49 | $rejectedBookings += $booking->getPersons()->getValue(); |
| 50 | break; |
| 51 | case BookingStatus::NO_SHOW: |
| 52 | $noShowBookings += $booking->getPersons()->getValue(); |
| 53 | break; |
| 54 | default: |
| 55 | $approvedBookings += $booking->getPersons()->getValue(); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return [ |
| 61 | 'approvedBookings' => $approvedBookings, |
| 62 | 'pendingBookings' => $pendingBookings, |
| 63 | 'canceledBookings' => $canceledBookings, |
| 64 | 'rejectedBookings' => $rejectedBookings, |
| 65 | 'noShowBookings' => $noShowBookings |
| 66 | ]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param Service $service |
| 71 | * @param array $bookingsCount |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function getAppointmentStatusWhenEditAppointment($service, $bookingsCount) |
| 76 | { |
| 77 | $totalBookings = array_sum($bookingsCount); |
| 78 | |
| 79 | if ($bookingsCount['canceledBookings'] === $totalBookings) { |
| 80 | return BookingStatus::CANCELED; |
| 81 | } |
| 82 | |
| 83 | if ($bookingsCount['noShowBookings'] === $totalBookings) { |
| 84 | return BookingStatus::NO_SHOW; |
| 85 | } |
| 86 | |
| 87 | if ($bookingsCount['rejectedBookings'] === $totalBookings) { |
| 88 | return BookingStatus::REJECTED; |
| 89 | } |
| 90 | |
| 91 | if ($bookingsCount['approvedBookings'] === 0 && $bookingsCount['pendingBookings'] === 0) { |
| 92 | return BookingStatus::CANCELED; |
| 93 | } |
| 94 | |
| 95 | return $bookingsCount['approvedBookings'] >= $service->getMinCapacity()->getValue() ? |
| 96 | BookingStatus::APPROVED : BookingStatus::PENDING; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * When booking status is changed, find out appointment status. |
| 101 | * |
| 102 | * If there is no any more 'approved' and 'pending' bookings, set appointment status to 'canceled' or 'rejected'. |
| 103 | * |
| 104 | * If appointment status is 'approved' or 'pending' and minimum capacity condition is not satisfied, |
| 105 | * set appointment status to 'pending'. |
| 106 | * |
| 107 | * @param Service $service |
| 108 | * @param array $bookingsCount |
| 109 | * @param string $requestedStatus |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function getAppointmentStatusWhenChangingBookingStatus($service, $bookingsCount, $requestedStatus) |
| 114 | { |
| 115 | if ($bookingsCount['approvedBookings'] === 0 && $bookingsCount['pendingBookings'] === 0) { |
| 116 | return $requestedStatus; |
| 117 | } |
| 118 | |
| 119 | return $bookingsCount['approvedBookings'] >= $service->getMinCapacity()->getValue() ? |
| 120 | BookingStatus::APPROVED : BookingStatus::PENDING; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * sort and merge appointments by date-time |
| 125 | * |
| 126 | * @param Collection $appointments |
| 127 | * |
| 128 | * @return Collection |
| 129 | * @throws InvalidArgumentException |
| 130 | */ |
| 131 | public function getSortedAndMergedAppointments($appointments) |
| 132 | { |
| 133 | $timeStampsData = []; |
| 134 | |
| 135 | /** @var Appointment $appointment */ |
| 136 | foreach ($appointments->getItems() as $index => $appointment) { |
| 137 | $timeStampStart = $appointment->getBookingStart()->getValue()->getTimestamp(); |
| 138 | |
| 139 | $timeStampEnd = $appointment->getBookingEnd()->getValue()->getTimestamp(); |
| 140 | |
| 141 | if (!isset($timeStampsData[$timeStampStart])) { |
| 142 | $timeStampsData[$timeStampStart] = [$timeStampEnd, $index]; |
| 143 | } else { |
| 144 | /** @var Appointment $passedAppointment */ |
| 145 | $passedAppointment = $appointments->getItem($timeStampsData[$timeStampStart][1]); |
| 146 | |
| 147 | if ($appointment->getBookingEnd()->getValue() > $passedAppointment->getBookingEnd()->getValue()) { |
| 148 | $timeStampsData[$timeStampStart] = [$timeStampEnd, $index]; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | ksort($timeStampsData); |
| 154 | |
| 155 | $mergedTimeStampsData = []; |
| 156 | |
| 157 | $previousInterval = null; |
| 158 | |
| 159 | foreach ($timeStampsData as $start => $currentInterval) { |
| 160 | if ($previousInterval !== null && $start <= $previousInterval[1]) { |
| 161 | if ($currentInterval[0] > $previousInterval[1]) { |
| 162 | $mergedTimeStampsData[$previousInterval[0]][0] = $currentInterval[0]; |
| 163 | } |
| 164 | } else { |
| 165 | $mergedTimeStampsData[$start] = $currentInterval; |
| 166 | |
| 167 | $previousInterval = [ |
| 168 | $start, |
| 169 | $currentInterval[0], |
| 170 | $currentInterval[1] |
| 171 | ]; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** @var Collection $sortedAndMergedAppointments */ |
| 176 | $sortedAndMergedAppointments = new Collection(); |
| 177 | |
| 178 | foreach ($mergedTimeStampsData as $start => $interval) { |
| 179 | /** @var Appointment $appointment */ |
| 180 | $appointment = $appointments->getItem($interval[1]); |
| 181 | |
| 182 | $appointment->setBookingStart( |
| 183 | new DateTimeValue(DateTimeService::getNowDateTimeObject()->setTimestamp($start)) |
| 184 | ); |
| 185 | |
| 186 | $appointment->setBookingEnd( |
| 187 | new DateTimeValue(DateTimeService::getNowDateTimeObject()->setTimestamp($interval[0])) |
| 188 | ); |
| 189 | |
| 190 | $sortedAndMergedAppointments->addItem($appointment); |
| 191 | } |
| 192 | |
| 193 | return $sortedAndMergedAppointments; |
| 194 | } |
| 195 | } |
| 196 |