AppointmentFactory.php
2 weeks ago
CustomerBookingExtraFactory.php
6 months ago
CustomerBookingFactory.php
2 weeks ago
CustomerBookingFactory.php
364 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\Domain\Factory\Booking\Appointment; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 12 | use AmeliaBooking\Domain\Entity\Bookable\Service\PackageCustomerService; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 14 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageCustomerServiceFactory; |
| 15 | use AmeliaBooking\Domain\Factory\Booking\Event\CustomerBookingEventTicketFactory; |
| 16 | use AmeliaBooking\Domain\Factory\Coupon\CouponFactory; |
| 17 | use AmeliaBooking\Domain\Factory\Payment\PaymentFactory; |
| 18 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 19 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 20 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 21 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 23 | use AmeliaBooking\Domain\ValueObjects\Number\Float\Price; |
| 24 | use AmeliaBooking\Domain\ValueObjects\PositiveDuration; |
| 25 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 26 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 27 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 28 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 29 | |
| 30 | /** |
| 31 | * Class CustomerBookingFactory |
| 32 | * |
| 33 | * @package AmeliaBooking\Domain\Factory\Booking\Appointment |
| 34 | */ |
| 35 | class CustomerBookingFactory |
| 36 | { |
| 37 | /** |
| 38 | * @param $data |
| 39 | * |
| 40 | * @return CustomerBooking |
| 41 | * @throws InvalidArgumentException |
| 42 | */ |
| 43 | public static function create($data) |
| 44 | { |
| 45 | $customerBooking = new CustomerBooking(); |
| 46 | |
| 47 | if (isset($data['id'])) { |
| 48 | $customerBooking->setId(new Id($data['id'])); |
| 49 | } |
| 50 | |
| 51 | if (isset($data['customerId'])) { |
| 52 | $customerBooking->setCustomerId(new Id($data['customerId'])); |
| 53 | } |
| 54 | |
| 55 | if (isset($data['status'])) { |
| 56 | $customerBooking->setStatus(new BookingStatus($data['status'])); |
| 57 | } |
| 58 | |
| 59 | if (isset($data['persons'])) { |
| 60 | $customerBooking->setPersons(new IntegerValue($data['persons'])); |
| 61 | } |
| 62 | |
| 63 | if (isset($data['price'])) { |
| 64 | $customerBooking->setPrice(new Price($data['price'])); |
| 65 | } |
| 66 | |
| 67 | if (isset($data['appointmentId'])) { |
| 68 | $customerBooking->setAppointmentId(new Id($data['appointmentId'])); |
| 69 | } |
| 70 | |
| 71 | if (isset($data['couponId'])) { |
| 72 | $customerBooking->setCouponId(new Id($data['couponId'])); |
| 73 | } |
| 74 | |
| 75 | if (isset($data['coupon'])) { |
| 76 | $customerBooking->setCoupon(CouponFactory::create($data['coupon'])); |
| 77 | } |
| 78 | |
| 79 | if (isset($data['customer'])) { |
| 80 | $customerBooking->setCustomer(UserFactory::create($data['customer'])); |
| 81 | } |
| 82 | |
| 83 | if (isset($data['customFields'])) { |
| 84 | if (is_string($data['customFields'])) { |
| 85 | $customerBooking->setCustomFields(new Json($data['customFields'])); |
| 86 | } elseif (json_encode($data['customFields']) !== false) { |
| 87 | $customerBooking->setCustomFields(new Json(json_encode($data['customFields']))); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (isset($data['info'])) { |
| 92 | $customerBooking->setInfo(new Json($data['info'])); |
| 93 | } |
| 94 | |
| 95 | if (isset($data['qrCodes'])) { |
| 96 | if (is_string($data['qrCodes'])) { |
| 97 | $customerBooking->setQrCodes(new Json($data['qrCodes'])); |
| 98 | } elseif (json_encode($data['qrCodes']) !== false) { |
| 99 | $customerBooking->setQrCodes(new Json(json_encode($data['qrCodes']))); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (isset($data['utcOffset'])) { |
| 104 | $customerBooking->setUtcOffset(new IntegerValue($data['utcOffset'])); |
| 105 | } |
| 106 | |
| 107 | if (isset($data['aggregatedPrice'])) { |
| 108 | $customerBooking->setAggregatedPrice(new BooleanValueObject($data['aggregatedPrice'])); |
| 109 | } |
| 110 | |
| 111 | if (isset($data['isChangedStatus'])) { |
| 112 | $customerBooking->setChangedStatus(new BooleanValueObject($data['isChangedStatus'])); |
| 113 | } |
| 114 | |
| 115 | if (isset($data['isLastBooking'])) { |
| 116 | $customerBooking->setLastBooking(new BooleanValueObject($data['isLastBooking'])); |
| 117 | } |
| 118 | |
| 119 | if (isset($data['setIcsFiles'])) { |
| 120 | $customerBooking->setIcsFiles($data['setIcsFiles']); |
| 121 | } |
| 122 | |
| 123 | if (isset($data['isNew'])) { |
| 124 | $customerBooking->setNew(new BooleanValueObject($data['isNew'])); |
| 125 | } |
| 126 | |
| 127 | if (isset($data['isUpdated'])) { |
| 128 | $customerBooking->setUpdated(new BooleanValueObject($data['isUpdated'])); |
| 129 | } |
| 130 | |
| 131 | if (isset($data['deposit'])) { |
| 132 | $customerBooking->setDeposit(new BooleanValueObject($data['deposit'])); |
| 133 | } |
| 134 | |
| 135 | if (isset($data['packageCustomerService'])) { |
| 136 | /** @var PackageCustomerService $packageCustomerService */ |
| 137 | $packageCustomerService = PackageCustomerServiceFactory::create($data['packageCustomerService']); |
| 138 | |
| 139 | $customerBooking->setPackageCustomerService($packageCustomerService); |
| 140 | } |
| 141 | |
| 142 | if (isset($data['duration'])) { |
| 143 | $customerBooking->setDuration(new PositiveDuration($data['duration'])); |
| 144 | } |
| 145 | |
| 146 | $payments = new Collection(); |
| 147 | |
| 148 | if (isset($data['payments'])) { |
| 149 | foreach ((array)$data['payments'] as $key => $value) { |
| 150 | $payments->addItem( |
| 151 | PaymentFactory::create($value), |
| 152 | $key |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $customerBooking->setPayments($payments); |
| 158 | |
| 159 | $extras = new Collection(); |
| 160 | |
| 161 | if (isset($data['extras'])) { |
| 162 | foreach ((array)$data['extras'] as $key => $value) { |
| 163 | $extras->addItem( |
| 164 | CustomerBookingExtraFactory::create($value), |
| 165 | $key |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $customerBooking->setExtras($extras); |
| 171 | |
| 172 | if (isset($data['token'])) { |
| 173 | $customerBooking->setToken(new Token($data['token'])); |
| 174 | } |
| 175 | |
| 176 | $ticketsBooking = new Collection(); |
| 177 | |
| 178 | if (!empty($data['ticketsData'])) { |
| 179 | foreach ((array)$data['ticketsData'] as $key => $value) { |
| 180 | $ticketsBooking->addItem( |
| 181 | CustomerBookingEventTicketFactory::create($value), |
| 182 | $key |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | $customerBooking->setTicketsBooking($ticketsBooking); |
| 188 | |
| 189 | if (!empty($data['created'])) { |
| 190 | $customerBooking->setCreated(new DateTimeValue(DateTimeService::getCustomDateTimeObjectFromUtc($data['created']))); |
| 191 | } |
| 192 | |
| 193 | if (!empty($data['tax'])) { |
| 194 | if (is_string($data['tax'])) { |
| 195 | $customerBooking->setTax(new Json($data['tax'])); |
| 196 | } elseif (json_encode($data['tax']) !== false) { |
| 197 | $customerBooking->setTax(new Json(json_encode($data['tax']))); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (!empty($data['ivyEntryId'])) { |
| 202 | $customerBooking->setIvyEntryId(new Id($data['ivyEntryId'])); |
| 203 | } |
| 204 | |
| 205 | return $customerBooking; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param array $rows |
| 210 | * |
| 211 | * @return array |
| 212 | */ |
| 213 | public static function reformat($rows) |
| 214 | { |
| 215 | $data = []; |
| 216 | |
| 217 | foreach ($rows as $row) { |
| 218 | $id = (int)$row['booking_id']; |
| 219 | |
| 220 | $customerId = !empty($row['customer_id']) ? (int)$row['customer_id'] : null; |
| 221 | |
| 222 | $paymentId = !empty($row['payment_id']) ? (int)$row['payment_id'] : null; |
| 223 | |
| 224 | $couponId = !empty($row['coupon_id']) ? (int)$row['coupon_id'] : null; |
| 225 | |
| 226 | $bookingEventTicketId = !empty($row['booking_ticket_id']) ? (int)$row['booking_ticket_id'] : null; |
| 227 | |
| 228 | $eventPeriodId = !empty($row['event_periodId']) ? (int)$row['event_periodId'] : null; |
| 229 | |
| 230 | $eventProviderId = !empty($row['provider_id']) ? (int)$row['provider_id'] : null; |
| 231 | |
| 232 | $eventId = !empty($row['event_id']) ? (int)$row['event_id'] : null; |
| 233 | |
| 234 | if ($id && empty($data[$id])) { |
| 235 | $data[$id] = [ |
| 236 | 'id' => $id, |
| 237 | 'appointmentId' => isset($row['booking_appointmentId']) ? (int)$row['booking_appointmentId'] : null, |
| 238 | 'customerId' => isset($row['booking_customerId']) ? (int)$row['booking_customerId'] : null, |
| 239 | 'status' => $row['booking_status'], |
| 240 | 'price' => $row['booking_price'], |
| 241 | 'persons' => isset($row['booking_persons']) ? (int)$row['booking_persons'] : null, |
| 242 | 'couponId' => isset($row['booking_couponId']) ? (int)$row['booking_couponId'] : null, |
| 243 | 'customFields' => !empty($row['booking_customFields']) ? $row['booking_customFields'] : null, |
| 244 | 'info' => !empty($row['booking_info']) ? $row['booking_info'] : null, |
| 245 | 'utcOffset' => isset($row['booking_utcOffset']) ? (int)$row['booking_utcOffset'] : null, |
| 246 | 'aggregatedPrice' => isset($row['booking_aggregatedPrice']) ? (int)$row['booking_aggregatedPrice'] : null, |
| 247 | 'duration' => !empty($row['booking_duration']) ? (int)$row['booking_duration'] : null, |
| 248 | 'token' => isset($row['booking_token']) ? $row['booking_token'] : null, |
| 249 | 'tax' => isset($row['booking_tax']) ? $row['booking_tax'] : null, |
| 250 | 'qrCodes' => isset($row['booking_qrCodes']) ? $row['booking_qrCodes'] : (isset($row['qrCodes']) ? $row['qrCodes'] : null), |
| 251 | 'created' => isset($row['booking_created']) ? $row['booking_created'] : null, |
| 252 | 'ivyEntryId' => isset($row['booking_ivyEntryId']) ? (int)$row['booking_ivyEntryId'] : null, |
| 253 | ]; |
| 254 | } |
| 255 | |
| 256 | if ($id && $customerId && empty($data[$id]['customer'])) { |
| 257 | $data[$id]['customer'] = [ |
| 258 | 'id' => $customerId, |
| 259 | 'firstName' => $row['customer_firstName'], |
| 260 | 'lastName' => $row['customer_lastName'], |
| 261 | 'email' => $row['customer_email'], |
| 262 | 'note' => $row['customer_note'], |
| 263 | 'phone' => $row['customer_phone'], |
| 264 | 'countryPhoneIso' => !empty($row['customer_countryPhoneIso']) ? $row['customer_countryPhoneIso'] : null, |
| 265 | 'gender' => $row['customer_gender'], |
| 266 | 'birthday' => $row['customer_birthday'], |
| 267 | 'customFields' => !empty($row['customer_customFields']) ? $row['customer_customFields'] : null, |
| 268 | ]; |
| 269 | } |
| 270 | |
| 271 | if ($id && $paymentId && empty($data[$id]['payments'][$paymentId])) { |
| 272 | $data[$id]['payments'][$paymentId] = [ |
| 273 | 'id' => $paymentId, |
| 274 | 'customerBookingId' => $id, |
| 275 | 'amount' => $row['payment_amount'], |
| 276 | 'dateTime' => $row['payment_dateTime'], |
| 277 | 'created' => !empty($row['payment_created']) ? $row['payment_created'] : null, |
| 278 | 'status' => $row['payment_status'], |
| 279 | 'gateway' => $row['payment_gateway'], |
| 280 | 'gatewayTitle' => $row['payment_gatewayTitle'], |
| 281 | 'transactionId' => !empty($row['payment_transactionId']) ? $row['payment_transactionId'] : null, |
| 282 | 'parentId' => isset($row['payment_parentId']) ? (int)$row['payment_parentId'] : null, |
| 283 | 'data' => $row['payment_data'], |
| 284 | 'wcOrderId' => isset($row['payment_wcOrderId']) ? (int)$row['payment_wcOrderId'] : null, |
| 285 | 'wcOrderItemId' => isset($row['payment_wcOrderItemId']) ? (int)$row['payment_wcOrderItemId'] : null, |
| 286 | 'invoiceNumber' => isset($row['payment_invoiceNumber']) ? (int)$row['payment_invoiceNumber'] : null |
| 287 | ]; |
| 288 | } |
| 289 | |
| 290 | if ($id && $couponId && empty($data[$id]['coupon'])) { |
| 291 | $data[$id]['coupon'] = [ |
| 292 | 'id' => $couponId, |
| 293 | 'code' => $row['coupon_code'], |
| 294 | 'discount' => $row['coupon_discount'], |
| 295 | 'deduction' => $row['coupon_deduction'], |
| 296 | 'limit' => isset($row['coupon_limit']) ? (int)$row['coupon_limit'] : null, |
| 297 | 'customerLimit' => isset($row['coupon_customerLimit']) ? (int)$row['coupon_customerLimit'] : null, |
| 298 | 'status' => $row['coupon_status'], |
| 299 | ]; |
| 300 | } |
| 301 | |
| 302 | if ($id && $bookingEventTicketId && empty($data[$id]['ticketsData'][$bookingEventTicketId])) { |
| 303 | $data[$id]['ticketsData'][$bookingEventTicketId] = [ |
| 304 | 'id' => $bookingEventTicketId, |
| 305 | 'eventTicketId' => (int)$row['booking_ticket_eventTicketId'], |
| 306 | 'customerBookingId' => $id, |
| 307 | 'persons' => (int)$row['booking_ticket_persons'], |
| 308 | 'price' => $row['booking_ticket_price'], |
| 309 | ]; |
| 310 | } |
| 311 | |
| 312 | if ($id && $eventId && empty($data[$id]['event'])) { |
| 313 | $data[$id]['event'] = [ |
| 314 | 'id' => $eventId, |
| 315 | 'name' => $row['event_name'], |
| 316 | 'status' => $row['event_status'], |
| 317 | 'customPricing' => $row['event_customPricing'], |
| 318 | 'organizerId' => isset($row['event_organizerId']) ? (int)$row['event_organizerId'] : null, |
| 319 | 'settings' => $row['event_settings'], |
| 320 | 'isWaitingList' => $row['event_settings'] |
| 321 | ? json_decode($row['event_settings'], true)['waitingList']['enabled'] |
| 322 | : false, |
| 323 | ]; |
| 324 | } |
| 325 | |
| 326 | if ($id && $eventProviderId) { |
| 327 | if ($data[$id]['event']['organizerId'] === $eventProviderId && empty($data[$id]['event']['organizer'])) { |
| 328 | $data[$id]['event']['organizer'] = [ |
| 329 | 'id' => $eventProviderId, |
| 330 | 'firstName' => $row['provider_firstName'], |
| 331 | 'lastName' => $row['provider_lastName'], |
| 332 | 'email' => $row['provider_email'], |
| 333 | 'picture' => $row['provider_pictureThumbPath'], |
| 334 | 'badgeId' => $row['provider_badgeId'], |
| 335 | ]; |
| 336 | } elseif ($data[$id]['event']['organizerId'] !== $eventProviderId && empty($data[$id]['event']['providers'][$eventProviderId])) { |
| 337 | $data[$id]['event']['providers'][$eventProviderId] = [ |
| 338 | 'id' => $eventProviderId, |
| 339 | 'firstName' => $row['provider_firstName'], |
| 340 | 'lastName' => $row['provider_lastName'], |
| 341 | 'email' => $row['provider_email'], |
| 342 | 'picture' => $row['provider_pictureThumbPath'], |
| 343 | 'badgeId' => $row['provider_badgeId'], |
| 344 | ]; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if ($id && $eventPeriodId && empty($data[$id]['event']['periods'][$eventPeriodId])) { |
| 349 | $data[$id]['event']['periods'][$eventPeriodId] = [ |
| 350 | 'id' => $eventPeriodId, |
| 351 | 'periodStart' => $row['event_periodStart'], |
| 352 | 'periodEnd' => $row['event_periodEnd'], |
| 353 | 'zoomMeeting' => $row['event_zoomMeeting'], |
| 354 | 'googleMeetUrl' => $row['event_googleMeetUrl'], |
| 355 | 'microsoftTeamsUrl' => $row['event_microsoftTeamsUrl'], |
| 356 | 'lessonSpace' => $row['event_lessonSpace'], |
| 357 | ]; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return $data; |
| 362 | } |
| 363 | } |
| 364 |