AbstractNotificationService.php
6 months ago
AbstractWhatsAppNotificationService.php
6 months ago
ApplicationNotificationService.php
6 months ago
AppointmentNotificationService.php
6 months ago
BasicWhatsAppNotificationService.php
6 months ago
EmailNotificationService.php
6 months ago
NotificationHelperService.php
6 months ago
SMSAPIService.php
6 months ago
SMSNotificationService.php
6 months ago
AppointmentNotificationService.php
437 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. 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 | $booking->getStatus()->getValue() !== BookingStatus::WAITING |
| 161 | ) |
| 162 | ? $invoiceService->generateInvoice( |
| 163 | $booking->getPayments()->getItem(0)->getId()->getValue() |
| 164 | ) |
| 165 | : null |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param AbstractNotificationService $notificationService |
| 175 | * @param Appointment $appointment |
| 176 | * @param bool $notifyProvider |
| 177 | * @param bool $notifyCustomers |
| 178 | * |
| 179 | * @throws QueryExecutionException |
| 180 | * @throws InvalidArgumentException |
| 181 | */ |
| 182 | public function sendRescheduledNotifications( |
| 183 | $notificationService, |
| 184 | $appointment, |
| 185 | $notifyProvider = true, |
| 186 | $notifyCustomers = true |
| 187 | ) { |
| 188 | if ($notifyProvider) { |
| 189 | /** @var Collection $providerNotifications */ |
| 190 | $providerNotifications = $notificationService->getByNameAndType( |
| 191 | "provider_appointment_rescheduled", |
| 192 | $notificationService->getType() |
| 193 | ); |
| 194 | |
| 195 | $sendDefault = $notificationService->sendDefault($providerNotifications, $appointment->toArray()); |
| 196 | |
| 197 | /** @var Notification $providerNotification */ |
| 198 | foreach ($providerNotifications->getItems() as $providerNotification) { |
| 199 | if ( |
| 200 | $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 201 | $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefault) |
| 202 | ) { |
| 203 | $notificationService->sendNotification( |
| 204 | $appointment->toArray(), |
| 205 | $providerNotification, |
| 206 | true |
| 207 | ); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if ($notifyCustomers && $appointment->isNotifyParticipants()) { |
| 213 | /** @var Collection $customerNotifications */ |
| 214 | $customerNotifications = $notificationService->getByNameAndType( |
| 215 | "customer_appointment_rescheduled", |
| 216 | $notificationService->getType() |
| 217 | ); |
| 218 | |
| 219 | $sendDefault = $notificationService->sendDefault($customerNotifications, $appointment->toArray()); |
| 220 | |
| 221 | /** @var Notification $customerNotification */ |
| 222 | foreach ($customerNotifications->getItems() as $customerNotification) { |
| 223 | if ( |
| 224 | $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 225 | $notificationService->checkCustom($customerNotification, $appointment->toArray(), $sendDefault) |
| 226 | ) { |
| 227 | /** @var CustomerBooking $booking */ |
| 228 | foreach ($appointment->getBookings()->getItems() as $bookingKey => $booking) { |
| 229 | if ( |
| 230 | (!$booking->isNew() || !$booking->isNew()->getValue()) && |
| 231 | ( |
| 232 | $booking->getStatus()->getValue() === BookingStatus::APPROVED || |
| 233 | $booking->getStatus()->getValue() === BookingStatus::PENDING |
| 234 | ) |
| 235 | ) { |
| 236 | $notificationService->sendNotification( |
| 237 | $appointment->toArray(), |
| 238 | $customerNotification, |
| 239 | true, |
| 240 | $bookingKey |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Send waiting list available spot notifications. |
| 251 | * Triggered when a booking for an approved appointment is canceled and there are waiting bookings. |
| 252 | * Sends one notification to provider and one to each waiting customer. |
| 253 | * |
| 254 | * @param AbstractNotificationService $notificationService |
| 255 | * @param Appointment $appointment |
| 256 | * @param Collection $waitingBookings Collection of CustomerBooking objects with status 'waiting' |
| 257 | * |
| 258 | * @throws InvalidArgumentException |
| 259 | * @throws QueryExecutionException |
| 260 | */ |
| 261 | public function sendWaitingListAvailableSpotNotification( |
| 262 | $notificationService, |
| 263 | $appointment, |
| 264 | $waitingBookings |
| 265 | ) { |
| 266 | if (!$waitingBookings->length()) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // Provider notification (single) |
| 271 | /** @var Collection $providerNotifications */ |
| 272 | $providerNotifications = $notificationService->getByNameAndType( |
| 273 | 'provider_appointment_waiting_available_spot', |
| 274 | $notificationService->getType() |
| 275 | ); |
| 276 | $sendDefaultProvider = $notificationService->sendDefault($providerNotifications, $appointment->toArray()); |
| 277 | foreach ($providerNotifications->getItems() as $providerNotification) { |
| 278 | if ( |
| 279 | $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 280 | $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefaultProvider) |
| 281 | ) { |
| 282 | $notificationService->sendNotification( |
| 283 | $appointment->toArray(), |
| 284 | $providerNotification, |
| 285 | true |
| 286 | ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Customer notifications (each waiting booking) |
| 291 | /** @var Collection $customerNotifications */ |
| 292 | $customerNotifications = $notificationService->getByNameAndType( |
| 293 | 'customer_appointment_waiting_available_spot', |
| 294 | $notificationService->getType() |
| 295 | ); |
| 296 | $sendDefaultCustomer = $notificationService->sendDefault($customerNotifications, $appointment->toArray()); |
| 297 | foreach ($customerNotifications->getItems() as $customerNotification) { |
| 298 | if ( |
| 299 | $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 300 | $notificationService->checkCustom($customerNotification, $appointment->toArray(), $sendDefaultCustomer) |
| 301 | ) { |
| 302 | foreach ($waitingBookings->getItems() as $waitingBooking) { |
| 303 | $bookingKey = null; |
| 304 | foreach ($appointment->getBookings()->getItems() as $appBookingKey => $appBooking) { |
| 305 | if ($appBooking->getId()->getValue() === $waitingBooking->getId()->getValue()) { |
| 306 | $bookingKey = $appBookingKey; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | if ($bookingKey !== null) { |
| 311 | $notificationService->sendNotification( |
| 312 | array_merge($appointment->toArray(), [ |
| 313 | 'bookings' => $appointment->getBookings()->toArray(), |
| 314 | ]), |
| 315 | $customerNotification, |
| 316 | true, |
| 317 | $bookingKey |
| 318 | ); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @param AbstractNotificationService $notificationService |
| 327 | * @param Appointment $appointment |
| 328 | * @param bool $notifyProvider |
| 329 | * @param bool $notifyCustomers |
| 330 | * |
| 331 | * @throws QueryExecutionException |
| 332 | * @throws InvalidArgumentException |
| 333 | */ |
| 334 | public function sendUpdatedNotifications( |
| 335 | $notificationService, |
| 336 | $appointment, |
| 337 | $notifyProvider, |
| 338 | $notifyCustomers |
| 339 | ) { |
| 340 | if ($notifyProvider) { |
| 341 | /** @var Collection $providerNotifications */ |
| 342 | $providerNotifications = $notificationService->getByNameAndType( |
| 343 | "provider_appointment_updated", |
| 344 | $notificationService->getType() |
| 345 | ); |
| 346 | |
| 347 | $sendDefault = $notificationService->sendDefault($providerNotifications, $appointment->toArray()); |
| 348 | |
| 349 | /** @var Notification $providerNotification */ |
| 350 | foreach ($providerNotifications->getItems() as $providerNotification) { |
| 351 | if ( |
| 352 | $providerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 353 | $notificationService->checkCustom($providerNotification, $appointment->toArray(), $sendDefault) |
| 354 | ) { |
| 355 | $appointmentArray = $appointment->toArray(); |
| 356 | $appointmentArray['sendForAllBookings'] = true; |
| 357 | |
| 358 | $notificationService->sendNotification( |
| 359 | $appointmentArray, |
| 360 | $providerNotification, |
| 361 | true |
| 362 | ); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if ($notifyCustomers && $appointment->isNotifyParticipants()) { |
| 368 | /** @var Collection $customerNotifications */ |
| 369 | $customerNotifications = $notificationService->getByNameAndType( |
| 370 | "customer_appointment_updated", |
| 371 | $notificationService->getType() |
| 372 | ); |
| 373 | |
| 374 | $sendDefault = $notificationService->sendDefault($customerNotifications, $appointment->toArray()); |
| 375 | |
| 376 | /** @var Notification $customerNotification */ |
| 377 | foreach ($customerNotifications->getItems() as $customerNotification) { |
| 378 | if ( |
| 379 | $customerNotification->getStatus()->getValue() === NotificationStatus::ENABLED && |
| 380 | $notificationService->checkCustom($customerNotification, $appointment->toArray(), $sendDefault) |
| 381 | ) { |
| 382 | /** @var CustomerBooking $booking */ |
| 383 | foreach ($appointment->getBookings()->getItems() as $bookingKey => $booking) { |
| 384 | if ( |
| 385 | $booking->getStatus()->getValue() === BookingStatus::APPROVED && |
| 386 | (!$booking->isNew() || !$booking->isNew()->getValue()) && |
| 387 | $booking->isUpdated() && |
| 388 | $booking->isUpdated()->getValue() |
| 389 | ) { |
| 390 | $notificationService->sendNotification( |
| 391 | $appointment->toArray(), |
| 392 | $customerNotification, |
| 393 | true, |
| 394 | $bookingKey |
| 395 | ); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * @param AbstractNotificationService $notificationService |
| 405 | * @param Event $event |
| 406 | * @param bool $logNotification |
| 407 | * @param int $bookingKey |
| 408 | * |
| 409 | * |
| 410 | * @throws InvalidArgumentException |
| 411 | * @throws QueryExecutionException |
| 412 | * @throws ContainerException |
| 413 | */ |
| 414 | public function sendQrNotifications( |
| 415 | $notificationService, |
| 416 | $event, |
| 417 | $bookingKey, |
| 418 | $logNotification = true |
| 419 | ) { |
| 420 | $notifications = $notificationService->getByNameAndType( |
| 421 | "customer_event_qr_code", |
| 422 | $notificationService->getType() |
| 423 | ); |
| 424 | |
| 425 | $qrNotification = $notifications->getItem($notifications->keys()[0]); |
| 426 | |
| 427 | if ($qrNotification->getStatus()->getValue() === NotificationStatus::ENABLED) { |
| 428 | $notificationService->sendNotification( |
| 429 | $event->toArray(), |
| 430 | $qrNotification, |
| 431 | $logNotification, |
| 432 | $bookingKey |
| 433 | ); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 |