| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\AppliedCoupon; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class CouponResource |
| 12 |
* |
| 13 |
* @package FluentCart\Api\Resource |
| 14 |
*/ |
| 15 |
|
| 16 |
class AppliedCouponResource extends BaseResourceApi |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the query builder instance for the Coupons model. |
| 21 |
* |
| 22 |
* @return Builder |
| 23 |
*/ |
| 24 |
|
| 25 |
public static function getQuery(): Builder |
| 26 |
{ |
| 27 |
return AppliedCoupon::query(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Retrieve order meta based on the provided parameters. |
| 32 |
* |
| 33 |
* @param array $params Optional. Additional parameters for data retrieval. |
| 34 |
* [ |
| 35 |
* 'order_id' => ( int ) Required. The ID of the order to filter the data. |
| 36 |
* ] |
| 37 |
* |
| 38 |
*/ |
| 39 |
public static function get(array $params = []) |
| 40 |
{ |
| 41 |
return static::getQuery()->where('coupon_id', Arr::get($params, 'coupon_id'))->get(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Create order meta data with the provided information. |
| 46 |
* |
| 47 |
* @param array $data Required. Array containing the necessary parameters for data creation. |
| 48 |
* [ |
| 49 |
* // Include required parameters for creating the data. |
| 50 |
* ] |
| 51 |
* @param array $params Optional. Additional parameters for data creation. |
| 52 |
* [ |
| 53 |
* // Include optional parameters, if any. |
| 54 |
* ] |
| 55 |
* |
| 56 |
*/ |
| 57 |
|
| 58 |
public static function find($id, $params = []) |
| 59 |
{ |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
public static function create($data, $params = []) |
| 64 |
{ |
| 65 |
$isCreated = static::getQuery()->insert($data); |
| 66 |
if ($isCreated) { |
| 67 |
return static::makeSuccessResponse( |
| 68 |
$isCreated, |
| 69 |
__('Created successfully!', 'fluent-cart') |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return static::makeErrorResponse([ |
| 74 |
['code' => 400, 'message' => __('Falied to create.', 'fluent-cart')] |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
public static function createOrUpdate($data, $id) |
| 79 |
{ |
| 80 |
if ($id) { |
| 81 |
return static::getQuery()->where('id', $id)->update($data); |
| 82 |
} else { |
| 83 |
return static::getQuery()->insert($data); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public static function update($data, $id = null, $params = []) |
| 88 |
{ |
| 89 |
|
| 90 |
$isUpdate = static::getQuery()->batchUpdate($data); |
| 91 |
|
| 92 |
if ($isUpdate) { |
| 93 |
return static::makeSuccessResponse( |
| 94 |
$isUpdate, |
| 95 |
__('Coupon updated.', 'fluent-cart') |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
return static::makeErrorResponse([ |
| 100 |
['code' => 400, 'message' => __('Coupon failed to update!', 'fluent-cart')] |
| 101 |
]); |
| 102 |
} |
| 103 |
|
| 104 |
public static function deleteRemovedCoupons($appliedCoupons, $orderId) |
| 105 |
{ |
| 106 |
$existingCoupons = static::getQuery()->where('order_id', $orderId)->get(); |
| 107 |
foreach ($existingCoupons as $existingCoupon) { |
| 108 |
$code = $existingCoupon['code']; |
| 109 |
|
| 110 |
$found = false; |
| 111 |
foreach ($appliedCoupons as $couponData) { |
| 112 |
if ($couponData['code'] === $code) { |
| 113 |
$found = true; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
if (!$found) { |
| 118 |
$existingCoupon->delete(); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public static function delete($id, $params = []) |
| 124 |
{ |
| 125 |
return static::getQuery()->where('order_id', $id)->delete(); |
| 126 |
} |
| 127 |
|
| 128 |
public static function bulkDeleteByOrderIds($ids, $params = []) |
| 129 |
{ |
| 130 |
return static::getQuery()->whereIn('order_id', $ids)->delete(); |
| 131 |
} |
| 132 |
|
| 133 |
} |