| 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.min_amount_basis' => 'nullable|in:subtotal,total', |
| 51 |
'conditions.max_discount_amount' => 'nullable|numeric|min:0', |
| 52 |
'conditions.apply_to_whole_cart' => 'nullable|sanitizeText', |
| 53 |
'conditions.apply_to_quantity' => 'nullable|sanitizeText', |
| 54 |
'conditions.buy_products' => 'nullable|array', |
| 55 |
'conditions.get_products' => 'nullable|array', |
| 56 |
'conditions.max_per_customer' => 'nullable|numeric', |
| 57 |
'conditions.excluded_categories' => 'nullable', |
| 58 |
'conditions.included_categories' => 'nullable', |
| 59 |
'conditions.excluded_products' => 'nullable', |
| 60 |
'conditions.included_products' => 'nullable', |
| 61 |
'conditions.email_restrictions' => 'nullable', |
| 62 |
'conditions.is_recurring' => 'nullable', |
| 63 |
'conditions.max_uses' => [ |
| 64 |
'nullable', |
| 65 |
'numeric', |
| 66 |
function ($attribute, $value) { |
| 67 |
if ($this->get("max_uses") < $this->get("max_per_customer")) { |
| 68 |
return sprintf(__("Max uses must be greater than or equal to max per customer.", 'fluent-cart')); |
| 69 |
} |
| 70 |
return null; |
| 71 |
}, |
| 72 |
], |
| 73 |
'amount' => [ |
| 74 |
'required', |
| 75 |
'numeric', |
| 76 |
'min:0', |
| 77 |
function ($attribute, $value) { |
| 78 |
if ($this->get("type") === 'percentage' && $value > 100) { |
| 79 |
return sprintf(__("For percentage type, the amount should not be greater than 100.", 'fluent-cart')); |
| 80 |
} |
| 81 |
return null; |
| 82 |
}, |
| 83 |
], |
| 84 |
'status' => 'required|in:active,expired,disabled,scheduled', |
| 85 |
'notes' => 'nullable|sanitizeTextArea', |
| 86 |
'stackable' => 'required|sanitizeText|maxLength:50', |
| 87 |
'show_on_checkout' => 'required|sanitizeText|maxLength:50', |
| 88 |
'start_date' => [ |
| 89 |
'required_if:end_date,!=,null', |
| 90 |
'string', |
| 91 |
'nullable' |
| 92 |
], |
| 93 |
'end_date' => [ |
| 94 |
'nullable', |
| 95 |
'string', |
| 96 |
function ($attribute, $value) use ($startDate, $endDate) { |
| 97 |
if ($value !== null) { |
| 98 |
$startDateTime = strtotime(trim($startDate)); |
| 99 |
$endDateTime = strtotime(trim($endDate)); |
| 100 |
|
| 101 |
if ($endDateTime <= $startDateTime) { |
| 102 |
return sprintf(esc_html__("The end date must be after the start date.", 'fluent-cart')); |
| 103 |
} |
| 104 |
} |
| 105 |
return null; |
| 106 |
}, |
| 107 |
], |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function messages(): array |
| 115 |
{ |
| 116 |
return [ |
| 117 |
'title.required' => esc_html__('Title is required.', 'fluent-cart'), |
| 118 |
'code.required' => esc_html__('Code is required.', 'fluent-cart'), |
| 119 |
'type.required' => esc_html__('Type is required.', 'fluent-cart'), |
| 120 |
'amount.required' => esc_html__('Amount is required.', 'fluent-cart'), |
| 121 |
'buy_quantity.required_if' => esc_html__('Buy quantity is required. ', 'fluent-cart'), |
| 122 |
'start_date.required_if' => esc_html__('Start date is required. ', 'fluent-cart'), |
| 123 |
'end_date.required_if' => esc_html__('End date is required. ', 'fluent-cart'), |
| 124 |
'end_date.date' => esc_html__('The end date type should be date.', 'fluent-cart'), |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function sanitize(): array |
| 132 |
{ |
| 133 |
return [ |
| 134 |
'title' => 'sanitize_text_field', |
| 135 |
'code' => 'sanitize_text_field', |
| 136 |
'priority' => 'intval', |
| 137 |
'type' => 'sanitize_text_field', |
| 138 |
'conditions' => function ($value) { |
| 139 |
|
| 140 |
$sanitizedData = []; |
| 141 |
$sanitizedData['min_purchase_amount'] = floatval(Arr::get($value, 'min_purchase_amount') ?? 0); |
| 142 |
$sanitizedData['max_discount_amount'] = floatval(Arr::get($value, 'max_discount_amount') ?? 0); |
| 143 |
$sanitizedData['max_purchase_amount'] = floatval(Arr::get($value, 'max_purchase_amount') ?? 0); |
| 144 |
// Only carry the basis when a valid value is supplied. When it is omitted the key is |
| 145 |
// left absent so create() can default it and update() can preserve the stored value — |
| 146 |
// never force 'subtotal' onto a legacy coupon whose client simply doesn't send the field. |
| 147 |
if (in_array(Arr::get($value, 'min_amount_basis'), ['subtotal', 'total'], true)) { |
| 148 |
$sanitizedData['min_amount_basis'] = Arr::get($value, 'min_amount_basis'); |
| 149 |
} |
| 150 |
$sanitizedData['apply_to_whole_cart'] = sanitize_text_field(Arr::get($value, 'apply_to_whole_cart') ?? 'no'); |
| 151 |
$sanitizedData['apply_to_quantity'] = sanitize_text_field(Arr::get($value, 'apply_to_quantity') ?? 'no'); |
| 152 |
$sanitizedData['max_uses'] = intval(Arr::get($value, 'max_uses') ?? 0); |
| 153 |
$sanitizedData['max_per_customer'] = intval(Arr::get($value, 'max_per_customer') ?? 0); |
| 154 |
$sanitizedData['excluded_categories'] = (is_array(Arr::get($value, 'excluded_categories')) ? Arr::get($value, 'excluded_categories') : []); |
| 155 |
$sanitizedData['included_categories'] = is_array(Arr::get($value, 'included_categories')) ? Arr::get($value, 'included_categories') : []; |
| 156 |
$sanitizedData['excluded_products'] = is_array(Arr::get($value, 'excluded_products')) ? Arr::get($value, 'excluded_products') : []; |
| 157 |
$sanitizedData['included_products'] = is_array(Arr::get($value, 'included_products')) ? Arr::get($value, 'included_products') : []; |
| 158 |
$sanitizedData['email_restrictions'] = sanitize_text_field(Arr::get($value, 'email_restrictions') ?? ''); |
| 159 |
$sanitizedData['is_recurring'] = Arr::get($value, 'is_recurring') === 'yes' ? 'yes' : 'no'; |
| 160 |
|
| 161 |
|
| 162 |
$arrayValues = ['excluded_categories', 'included_categories', 'excluded_products', 'included_products']; |
| 163 |
foreach ($arrayValues as $key) { |
| 164 |
$sanitizedData[$key] = array_unique(array_map('sanitize_text_field', $sanitizedData[$key])); |
| 165 |
} |
| 166 |
|
| 167 |
return $sanitizedData; |
| 168 |
}, |
| 169 |
'amount' => 'floatval', |
| 170 |
'conditions.apply_to_quantity' => 'sanitize_text_field', |
| 171 |
'conditions.buy_quantity' => 'intval', |
| 172 |
'conditions.get_quantity' => 'intval', |
| 173 |
'conditions.max_uses' => 'intval', |
| 174 |
'conditions.max_per_customer' => 'intval', |
| 175 |
'status' => 'sanitize_text_field', |
| 176 |
'notes' => 'sanitize_text_field', |
| 177 |
'stackable' => 'sanitize_text_field', |
| 178 |
'show_on_checkout' => 'sanitize_text_field', |
| 179 |
'start_date' => 'sanitize_text_field', |
| 180 |
'end_date' => 'sanitize_text_field', |
| 181 |
'metaValue' => function ($value) { |
| 182 |
return $value; |
| 183 |
} |
| 184 |
]; |
| 185 |
} |
| 186 |
} |
| 187 |
|