AppointmentFactory.php
1 year ago
CustomerBookingExtraFactory.php
2 years ago
CustomerBookingFactory.php
1 year ago
CustomerBookingFactory.php
281 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\Bookable\Service\PackageCustomerService; |
| 12 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 13 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageCustomerServiceFactory; |
| 14 | use AmeliaBooking\Domain\Factory\Booking\Event\CustomerBookingEventTicketFactory; |
| 15 | use AmeliaBooking\Domain\Factory\Coupon\CouponFactory; |
| 16 | use AmeliaBooking\Domain\Factory\Payment\PaymentFactory; |
| 17 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 18 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 19 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 20 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 21 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Number\Float\Price; |
| 23 | use AmeliaBooking\Domain\ValueObjects\PositiveDuration; |
| 24 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 25 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 26 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 27 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 28 | |
| 29 | /** |
| 30 | * Class CustomerBookingFactory |
| 31 | * |
| 32 | * @package AmeliaBooking\Domain\Factory\Booking\Appointment |
| 33 | */ |
| 34 | class CustomerBookingFactory |
| 35 | { |
| 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 | } else if (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['utcOffset'])) { |
| 96 | $customerBooking->setUtcOffset(new IntegerValue($data['utcOffset'])); |
| 97 | } |
| 98 | |
| 99 | if (isset($data['aggregatedPrice'])) { |
| 100 | $customerBooking->setAggregatedPrice(new BooleanValueObject($data['aggregatedPrice'])); |
| 101 | } |
| 102 | |
| 103 | if (isset($data['isChangedStatus'])) { |
| 104 | $customerBooking->setChangedStatus(new BooleanValueObject($data['isChangedStatus'])); |
| 105 | } |
| 106 | |
| 107 | if (isset($data['isLastBooking'])) { |
| 108 | $customerBooking->setLastBooking(new BooleanValueObject($data['isLastBooking'])); |
| 109 | } |
| 110 | |
| 111 | if (isset($data['deposit'])) { |
| 112 | $customerBooking->setDeposit(new BooleanValueObject($data['deposit'])); |
| 113 | } |
| 114 | |
| 115 | if (isset($data['packageCustomerService'])) { |
| 116 | /** @var PackageCustomerService $packageCustomerService */ |
| 117 | $packageCustomerService = PackageCustomerServiceFactory::create($data['packageCustomerService']); |
| 118 | |
| 119 | $customerBooking->setPackageCustomerService($packageCustomerService); |
| 120 | } |
| 121 | |
| 122 | if (isset($data['duration'])) { |
| 123 | $customerBooking->setDuration(new PositiveDuration($data['duration'])); |
| 124 | } |
| 125 | |
| 126 | $payments = new Collection(); |
| 127 | |
| 128 | if (isset($data['payments'])) { |
| 129 | foreach ((array)$data['payments'] as $key => $value) { |
| 130 | $payments->addItem( |
| 131 | PaymentFactory::create($value), |
| 132 | $key |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $customerBooking->setPayments($payments); |
| 138 | |
| 139 | $extras = new Collection(); |
| 140 | |
| 141 | if (isset($data['extras'])) { |
| 142 | foreach ((array)$data['extras'] as $key => $value) { |
| 143 | $extras->addItem( |
| 144 | CustomerBookingExtraFactory::create($value), |
| 145 | $key |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $customerBooking->setExtras($extras); |
| 151 | |
| 152 | if (isset($data['token'])) { |
| 153 | $customerBooking->setToken(new Token($data['token'])); |
| 154 | } |
| 155 | |
| 156 | $ticketsBooking = new Collection(); |
| 157 | |
| 158 | if (!empty($data['ticketsData'])) { |
| 159 | foreach ((array)$data['ticketsData'] as $key => $value) { |
| 160 | $ticketsBooking->addItem( |
| 161 | CustomerBookingEventTicketFactory::create($value), |
| 162 | $key |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $customerBooking->setTicketsBooking($ticketsBooking); |
| 168 | |
| 169 | if (!empty($data['created'])) { |
| 170 | $customerBooking->setCreated(new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['created']))); |
| 171 | } |
| 172 | |
| 173 | if (!empty($data['tax'])) { |
| 174 | if (is_string($data['tax'])) { |
| 175 | $customerBooking->setTax(new Json($data['tax'])); |
| 176 | } else if (json_encode($data['tax']) !== false) { |
| 177 | $customerBooking->setTax(new Json(json_encode($data['tax']))); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $customerBooking; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param array $rows |
| 186 | * |
| 187 | * @return array |
| 188 | */ |
| 189 | public static function reformat($rows) |
| 190 | { |
| 191 | $data = []; |
| 192 | |
| 193 | foreach ($rows as $row) { |
| 194 | $id = $row['booking_id']; |
| 195 | |
| 196 | $customerId = !empty($row['customer_id']) ? $row['customer_id'] : null; |
| 197 | |
| 198 | $paymentId = !empty($row['payment_id']) ? $row['payment_id'] : null; |
| 199 | |
| 200 | $couponId = !empty($row['coupon_id']) ? $row['coupon_id'] : null; |
| 201 | |
| 202 | $bookingEventTicketId = !empty($row['booking_ticket_id']) ? $row['booking_ticket_id'] : null; |
| 203 | |
| 204 | if ($id && empty($data[$id])) { |
| 205 | $data[$id] = [ |
| 206 | 'id' => $id, |
| 207 | 'appointmentId' => $row['booking_appointmentId'], |
| 208 | 'customerId' => $row['booking_customerId'], |
| 209 | 'status' => $row['booking_status'], |
| 210 | 'price' => $row['booking_price'], |
| 211 | 'persons' => $row['booking_persons'], |
| 212 | 'couponId' => $row['booking_couponId'], |
| 213 | 'customFields' => !empty($row['booking_customFields']) ? $row['booking_customFields'] : null, |
| 214 | 'info' => !empty($row['booking_info']) ? $row['booking_info'] : null, |
| 215 | 'utcOffset' => $row['booking_utcOffset'], |
| 216 | 'aggregatedPrice' => $row['booking_aggregatedPrice'], |
| 217 | 'duration' => !empty($row['booking_duration']) ? $row['booking_duration'] : null, |
| 218 | 'token' => isset($row['booking_token']) ? $row['booking_token'] : null, |
| 219 | 'tax' => isset($row['booking_tax']) ? $row['booking_tax'] : null, |
| 220 | ]; |
| 221 | } |
| 222 | |
| 223 | if ($data[$id] && $customerId && empty($data[$id]['customer'])) { |
| 224 | $data[$id]['customer'] = [ |
| 225 | 'id' => $customerId, |
| 226 | 'firstName' => $row['customer_firstName'], |
| 227 | 'lastName' => $row['customer_lastName'], |
| 228 | 'email' => $row['customer_email'], |
| 229 | 'note' => $row['customer_note'], |
| 230 | 'phone' => $row['customer_phone'], |
| 231 | 'gender' => $row['customer_gender'], |
| 232 | 'birthday' => $row['customer_birthday'], |
| 233 | ]; |
| 234 | } |
| 235 | |
| 236 | if ($data[$id] && $paymentId && empty($data[$id]['payments'][$paymentId])) { |
| 237 | $data[$id]['payments'][$paymentId] = [ |
| 238 | 'id' => $paymentId, |
| 239 | 'customerBookingId' => $id, |
| 240 | 'amount' => $row['payment_amount'], |
| 241 | 'dateTime' => $row['payment_dateTime'], |
| 242 | 'created' => !empty($row['payment_created']) ? $row['payment_created'] : null, |
| 243 | 'status' => $row['payment_status'], |
| 244 | 'gateway' => $row['payment_gateway'], |
| 245 | 'gatewayTitle' => $row['payment_gatewayTitle'], |
| 246 | 'transactionId' => !empty($row['payment_transactionId']) ? $row['payment_transactionId'] : null, |
| 247 | 'parentId' => !empty($row['payment_parentId']) ? $row['payment_parentId'] : null, |
| 248 | 'data' => $row['payment_data'], |
| 249 | 'wcOrderId' => !empty($row['payment_wcOrderId']) ? $row['payment_wcOrderId'] : null, |
| 250 | 'wcOrderItemId' => !empty($row['payment_wcOrderItemId']) ? $row['payment_wcOrderItemId'] : null, |
| 251 | 'invoiceNumber' => !empty($row['payment_invoiceNumber']) ? $row['payment_invoiceNumber'] : null |
| 252 | ]; |
| 253 | } |
| 254 | |
| 255 | if ($data[$id] && $couponId && empty($data[$id]['coupon'])) { |
| 256 | $data[$id]['coupon'] = [ |
| 257 | 'id' => $couponId, |
| 258 | 'code' => $row['coupon_code'], |
| 259 | 'discount' => $row['coupon_discount'], |
| 260 | 'deduction' => $row['coupon_deduction'], |
| 261 | 'limit' => $row['coupon_limit'], |
| 262 | 'customerLimit' => $row['coupon_customerLimit'], |
| 263 | 'status' => $row['coupon_status'], |
| 264 | ]; |
| 265 | } |
| 266 | |
| 267 | if ($data[$id] && $bookingEventTicketId && empty($data[$id]['ticketsData'][$bookingEventTicketId])) { |
| 268 | $data[$id]['ticketsData'][$bookingEventTicketId] = [ |
| 269 | 'id' => $bookingEventTicketId, |
| 270 | 'eventTicketId' => $row['booking_ticket_eventTicketId'], |
| 271 | 'customerBookingId' => $id, |
| 272 | 'persons' => $row['booking_ticket_persons'], |
| 273 | 'price' => $row['booking_ticket_price'], |
| 274 | ]; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return $data; |
| 279 | } |
| 280 | } |
| 281 |