AppointmentFactory.php
1 year ago
CustomerBookingExtraFactory.php
1 year ago
CustomerBookingFactory.php
1 year ago
AppointmentFactory.php
384 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Domain\Factory\Booking\Appointment; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 12 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 13 | use AmeliaBooking\Domain\Factory\Location\LocationFactory; |
| 14 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 15 | use AmeliaBooking\Domain\Factory\Zoom\ZoomFactory; |
| 16 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 17 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 18 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 19 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 20 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 21 | use AmeliaBooking\Domain\ValueObjects\String\Description; |
| 22 | use AmeliaBooking\Domain\ValueObjects\String\Label; |
| 23 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 24 | |
| 25 | /** |
| 26 | * Class AppointmentFactory |
| 27 | * |
| 28 | * @package AmeliaBooking\Domain\Factory\Booking\Appointment |
| 29 | */ |
| 30 | class AppointmentFactory |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * @param $data |
| 35 | * |
| 36 | * @return Appointment |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public static function create($data) |
| 40 | { |
| 41 | $appointment = new Appointment( |
| 42 | new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['bookingStart'])), |
| 43 | new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['bookingEnd'])), |
| 44 | $data['notifyParticipants'], |
| 45 | new Id($data['serviceId']), |
| 46 | new Id($data['providerId']) |
| 47 | ); |
| 48 | |
| 49 | if (!empty($data['id'])) { |
| 50 | $appointment->setId(new Id($data['id'])); |
| 51 | } |
| 52 | |
| 53 | if (!empty($data['parentId'])) { |
| 54 | $appointment->setParentId(new Id($data['parentId'])); |
| 55 | } |
| 56 | |
| 57 | if (!empty($data['locationId'])) { |
| 58 | $appointment->setLocationId(new Id($data['locationId'])); |
| 59 | } |
| 60 | |
| 61 | if (!empty($data['location'])) { |
| 62 | $appointment->setLocation(LocationFactory::create($data['location'])); |
| 63 | } |
| 64 | |
| 65 | if (isset($data['internalNotes'])) { |
| 66 | $appointment->setInternalNotes(new Description($data['internalNotes'])); |
| 67 | } |
| 68 | |
| 69 | if (isset($data['status'])) { |
| 70 | $appointment->setStatus(new BookingStatus($data['status'])); |
| 71 | } |
| 72 | |
| 73 | if (isset($data['provider'])) { |
| 74 | $appointment->setProvider(UserFactory::create($data['provider'])); |
| 75 | } |
| 76 | |
| 77 | if (isset($data['service'])) { |
| 78 | $appointment->setService(ServiceFactory::create($data['service'])); |
| 79 | } |
| 80 | |
| 81 | if (!empty($data['googleCalendarEventId'])) { |
| 82 | $appointment->setGoogleCalendarEventId(new Token($data['googleCalendarEventId'])); |
| 83 | } |
| 84 | |
| 85 | if (!empty($data['googleMeetUrl'])) { |
| 86 | $appointment->setGoogleMeetUrl($data['googleMeetUrl']); |
| 87 | } |
| 88 | |
| 89 | if (!empty($data['outlookCalendarEventId'])) { |
| 90 | $appointment->setOutlookCalendarEventId(new Label($data['outlookCalendarEventId'])); |
| 91 | } |
| 92 | |
| 93 | if (!empty($data['microsoftTeamsUrl'])) { |
| 94 | $appointment->setMicrosoftTeamsUrl($data['microsoftTeamsUrl']); |
| 95 | } |
| 96 | |
| 97 | if (!empty($data['appleCalendarEventId'])) { |
| 98 | $appointment->setAppleCalendarEventId(new Label($data['appleCalendarEventId'])); |
| 99 | } |
| 100 | |
| 101 | if (!empty($data['zoomMeeting']['id'])) { |
| 102 | $zoomMeeting = ZoomFactory::create( |
| 103 | $data['zoomMeeting'] |
| 104 | ); |
| 105 | |
| 106 | $appointment->setZoomMeeting($zoomMeeting); |
| 107 | } |
| 108 | |
| 109 | if (isset($data['lessonSpace']) && !empty($data['lessonSpace'])) { |
| 110 | $appointment->setLessonSpace($data['lessonSpace']); |
| 111 | } |
| 112 | |
| 113 | if (isset($data['isRescheduled'])) { |
| 114 | $appointment->setRescheduled(new BooleanValueObject($data['isRescheduled'])); |
| 115 | } |
| 116 | |
| 117 | if (array_key_exists('isChangedStatus', $data)) { |
| 118 | $appointment->setChangedStatus(new BooleanValueObject($data['isChangedStatus'])); |
| 119 | } |
| 120 | |
| 121 | if (!empty($data['initialAppointmentDateTime']['bookingStart'])) { |
| 122 | $appointment->setInitialBookingStart( |
| 123 | new DateTimeValue( |
| 124 | DateTimeService::getCustomDateTimeObject($data['initialAppointmentDateTime']['bookingStart']) |
| 125 | ) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | if (!empty($data['initialAppointmentDateTime']['bookingEnd'])) { |
| 130 | $appointment->setInitialBookingEnd( |
| 131 | new DateTimeValue( |
| 132 | DateTimeService::getCustomDateTimeObject($data['initialAppointmentDateTime']['bookingEnd']) |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | $bookings = new Collection(); |
| 138 | |
| 139 | if (isset($data['bookings'])) { |
| 140 | foreach ((array)$data['bookings'] as $key => $value) { |
| 141 | $bookings->addItem( |
| 142 | CustomerBookingFactory::create($value), |
| 143 | $key |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | $appointment->setBookings($bookings); |
| 149 | |
| 150 | return $appointment; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @param array $rows |
| 155 | * |
| 156 | * @return Collection |
| 157 | * @throws InvalidArgumentException |
| 158 | */ |
| 159 | public static function createCollection($rows) |
| 160 | { |
| 161 | $appointments = []; |
| 162 | |
| 163 | foreach ($rows as $row) { |
| 164 | $appointmentId = $row['appointment_id']; |
| 165 | $bookingId = isset($row['booking_id']) ? $row['booking_id'] : null; |
| 166 | $bookingExtraId = isset($row['bookingExtra_id']) ? $row['bookingExtra_id'] : null; |
| 167 | $paymentId = isset($row['payment_id']) ? $row['payment_id'] : null; |
| 168 | $couponId = isset($row['coupon_id']) ? $row['coupon_id'] : null; |
| 169 | $customerId = isset($row['customer_id']) ? $row['customer_id'] : null; |
| 170 | $providerId = isset($row['provider_id']) ? $row['provider_id'] : null; |
| 171 | $locationId = isset($row['location_id']) ? $row['location_id'] : null; |
| 172 | $serviceId = isset($row['service_id']) ? $row['service_id'] : null; |
| 173 | |
| 174 | if (!array_key_exists($appointmentId, $appointments)) { |
| 175 | $zoomMeetingJson = !empty($row['appointment_zoom_meeting']) ? |
| 176 | json_decode($row['appointment_zoom_meeting'], true) : null; |
| 177 | |
| 178 | $appointments[$appointmentId] = [ |
| 179 | 'id' => $appointmentId, |
| 180 | 'parentId' => isset($row['appointment_parentId']) ? |
| 181 | $row['appointment_parentId'] : null, |
| 182 | 'bookingStart' => DateTimeService::getCustomDateTimeFromUtc( |
| 183 | $row['appointment_bookingStart'] |
| 184 | ), |
| 185 | 'bookingEnd' => DateTimeService::getCustomDateTimeFromUtc( |
| 186 | $row['appointment_bookingEnd'] |
| 187 | ), |
| 188 | 'notifyParticipants' => isset($row['appointment_notifyParticipants']) ? |
| 189 | $row['appointment_notifyParticipants'] : null, |
| 190 | 'serviceId' => $row['appointment_serviceId'], |
| 191 | 'providerId' => $row['appointment_providerId'], |
| 192 | 'locationId' => isset($row['appointment_locationId']) ? |
| 193 | $row['appointment_locationId'] : null, |
| 194 | 'internalNotes' => isset($row['appointment_internalNotes']) ? |
| 195 | $row['appointment_internalNotes'] : null, |
| 196 | 'status' => $row['appointment_status'], |
| 197 | 'googleCalendarEventId' => isset($row['appointment_google_calendar_event_id']) ? |
| 198 | $row['appointment_google_calendar_event_id'] : null, |
| 199 | 'googleMeetUrl' => isset($row['appointment_google_meet_url']) ? |
| 200 | $row['appointment_google_meet_url'] : null, |
| 201 | 'outlookCalendarEventId' => isset($row['appointment_outlook_calendar_event_id']) ? |
| 202 | $row['appointment_outlook_calendar_event_id'] : null, |
| 203 | 'microsoftTeamsUrl' => isset($row['appointment_microsoft_teams_url']) ? |
| 204 | $row['appointment_microsoft_teams_url'] : null, |
| 205 | 'appleCalendarEventId' => isset($row['appointment_apple_calendar_event_id']) ? |
| 206 | $row['appointment_apple_calendar_event_id'] : null, |
| 207 | 'zoomMeeting' => [ |
| 208 | 'id' => $zoomMeetingJson ? $zoomMeetingJson['id'] : null, |
| 209 | 'startUrl' => $zoomMeetingJson ? $zoomMeetingJson['startUrl'] : null, |
| 210 | 'joinUrl' => $zoomMeetingJson ? $zoomMeetingJson['joinUrl'] : null, |
| 211 | ], |
| 212 | 'lessonSpace' => !empty($row['appointment_lesson_space']) ? $row['appointment_lesson_space'] : null, |
| 213 | ]; |
| 214 | } |
| 215 | |
| 216 | if ($bookingId && !isset($appointments[$appointmentId]['bookings'][$bookingId])) { |
| 217 | $appointments[$appointmentId]['bookings'][$bookingId] = [ |
| 218 | 'id' => $bookingId, |
| 219 | 'appointmentId' => $appointmentId, |
| 220 | 'customerId' => $row['booking_customerId'], |
| 221 | 'status' => $row['booking_status'], |
| 222 | 'couponId' => $couponId, |
| 223 | 'price' => $row['booking_price'], |
| 224 | 'persons' => $row['booking_persons'], |
| 225 | 'customFields' => isset($row['booking_customFields']) ? $row['booking_customFields'] : null, |
| 226 | 'info' => isset($row['booking_info']) ? $row['booking_info'] : null, |
| 227 | 'utcOffset' => isset($row['booking_utcOffset']) ? $row['booking_utcOffset'] : null, |
| 228 | 'aggregatedPrice' => isset($row['booking_aggregatedPrice']) ? |
| 229 | $row['booking_aggregatedPrice'] : null, |
| 230 | 'packageCustomerService' => !empty($row['booking_packageCustomerServiceId']) ? [ |
| 231 | 'id' => $row['booking_packageCustomerServiceId'], |
| 232 | 'serviceId' => !empty($row['package_customer_service_serviceId']) ? |
| 233 | $row['package_customer_service_serviceId'] : null, |
| 234 | 'bookingsCount' => !empty($row['package_customer_service_bookingsCount']) ? |
| 235 | $row['package_customer_service_bookingsCount'] : null, |
| 236 | 'packageCustomer' => [ |
| 237 | 'id' => !empty($row['package_customer_id']) ? |
| 238 | $row['package_customer_id'] : null, |
| 239 | 'packageId' => !empty($row['package_customer_packageId']) ? |
| 240 | $row['package_customer_packageId'] : null, |
| 241 | 'price' => !empty($row['package_customer_price']) ? |
| 242 | $row['package_customer_price'] : null, |
| 243 | 'couponId' => !empty($row['package_customer_couponId']) ? |
| 244 | $row['package_customer_couponId'] : null, |
| 245 | 'tax' => !empty($row['package_customer_tax']) ? |
| 246 | $row['package_customer_tax'] : null, |
| 247 | ] |
| 248 | ] : null, |
| 249 | 'duration' => isset($row['booking_duration']) ? $row['booking_duration'] : null, |
| 250 | 'created' => !empty($row['booking_created']) ? DateTimeService::getCustomDateTimeFromUtc($row['booking_created']) : null, |
| 251 | 'tax' => isset($row['booking_tax']) ? $row['booking_tax'] : null, |
| 252 | ]; |
| 253 | } |
| 254 | |
| 255 | if ($bookingId && $bookingExtraId) { |
| 256 | $appointments[$appointmentId]['bookings'][$bookingId]['extras'][$bookingExtraId] = |
| 257 | [ |
| 258 | 'id' => $bookingExtraId, |
| 259 | 'customerBookingId' => $bookingId, |
| 260 | 'extraId' => $row['bookingExtra_extraId'], |
| 261 | 'quantity' => $row['bookingExtra_quantity'], |
| 262 | 'price' => $row['bookingExtra_price'], |
| 263 | 'aggregatedPrice' => $row['bookingExtra_aggregatedPrice'], |
| 264 | 'tax' => isset($row['bookingExtra_tax']) ? $row['bookingExtra_tax'] : null, |
| 265 | ]; |
| 266 | } |
| 267 | |
| 268 | if ($bookingId && $paymentId) { |
| 269 | $appointments[$appointmentId]['bookings'][$bookingId]['payments'][$paymentId] = |
| 270 | [ |
| 271 | 'id' => $paymentId, |
| 272 | 'customerBookingId' => $bookingId, |
| 273 | 'packageCustomerId' => !empty($row['payment_packageCustomerId']) ? $row['payment_packageCustomerId'] : null, |
| 274 | 'status' => $row['payment_status'], |
| 275 | 'dateTime' => DateTimeService::getCustomDateTimeFromUtc($row['payment_dateTime']), |
| 276 | 'gateway' => $row['payment_gateway'], |
| 277 | 'gatewayTitle' => $row['payment_gatewayTitle'], |
| 278 | 'transactionId' => !empty($row['payment_transactionId']) ? $row['payment_transactionId'] : null, |
| 279 | 'parentId' => !empty($row['payment_parentId']) ? $row['payment_parentId'] : null, |
| 280 | 'amount' => $row['payment_amount'], |
| 281 | 'data' => $row['payment_data'], |
| 282 | 'invoiceNumber' => !empty($row['payment_invoiceNumber']) ? $row['payment_invoiceNumber'] : null, |
| 283 | 'wcOrderId' => !empty($row['payment_wcOrderId']) ? $row['payment_wcOrderId'] : null, |
| 284 | 'wcOrderItemId' => !empty($row['payment_wcOrderItemId']) ? |
| 285 | $row['payment_wcOrderItemId'] : null, |
| 286 | 'created' => !empty($row['payment_created']) ? $row['payment_created'] : null, |
| 287 | ]; |
| 288 | } |
| 289 | |
| 290 | if ($bookingId && $couponId) { |
| 291 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['id'] = $couponId; |
| 292 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['code'] = $row['coupon_code']; |
| 293 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['discount'] = $row['coupon_discount']; |
| 294 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['deduction'] = $row['coupon_deduction']; |
| 295 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['limit'] = $row['coupon_limit']; |
| 296 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['customerLimit'] = $row['coupon_customerLimit']; |
| 297 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['status'] = $row['coupon_status']; |
| 298 | $appointments[$appointmentId]['bookings'][$bookingId]['coupon']['expirationDate'] = $row['coupon_expirationDate']; |
| 299 | } |
| 300 | |
| 301 | if ($bookingId && $customerId) { |
| 302 | $appointments[$appointmentId]['bookings'][$bookingId]['customer'] = |
| 303 | [ |
| 304 | 'id' => $customerId, |
| 305 | 'firstName' => $row['customer_firstName'], |
| 306 | 'lastName' => $row['customer_lastName'], |
| 307 | 'email' => $row['customer_email'], |
| 308 | 'note' => $row['customer_note'], |
| 309 | 'phone' => $row['customer_phone'], |
| 310 | 'gender' => $row['customer_gender'], |
| 311 | 'status' => $row['customer_status'], |
| 312 | 'type' => 'customer', |
| 313 | ]; |
| 314 | } |
| 315 | |
| 316 | if ($bookingId && $locationId) { |
| 317 | $appointments[$appointmentId]['location'] = |
| 318 | [ |
| 319 | 'id' => $locationId, |
| 320 | 'name' => !empty($row['location_name']) ? $row['location_name'] : '', |
| 321 | 'address' => !empty($row['location_address']) ? $row['location_address'] : '', |
| 322 | 'description' => !empty($row['location_description']) ? $row['location_description'] : null, |
| 323 | 'status' => !empty($row['location_status']) ? $row['location_status'] : null, |
| 324 | 'phone' => !empty($row['location_phone']) ? $row['location_phone'] : null, |
| 325 | 'latitude' => !empty($row['location_latitude']) ? $row['location_latitude'] : null, |
| 326 | 'longitude' => !empty($row['location_longitude']) ? $row['location_longitude'] : null, |
| 327 | 'pictureFullPath' => !empty($row['location_pictureFullPath']) ? $row['location_pictureFullPath'] : null, |
| 328 | 'pictureThumbPath' => !empty($row['location_pictureThumbPath']) ? $row['location_pictureThumbPath'] : null, |
| 329 | 'pin' => !empty($row['location_pin']) ? $row['location_pin'] : null, |
| 330 | 'translations' => !empty($row['location_translations']) ? $row['location_translations'] : null |
| 331 | ]; |
| 332 | } |
| 333 | |
| 334 | if ($bookingId && $providerId) { |
| 335 | $appointments[$appointmentId]['provider'] = |
| 336 | [ |
| 337 | 'id' => $providerId, |
| 338 | 'firstName' => $row['provider_firstName'], |
| 339 | 'lastName' => $row['provider_lastName'], |
| 340 | 'email' => $row['provider_email'], |
| 341 | 'note' => $row['provider_note'], |
| 342 | 'description' => $row['provider_description'], |
| 343 | 'phone' => $row['provider_phone'], |
| 344 | 'gender' => $row['provider_gender'], |
| 345 | 'timeZone' => !empty($row['provider_timeZone']) ? $row['provider_timeZone'] : null, |
| 346 | 'type' => 'provider', |
| 347 | ]; |
| 348 | } |
| 349 | |
| 350 | if ($serviceId) { |
| 351 | $appointments[$appointmentId]['service']['id'] = $row['service_id']; |
| 352 | $appointments[$appointmentId]['service']['name'] = $row['service_name']; |
| 353 | $appointments[$appointmentId]['service']['description'] = $row['service_description']; |
| 354 | $appointments[$appointmentId]['service']['color'] = $row['service_color']; |
| 355 | $appointments[$appointmentId]['service']['price'] = $row['service_price']; |
| 356 | $appointments[$appointmentId]['service']['status'] = $row['service_status']; |
| 357 | $appointments[$appointmentId]['service']['categoryId'] = $row['service_categoryId']; |
| 358 | $appointments[$appointmentId]['service']['minCapacity'] = $row['service_minCapacity']; |
| 359 | $appointments[$appointmentId]['service']['maxCapacity'] = $row['service_maxCapacity']; |
| 360 | $appointments[$appointmentId]['service']['duration'] = $row['service_duration']; |
| 361 | $appointments[$appointmentId]['service']['timeBefore'] = isset($row['service_timeBefore']) |
| 362 | ? $row['service_timeBefore'] : null; |
| 363 | $appointments[$appointmentId]['service']['timeAfter'] = isset($row['service_timeAfter']) |
| 364 | ? $row['service_timeAfter'] : null; |
| 365 | $appointments[$appointmentId]['service']['aggregatedPrice'] = isset($row['service_aggregatedPrice']) |
| 366 | ? $row['service_aggregatedPrice'] : null; |
| 367 | $appointments[$appointmentId]['service']['settings'] = isset($row['service_settings']) |
| 368 | ? $row['service_settings'] : null; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | $collection = new Collection(); |
| 373 | |
| 374 | foreach ($appointments as $key => $value) { |
| 375 | $collection->addItem( |
| 376 | self::create($value), |
| 377 | $key |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | return $collection; |
| 382 | } |
| 383 | } |
| 384 |