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 / GetPackageAppointmentsCommandHandler.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
GetPackageAppointmentsCommandHandler.php
279 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 $packageCustomerIds = $packageCustomerRepository->getIds(
103 [
104 'purchased' => !empty($params['dates']) ? $params['dates'] : [],
105 'packages' => !empty($params['packageId']) ? [$params['packageId']] : [],
106 'itemsPerPage' => $itemsPerPageBackEnd,
107 'page' => !empty($params['page']) ? $params['page'] : null,
108 'packageStatus' => !empty($params['packageStatus']) ? $params['packageStatus'] : null,
109 'customerId' => $customerId,
110 ]
111 );
112
113 $noResultsManagePackagesFilters = empty($packageCustomerIds);
114
115 $totalPackagePurchases = $packageCustomerRepository->getPackagePurchasedCount(
116 [
117 'packageCustomerIds' => !empty($packageCustomerIds) ? $packageCustomerIds : [],
118 'purchased' => !empty($params['dates']) ? $params['dates'] : [],
119 'packages' => !empty($params['packageId']) ? [$params['packageId']] : [],
120 'packageStatus' => !empty($params['packageStatus']) ? $params['packageStatus'] : null,
121 'customerId' => $customerId
122 ]
123 );
124 }
125
126 /** @var Collection $appointments */
127 $appointments = new Collection();
128
129 if (isset($params['customerId'])) {
130 unset($params['customerId']);
131 }
132
133 $customersNoShowCountIds = [];
134
135 $noShowTagEnabled = $settingsDS->getSetting('roles', 'enableNoShowTag');
136
137 if (!$noResultsManagePackagesFilters) {
138 $availablePackageBookings = $packageAS->getPackageAvailability(
139 $appointments,
140 [
141 'packageCustomerIds' => !empty($packageCustomerIds) ? $packageCustomerIds : [],
142 'purchased' => !empty($params['dates']) ? $params['dates'] : [],
143 'customerId' => $customerId,
144 'packageId' => !empty($params['packageId']) ? (int)$params['packageId'] : null,
145 'managePackagePage' => true
146 ]
147 );
148
149 if ($noShowTagEnabled && !!$availablePackageBookings) {
150 $customersNoShowCountIds[] = $availablePackageBookings[0]['customerId'];
151 }
152 }
153
154 /** @var Collection $services */
155 $services = $serviceRepository->getAllArrayIndexedById();
156
157 $packageAS->setPackageBookingsForAppointments($appointments);
158
159 $occupiedTimes = [];
160
161 $currentDateTime = DateTimeService::getNowDateTimeObject();
162
163 $groupedAppointments = [];
164
165 /** @var Appointment $appointment */
166 foreach ($appointments->getItems() as $appointment) {
167 /** @var Service $service */
168 $service = $services->getItem($appointment->getServiceId()->getValue());
169
170 $bookingsCount = 0;
171
172 /** @var CustomerBooking $booking */
173 foreach ($appointment->getBookings()->getItems() as $booking) {
174 // fix for wrongly saved JSON
175 if (
176 $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 (
232 !empty($params['packageId']) &&
233 empty($params['services']) &&
234 empty($params['providers']) &&
235 empty($params['locations']) &&
236 !$noResultsManagePackagesFilters
237 ) {
238 /** @var AbstractPackageApplicationService $packageApplicationService */
239 $packageApplicationService = $this->container->get('application.bookable.package');
240
241 /** @var Collection $emptyBookedPackages */
242 $emptyBookedPackages = $packageApplicationService->getEmptyPackages(
243 [
244 'packageCustomerIds' => !empty($packageCustomerIds) ? $packageCustomerIds : [],
245 'packages' => [$params['packageId']],
246 'purchased' => !empty($params['dates']) ? $params['dates'] : [],
247 'customerId' => $customerId
248 ]
249 );
250 }
251
252 $customersNoShowCount = [];
253
254 if ($noShowTagEnabled && $customersNoShowCountIds) {
255 /** @var CustomerBookingRepository $bookingRepository */
256 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
257
258 $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds);
259 }
260
261
262 $result->setResult(CommandResult::RESULT_SUCCESS);
263 $result->setMessage('Successfully retrieved appointments');
264 $result->setData(
265 [
266 Entities::APPOINTMENTS =>
267 !empty($params['asArray']) && filter_var($params['asArray'], FILTER_VALIDATE_BOOLEAN) ? $appointments->toArray() : $groupedAppointments,
268 'availablePackageBookings' => $availablePackageBookings,
269 'emptyPackageBookings' => !empty($emptyBookedPackages) ? $emptyBookedPackages->toArray() : [],
270 'occupied' => $occupiedTimes,
271 'totalPackagePurchases' => $totalPackagePurchases,
272 'customersNoShowCount' => $customersNoShowCount
273 ]
274 );
275
276 return $result;
277 }
278 }
279