PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.11
Booking for Appointments and Events Calendar – Amelia v1.2.11
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Domain / Factory / Coupon / CouponFactory.php
ameliabooking / src / Domain / Factory / Coupon Last commit date
CouponFactory.php 2 years ago
CouponFactory.php
203 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\Coupon;
8
9 use AmeliaBooking\Domain\Collection\Collection;
10 use AmeliaBooking\Domain\Entity\Coupon\Coupon;
11 use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory;
12 use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory;
13 use AmeliaBooking\Domain\Factory\Bookable\Service\PackageFactory;
14 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
15 use AmeliaBooking\Domain\ValueObjects\BooleanValueObject;
16 use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue;
17 use AmeliaBooking\Domain\ValueObjects\DiscountFixedValue;
18 use AmeliaBooking\Domain\ValueObjects\DiscountPercentageValue;
19 use AmeliaBooking\Domain\ValueObjects\Number\Integer\PositiveInteger;
20 use AmeliaBooking\Domain\ValueObjects\Number\Integer\WholeNumber;
21 use AmeliaBooking\Domain\ValueObjects\String\CouponCode;
22 use AmeliaBooking\Domain\ValueObjects\String\Status;
23 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
24
25 /**
26 * Class CouponFactory
27 *
28 * @package AmeliaBooking\Domain\Factory\Coupon
29 */
30 class CouponFactory
31 {
32 /**
33 * @param $data
34 *
35 * @return Coupon
36 * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException
37 */
38 public static function create($data)
39 {
40 $coupon = new Coupon();
41
42 if (isset($data['id'])) {
43 $coupon->setId(new Id($data['id']));
44 }
45
46 if (isset($data['code'])) {
47 $coupon->setCode(new CouponCode($data['code']));
48 }
49
50 if (isset($data['discount'])) {
51 $coupon->setDiscount(new DiscountPercentageValue($data['discount']));
52 }
53
54 if (isset($data['deduction'])) {
55 $coupon->setDeduction(new DiscountFixedValue($data['deduction']));
56 }
57
58 if (isset($data['limit'])) {
59 $coupon->setLimit(new PositiveInteger($data['limit']));
60 }
61
62 if (isset($data['status'])) {
63 $coupon->setStatus(new Status($data['status']));
64 }
65
66 $serviceList = new Collection();
67
68 if (isset($data['serviceList'])) {
69 foreach ((array)$data['serviceList'] as $key => $value) {
70 $serviceList->addItem(
71 ServiceFactory::create($value),
72 $key
73 );
74 }
75 }
76
77 $eventList = new Collection();
78
79 if (isset($data['eventList'])) {
80 foreach ((array)$data['eventList'] as $key => $value) {
81 $eventList->addItem(
82 EventFactory::create($value),
83 $key
84 );
85 }
86 }
87
88 $packageList = new Collection();
89
90 if (isset($data['packageList'])) {
91 foreach ((array)$data['packageList'] as $key => $value) {
92 $packageList->addItem(
93 PackageFactory::create($value),
94 $key
95 );
96 }
97 }
98
99 if (isset($data['customerLimit'])) {
100 $coupon->setCustomerLimit(new WholeNumber((int)$data['customerLimit']));
101 }
102
103 if (isset($data['notificationInterval'])) {
104 $coupon->setNotificationInterval(new WholeNumber($data['notificationInterval']));
105 }
106
107 if (isset($data['notificationRecurring'])) {
108 $coupon->setNotificationRecurring(new BooleanValueObject($data['notificationRecurring']));
109 }
110
111 if (isset($data['used'])) {
112 $coupon->setUsed(new WholeNumber($data['used']));
113 }
114
115 if (!empty($data['expirationDate'])) {
116 if (is_string($data['expirationDate'])) {
117 $coupon->setExpirationDate(new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['expirationDate'])));
118 } else {
119 $coupon->setExpirationDate(new DateTimeValue($data['expirationDate']));
120 }
121 }
122
123 $coupon->setServiceList($serviceList);
124 $coupon->setEventList($eventList);
125 $coupon->setPackageList($packageList);
126
127 return $coupon;
128 }
129
130 /**
131 * @param array $rows
132 *
133 * @return Collection
134 * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException
135 */
136 public static function createCollection($rows)
137 {
138 $coupons = [];
139
140 foreach ($rows as $row) {
141 $couponId = $row['coupon_id'];
142 $serviceId = isset($row['service_id']) ? $row['service_id'] : null;
143 $eventId = isset($row['event_id']) ? $row['event_id'] : null;
144 $packageId = isset($row['package_id']) ? $row['package_id'] : null;
145 $bookingId = isset($row['booking_id']) ? $row['booking_id'] : null;
146
147 $coupons[$couponId]['id'] = $couponId;
148 $coupons[$couponId]['code'] = $row['coupon_code'];
149 $coupons[$couponId]['discount'] = $row['coupon_discount'];
150 $coupons[$couponId]['deduction'] = $row['coupon_deduction'];
151 $coupons[$couponId]['limit'] = $row['coupon_limit'];
152 $coupons[$couponId]['customerLimit'] = $row['coupon_customerLimit'];
153 $coupons[$couponId]['notificationInterval'] = $row['coupon_notificationInterval'];
154 $coupons[$couponId]['notificationRecurring'] = $row['coupon_notificationRecurring'];
155 $coupons[$couponId]['status'] = $row['coupon_status'];
156 $coupons[$couponId]['expirationDate'] = $row['coupon_expirationDate'];
157
158 if ($bookingId) {
159 $coupons[$couponId]['bookings'][$bookingId] = $bookingId;
160 }
161
162 if ($serviceId) {
163 $coupons[$couponId]['serviceList'][$serviceId]['id'] = $serviceId;
164 $coupons[$couponId]['serviceList'][$serviceId]['name'] = $row['service_name'];
165 $coupons[$couponId]['serviceList'][$serviceId]['description'] = $row['service_description'];
166 $coupons[$couponId]['serviceList'][$serviceId]['color'] = $row['service_color'];
167 $coupons[$couponId]['serviceList'][$serviceId]['status'] = $row['service_status'];
168 $coupons[$couponId]['serviceList'][$serviceId]['categoryId'] = $row['service_categoryId'];
169 $coupons[$couponId]['serviceList'][$serviceId]['duration'] = $row['service_duration'];
170 $coupons[$couponId]['serviceList'][$serviceId]['price'] = $row['service_price'];
171 $coupons[$couponId]['serviceList'][$serviceId]['minCapacity'] = $row['service_minCapacity'];
172 $coupons[$couponId]['serviceList'][$serviceId]['maxCapacity'] = $row['service_maxCapacity'];
173 }
174
175 if ($eventId) {
176 $coupons[$couponId]['eventList'][$eventId]['id'] = $eventId;
177 $coupons[$couponId]['eventList'][$eventId]['name'] = $row['event_name'];
178 $coupons[$couponId]['eventList'][$eventId]['price'] = $row['event_price'];
179 }
180
181 if ($packageId) {
182 $coupons[$couponId]['packageList'][$packageId]['id'] = $packageId;
183 $coupons[$couponId]['packageList'][$packageId]['name'] = $row['package_name'];
184 $coupons[$couponId]['packageList'][$packageId]['price'] = $row['package_price'];
185 }
186 }
187
188 $couponsCollection = new Collection();
189
190 foreach ($coupons as $couponKey => $couponArray) {
191
192 $couponArray['used'] = isset($couponArray['bookings']) ? sizeof($couponArray['bookings']) : 0;
193
194 $couponsCollection->addItem(
195 self::create($couponArray),
196 $couponKey
197 );
198 }
199
200 return $couponsCollection;
201 }
202 }
203