PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Booking / Appointment / UpdateAppointmentStatusCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 1 year ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 1 year ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 1 year ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 2 years ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 1 year ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 1 year ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 1 year ago GetIcsCommand.php 1 year ago GetIcsCommandHandler.php 4 years ago GetPackageAppointmentsCommand.php 1 year ago GetPackageAppointmentsCommandHandler.php 1 year ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 year ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 1 year ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 1 year ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 1 year ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 1 year ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 1 year ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 1 year ago
UpdateAppointmentStatusCommandHandler.php
236 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\User\UserApplicationService;
12 use AmeliaBooking\Domain\Collection\Collection;
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\ValueObjects\BooleanValueObject;
21 use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue;
22 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
23 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
24 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
25 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
26 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
27 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
28 use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings;
29 use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
30 use Interop\Container\Exception\ContainerException;
31
32 /**
33 * Class UpdateAppointmentStatusCommandHandler
34 *
35 * @package AmeliaBooking\Application\Commands\Booking\Appointment
36 */
37 class UpdateAppointmentStatusCommandHandler extends CommandHandler
38 {
39 /**
40 * @var array
41 */
42 public $mandatoryFields = [
43 'status'
44 ];
45
46 /**
47 * @param UpdateAppointmentStatusCommand $command
48 *
49 * @return CommandResult
50 *
51 * @throws AccessDeniedException
52 * @throws InvalidArgumentException
53 * @throws QueryExecutionException
54 * @throws ContainerException
55 * @throws NotFoundException
56 */
57 public function handle(UpdateAppointmentStatusCommand $command)
58 {
59 if (!$command->getPermissionService()->currentUserCanWriteStatus(Entities::APPOINTMENTS)) {
60 throw new AccessDeniedException('You are not allowed to update appointment status');
61 }
62
63 $result = new CommandResult();
64
65 $this->checkMandatoryFields($command);
66
67 /** @var CustomerBookingRepository $bookingRepository */
68 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
69 /** @var AppointmentRepository $appointmentRepo */
70 $appointmentRepo = $this->container->get('domain.booking.appointment.repository');
71 /** @var BookingApplicationService $bookingAS */
72 $bookingAS = $this->container->get('application.booking.booking.service');
73 /** @var UserApplicationService $userAS */
74 $userAS = $command->getUserApplicationService();
75 /** @var AppointmentApplicationService $appointmentAS */
76 $appointmentAS = $this->container->get('application.booking.appointment.service');
77 /** @var BookableApplicationService $bookableAS */
78 $bookableAS = $this->container->get('application.bookable.service');
79
80 $appointmentId = (int)$command->getArg('id');
81 $requestedStatus = $command->getField('status');
82
83 /** @var Appointment $appointment */
84 $appointment = $appointmentRepo->getById($appointmentId);
85
86 $packageCustomerId = $command->getField('packageCustomerId');
87
88 if ($packageCustomerId) {
89 $appArray = array_filter(
90 $appointment->getBookings()->getItems(),
91 function ($booking) use ($packageCustomerId) {
92 /** @var Id $pcId */
93 $pcId = $booking->getPackageCustomerService() ?
94 $booking->getPackageCustomerService()->getPackageCustomer()->getId() : null;
95
96 return $pcId && $pcId->getValue() === $packageCustomerId;
97 }
98 );
99
100 $appointment->setBookings(new Collection($appArray));
101 }
102
103 $oldStatus = $appointment->getStatus()->getValue();
104
105 if (
106 $bookingAS->isBookingApprovedOrPending($requestedStatus) &&
107 $bookingAS->isBookingCanceledOrRejectedOrNoShow($appointment->getStatus()->getValue())
108 ) {
109 /** @var AbstractUser $user */
110 $user = $this->container->get('logged.in.user');
111
112 if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) {
113 $result->setResult(CommandResult::RESULT_ERROR);
114 $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']);
115 $result->setData(
116 [
117 'timeSlotUnavailable' => true,
118 'status' => $appointment->getStatus()->getValue()
119 ]
120 );
121
122 return $result;
123 }
124 }
125
126 $oldAppointmentArray = $appointment->toArray();
127
128 /** @var CustomerBooking $booking */
129 foreach ($appointment->getBookings()->getItems() as $booking) {
130 $booking->setStatus(new BookingStatus($requestedStatus));
131 }
132
133 /** @var Service $service */
134 $service = $bookableAS->getAppointmentService(
135 $appointment->getServiceId()->getValue(),
136 $appointment->getProviderId()->getValue()
137 );
138
139 if (
140 $requestedStatus === BookingStatus::APPROVED &&
141 (
142 (
143 $service->getMaxCapacity()->getValue() === 1 &&
144 $appointment->getBookings()->length() > 1
145 ) || (
146 $service->getMaxCapacity()->getValue() > 1 &&
147 $appointment->getBookings()->length() > $service->getMaxCapacity()->getValue()
148 )
149 )
150 ) {
151 $result->setResult(CommandResult::RESULT_SUCCESS);
152 $result->setMessage('Appointment status not updated');
153 $result->setData(
154 [
155 Entities::APPOINTMENT => $appointment->toArray(),
156 'bookingsWithChangedStatus' => [],
157 'status' => $appointment->getStatus()->getValue(),
158 'oldStatus' => $appointment->getStatus()->getValue(),
159 'message' => BackendStrings::getEventStrings()['maximum_capacity_reached'],
160 ]
161 );
162
163 return $result;
164 }
165
166 $appointment->setStatus(new BookingStatus($requestedStatus));
167
168 $appointmentRepo->beginTransaction();
169
170 do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus);
171
172 $appointment->setBookingEnd(
173 new DateTimeValue(
174 DateTimeService::getCustomDateTimeObject(
175 $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s')
176 )->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second')
177 )
178 );
179
180 $appointmentRepo->updateFieldById(
181 $appointmentId,
182 DateTimeService::getCustomDateTimeObjectInUtc(
183 $appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s')
184 )->format('Y-m-d H:i:s'),
185 'bookingEnd'
186 );
187
188
189 if ($packageCustomerId) {
190 /** @var CustomerBooking $booking */
191 foreach ($appointment->getBookings()->getItems() as $booking) {
192 $bookingRepository->updateStatusById($booking->getId()->getValue(), $requestedStatus);
193 }
194
195 if ($appointment->getBookings()->length() === 1) {
196 $appointmentRepo->updateStatusById($appointmentId, $requestedStatus);
197 }
198 } else {
199 $bookingRepository->updateStatusByAppointmentId($appointmentId, $requestedStatus);
200 $appointmentRepo->updateStatusById($appointmentId, $requestedStatus);
201 }
202
203 $appointmentRepo->commit();
204
205 do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus);
206
207 /** @var CustomerBooking $booking */
208 foreach ($appointment->getBookings()->getItems() as $booking) {
209 if (
210 $booking->getStatus()->getValue() === BookingStatus::APPROVED &&
211 ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED)
212 ) {
213 $booking->setChangedStatus(new BooleanValueObject(true));
214 }
215 }
216
217 $appointmentArray = $appointment->toArray();
218 $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray);
219
220 $result->setResult(CommandResult::RESULT_SUCCESS);
221 $result->setMessage('Successfully updated appointment status');
222 $result->setData(
223 [
224 Entities::APPOINTMENT => $appointmentArray,
225 'bookingsWithChangedStatus' => $bookingsWithChangedStatus,
226 'status' => $requestedStatus,
227 'oldStatus' => $oldStatus,
228 'message' =>
229 BackendStrings::getAppointmentStrings()['appointment_status_changed'] . strtolower(BackendStrings::getCommonStrings()[$requestedStatus])
230 ]
231 );
232
233 return $result;
234 }
235 }
236