ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
GetPackageAppointmentsCommandHandler.php
AddAppointmentCommand.php
1 year ago
AddAppointmentCommandHandler.php
6 months ago
AddBookingCommand.php
1 year ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
1 year ago
ApproveBookingRemotelyCommandHandler.php
6 months ago
CancelBookingCommand.php
1 year ago
CancelBookingCommandHandler.php
6 months ago
CancelBookingRemotelyCommand.php
1 year ago
CancelBookingRemotelyCommandHandler.php
6 months ago
DeleteAppointmentCommand.php
1 year ago
DeleteAppointmentCommandHandler.php
1 year ago
DeleteBookingCommand.php
1 year ago
DeleteBookingCommandHandler.php
10 months ago
DeleteBookingRemotelyCommand.php
10 months ago
DeleteBookingRemotelyCommandHandler.php
6 months ago
GetAppointmentBookingsCommand.php
6 months ago
GetAppointmentBookingsCommandHandler.php
6 months ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
5 months ago
GetAppointmentsCommand.php
1 year ago
GetAppointmentsCommandHandler.php
6 months ago
GetIcsCommand.php
6 months ago
GetIcsCommandHandler.php
4 years ago
GetPackageAppointmentsCommand.php
1 year ago
GetPackageAppointmentsCommandHandler.php
6 months ago
GetTimeSlotsCommand.php
1 year ago
GetTimeSlotsCommandHandler.php
10 months ago
ReassignBookingCommand.php
1 year ago
ReassignBookingCommandHandler.php
10 months ago
RejectBookingRemotelyCommand.php
1 year ago
RejectBookingRemotelyCommandHandler.php
6 months ago
SuccessfulBookingCommand.php
1 year ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
1 year ago
UpdateAppointmentCommandHandler.php
6 months ago
UpdateAppointmentStatusCommand.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
5 months ago
UpdateAppointmentTimeCommand.php
1 year ago
UpdateAppointmentTimeCommandHandler.php
10 months ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
5 months ago
GetPackageAppointmentsCommandHandler.php
283 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Booking\Appointment; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService; |
| 10 | use AmeliaBooking\Application\Services\Booking\BookingApplicationService; |
| 11 | use AmeliaBooking\Domain\Collection\Collection; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 15 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 16 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 17 | use AmeliaBooking\Domain\Entity\Entities; |
| 18 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 19 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 20 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 21 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 22 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageCustomerRepository; |
| 23 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 24 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 25 | |
| 26 | /** |
| 27 | * Class GetPackageAppointmentsCommandHandler |
| 28 | * |
| 29 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 30 | */ |
| 31 | class GetPackageAppointmentsCommandHandler extends CommandHandler |
| 32 | { |
| 33 | /** |
| 34 | * @param GetPackageAppointmentsCommand $command |
| 35 | * |
| 36 | * @return CommandResult |
| 37 | * |
| 38 | * @throws InvalidArgumentException |
| 39 | * @throws QueryExecutionException |
| 40 | * @throws AccessDeniedException |
| 41 | */ |
| 42 | public function handle(GetPackageAppointmentsCommand $command) |
| 43 | { |
| 44 | $result = new CommandResult(); |
| 45 | |
| 46 | /** @var ServiceRepository $serviceRepository */ |
| 47 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 48 | |
| 49 | /** @var PackageCustomerRepository $packageCustomerRepository */ |
| 50 | $packageCustomerRepository = $this->container->get('domain.bookable.packageCustomer.repository'); |
| 51 | |
| 52 | /** @var AbstractPackageApplicationService $packageAS */ |
| 53 | $packageAS = $this->container->get('application.bookable.package'); |
| 54 | |
| 55 | /** @var SettingsService $settingsDS */ |
| 56 | $settingsDS = $this->container->get('domain.settings.service'); |
| 57 | |
| 58 | /** @var BookingApplicationService $bookingAS */ |
| 59 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 60 | |
| 61 | /** @var AppointmentApplicationService $appointmentAS */ |
| 62 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 63 | |
| 64 | try { |
| 65 | /** @var AbstractUser $user */ |
| 66 | $user = $command->getUserApplicationService()->authorization(null, $command->getCabinetType()); |
| 67 | } catch (AuthorizationException $e) { |
| 68 | $result->setResult(CommandResult::RESULT_ERROR); |
| 69 | $result->setData( |
| 70 | [ |
| 71 | 'reauthorize' => true |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | |
| 78 | $params = $command->getField('params'); |
| 79 | |
| 80 | if ($user && $user->getType() === Entities::PROVIDER) { |
| 81 | $params['providers'] = [$user->getId()->getValue()]; |
| 82 | } |
| 83 | |
| 84 | if ($user && $user->getType() === Entities::CUSTOMER) { |
| 85 | $params['customers'] = [$user->getId()->getValue()]; |
| 86 | } |
| 87 | |
| 88 | if (!empty($params['dates'])) { |
| 89 | !empty($params['dates'][0]) ? $params['dates'][0] .= ' 00:00:00' : null; |
| 90 | !empty($params['dates'][1]) ? $params['dates'][1] .= ' 23:59:59' : null; |
| 91 | } |
| 92 | |
| 93 | $availablePackageBookings = []; |
| 94 | |
| 95 | $packageCustomerIds = []; |
| 96 | |
| 97 | $noResultsManagePackagesFilters = false; |
| 98 | |
| 99 | $totalPackagePurchases = 0; |
| 100 | |
| 101 | $customers = isset($params['customerId']) ? [$params['customerId']] : []; |
| 102 | |
| 103 | if (!empty($params['packageStatus']) || !empty($params['page']) || !empty($params['bookingsCount'])) { |
| 104 | $packageCustomerIds = $packageCustomerRepository->getFilteredIds( |
| 105 | [ |
| 106 | 'dates' => !empty($params['dates']) ? $params['dates'] : [], |
| 107 | 'packages' => !empty($params['packageId']) ? [$params['packageId']] : [], |
| 108 | 'page' => !empty($params['page']) ? $params['page'] : null, |
| 109 | 'status' => !empty($params['packageStatus']) ? $params['packageStatus'] : null, |
| 110 | 'customers' => $customers, |
| 111 | ], |
| 112 | $settingsDS->getSetting('general', 'itemsPerPage') |
| 113 | ); |
| 114 | |
| 115 | $noResultsManagePackagesFilters = !$packageCustomerIds; |
| 116 | |
| 117 | $totalPackagePurchases = sizeof($packageCustomerRepository->getFilteredIds( |
| 118 | [ |
| 119 | 'packageCustomerIds' => $packageCustomerIds ?: [], |
| 120 | 'purchased' => !empty($params['dates']) ? $params['dates'] : [], |
| 121 | 'packages' => !empty($params['packageId']) ? [$params['packageId']] : [], |
| 122 | 'packageStatus' => !empty($params['packageStatus']) ? $params['packageStatus'] : null, |
| 123 | 'customers' => $customers |
| 124 | ] |
| 125 | )); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @var Collection $appointments |
| 130 | */ |
| 131 | $appointments = new Collection(); |
| 132 | |
| 133 | if (isset($params['customerId'])) { |
| 134 | unset($params['customerId']); |
| 135 | } |
| 136 | |
| 137 | $customersNoShowCountIds = []; |
| 138 | |
| 139 | $noShowTagEnabled = $settingsDS->isFeatureEnabled('noShowTag'); |
| 140 | |
| 141 | if (!$noResultsManagePackagesFilters) { |
| 142 | $availablePackageBookings = $packageAS->getPackageAvailability( |
| 143 | $appointments, |
| 144 | [ |
| 145 | 'packageCustomerIds' => !empty($packageCustomerIds) ? $packageCustomerIds : [], |
| 146 | 'purchased' => !empty($params['dates']) ? $params['dates'] : [], |
| 147 | 'customers' => $customers, |
| 148 | 'packageId' => !empty($params['packageId']) ? (int)$params['packageId'] : null, |
| 149 | 'managePackagePage' => true |
| 150 | ] |
| 151 | ); |
| 152 | |
| 153 | if ($noShowTagEnabled && !!$availablePackageBookings) { |
| 154 | $customersNoShowCountIds[] = $availablePackageBookings[0]['customerId']; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** @var Collection $services */ |
| 159 | $services = $serviceRepository->getAllArrayIndexedById(); |
| 160 | |
| 161 | $packageAS->setPackageBookingsForAppointments($appointments); |
| 162 | |
| 163 | $occupiedTimes = []; |
| 164 | |
| 165 | $currentDateTime = DateTimeService::getNowDateTimeObject(); |
| 166 | |
| 167 | $groupedAppointments = []; |
| 168 | |
| 169 | /** @var Appointment $appointment */ |
| 170 | foreach ($appointments->getItems() as $appointment) { |
| 171 | /** @var Service $service */ |
| 172 | $service = $services->getItem($appointment->getServiceId()->getValue()); |
| 173 | |
| 174 | $bookingsCount = 0; |
| 175 | |
| 176 | /** @var CustomerBooking $booking */ |
| 177 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 178 | // fix for wrongly saved JSON |
| 179 | if ( |
| 180 | $booking->getCustomFields() && |
| 181 | json_decode($booking->getCustomFields()->getValue(), true) === null |
| 182 | ) { |
| 183 | $booking->setCustomFields(null); |
| 184 | } |
| 185 | |
| 186 | if ($bookingAS->isBookingApprovedOrPending($booking->getStatus()->getValue())) { |
| 187 | $bookingsCount++; |
| 188 | } |
| 189 | |
| 190 | if ($noShowTagEnabled && !in_array($booking->getCustomerId()->getValue(), $customersNoShowCountIds)) { |
| 191 | $customersNoShowCountIds[] = $booking->getCustomerId()->getValue(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | $appointmentAS->calculateAndSetAppointmentEnd($appointment, $service); |
| 196 | |
| 197 | $minimumCancelTimeInSeconds = $settingsDS |
| 198 | ->getEntitySettings($service->getSettings()) |
| 199 | ->getGeneralSettings() |
| 200 | ->getMinimumTimeRequirementPriorToCanceling(); |
| 201 | |
| 202 | $minimumCancelTime = DateTimeService::getCustomDateTimeObject( |
| 203 | $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 204 | )->modify("-{$minimumCancelTimeInSeconds} seconds"); |
| 205 | |
| 206 | $date = $appointment->getBookingStart()->getValue()->format('Y-m-d'); |
| 207 | |
| 208 | $cancelable = $currentDateTime <= $minimumCancelTime; |
| 209 | |
| 210 | $minimumRescheduleTimeInSeconds = $settingsDS |
| 211 | ->getEntitySettings($service->getSettings()) |
| 212 | ->getGeneralSettings() |
| 213 | ->getMinimumTimeRequirementPriorToRescheduling(); |
| 214 | |
| 215 | $minimumRescheduleTime = DateTimeService::getCustomDateTimeObject( |
| 216 | $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 217 | )->modify("-{$minimumRescheduleTimeInSeconds} seconds"); |
| 218 | |
| 219 | $reschedulable = $currentDateTime <= $minimumRescheduleTime; |
| 220 | |
| 221 | $groupedAppointments[$date]['date'] = $date; |
| 222 | |
| 223 | $groupedAppointments[$date]['appointments'][] = array_merge( |
| 224 | $appointment->toArray(), |
| 225 | [ |
| 226 | 'cancelable' => $cancelable, |
| 227 | 'reschedulable' => $reschedulable, |
| 228 | 'past' => $currentDateTime >= $appointment->getBookingStart()->getValue() |
| 229 | ] |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | $emptyBookedPackages = null; |
| 234 | |
| 235 | if ( |
| 236 | !empty($params['packageId']) && |
| 237 | empty($params['services']) && |
| 238 | empty($params['providers']) && |
| 239 | empty($params['locations']) && |
| 240 | !$noResultsManagePackagesFilters |
| 241 | ) { |
| 242 | /** @var AbstractPackageApplicationService $packageApplicationService */ |
| 243 | $packageApplicationService = $this->container->get('application.bookable.package'); |
| 244 | |
| 245 | /** @var Collection $emptyBookedPackages */ |
| 246 | $emptyBookedPackages = $packageApplicationService->getEmptyPackages( |
| 247 | [ |
| 248 | 'packageCustomerIds' => !empty($packageCustomerIds) ? $packageCustomerIds : [], |
| 249 | 'packages' => [$params['packageId']], |
| 250 | 'purchased' => !empty($params['dates']) ? $params['dates'] : [], |
| 251 | 'customers' => $customers |
| 252 | ] |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | $customersNoShowCount = []; |
| 257 | |
| 258 | if ($noShowTagEnabled && $customersNoShowCountIds) { |
| 259 | /** @var CustomerBookingRepository $bookingRepository */ |
| 260 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 261 | |
| 262 | $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 267 | $result->setMessage('Successfully retrieved appointments'); |
| 268 | $result->setData( |
| 269 | [ |
| 270 | Entities::APPOINTMENTS => |
| 271 | !empty($params['asArray']) && filter_var($params['asArray'], FILTER_VALIDATE_BOOLEAN) ? $appointments->toArray() : $groupedAppointments, |
| 272 | 'availablePackageBookings' => $availablePackageBookings, |
| 273 | 'emptyPackageBookings' => !empty($emptyBookedPackages) ? $emptyBookedPackages->toArray() : [], |
| 274 | 'occupied' => $occupiedTimes, |
| 275 | 'totalPackagePurchases' => $totalPackagePurchases, |
| 276 | 'customersNoShowCount' => $customersNoShowCount |
| 277 | ] |
| 278 | ); |
| 279 | |
| 280 | return $result; |
| 281 | } |
| 282 | } |
| 283 |