| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Models\Coupon; |
| 7 |
use FluentCart\App\Services\Coupon\CouponServiceAdmin; |
| 8 |
use FluentCart\App\Services\DateTime\DateTime; |
| 9 |
use FluentCart\Framework\Database\Orm\Builder; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
use FluentCart\Framework\Support\Collection; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class CouponResource |
| 16 |
* |
| 17 |
* @package FluentCart\Api\Resource |
| 18 |
*/ |
| 19 |
class CouponResource extends BaseResourceApi |
| 20 |
{ |
| 21 |
|
| 22 |
/** |
| 23 |
* Get the query builder instance for the Coupons model. |
| 24 |
* |
| 25 |
* @return Builder |
| 26 |
*/ |
| 27 |
|
| 28 |
public static function getQuery(): Builder |
| 29 |
{ |
| 30 |
return Coupon::query(); |
| 31 |
} |
| 32 |
|
| 33 |
public static function get(array $params = []) |
| 34 |
{ |
| 35 |
$coupons = static::getQuery() |
| 36 |
->withCount([ |
| 37 |
'appliedCoupons as total_items' => function ($query) { |
| 38 |
$query->selectRaw('count(*)'); |
| 39 |
} |
| 40 |
]) |
| 41 |
->orderBy( |
| 42 |
sanitize_sql_orderby(Arr::get($params, 'order_by', 'id')), |
| 43 |
sanitize_sql_orderby(Arr::get($params, 'order_type', 'DESC')) |
| 44 |
) |
| 45 |
->paginate(Arr::get($params, 'per_page', 15), ['*'], 'page', Arr::get($params, 'page')); |
| 46 |
|
| 47 |
return [ |
| 48 |
'data' => $coupons, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
public static function find($id, $params = []) |
| 53 |
{ |
| 54 |
$type = Arr::get($params, 'type', ''); |
| 55 |
|
| 56 |
if (!empty($type) && $type === 'byCode') { |
| 57 |
$code = Arr::get($params, 'code', null); |
| 58 |
$coupon = static::getQuery()->whereRaw("BINARY `code` = ?", [$code])->first(); |
| 59 |
return $coupon; |
| 60 |
} |
| 61 |
|
| 62 |
return static::getQuery()->find($id); |
| 63 |
} |
| 64 |
|
| 65 |
public static function viewDetails($id) |
| 66 |
{ |
| 67 |
$coupon = static::getQuery()->with(['activities.user'])->find($id); |
| 68 |
|
| 69 |
if (!empty($coupon->end_date) && $coupon->end_date !== '0000-00-00 00:00:00') { |
| 70 |
$endDate = DateTime::anyTimeToGmt($coupon->end_date); |
| 71 |
$now = new DateTime(); |
| 72 |
|
| 73 |
if ($endDate < $now && $coupon->getStatus() !== 'expired') { |
| 74 |
$coupon->setStatus('expired'); |
| 75 |
$coupon->save(); |
| 76 |
} |
| 77 |
} |
| 78 |
return $coupon; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public static function create($data, $params = []) |
| 84 |
{ |
| 85 |
$data = self::formatAmount($data); |
| 86 |
$isCreated = static::getQuery()->create($data); |
| 87 |
|
| 88 |
if ($isCreated) { |
| 89 |
return static::makeSuccessResponse( |
| 90 |
$isCreated, |
| 91 |
__('Coupon created successfully!', 'fluent-cart') |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
return static::makeErrorResponse([ |
| 96 |
['code' => 400, 'message' => __('Coupon creation failed.', 'fluent-cart')] |
| 97 |
]); |
| 98 |
} |
| 99 |
|
| 100 |
public static function listCoupon() |
| 101 |
{ |
| 102 |
return static::getQuery() |
| 103 |
->where('status', 'active') |
| 104 |
->pluck('code') |
| 105 |
->toArray(); |
| 106 |
} |
| 107 |
|
| 108 |
public static function update($data, $id, $params = []) |
| 109 |
{ |
| 110 |
if (Arr::get($data, 'max_uses', null) == '') { |
| 111 |
$data['max_uses'] = null; |
| 112 |
} |
| 113 |
if (Arr::get($data, 'max_per_customer', null) == '') { |
| 114 |
$data['max_per_customer'] = null; |
| 115 |
} |
| 116 |
if (Arr::get($data, 'max_discount_amount', null) == '') { |
| 117 |
$data['max_discount_amount'] = null; |
| 118 |
} |
| 119 |
if (Arr::get($data, 'min_purchase_amount', null) == '') { |
| 120 |
$data['min_purchase_amount'] = null; |
| 121 |
} |
| 122 |
|
| 123 |
if (!$id) { |
| 124 |
return static::makeErrorResponse([ |
| 125 |
['code' => 403, 'message' => __('Please edit a valid coupon!', 'fluent-cart')] |
| 126 |
]); |
| 127 |
} |
| 128 |
|
| 129 |
$hasCoupon = self::find($id); |
| 130 |
|
| 131 |
if (!$hasCoupon) { |
| 132 |
return static::makeErrorResponse([ |
| 133 |
['code' => 404, 'message' => __('Coupon not found, please reload the page and try again!', 'fluent-cart')] |
| 134 |
]); |
| 135 |
} |
| 136 |
|
| 137 |
$data = self::formatAmount($data); |
| 138 |
|
| 139 |
$isUpdated = $hasCoupon->update($data); |
| 140 |
|
| 141 |
if ($isUpdated) { |
| 142 |
return static::makeSuccessResponse($hasCoupon, __('Coupon updated successfully!', 'fluent-cart')); |
| 143 |
} |
| 144 |
|
| 145 |
return static::makeErrorResponse([ |
| 146 |
['code' => 400, 'message' => __('Coupon update failed.', 'fluent-cart')] |
| 147 |
]); |
| 148 |
} |
| 149 |
|
| 150 |
public static function delete($id, $params = []) |
| 151 |
{ |
| 152 |
if (!$id) { |
| 153 |
return static::makeErrorResponse([ |
| 154 |
['code' => 403, 'message' => __('Please use a valid coupon ID!', 'fluent-cart')] |
| 155 |
]); |
| 156 |
} |
| 157 |
|
| 158 |
$hasCoupon = self::find($id); |
| 159 |
|
| 160 |
if ($hasCoupon) { |
| 161 |
if ($hasCoupon->delete()) { |
| 162 |
return static::makeSuccessResponse('', __('Coupon successfully deleted.', 'fluent-cart')); |
| 163 |
} |
| 164 |
|
| 165 |
return static::makeErrorResponse([ |
| 166 |
['code' => 400, 'message' => __('Coupon deletion failed!', 'fluent-cart')] |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
return static::makeErrorResponse([ |
| 171 |
['code' => 404, 'message' => __('Coupon not found in database, failed to remove.', 'fluent-cart')] |
| 172 |
]); |
| 173 |
} |
| 174 |
|
| 175 |
private static function formatAmount($data) |
| 176 |
{ |
| 177 |
if (empty(Arr::get($data, 'conditions.max_discount_amount', ''))) { |
| 178 |
$data['max_discount_amount'] = null; |
| 179 |
} |
| 180 |
if (Arr::get($data, 'type') !== 'percentage') { |
| 181 |
$data['amount'] = Helper::toCent(Arr::get($data, 'amount', 0)); |
| 182 |
} |
| 183 |
if (!empty(Arr::get($data, 'conditions.max_discount_amount'))) { |
| 184 |
$data['conditions']['max_discount_amount'] = Helper::toCent($data['conditions']['max_discount_amount']); |
| 185 |
} |
| 186 |
$data['conditions']['min_purchase_amount'] = Helper::toCent(Arr::get($data['conditions'], 'min_purchase_amount', 0)); |
| 187 |
return $data; |
| 188 |
} |
| 189 |
|
| 190 |
public static function applyCoupon($data, $returnCouponService = true) |
| 191 |
{ |
| 192 |
$couponCode = Arr::get($data, 'coupon_code'); |
| 193 |
$lineItems = Arr::except(Arr::get($data, 'order_items', []), ['*']); |
| 194 |
$orderId = Arr::get($data, 'order_uuid', null); |
| 195 |
$getAppliedCouponLists = Arr::get($data, 'applied_coupons', []); |
| 196 |
$previouslyAppliedCouponCodes = new Collection(); |
| 197 |
if (!empty($orderId)) { |
| 198 |
$order = OrderResource::find($orderId, ['with' => 'appliedCoupons']); |
| 199 |
$previouslyAppliedCouponCodes = $order->appliedCoupons; |
| 200 |
$previouslyAppliedCouponCodes = static::makeCouponFromAppliedCoupons($previouslyAppliedCouponCodes); |
| 201 |
} |
| 202 |
$appliedKeys = $previouslyAppliedCouponCodes->pluck('id')->toArray(); |
| 203 |
$getAppliedCouponLists = array_diff($getAppliedCouponLists, $appliedKeys); |
| 204 |
|
| 205 |
if (!empty($getAppliedCouponLists)) { |
| 206 |
$getAppliedCouponLists = Coupon::query()->whereIn('id', $getAppliedCouponLists)->get()->keyBy('code')->toArray(); |
| 207 |
$previouslyAppliedCouponCodes = $previouslyAppliedCouponCodes->merge($getAppliedCouponLists); |
| 208 |
} |
| 209 |
|
| 210 |
$couponService = new CouponServiceAdmin($lineItems, null, $previouslyAppliedCouponCodes->keys()->toArray()); |
| 211 |
$couponApplied = $couponService->applyCoupon($couponCode); |
| 212 |
if (is_wp_error($couponApplied)) { |
| 213 |
return $couponApplied; |
| 214 |
} |
| 215 |
|
| 216 |
$calculated_items = $couponService->getCalculatedLineItems(); |
| 217 |
|
| 218 |
$applied_coupons = $couponService->getDiscountData(); |
| 219 |
|
| 220 |
$errors = $couponService->getCouponErrors(); |
| 221 |
|
| 222 |
|
| 223 |
if($errors->has($couponCode)){ |
| 224 |
return static::makeErrorResponse([ |
| 225 |
['code' => 400, 'message' => $errors->get($couponCode)->get_error_message()] |
| 226 |
]); |
| 227 |
} |
| 228 |
|
| 229 |
if ($returnCouponService) { |
| 230 |
return [ |
| 231 |
'applied_coupons' => $applied_coupons, |
| 232 |
'calculated_items' => $calculated_items, |
| 233 |
]; |
| 234 |
} |
| 235 |
|
| 236 |
return new WP_Error(__('Something went wrong while updating the cart.', 'fluent-cart')); |
| 237 |
} |
| 238 |
|
| 239 |
public static function cancelCoupon($data, $returnCouponService = true) |
| 240 |
{ |
| 241 |
$couponId = Arr::get($data, 'id', null); |
| 242 |
$couponCode = Arr::get($data, 'coupon_code', null); |
| 243 |
$lineItems = Arr::except(Arr::get($data, 'order_items', []), ['*']); |
| 244 |
$orderId = Arr::get($data, 'order_uuid', null); |
| 245 |
$getAppliedCouponLists = Arr::get($data, 'applied_coupons', []); |
| 246 |
$previouslyAppliedCouponCodes = null; |
| 247 |
|
| 248 |
if (!empty($orderId)) { |
| 249 |
$order = OrderResource::find($orderId, ['with' => ['order_items', 'appliedCoupons']]); |
| 250 |
if (!empty($couponId)) { |
| 251 |
$order->appliedCoupons()->where('id', $couponId)->delete(); |
| 252 |
Coupon::query()->where('code', $couponCode)->decrement('use_count', 1); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
if (!empty($getAppliedCouponLists)) { |
| 258 |
$getAppliedCouponLists = Coupon::query()->whereIn('id', $getAppliedCouponLists)->get()->keyBy('code')->toArray(); |
| 259 |
$previouslyAppliedCouponCodes = Collection::make($getAppliedCouponLists); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
$couponService = new CouponServiceAdmin($lineItems, null, $previouslyAppliedCouponCodes->keys()->toArray()); |
| 264 |
|
| 265 |
$isCancelled = $couponService->cancelCoupon($couponCode); |
| 266 |
|
| 267 |
if (is_wp_error($isCancelled)) { |
| 268 |
return $isCancelled; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
$calculated_items = $couponService->getCalculatedLineItems(); |
| 273 |
|
| 274 |
$applied_coupons = $couponService->getDiscountData(); |
| 275 |
|
| 276 |
if ($returnCouponService) { |
| 277 |
return [ |
| 278 |
'applied_coupons' => $applied_coupons, |
| 279 |
'calculated_items' => $calculated_items, |
| 280 |
]; |
| 281 |
} |
| 282 |
|
| 283 |
return new WP_Error(__('Something went wrong while updating the cart.', 'fluent-cart')); |
| 284 |
} |
| 285 |
|
| 286 |
public static function reapplyCoupon($data, $returnCouponService = true) |
| 287 |
{ |
| 288 |
$lineItems = Arr::except(Arr::get($data, 'order_items', []), ['*']); |
| 289 |
$orderId = Arr::get($data, 'order_uuid', null); |
| 290 |
$previouslyAppliedCouponCodes = null; |
| 291 |
|
| 292 |
|
| 293 |
if (!empty($orderId)) { |
| 294 |
$order = OrderResource::find($orderId, ['with' => 'appliedCoupons']); |
| 295 |
if (empty($lineItems)) { |
| 296 |
$order->appliedCoupons()->delete(); |
| 297 |
} else { |
| 298 |
$previouslyAppliedCouponCodes = $order->appliedCoupons; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
$previouslyAppliedCouponCodes = static::makeCouponFromAppliedCoupons($previouslyAppliedCouponCodes); |
| 303 |
|
| 304 |
$couponService = new CouponServiceAdmin($lineItems, $previouslyAppliedCouponCodes); |
| 305 |
$couponService->reapplyCoupons(); |
| 306 |
|
| 307 |
$calculated_items = $couponService->getCalculatedLineItems(); |
| 308 |
|
| 309 |
$applied_coupons = $couponService->getDiscountData(); |
| 310 |
|
| 311 |
if ($returnCouponService) { |
| 312 |
return [ |
| 313 |
'applied_coupons' => $applied_coupons, |
| 314 |
'calculated_items' => $calculated_items, |
| 315 |
]; |
| 316 |
} |
| 317 |
|
| 318 |
return new WP_Error(__('Something went wrong while updating the cart.', 'fluent-cart')); |
| 319 |
} |
| 320 |
|
| 321 |
private static function makeCouponFromAppliedCoupons($previouslyAppliedCouponCodes) |
| 322 |
{ |
| 323 |
if (empty($previouslyAppliedCouponCodes)) { |
| 324 |
return new Collection(); |
| 325 |
} |
| 326 |
|
| 327 |
return $previouslyAppliedCouponCodes->mapWithKeys(function ($coupon) { |
| 328 |
return [ |
| 329 |
$coupon['code'] => [ |
| 330 |
'id' => Arr::get($coupon, 'coupon_id', ''), |
| 331 |
'title' => $coupon['title'], |
| 332 |
'priority' => $coupon['priority'] ?? 1, |
| 333 |
'code' => $coupon['code'], |
| 334 |
'amount' => $coupon['amount'], |
| 335 |
'type' => 'fixed', |
| 336 |
'stackable' => 'yes', |
| 337 |
'conditions' => [ |
| 338 |
'min_purchase_amount' => 0, |
| 339 |
'max_purchase_amount' => 0, |
| 340 |
'included_products' => [], |
| 341 |
'excluded_products' => [], |
| 342 |
'included_categories' => [], |
| 343 |
'excluded_categories' => [], |
| 344 |
'allowed_user_ids' => [], |
| 345 |
'allowed_roles' => [], |
| 346 |
'max_uses' => 0, |
| 347 |
'max_per_customer' => 0, |
| 348 |
] |
| 349 |
] |
| 350 |
]; |
| 351 |
}); |
| 352 |
} |
| 353 |
} |
| 354 |
|