ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
ReassignBookingCommandHandler.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
ReassignBookingCommandHandler.php
567 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\BookableApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService; |
| 10 | use AmeliaBooking\Application\Services\Booking\BookingApplicationService; |
| 11 | use AmeliaBooking\Application\Services\Payment\PaymentApplicationService; |
| 12 | use AmeliaBooking\Application\Services\Reservation\AppointmentReservationService; |
| 13 | use AmeliaBooking\Application\Services\TimeSlot\TimeSlotService as ApplicationTimeSlotService; |
| 14 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 15 | use AmeliaBooking\Domain\Collection\Collection; |
| 16 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 17 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 18 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 19 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 20 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 21 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 22 | use AmeliaBooking\Domain\Entity\Entities; |
| 23 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 24 | use AmeliaBooking\Domain\Entity\User\Customer; |
| 25 | use AmeliaBooking\Domain\Factory\Booking\Appointment\AppointmentFactory; |
| 26 | use AmeliaBooking\Domain\Services\Booking\AppointmentDomainService; |
| 27 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 28 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 29 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 30 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 31 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 32 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 33 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 34 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 35 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 36 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 37 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 38 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 39 | use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository; |
| 40 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 41 | use Exception; |
| 42 | use Interop\Container\Exception\ContainerException; |
| 43 | |
| 44 | /** |
| 45 | * Class ReassignBookingCommandHandler |
| 46 | * |
| 47 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 48 | */ |
| 49 | class ReassignBookingCommandHandler extends CommandHandler |
| 50 | { |
| 51 | /** |
| 52 | * @var array |
| 53 | */ |
| 54 | public $mandatoryFields = [ |
| 55 | 'bookingStart' |
| 56 | ]; |
| 57 | |
| 58 | /** |
| 59 | * @param ReassignBookingCommand $command |
| 60 | * |
| 61 | * @return CommandResult |
| 62 | * |
| 63 | * @throws AccessDeniedException |
| 64 | * @throws InvalidArgumentException |
| 65 | * @throws QueryExecutionException |
| 66 | * @throws NotFoundException |
| 67 | * @throws ContainerException |
| 68 | * @throws Exception |
| 69 | */ |
| 70 | public function handle(ReassignBookingCommand $command) |
| 71 | { |
| 72 | $this->checkMandatoryFields($command); |
| 73 | |
| 74 | $result = new CommandResult(); |
| 75 | |
| 76 | /** @var UserApplicationService $userAS */ |
| 77 | $userAS = $this->container->get('application.user.service'); |
| 78 | /** @var SettingsService $settingsDS */ |
| 79 | $settingsDS = $this->container->get('domain.settings.service'); |
| 80 | /** @var AppointmentRepository $appointmentRepository */ |
| 81 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 82 | /** @var CustomerBookingRepository $bookingRepository */ |
| 83 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 84 | /** @var CustomerRepository $customerRepository */ |
| 85 | $customerRepository = $this->container->get('domain.users.customers.repository'); |
| 86 | /** @var AppointmentApplicationService $appointmentAS */ |
| 87 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 88 | /** @var AppointmentDomainService $appointmentDS */ |
| 89 | $appointmentDS = $this->container->get('domain.booking.appointment.service'); |
| 90 | /** @var BookableApplicationService $bookableAS */ |
| 91 | $bookableAS = $this->container->get('application.bookable.service'); |
| 92 | /** @var AppointmentReservationService $reservationService */ |
| 93 | $reservationService = $this->container->get('application.reservation.service')->get(Entities::APPOINTMENT); |
| 94 | /** @var PaymentApplicationService $paymentAS */ |
| 95 | $paymentAS = $this->container->get('application.payment.service'); |
| 96 | |
| 97 | try { |
| 98 | /** @var AbstractUser $user */ |
| 99 | $user = $command->getUserApplicationService()->authorization( |
| 100 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 101 | $command->getCabinetType() |
| 102 | ); |
| 103 | } catch (AuthorizationException $e) { |
| 104 | $result->setResult(CommandResult::RESULT_ERROR); |
| 105 | $result->setData( |
| 106 | [ |
| 107 | 'reauthorize' => true |
| 108 | ] |
| 109 | ); |
| 110 | |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | if ($userAS->isCustomer($user) && !$settingsDS->getSetting('roles', 'allowCustomerReschedule') |
| 115 | ) { |
| 116 | throw new AccessDeniedException('You are not allowed to update appointment'); |
| 117 | } |
| 118 | |
| 119 | /** @var Appointment $oldAppointment */ |
| 120 | $oldAppointment = $reservationService->getReservationByBookingId((int)$command->getArg('id')); |
| 121 | |
| 122 | $initialBookingStart = $oldAppointment->getBookingStart()->getValue(); |
| 123 | $initialBookingEnd = $oldAppointment->getBookingEnd()->getValue(); |
| 124 | |
| 125 | /** @var CustomerBooking $booking */ |
| 126 | $booking = $oldAppointment->getBookings()->getItem((int)$command->getArg('id')); |
| 127 | |
| 128 | $oldAppointmentStatusChanged = false; |
| 129 | |
| 130 | /** @var CustomerBooking $oldAppointmentBooking */ |
| 131 | foreach ($oldAppointment->getBookings()->getItems() as $oldAppointmentBooking) { |
| 132 | if ($userAS->isAmeliaUser($user) && |
| 133 | $userAS->isCustomer($user) && |
| 134 | ($booking->getId()->getValue() === $oldAppointmentBooking->getId()->getValue()) && |
| 135 | ($user->getId() && $oldAppointmentBooking->getCustomerId()->getValue() !== $user->getId()->getValue()) |
| 136 | ) { |
| 137 | throw new AccessDeniedException('You are not allowed to update appointment'); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** @var Service $service */ |
| 142 | $service = $bookableAS->getAppointmentService( |
| 143 | $oldAppointment->getServiceId()->getValue(), |
| 144 | $oldAppointment->getProviderId()->getValue() |
| 145 | ); |
| 146 | |
| 147 | $minimumRescheduleTimeInSeconds = $settingsDS |
| 148 | ->getEntitySettings($service->getSettings()) |
| 149 | ->getGeneralSettings() |
| 150 | ->getMinimumTimeRequirementPriorToRescheduling(); |
| 151 | |
| 152 | try { |
| 153 | $reservationService->inspectMinimumCancellationTime( |
| 154 | $oldAppointment->getBookingStart()->getValue(), |
| 155 | $minimumRescheduleTimeInSeconds |
| 156 | ); |
| 157 | } catch (BookingCancellationException $e) { |
| 158 | $result->setResult(CommandResult::RESULT_ERROR); |
| 159 | $result->setMessage('You are not allowed to update booking'); |
| 160 | $result->setData( |
| 161 | [ |
| 162 | 'rescheduleBookingUnavailable' => true |
| 163 | ] |
| 164 | ); |
| 165 | |
| 166 | return $result; |
| 167 | } |
| 168 | |
| 169 | $bookingStart = $command->getField('bookingStart'); |
| 170 | |
| 171 | $bookingStartInUtc = DateTimeService::getCustomDateTimeObject( |
| 172 | $bookingStart |
| 173 | )->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 174 | |
| 175 | if ($command->getField('timeZone') === 'UTC') { |
| 176 | $bookingStart = DateTimeService::getCustomDateTimeFromUtc( |
| 177 | $bookingStart |
| 178 | ); |
| 179 | } elseif ($command->getField('timeZone')) { |
| 180 | $bookingStart = DateTimeService::getDateTimeObjectInTimeZone( |
| 181 | $bookingStart, |
| 182 | $command->getField('timeZone') |
| 183 | )->setTimezone(DateTimeService::getTimeZone())->format('Y-m-d H:i:s'); |
| 184 | } elseif ($command->getField('utcOffset') !== null && |
| 185 | $settingsDS->getSetting('general', 'showClientTimeZone') |
| 186 | ) { |
| 187 | $bookingStart = DateTimeService::getCustomDateTimeFromUtc( |
| 188 | $bookingStart |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** @var ApplicationTimeSlotService $applicationTimeSlotService */ |
| 193 | $applicationTimeSlotService = $this->container->get('application.timeSlot.service'); |
| 194 | |
| 195 | if (!$applicationTimeSlotService->isSlotFree( |
| 196 | $service, |
| 197 | DateTimeService::getCustomDateTimeObject( |
| 198 | $bookingStart |
| 199 | ), |
| 200 | DateTimeService::getCustomDateTimeObject( |
| 201 | $bookingStart |
| 202 | ), |
| 203 | DateTimeService::getCustomDateTimeObject( |
| 204 | $bookingStart |
| 205 | ), |
| 206 | $oldAppointment->getProviderId()->getValue(), |
| 207 | $oldAppointment->getLocationId() ? $oldAppointment->getLocationId()->getValue() : null, |
| 208 | $booking->getExtras()->getItems(), |
| 209 | $oldAppointment->getId()->getValue(), |
| 210 | $booking->getPersons()->getValue(), |
| 211 | true |
| 212 | )) { |
| 213 | $result->setResult(CommandResult::RESULT_ERROR); |
| 214 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 215 | $result->setData( |
| 216 | [ |
| 217 | 'timeSlotUnavailable' => true |
| 218 | ] |
| 219 | ); |
| 220 | |
| 221 | return $result; |
| 222 | } |
| 223 | |
| 224 | /** @var AppointmentReservationService $reservationService */ |
| 225 | $reservationService = $this->container->get('application.reservation.service')->get(Entities::APPOINTMENT); |
| 226 | if ($reservationService->checkLimitsPerCustomer($service, $oldAppointmentBooking->getCustomerId()->getValue(), DateTimeService::getCustomDateTimeObject($bookingStart), $oldAppointmentBooking->getId()->getValue())) { |
| 227 | $result->setResult(CommandResult::RESULT_ERROR); |
| 228 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 229 | $result->setData( |
| 230 | [ |
| 231 | 'timeSlotUnavailable' => true |
| 232 | ] |
| 233 | ); |
| 234 | |
| 235 | return $result; |
| 236 | } |
| 237 | |
| 238 | $setTimeZone = false; |
| 239 | |
| 240 | if ($booking->getInfo() && $booking->getInfo()->getValue()) { |
| 241 | $info = json_decode($booking->getInfo()->getValue(), true); |
| 242 | |
| 243 | if (empty($info['timeZone'])) { |
| 244 | $setTimeZone = true; |
| 245 | } |
| 246 | } else if (!$booking->getInfo() || $booking->getInfo()->getValue() === null) { |
| 247 | $setTimeZone = true; |
| 248 | } |
| 249 | |
| 250 | if ($setTimeZone && |
| 251 | (!$booking->getUtcOffset() || $booking->getInfo()->getValue() === null) && |
| 252 | $userAS->isCustomer($user) && |
| 253 | $command->getField('timeZone') && |
| 254 | $command->getField('timeZone') !== 'UTC' && |
| 255 | $command->getField('utcOffset') !== null && |
| 256 | $settingsDS->getSetting('general', 'showClientTimeZone') |
| 257 | ) { |
| 258 | /** @var Customer $customer */ |
| 259 | $customer = $customerRepository->getById($booking->getCustomerId()->getValue()); |
| 260 | |
| 261 | $booking->setInfo( |
| 262 | new Json( |
| 263 | json_encode( |
| 264 | [ |
| 265 | 'firstName' => $customer->getFirstName()->getValue(), |
| 266 | 'lastName' => $customer->getLastName()->getValue(), |
| 267 | 'phone' => null, |
| 268 | 'locale' => null, |
| 269 | 'timeZone' => $command->getField('timeZone'), |
| 270 | 'urlParams' => null, |
| 271 | ] |
| 272 | ) |
| 273 | ) |
| 274 | ); |
| 275 | |
| 276 | $bookingRepository->updateFieldById( |
| 277 | $booking->getId()->getValue(), |
| 278 | $booking->getInfo()->getValue(), |
| 279 | 'info' |
| 280 | ); |
| 281 | |
| 282 | $booking->setUtcOffset(new IntegerValue($command->getField('utcOffset'))); |
| 283 | |
| 284 | $bookingRepository->updateFieldById( |
| 285 | $booking->getId()->getValue(), |
| 286 | $booking->getUtcOffset()->getValue(), |
| 287 | 'utcOffset' |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | /** @var Collection $existingAppointments */ |
| 292 | $existingAppointments = $appointmentRepository->getFiltered( |
| 293 | [ |
| 294 | 'dates' => [$bookingStart, $bookingStart], |
| 295 | 'services' => [$oldAppointment->getServiceId()->getValue()], |
| 296 | 'providers' => [$oldAppointment->getProviderId()->getValue()] |
| 297 | ] |
| 298 | ); |
| 299 | |
| 300 | /** @var Appointment $newAppointment */ |
| 301 | $newAppointment = null; |
| 302 | |
| 303 | /** @var Appointment $existingAppointment */ |
| 304 | $existingAppointment = $existingAppointments->length() ? |
| 305 | $existingAppointments->getItem($existingAppointments->keys()[0]) : null; |
| 306 | |
| 307 | if ($existingAppointment && |
| 308 | $existingAppointment->getId()->getValue() === $oldAppointment->getId()->getValue() && |
| 309 | $existingAppointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') === |
| 310 | $oldAppointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 311 | ) { |
| 312 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 313 | $result->setMessage('Successfully updated appointment'); |
| 314 | $result->setData( |
| 315 | [ |
| 316 | Entities::BOOKING => $booking->toArray(), |
| 317 | 'newAppointment' => null, |
| 318 | 'oldAppointment' => $oldAppointment->toArray(), |
| 319 | 'oldAppointmentStatusChanged' => false, |
| 320 | 'existingAppointment' => $existingAppointment->toArray(), |
| 321 | 'existingAppointmentStatusChanged' => false, |
| 322 | ] |
| 323 | ); |
| 324 | |
| 325 | return $result; |
| 326 | } |
| 327 | |
| 328 | if ($existingAppointment && |
| 329 | $existingAppointment->getId()->getValue() === $oldAppointment->getId()->getValue() |
| 330 | ) { |
| 331 | $existingAppointment = null; |
| 332 | } |
| 333 | |
| 334 | $bookingStatus = $settingsDS |
| 335 | ->getEntitySettings($service->getSettings()) |
| 336 | ->getGeneralSettings() |
| 337 | ->getDefaultAppointmentStatus(); |
| 338 | |
| 339 | $existingAppointmentStatusChanged = false; |
| 340 | |
| 341 | $appointmentRepository->beginTransaction(); |
| 342 | |
| 343 | do_action('amelia_before_booking_rescheduled', $oldAppointment->toArray(), $booking->toArray(), $bookingStart); |
| 344 | |
| 345 | if ($existingAppointment === null && |
| 346 | ( |
| 347 | $oldAppointment->getBookings()->length() === 1 || |
| 348 | $bookingStart === $oldAppointment->getBookingStart()->getValue()->format('Y-m-d H:i') |
| 349 | ) |
| 350 | ) { |
| 351 | /** @var BookingApplicationService $bookingAS */ |
| 352 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 353 | |
| 354 | if ($bookingStart !== $oldAppointment->getBookingStart()->getValue()->format('Y-m-d H:i')) { |
| 355 | $bookingAS->bookingRescheduled( |
| 356 | $oldAppointment->getId()->getValue(), |
| 357 | Entities::APPOINTMENT, |
| 358 | $booking->getCustomerId()->getValue(), |
| 359 | Entities::CUSTOMER |
| 360 | ); |
| 361 | |
| 362 | $bookingAS->bookingRescheduled( |
| 363 | $oldAppointment->getId()->getValue(), |
| 364 | Entities::APPOINTMENT, |
| 365 | $oldAppointment->getProviderId()->getValue(), |
| 366 | Entities::PROVIDER |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | $oldAppointment->setBookingStart( |
| 371 | new DateTimeValue( |
| 372 | DateTimeService::getCustomDateTimeObject( |
| 373 | $bookingStart |
| 374 | ) |
| 375 | ) |
| 376 | ); |
| 377 | |
| 378 | $oldAppointment->setBookingEnd( |
| 379 | new DateTimeValue( |
| 380 | DateTimeService::getCustomDateTimeObject($bookingStart) |
| 381 | ->modify( |
| 382 | '+' . $appointmentAS->getAppointmentLengthTime($oldAppointment, $service) . ' second' |
| 383 | ) |
| 384 | ) |
| 385 | ); |
| 386 | |
| 387 | if ($oldAppointment->getStatus()->getValue() === BookingStatus::APPROVED && $bookingStatus === BookingStatus::PENDING) { |
| 388 | $oldAppointment->setStatus(new BookingStatus($bookingStatus)); |
| 389 | $booking->setStatus(new BookingStatus(BookingStatus::PENDING)); |
| 390 | $booking->setChangedStatus(new BooleanValueObject(true)); |
| 391 | |
| 392 | $bookingRepository->updateFieldById( |
| 393 | $booking->getId()->getValue(), |
| 394 | $bookingStatus, |
| 395 | 'status' |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc); |
| 400 | |
| 401 | $appointmentRepository->update($oldAppointment->getId()->getValue(), $oldAppointment); |
| 402 | |
| 403 | $oldAppointment->setRescheduled(new BooleanValueObject(true)); |
| 404 | |
| 405 | $reservationService->updateWooCommerceOrder($booking, $oldAppointment); |
| 406 | } else { |
| 407 | $oldAppointment->getBookings()->deleteItem($booking->getId()->getValue()); |
| 408 | |
| 409 | if ($existingAppointment !== null) { |
| 410 | $booking->setAppointmentId($existingAppointment->getId()); |
| 411 | |
| 412 | if ($booking->getStatus()->getValue() === BookingStatus::APPROVED && $bookingStatus === BookingStatus::PENDING) { |
| 413 | $booking->setStatus(new BookingStatus(BookingStatus::PENDING)); |
| 414 | $booking->setChangedStatus(new BooleanValueObject(true)); |
| 415 | |
| 416 | $bookingRepository->updateFieldById( |
| 417 | $booking->getId()->getValue(), |
| 418 | $bookingStatus, |
| 419 | 'status' |
| 420 | ); |
| 421 | } |
| 422 | |
| 423 | $existingAppointment->getBookings()->addItem($booking, $booking->getId()->getValue()); |
| 424 | |
| 425 | $existingAppointmentStatus = $appointmentDS->getAppointmentStatusWhenEditAppointment( |
| 426 | $service, |
| 427 | $appointmentDS->getBookingsStatusesCount($existingAppointment) |
| 428 | ); |
| 429 | |
| 430 | $existingAppointmentStatusChanged = $existingAppointment->getStatus()->getValue() !== $existingAppointmentStatus; |
| 431 | |
| 432 | $existingAppointment->setStatus(new BookingStatus($existingAppointmentStatus)); |
| 433 | |
| 434 | $existingAppointment->setBookingEnd( |
| 435 | new DateTimeValue( |
| 436 | DateTimeService::getCustomDateTimeObject($bookingStart) |
| 437 | ->modify( |
| 438 | '+' . $appointmentAS->getAppointmentLengthTime($existingAppointment, $service) . ' second' |
| 439 | ) |
| 440 | ) |
| 441 | ); |
| 442 | |
| 443 | $bookingRepository->updateFieldById( |
| 444 | $booking->getId()->getValue(), |
| 445 | $existingAppointment->getId()->getValue(), |
| 446 | 'appointmentId' |
| 447 | ); |
| 448 | |
| 449 | $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc); |
| 450 | |
| 451 | $appointmentRepository->update($existingAppointment->getId()->getValue(), $existingAppointment); |
| 452 | |
| 453 | $reservationService->updateWooCommerceOrder($booking, $existingAppointment); |
| 454 | } else { |
| 455 | $newAppointment = AppointmentFactory::create( |
| 456 | array_merge( |
| 457 | $oldAppointment->toArray(), |
| 458 | [ |
| 459 | 'id' => null, |
| 460 | 'googleCalendarEventId' => null, |
| 461 | 'outlookCalendarEventId' => null, |
| 462 | 'zoomMeeting' => null, |
| 463 | 'bookings' => [], |
| 464 | ] |
| 465 | ) |
| 466 | ); |
| 467 | |
| 468 | $newAppointment->getBookings()->addItem($booking, $booking->getId()->getValue()); |
| 469 | |
| 470 | $newAppointment->setBookingStart( |
| 471 | new DateTimeValue( |
| 472 | DateTimeService::getCustomDateTimeObject( |
| 473 | $bookingStart |
| 474 | ) |
| 475 | ) |
| 476 | ); |
| 477 | |
| 478 | $newAppointment->setBookingEnd( |
| 479 | new DateTimeValue( |
| 480 | DateTimeService::getCustomDateTimeObject($bookingStart) |
| 481 | ->modify( |
| 482 | '+' . $appointmentAS->getAppointmentLengthTime($newAppointment, $service) . ' second' |
| 483 | ) |
| 484 | ) |
| 485 | ); |
| 486 | |
| 487 | $newAppointment->setRescheduled(new BooleanValueObject(true)); |
| 488 | |
| 489 | $newAppointmentStatus = $appointmentDS->getAppointmentStatusWhenEditAppointment( |
| 490 | $service, |
| 491 | $appointmentDS->getBookingsStatusesCount($newAppointment) |
| 492 | ); |
| 493 | |
| 494 | $newAppointment->setStatus(new BookingStatus($newAppointmentStatus)); |
| 495 | |
| 496 | $newAppointmentId = $appointmentRepository->add($newAppointment); |
| 497 | |
| 498 | $newAppointment->setId(new Id($newAppointmentId)); |
| 499 | |
| 500 | $booking->setAppointmentId(new Id($newAppointmentId)); |
| 501 | |
| 502 | $bookingRepository->updateFieldById( |
| 503 | $booking->getId()->getValue(), |
| 504 | $newAppointmentId, |
| 505 | 'appointmentId' |
| 506 | ); |
| 507 | |
| 508 | $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc); |
| 509 | |
| 510 | $reservationService->updateWooCommerceOrder($booking, $newAppointment); |
| 511 | } |
| 512 | |
| 513 | if ($oldAppointment->getBookings()->length() === 0) { |
| 514 | $appointmentRepository->delete($oldAppointment->getId()->getValue()); |
| 515 | |
| 516 | $oldAppointment->setStatus(new BookingStatus(BookingStatus::CANCELED)); |
| 517 | |
| 518 | $oldAppointmentStatusChanged = true; |
| 519 | } else { |
| 520 | $oldAppointmentStatus = $appointmentDS->getAppointmentStatusWhenEditAppointment( |
| 521 | $service, |
| 522 | $appointmentDS->getBookingsStatusesCount($oldAppointment) |
| 523 | ); |
| 524 | |
| 525 | $oldAppointmentStatusChanged = $oldAppointment->getStatus()->getValue() !== $oldAppointmentStatus; |
| 526 | |
| 527 | $oldAppointment->setStatus(new BookingStatus($oldAppointmentStatus)); |
| 528 | |
| 529 | $oldAppointment->setBookingEnd( |
| 530 | new DateTimeValue( |
| 531 | DateTimeService::getCustomDateTimeObject( |
| 532 | $oldAppointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 533 | )->modify( |
| 534 | '+' . $appointmentAS->getAppointmentLengthTime($oldAppointment, $service) . ' second' |
| 535 | ) |
| 536 | ) |
| 537 | ); |
| 538 | |
| 539 | $appointmentRepository->update($oldAppointment->getId()->getValue(), $oldAppointment); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | $appointmentRepository->commit(); |
| 544 | |
| 545 | do_action('amelia_after_booking_rescheduled', $oldAppointment->toArray(), $booking->toArray(), $bookingStart); |
| 546 | |
| 547 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 548 | $result->setMessage('Successfully updated appointment'); |
| 549 | $result->setData( |
| 550 | [ |
| 551 | Entities::BOOKING => $booking->toArray(), |
| 552 | 'newAppointment' => $newAppointment ? $newAppointment->toArray() : null, |
| 553 | 'oldAppointment' => $oldAppointment->toArray(), |
| 554 | 'oldAppointmentStatusChanged' => $oldAppointmentStatusChanged, |
| 555 | 'existingAppointment' => $existingAppointment ? $existingAppointment->toArray() : null, |
| 556 | 'existingAppointmentStatusChanged' => $existingAppointmentStatusChanged, |
| 557 | 'initialAppointmentDateTime' => [ |
| 558 | 'bookingStart' => $initialBookingStart->format('Y-m-d H:i:s'), |
| 559 | 'bookingEnd' => $initialBookingEnd->format('Y-m-d H:i:s'), |
| 560 | ], |
| 561 | ] |
| 562 | ); |
| 563 | |
| 564 | return $result; |
| 565 | } |
| 566 | } |
| 567 |