CouponFactory.php
236 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\Coupon; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Entity\Coupon\Coupon; |
| 12 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 13 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 14 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageFactory; |
| 15 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 16 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 17 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 18 | use AmeliaBooking\Domain\ValueObjects\DiscountFixedValue; |
| 19 | use AmeliaBooking\Domain\ValueObjects\DiscountPercentageValue; |
| 20 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\PositiveInteger; |
| 21 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\WholeNumber; |
| 22 | use AmeliaBooking\Domain\ValueObjects\String\CouponCode; |
| 23 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 24 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 25 | |
| 26 | /** |
| 27 | * Class CouponFactory |
| 28 | * |
| 29 | * @package AmeliaBooking\Domain\Factory\Coupon |
| 30 | */ |
| 31 | class CouponFactory |
| 32 | { |
| 33 | /** |
| 34 | * @param $data |
| 35 | * |
| 36 | * @return Coupon |
| 37 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 38 | */ |
| 39 | public static function create($data) |
| 40 | { |
| 41 | $coupon = new Coupon(); |
| 42 | |
| 43 | if (isset($data['id'])) { |
| 44 | $coupon->setId(new Id($data['id'])); |
| 45 | } |
| 46 | |
| 47 | if (isset($data['code'])) { |
| 48 | $coupon->setCode(new CouponCode($data['code'])); |
| 49 | } |
| 50 | |
| 51 | if (isset($data['discount'])) { |
| 52 | $coupon->setDiscount(new DiscountPercentageValue($data['discount'])); |
| 53 | } |
| 54 | |
| 55 | if (isset($data['deduction'])) { |
| 56 | $coupon->setDeduction(new DiscountFixedValue($data['deduction'])); |
| 57 | } |
| 58 | |
| 59 | if (isset($data['limit'])) { |
| 60 | $coupon->setLimit(new WholeNumber($data['limit'])); |
| 61 | } |
| 62 | |
| 63 | if (isset($data['status'])) { |
| 64 | $coupon->setStatus(new Status($data['status'])); |
| 65 | } |
| 66 | |
| 67 | $serviceList = new Collection(); |
| 68 | |
| 69 | if (isset($data['serviceList'])) { |
| 70 | foreach ((array)$data['serviceList'] as $key => $value) { |
| 71 | $serviceList->addItem( |
| 72 | ServiceFactory::create($value), |
| 73 | $key |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $eventList = new Collection(); |
| 79 | |
| 80 | if (isset($data['eventList'])) { |
| 81 | foreach ((array)$data['eventList'] as $key => $value) { |
| 82 | $eventList->addItem( |
| 83 | EventFactory::create($value), |
| 84 | $key |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $packageList = new Collection(); |
| 90 | |
| 91 | if (isset($data['packageList'])) { |
| 92 | foreach ((array)$data['packageList'] as $key => $value) { |
| 93 | $packageList->addItem( |
| 94 | PackageFactory::create($value), |
| 95 | $key |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (isset($data['customerLimit'])) { |
| 101 | $coupon->setCustomerLimit(new WholeNumber((int)$data['customerLimit'])); |
| 102 | } |
| 103 | |
| 104 | if (isset($data['notificationInterval'])) { |
| 105 | $coupon->setNotificationInterval(new WholeNumber($data['notificationInterval'])); |
| 106 | } |
| 107 | |
| 108 | if (isset($data['notificationRecurring'])) { |
| 109 | $coupon->setNotificationRecurring(new BooleanValueObject($data['notificationRecurring'])); |
| 110 | } |
| 111 | |
| 112 | if (isset($data['used'])) { |
| 113 | $coupon->setUsed(new WholeNumber($data['used'])); |
| 114 | } |
| 115 | |
| 116 | if (!empty($data['expirationDate'])) { |
| 117 | if (is_string($data['expirationDate'])) { |
| 118 | $coupon->setExpirationDate(new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['expirationDate']))); |
| 119 | } else { |
| 120 | $coupon->setExpirationDate(new DateTimeValue($data['expirationDate'])); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (!empty($data['startDate'])) { |
| 125 | if (is_string($data['startDate'])) { |
| 126 | $coupon->setStartDate(new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['startDate']))); |
| 127 | } else { |
| 128 | $coupon->setStartDate(new DateTimeValue($data['startDate'])); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!empty($data['allServices'])) { |
| 133 | $coupon->setAllServices(new BooleanValueObject($data['allServices'])); |
| 134 | } |
| 135 | |
| 136 | if (!empty($data['allEvents'])) { |
| 137 | $coupon->setAllEvents(new BooleanValueObject($data['allEvents'])); |
| 138 | } |
| 139 | |
| 140 | if (!empty($data['allPackages'])) { |
| 141 | $coupon->setAllPackages(new BooleanValueObject($data['allPackages'])); |
| 142 | } |
| 143 | |
| 144 | $coupon->setServiceList($serviceList); |
| 145 | $coupon->setEventList($eventList); |
| 146 | $coupon->setPackageList($packageList); |
| 147 | |
| 148 | return $coupon; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param array $rows |
| 153 | * |
| 154 | * @return Collection |
| 155 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 156 | */ |
| 157 | public static function createCollection($rows) |
| 158 | { |
| 159 | $coupons = []; |
| 160 | |
| 161 | foreach ($rows as $row) { |
| 162 | $couponId = $row['coupon_id']; |
| 163 | $serviceId = isset($row['service_id']) ? $row['service_id'] : null; |
| 164 | $eventId = isset($row['event_id']) ? $row['event_id'] : null; |
| 165 | $packageId = isset($row['package_id']) ? $row['package_id'] : null; |
| 166 | $bookingId = isset($row['booking_id']) ? $row['booking_id'] : null; |
| 167 | $eventPeriodId = isset($row['event_periodId']) ? $row['event_periodId'] : null; |
| 168 | |
| 169 | $coupons[$couponId]['id'] = $couponId; |
| 170 | $coupons[$couponId]['code'] = $row['coupon_code']; |
| 171 | $coupons[$couponId]['discount'] = $row['coupon_discount']; |
| 172 | $coupons[$couponId]['deduction'] = $row['coupon_deduction']; |
| 173 | $coupons[$couponId]['limit'] = $row['coupon_limit']; |
| 174 | $coupons[$couponId]['customerLimit'] = $row['coupon_customerLimit']; |
| 175 | $coupons[$couponId]['notificationInterval'] = $row['coupon_notificationInterval']; |
| 176 | $coupons[$couponId]['notificationRecurring'] = $row['coupon_notificationRecurring']; |
| 177 | $coupons[$couponId]['status'] = $row['coupon_status']; |
| 178 | $coupons[$couponId]['expirationDate'] = $row['coupon_expirationDate']; |
| 179 | $coupons[$couponId]['startDate'] = $row['coupon_startDate']; |
| 180 | $coupons[$couponId]['allServices'] = $row['coupon_allServices']; |
| 181 | $coupons[$couponId]['allEvents'] = $row['coupon_allEvents']; |
| 182 | $coupons[$couponId]['allPackages'] = $row['coupon_allPackages']; |
| 183 | |
| 184 | if ($bookingId) { |
| 185 | $coupons[$couponId]['bookings'][$bookingId] = $bookingId; |
| 186 | } |
| 187 | |
| 188 | if ($serviceId) { |
| 189 | $coupons[$couponId]['serviceList'][$serviceId]['id'] = $serviceId; |
| 190 | $coupons[$couponId]['serviceList'][$serviceId]['name'] = $row['service_name']; |
| 191 | $coupons[$couponId]['serviceList'][$serviceId]['description'] = $row['service_description']; |
| 192 | $coupons[$couponId]['serviceList'][$serviceId]['color'] = $row['service_color']; |
| 193 | $coupons[$couponId]['serviceList'][$serviceId]['status'] = $row['service_status']; |
| 194 | $coupons[$couponId]['serviceList'][$serviceId]['categoryId'] = $row['service_categoryId']; |
| 195 | $coupons[$couponId]['serviceList'][$serviceId]['duration'] = $row['service_duration']; |
| 196 | $coupons[$couponId]['serviceList'][$serviceId]['price'] = $row['service_price']; |
| 197 | $coupons[$couponId]['serviceList'][$serviceId]['minCapacity'] = $row['service_minCapacity']; |
| 198 | $coupons[$couponId]['serviceList'][$serviceId]['maxCapacity'] = $row['service_maxCapacity']; |
| 199 | } |
| 200 | |
| 201 | if ($eventId) { |
| 202 | $coupons[$couponId]['eventList'][$eventId]['id'] = $eventId; |
| 203 | $coupons[$couponId]['eventList'][$eventId]['name'] = $row['event_name']; |
| 204 | $coupons[$couponId]['eventList'][$eventId]['price'] = $row['event_price']; |
| 205 | } |
| 206 | |
| 207 | if ($eventPeriodId && !isset($coupons[$couponId]['eventList'][$eventId]['periods'][$eventPeriodId])) { |
| 208 | $coupons[$couponId]['eventList'][$eventId]['periods'][$eventPeriodId] = [ |
| 209 | 'id' => $eventPeriodId, |
| 210 | 'periodStart' => DateTimeService::getCustomDateTimeFromUtc($row['event_periodStart']), |
| 211 | 'periodEnd' => DateTimeService::getCustomDateTimeFromUtc($row['event_periodEnd']), |
| 212 | ]; |
| 213 | } |
| 214 | |
| 215 | if ($packageId) { |
| 216 | $coupons[$couponId]['packageList'][$packageId]['id'] = $packageId; |
| 217 | $coupons[$couponId]['packageList'][$packageId]['name'] = $row['package_name']; |
| 218 | $coupons[$couponId]['packageList'][$packageId]['price'] = $row['package_price']; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $couponsCollection = new Collection(); |
| 223 | |
| 224 | foreach ($coupons as $couponKey => $couponArray) { |
| 225 | $couponArray['used'] = isset($couponArray['bookings']) ? sizeof($couponArray['bookings']) : 0; |
| 226 | |
| 227 | $couponsCollection->addItem( |
| 228 | self::create($couponArray), |
| 229 | $couponKey |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | return $couponsCollection; |
| 234 | } |
| 235 | } |
| 236 |