PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Http / Requests / CouponRequest.php

CouponRequest.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at app/Http/Requests/CouponRequest.php

180 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Http\Requests;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\Coupon;
7 use FluentCart\Framework\Foundation\RequestGuard;
8 use FluentCart\Framework\Support\Arr;
9
10 class CouponRequest extends RequestGuard
11 {
12
13 /**
14 * @return array
15 */
16 public function rules(): array
17 {
18 $startDate = $this->get("start_date");
19 $endDate = $this->get("end_date");
20
21 return [
22 'title' => 'required|sanitizeText|maxLength:200',
23 'code' => [
24 'required',
25 'string',
26 'maxLength:50',
27 function ($attribute, $value) {
28 $id = absint(App::request()->get('id'));
29
30 // Skip the check if updating and the code belongs to the same record
31 if ($id) {
32 $existing = Coupon::query()->where('code', $value)
33 ->where('id', '!=', $id)
34 ->first();
35 } else {
36 $existing = Coupon::query()->where('code', $value)->first();
37 }
38
39 if ($existing) {
40 return sprintf(__('This coupon code is already in use.', 'fluent-cart'));
41 }
42 return null;
43 }
44
45 ],
46 'priority' => 'nullable|numeric|min:0',
47 'type' => 'required|in:fixed,percentage,free_shipping,buy_x_get_y',
48 'conditions' => 'nullable|array',
49 'conditions.min_purchase_amount' => 'nullable|numeric|min:0',
50 'conditions.max_discount_amount' => 'nullable|numeric|min:0',
51 'conditions.apply_to_whole_cart' => 'nullable|sanitizeText',
52 'conditions.apply_to_quantity' => 'nullable|sanitizeText',
53 'conditions.buy_products' => 'nullable|array',
54 'conditions.get_products' => 'nullable|array',
55 'conditions.max_per_customer' => 'nullable|numeric',
56 'conditions.excluded_categories' => 'nullable',
57 'conditions.included_categories' => 'nullable',
58 'conditions.excluded_products' => 'nullable',
59 'conditions.included_products' => 'nullable',
60 'conditions.email_restrictions' => 'nullable',
61 'conditions.is_recurring' => 'nullable',
62 'conditions.max_uses' => [
63 'nullable',
64 'numeric',
65 function ($attribute, $value) {
66 if ($this->get("max_uses") < $this->get("max_per_customer")) {
67 return sprintf(__("Max uses must be greater than or equal to max per customer.", 'fluent-cart'));
68 }
69 return null;
70 },
71 ],
72 'amount' => [
73 'required',
74 'numeric',
75 'min:0',
76 function ($attribute, $value) {
77 if ($this->get("type") === 'percentage' && $value > 100) {
78 return sprintf(__("For percentage type, the amount should not be greater than 100.", 'fluent-cart'));
79 }
80 return null;
81 },
82 ],
83 'status' => 'required|in:active,expired,disabled,scheduled',
84 'notes' => 'nullable|sanitizeTextArea',
85 'stackable' => 'required|sanitizeText|maxLength:50',
86 'show_on_checkout' => 'required|sanitizeText|maxLength:50',
87 'start_date' => [
88 'required_if:end_date,!=,null',
89 'string',
90 'nullable'
91 ],
92 'end_date' => [
93 'nullable',
94 'string',
95 function ($attribute, $value) use ($startDate, $endDate) {
96 if ($value !== null) {
97 $startDateTime = strtotime(trim($startDate));
98 $endDateTime = strtotime(trim($endDate));
99
100 if ($endDateTime <= $startDateTime) {
101 return sprintf(esc_html__("The end date must be after the start date.", 'fluent-cart'));
102 }
103 }
104 return null;
105 },
106 ],
107 ];
108 }
109
110 /**
111 * @return array
112 */
113 public function messages(): array
114 {
115 return [
116 'title.required' => esc_html__('Title is required.', 'fluent-cart'),
117 'code.required' => esc_html__('Code is required.', 'fluent-cart'),
118 'type.required' => esc_html__('Type is required.', 'fluent-cart'),
119 'amount.required' => esc_html__('Amount is required.', 'fluent-cart'),
120 'buy_quantity.required_if' => esc_html__('Buy quantity is required. ', 'fluent-cart'),
121 'start_date.required_if' => esc_html__('Start date is required. ', 'fluent-cart'),
122 'end_date.required_if' => esc_html__('End date is required. ', 'fluent-cart'),
123 'end_date.date' => esc_html__('The end date type should be date.', 'fluent-cart'),
124 ];
125 }
126
127 /**
128 * @return array
129 */
130 public function sanitize(): array
131 {
132 return [
133 'title' => 'sanitize_text_field',
134 'code' => 'sanitize_text_field',
135 'priority' => 'intval',
136 'type' => 'sanitize_text_field',
137 'conditions' => function ($value) {
138
139 $sanitizedData = [];
140 $sanitizedData['min_purchase_amount'] = floatval(Arr::get($value, 'min_purchase_amount') ?? 0);
141 $sanitizedData['max_discount_amount'] = floatval(Arr::get($value, 'max_discount_amount') ?? 0);
142 $sanitizedData['max_purchase_amount'] = floatval(Arr::get($value, 'max_purchase_amount') ?? 0);
143 $sanitizedData['apply_to_whole_cart'] = sanitize_text_field(Arr::get($value, 'apply_to_whole_cart') ?? 'no');
144 $sanitizedData['apply_to_quantity'] = sanitize_text_field(Arr::get($value, 'apply_to_quantity') ?? 'no');
145 $sanitizedData['max_uses'] = intval(Arr::get($value, 'max_uses') ?? 0);
146 $sanitizedData['max_per_customer'] = intval(Arr::get($value, 'max_per_customer') ?? 0);
147 $sanitizedData['excluded_categories'] = (is_array(Arr::get($value, 'excluded_categories')) ? Arr::get($value, 'excluded_categories') : []);
148 $sanitizedData['included_categories'] = is_array(Arr::get($value, 'included_categories')) ? Arr::get($value, 'included_categories') : [];
149 $sanitizedData['excluded_products'] = is_array(Arr::get($value, 'excluded_products')) ? Arr::get($value, 'excluded_products') : [];
150 $sanitizedData['included_products'] = is_array(Arr::get($value, 'included_products')) ? Arr::get($value, 'included_products') : [];
151 $sanitizedData['email_restrictions'] = sanitize_text_field(Arr::get($value, 'email_restrictions') ?? '');
152 $sanitizedData['is_recurring'] = Arr::get($value, 'is_recurring') === 'yes' ? 'yes' : 'no';
153
154
155 $arrayValues = ['excluded_categories', 'included_categories', 'excluded_products', 'included_products'];
156 foreach ($arrayValues as $key) {
157 $sanitizedData[$key] = array_unique(array_map('sanitize_text_field', $sanitizedData[$key]));
158 }
159
160 return $sanitizedData;
161 },
162 'amount' => 'floatval',
163 'conditions.apply_to_quantity' => 'sanitize_text_field',
164 'conditions.buy_quantity' => 'intval',
165 'conditions.get_quantity' => 'intval',
166 'conditions.max_uses' => 'intval',
167 'conditions.max_per_customer' => 'intval',
168 'status' => 'sanitize_text_field',
169 'notes' => 'sanitize_text_field',
170 'stackable' => 'sanitize_text_field',
171 'show_on_checkout' => 'sanitize_text_field',
172 'start_date' => 'sanitize_text_field',
173 'end_date' => 'sanitize_text_field',
174 'metaValue' => function ($value) {
175 return $value;
176 }
177 ];
178 }
179 }
180