PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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 1 year ago
CouponFactory.php
218 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 if (!empty($data['allServices'])) {
124 $coupon->setAllServices(new BooleanValueObject($data['allServices']));
125 }
126
127 if (!empty($data['allEvents'])) {
128 $coupon->setAllEvents(new BooleanValueObject($data['allEvents']));
129 }
130
131 if (!empty($data['allPackages'])) {
132 $coupon->setAllPackages(new BooleanValueObject($data['allPackages']));
133 }
134
135 $coupon->setServiceList($serviceList);
136 $coupon->setEventList($eventList);
137 $coupon->setPackageList($packageList);
138
139 return $coupon;
140 }
141
142 /**
143 * @param array $rows
144 *
145 * @return Collection
146 * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException
147 */
148 public static function createCollection($rows)
149 {
150 $coupons = [];
151
152 foreach ($rows as $row) {
153 $couponId = $row['coupon_id'];
154 $serviceId = isset($row['service_id']) ? $row['service_id'] : null;
155 $eventId = isset($row['event_id']) ? $row['event_id'] : null;
156 $packageId = isset($row['package_id']) ? $row['package_id'] : null;
157 $bookingId = isset($row['booking_id']) ? $row['booking_id'] : null;
158
159 $coupons[$couponId]['id'] = $couponId;
160 $coupons[$couponId]['code'] = $row['coupon_code'];
161 $coupons[$couponId]['discount'] = $row['coupon_discount'];
162 $coupons[$couponId]['deduction'] = $row['coupon_deduction'];
163 $coupons[$couponId]['limit'] = $row['coupon_limit'];
164 $coupons[$couponId]['customerLimit'] = $row['coupon_customerLimit'];
165 $coupons[$couponId]['notificationInterval'] = $row['coupon_notificationInterval'];
166 $coupons[$couponId]['notificationRecurring'] = $row['coupon_notificationRecurring'];
167 $coupons[$couponId]['status'] = $row['coupon_status'];
168 $coupons[$couponId]['expirationDate'] = $row['coupon_expirationDate'];
169 $coupons[$couponId]['allServices'] = $row['coupon_allServices'];
170 $coupons[$couponId]['allEvents'] = $row['coupon_allEvents'];
171 $coupons[$couponId]['allPackages'] = $row['coupon_allPackages'];
172
173 if ($bookingId) {
174 $coupons[$couponId]['bookings'][$bookingId] = $bookingId;
175 }
176
177 if ($serviceId) {
178 $coupons[$couponId]['serviceList'][$serviceId]['id'] = $serviceId;
179 $coupons[$couponId]['serviceList'][$serviceId]['name'] = $row['service_name'];
180 $coupons[$couponId]['serviceList'][$serviceId]['description'] = $row['service_description'];
181 $coupons[$couponId]['serviceList'][$serviceId]['color'] = $row['service_color'];
182 $coupons[$couponId]['serviceList'][$serviceId]['status'] = $row['service_status'];
183 $coupons[$couponId]['serviceList'][$serviceId]['categoryId'] = $row['service_categoryId'];
184 $coupons[$couponId]['serviceList'][$serviceId]['duration'] = $row['service_duration'];
185 $coupons[$couponId]['serviceList'][$serviceId]['price'] = $row['service_price'];
186 $coupons[$couponId]['serviceList'][$serviceId]['minCapacity'] = $row['service_minCapacity'];
187 $coupons[$couponId]['serviceList'][$serviceId]['maxCapacity'] = $row['service_maxCapacity'];
188 }
189
190 if ($eventId) {
191 $coupons[$couponId]['eventList'][$eventId]['id'] = $eventId;
192 $coupons[$couponId]['eventList'][$eventId]['name'] = $row['event_name'];
193 $coupons[$couponId]['eventList'][$eventId]['price'] = $row['event_price'];
194 }
195
196 if ($packageId) {
197 $coupons[$couponId]['packageList'][$packageId]['id'] = $packageId;
198 $coupons[$couponId]['packageList'][$packageId]['name'] = $row['package_name'];
199 $coupons[$couponId]['packageList'][$packageId]['price'] = $row['package_price'];
200 }
201 }
202
203 $couponsCollection = new Collection();
204
205 foreach ($coupons as $couponKey => $couponArray) {
206
207 $couponArray['used'] = isset($couponArray['bookings']) ? sizeof($couponArray['bookings']) : 0;
208
209 $couponsCollection->addItem(
210 self::create($couponArray),
211 $couponKey
212 );
213 }
214
215 return $couponsCollection;
216 }
217 }
218