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