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