PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.38
Booking for Appointments and Events Calendar – Amelia v1.2.38
2.4.9 2.4.8 2.4.7 2.4.6 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 / Services / Notification / AppointmentNotificationService.php
ameliabooking / src / Application / Services / Notification Last commit date
AbstractNotificationService.php 1 year ago AbstractWhatsAppNotificationService.php 1 year ago ApplicationNotificationService.php 9 months ago AppointmentNotificationService.php 9 months ago BasicWhatsAppNotificationService.php 1 year ago EmailNotificationService.php 9 months ago NotificationHelperService.php 1 year ago SMSAPIService.php 1 year ago SMSNotificationService.php 9 months ago
AppointmentNotificationService.php
360 lines
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Application\Services\Notification;
9
10 use AmeliaBooking\Application\Services\Invoice\AbstractInvoiceApplicationService;
11 use AmeliaBooking\Domain\Collection\Collection;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment;
14 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
15 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
16 use AmeliaBooking\Domain\Entity\Notification\Notification;
17 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
18 use AmeliaBooking\Domain\ValueObjects\String\NotificationStatus;
19 use AmeliaBooking\Infrastructure\Common\Container;
20 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
21 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
22 use Interop\Container\Exception\ContainerException;
23
24 /**
25 * Class AppointmentNotificationService
26 *
27 * @package AmeliaBooking\Application\Services\Notification
28 */
29 class AppointmentNotificationService
30 {
31 protected $container;
32
33 /**
34 * AppointmentNotificationService constructor.
35 *
36 * @param Container $container
37 */
38 public function __construct(Container $container)
39 {
40 $this->container = $container;
41 }
42
43 /**
44 * @param AbstractNotificationService $notificationService
45 * @param Appointment $appointment
46 * @param bool $logNotification
47 *
48 * @throws InvalidArgumentException
49 * @throws QueryExecutionException
50 * @throws ContainerException
51 */
52 public function sendProviderStatusNotifications(
53 $notificationService,
54 $appointment,
55 $logNotification = true
56 ) {
57 /** @var Collection $providerNotifications */
58 $providerNotifications = $notificationService->getByNameAndType(
59 "provider_appointment_{$appointment->getStatus()->getValue()}",
60 $notificationService->getType()
61 );
62
63 $sendDefault = $notificationService->sendDefault($providerNotifications, $appointment->toArray());
64
65 /** @var Notification $providerNotification */
66 foreach ($providerNotifications->getItems() as $providerNotification) {
67 if (
68 $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
69 $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefault)
70 ) {
71 $notificationService->sendNotification(
72 $appointment->toArray(),
73 $providerNotification,
74 $logNotification
75 );
76 }
77 }
78 }
79
80 /**
81 * @param AbstractNotificationService $notificationService
82 * @param Appointment $appointment
83 * @param Collection $bookings
84 * @param bool $logNotification
85 * @param bool $isBackend
86 * @param bool $sendInvoice
87 *
88 * @throws InvalidArgumentException
89 * @throws QueryExecutionException
90 * @throws NotFoundException
91 * @throws ContainerException
92 */
93 public function sendCustomersStatusNotifications(
94 $notificationService,
95 $appointment,
96 $bookings,
97 $logNotification = true,
98 $isBackend = true,
99 $sendInvoice = false,
100 $notifyCustomers = true
101 ) {
102 /** @var AbstractInvoiceApplicationService $invoiceService */
103 $invoiceService = $this->container->get('application.invoice.service');
104
105 /** @var Collection $statusNotifications */
106 $statusNotifications = new Collection();
107
108 /** @var CustomerBooking $booking */
109 foreach ($bookings->getItems() as $bookingKey => $booking) {
110 if ($booking->isChangedStatus() && $booking->isChangedStatus()->getValue()) {
111 $notificationStatus = $appointment->getStatus()->getValue() === BookingStatus::PENDING &&
112 (
113 $booking->getStatus()->getValue() === BookingStatus::APPROVED ||
114 $booking->getStatus()->getValue() === BookingStatus::PENDING
115 )
116 ? BookingStatus::PENDING
117 : $booking->getStatus()->getValue();
118
119 if (!$statusNotifications->keyExists($notificationStatus)) {
120 $statusNotifications->addItem(
121 $notificationService->getByNameAndType(
122 "customer_appointment_{$notificationStatus}",
123 $notificationService->getType()
124 ),
125 $notificationStatus
126 );
127 }
128
129 /** @var Collection $customerNotifications */
130 $customerNotifications = $statusNotifications->getItem($notificationStatus);
131
132 $sendDefault = $notificationService->sendDefault($customerNotifications, $appointment->toArray());
133
134 /** @var Notification $customerNotification */
135 foreach ($customerNotifications->getItems() as $customerNotification) {
136 if (
137 $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
138 $notificationService->checkCustom(
139 $customerNotification,
140 $appointment->toArray(),
141 $sendDefault
142 ) && $notifyCustomers
143 ) {
144 $notificationService->sendNotification(
145 array_merge(
146 $appointment->toArray(),
147 [
148 'bookings' => $bookings->toArray(),
149 'isBackend' => $isBackend,
150 ]
151 ),
152 $customerNotification,
153 $logNotification,
154 $bookingKey,
155 null,
156 (
157 $sendInvoice &&
158 $booking->getPayments()->length() &&
159 $booking->getPayments()->keyExists(0)
160 )
161 ? $invoiceService->generateInvoice(
162 $booking->getPayments()->getItem(0)->getId()->getValue()
163 )
164 : null
165 );
166 }
167 }
168 }
169 }
170 }
171
172 /**
173 * @param AbstractNotificationService $notificationService
174 * @param Appointment $appointment
175 * @param bool $notifyProvider
176 * @param bool $notifyCustomers
177 *
178 * @throws QueryExecutionException
179 * @throws InvalidArgumentException
180 */
181 public function sendRescheduledNotifications(
182 $notificationService,
183 $appointment,
184 $notifyProvider = true,
185 $notifyCustomers = true
186 ) {
187 if ($notifyProvider) {
188 /** @var Collection $providerNotifications */
189 $providerNotifications = $notificationService->getByNameAndType(
190 "provider_appointment_rescheduled",
191 $notificationService->getType()
192 );
193
194 $sendDefault = $notificationService->sendDefault($providerNotifications, $appointment->toArray());
195
196 /** @var Notification $providerNotification */
197 foreach ($providerNotifications->getItems() as $providerNotification) {
198 if (
199 $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
200 $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefault)
201 ) {
202 $notificationService->sendNotification(
203 $appointment->toArray(),
204 $providerNotification,
205 true
206 );
207 }
208 }
209 }
210
211 if ($notifyCustomers && $appointment->isNotifyParticipants()) {
212 /** @var Collection $customerNotifications */
213 $customerNotifications = $notificationService->getByNameAndType(
214 "customer_appointment_rescheduled",
215 $notificationService->getType()
216 );
217
218 $sendDefault = $notificationService->sendDefault($customerNotifications, $appointment->toArray());
219
220 /** @var Notification $customerNotification */
221 foreach ($customerNotifications->getItems() as $customerNotification) {
222 if (
223 $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
224 $notificationService->checkCustom($customerNotification, $appointment->toArray(), $sendDefault)
225 ) {
226 /** @var CustomerBooking $booking */
227 foreach ($appointment->getBookings()->getItems() as $bookingKey => $booking) {
228 if (
229 (!$booking->isNew() || !$booking->isNew()->getValue()) &&
230 (
231 $booking->getStatus()->getValue() === BookingStatus::APPROVED ||
232 $booking->getStatus()->getValue() === BookingStatus::PENDING
233 )
234 ) {
235 $notificationService->sendNotification(
236 $appointment->toArray(),
237 $customerNotification,
238 true,
239 $bookingKey
240 );
241 }
242 }
243 }
244 }
245 }
246 }
247
248 /**
249 * @param AbstractNotificationService $notificationService
250 * @param Appointment $appointment
251 * @param bool $notifyProvider
252 * @param bool $notifyCustomers
253 *
254 * @throws QueryExecutionException
255 * @throws InvalidArgumentException
256 */
257 public function sendUpdatedNotifications(
258 $notificationService,
259 $appointment,
260 $notifyProvider,
261 $notifyCustomers
262 ) {
263 if ($notifyProvider) {
264 /** @var Collection $providerNotifications */
265 $providerNotifications = $notificationService->getByNameAndType(
266 "provider_appointment_updated",
267 $notificationService->getType()
268 );
269
270 $sendDefault = $notificationService->sendDefault($providerNotifications, $appointment->toArray());
271
272 /** @var Notification $providerNotification */
273 foreach ($providerNotifications->getItems() as $providerNotification) {
274 if (
275 $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
276 $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefault)
277 ) {
278 $appointmentArray = $appointment->toArray();
279 $appointmentArray['sendForAllBookings'] = true;
280
281 $notificationService->sendNotification(
282 $appointmentArray,
283 $providerNotification,
284 true
285 );
286 }
287 }
288 }
289
290 if ($notifyCustomers && $appointment->isNotifyParticipants()) {
291 /** @var Collection $customerNotifications */
292 $customerNotifications = $notificationService->getByNameAndType(
293 "customer_appointment_updated",
294 $notificationService->getType()
295 );
296
297 $sendDefault = $notificationService->sendDefault($customerNotifications, $appointment->toArray());
298
299 /** @var Notification $customerNotification */
300 foreach ($customerNotifications->getItems() as $customerNotification) {
301 if (
302 $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED &&
303 $notificationService->checkCustom($customerNotification, $appointment->toArray(), $sendDefault)
304 ) {
305 /** @var CustomerBooking $booking */
306 foreach ($appointment->getBookings()->getItems() as $bookingKey => $booking) {
307 if (
308 $booking->getStatus()->getValue() === BookingStatus::APPROVED &&
309 (!$booking->isNew() || !$booking->isNew()->getValue()) &&
310 $booking->isUpdated() &&
311 $booking->isUpdated()->getValue()
312 ) {
313 $notificationService->sendNotification(
314 $appointment->toArray(),
315 $customerNotification,
316 true,
317 $bookingKey
318 );
319 }
320 }
321 }
322 }
323 }
324 }
325
326 /**
327 * @param AbstractNotificationService $notificationService
328 * @param Event $event
329 * @param bool $logNotification
330 * @param int $bookingKey
331 *
332 *
333 * @throws InvalidArgumentException
334 * @throws QueryExecutionException
335 * @throws ContainerException
336 */
337 public function sendQrNotifications(
338 $notificationService,
339 $event,
340 $bookingKey,
341 $logNotification = true
342 ) {
343 $notifications = $notificationService->getByNameAndType(
344 "customer_event_qr_code",
345 $notificationService->getType()
346 );
347
348 $qrNotification = $notifications->getItem($notifications->keys()[0]);
349
350 if ($qrNotification->getStatus()->getValue() === NotificationStatus::ENABLED) {
351 $notificationService->sendNotification(
352 $event->toArray(),
353 $qrNotification,
354 $logNotification,
355 $bookingKey
356 );
357 }
358 }
359 }
360