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