PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.33
OttoKit: All-in-One Automation Platform v1.1.33
1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / amelia / actions / create-coupon.php
suretriggers / src / Integrations / amelia / actions Last commit date
create-coupon.php 10 months ago
create-coupon.php
260 lines
1 <?php
2 /**
3 * CreateCoupon.
4 * php version 5.6
5 *
6 * @category CreateCoupon
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\Amelia\Actions;
15
16 use Exception;
17 use SureTriggers\Integrations\AutomateAction;
18 use SureTriggers\Traits\SingletonLoader;
19 use AmeliaBooking\Domain\Factory\Coupon\CouponFactory;
20 use AmeliaBooking\Application\Services\Coupon\CouponApplicationService;
21
22 /**
23 * CreateCoupon
24 *
25 * @category CreateCoupon
26 * @package SureTriggers
27 * @author BSF <username@example.com>
28 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
29 * @link https://www.brainstormforce.com/
30 * @since 1.0.0
31 */
32 class CreateCoupon extends AutomateAction {
33
34 /**
35 * Integration type.
36 *
37 * @var string
38 */
39 public $integration = 'Amelia';
40
41 /**
42 * Action name.
43 *
44 * @var string
45 */
46 public $action = 'amelia_create_coupon';
47
48 use SingletonLoader;
49
50 /**
51 * Register a action.
52 *
53 * @param array $actions actions.
54 * @return array
55 */
56 public function register( $actions ) {
57 $actions[ $this->integration ][ $this->action ] = [
58 'label' => __( 'Create Coupon', 'suretriggers' ),
59 'action' => $this->action,
60 'function' => [ $this, 'action_listener' ],
61 ];
62
63 return $actions;
64 }
65
66 /**
67 * Action listener.
68 *
69 * @param int $user_id User ID.
70 * @param int $automation_id Automation ID.
71 * @param array $fields Fields.
72 * @param array $selected_options Selected options.
73 *
74 * @return array|void
75 */
76 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
77 $coupon_code = isset( $selected_options['coupon_code'] ) ? sanitize_text_field( $selected_options['coupon_code'] ) : '';
78 $discount_type = isset( $selected_options['discount_type'] ) ? sanitize_text_field( $selected_options['discount_type'] ) : 'percentage';
79 $discount_value = isset( $selected_options['discount_value'] ) ? floatval( $selected_options['discount_value'] ) : 0;
80 $usage_limit = isset( $selected_options['usage_limit'] ) ? intval( $selected_options['usage_limit'] ) : 1;
81 $customer_limit = isset( $selected_options['customer_limit'] ) ? intval( $selected_options['customer_limit'] ) : 1;
82 $expiration_date = isset( $selected_options['expiration_date'] ) ? sanitize_text_field( $selected_options['expiration_date'] ) : '';
83 $status = isset( $selected_options['status'] ) ? sanitize_text_field( $selected_options['status'] ) : 'visible';
84 $service_ids = isset( $selected_options['service_ids'] ) ? $this->parse_comma_separated_ids( $selected_options['service_ids'] ) : [];
85 $event_ids = isset( $selected_options['event_ids'] ) ? $this->parse_comma_separated_ids( $selected_options['event_ids'] ) : [];
86 $package_ids = isset( $selected_options['package_ids'] ) ? $this->parse_comma_separated_ids( $selected_options['package_ids'] ) : [];
87 $all_services = isset( $selected_options['all_services'] ) ? (bool) intval( $selected_options['all_services'] ) : false;
88 $all_events = isset( $selected_options['all_events'] ) ? (bool) intval( $selected_options['all_events'] ) : false;
89 $all_packages = isset( $selected_options['all_packages'] ) ? (bool) intval( $selected_options['all_packages'] ) : false;
90
91
92 // Validate required fields.
93 if ( empty( $coupon_code ) ) {
94 return [
95 'status' => 'error',
96 'message' => 'Coupon code is required.',
97 ];
98 }
99
100 if ( $discount_value <= 0 ) {
101 return [
102 'status' => 'error',
103 'message' => 'Discount value must be greater than 0.',
104 ];
105 }
106
107 // Check if Amelia classes exist.
108 if ( ! class_exists( 'AmeliaBooking\Domain\Factory\Coupon\CouponFactory' ) ) {
109 return [
110 'status' => 'error',
111 'message' => 'Amelia CouponFactory class not found.',
112 ];
113 }
114
115 try {
116 // Check if AMELIA_PATH is defined.
117 if ( ! defined( 'AMELIA_PATH' ) ) {
118 return [
119 'status' => 'error',
120 'message' => 'Amelia plugin is not active or AMELIA_PATH constant is not defined.',
121 ];
122 }
123
124 // Get Amelia container.
125 $container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php';
126
127 // Get repositories and services.
128 $service_repository = $container->get( 'domain.bookable.service.repository' );
129 $package_repository = $container->get( 'domain.bookable.package.repository' );
130 $event_service = $container->get( 'application.booking.event.service' );
131 $coupon_service = $container->get( 'application.coupon.service' );
132 $coupon_repository = $container->get( 'domain.coupon.repository' );
133
134 // Prepare coupon data.
135 $coupon_data = [
136 'code' => $coupon_code,
137 'status' => $status,
138 'limit' => $usage_limit,
139 'customerLimit' => $customer_limit,
140 'used' => 0,
141 'allServices' => $all_services,
142 'allEvents' => $all_events,
143 'allPackages' => $all_packages,
144 ];
145
146 // Set discount based on type.
147 if ( 'percentage' === $discount_type ) {
148 $coupon_data['discount'] = $discount_value;
149 $coupon_data['deduction'] = 0;
150 } else {
151 $coupon_data['discount'] = 0;
152 $coupon_data['deduction'] = $discount_value;
153 }
154
155 // Set expiration date if provided.
156 if ( ! empty( $expiration_date ) ) {
157 $coupon_data['expirationDate'] = $expiration_date;
158 }
159
160 // Create coupon using factory.
161 $coupon = CouponFactory::create( $coupon_data );
162
163 // Fetch and set service collections.
164 if ( ! empty( $service_ids ) ) {
165 $services = $service_repository->getByCriteria( [ 'services' => $service_ids ] );
166 $coupon->setServiceList( $services );
167 } else {
168 if ( class_exists( '\AmeliaBooking\Domain\Collection\Collection' ) ) {
169 $coupon->setServiceList( new \AmeliaBooking\Domain\Collection\Collection() );
170 }
171 }
172
173 // Fetch and set event collections.
174 if ( ! empty( $event_ids ) ) {
175 $events = $event_service->getEventsByIds( $event_ids, [] );
176 $coupon->setEventList( $events );
177 } else {
178 if ( class_exists( '\AmeliaBooking\Domain\Collection\Collection' ) ) {
179 $coupon->setEventList( new \AmeliaBooking\Domain\Collection\Collection() );
180 }
181 }
182
183 // Fetch and set package collections.
184 if ( ! empty( $package_ids ) ) {
185 $packages = $package_repository->getByCriteria( [ 'packages' => $package_ids ] );
186 $coupon->setPackageList( $packages );
187 } else {
188 if ( class_exists( '\AmeliaBooking\Domain\Collection\Collection' ) ) {
189 $coupon->setPackageList( new \AmeliaBooking\Domain\Collection\Collection() );
190 }
191 }
192
193 // Start transaction.
194 $coupon_repository->beginTransaction();
195
196 // Add coupon using application service.
197 $coupon_id = $coupon_service->add( $coupon );
198
199 if ( $coupon_id ) {
200 // Commit transaction.
201 $coupon_repository->commit();
202
203 return [
204 'status' => 'success',
205 'message' => 'Coupon created successfully',
206 'coupon_id' => $coupon_id,
207 'coupon_code' => $coupon_code,
208 'discount_type' => $discount_type,
209 'discount_value' => $discount_value,
210 ];
211 } else {
212 // Rollback transaction.
213 $coupon_repository->rollback();
214
215 return [
216 'status' => 'error',
217 'message' => 'Failed to create coupon',
218 ];
219 }
220 } catch ( Exception $e ) {
221 // Rollback transaction if it was started.
222 if ( isset( $coupon_repository ) ) {
223 $coupon_repository->rollback();
224 }
225
226 return [
227 'status' => 'error',
228 'message' => 'An error occurred while creating the coupon: ' . $e->getMessage(),
229 ];
230 }
231 }
232
233 /**
234 * Parse comma-separated IDs string into array of integers.
235 *
236 * @param string|array $ids Comma-separated string or array of IDs.
237 * @return array
238 */
239 private function parse_comma_separated_ids( $ids ) {
240 if ( is_array( $ids ) ) {
241 return array_map( 'intval', array_filter( $ids ) );
242 }
243
244 if ( empty( $ids ) || ! is_string( $ids ) ) {
245 return [];
246 }
247
248 // Split by comma and clean up.
249 $ids_array = explode( ',', $ids );
250 $ids_array = array_map( 'trim', $ids_array );
251 $ids_array = array_filter( $ids_array );
252 $ids_array = array_map( 'intval', $ids_array );
253
254 // Remove any zero values.
255 return array_filter( $ids_array );
256 }
257 }
258
259 CreateCoupon::get_instance();
260