| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource\FrontendResource; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\BaseResourceApi; |
| 6 |
use FluentCart\Api\Resource\OrderResource; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Models\Customer; |
| 9 |
use FluentCart\Framework\Database\Orm\Builder; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class CustomerResource extends BaseResourceApi |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
public static function getQuery(): Builder |
| 17 |
{ |
| 18 |
return Customer::query(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Get customers based on specified parameters. |
| 23 |
* |
| 24 |
* @param array $params Array containing the necessary parameters. |
| 25 |
* [ |
| 26 |
* "params" => (array) Required. |
| 27 |
* [ |
| 28 |
* 'search' => (string) Optional. Search Customer. |
| 29 |
* [ |
| 30 |
* "column name(e.g., first_name|last_name|email|id)" => [ |
| 31 |
* column => "column name(e.g., first_name|last_name|email|id)", |
| 32 |
* operator => "operator (e.g., like_all|rlike|or_rlike|or_like_all)", |
| 33 |
* value => "value" ] |
| 34 |
* ], |
| 35 |
* 'filters' => (string) Optional. Filters customer. |
| 36 |
* [ |
| 37 |
* "column name(e.g., first_name|last_name|email)" => [ |
| 38 |
* column => "column name(e.g., first_name|last_name|email)", |
| 39 |
* operator => "operator (e.g., between|or_between|like_all|in)", |
| 40 |
* value => "value" ] |
| 41 |
* ], |
| 42 |
* 'order_by' => (string) Optional. Column to order by, |
| 43 |
* 'order_type' => (string) Optional. Order type for sorting (ASC or DESC), |
| 44 |
* 'per_page' => (int) Optional. Number of items for per page, |
| 45 |
* 'page' => (int) Optional. Page number for pagination |
| 46 |
* ] |
| 47 |
* ] |
| 48 |
* |
| 49 |
*/ |
| 50 |
public static function get(array $params = []) |
| 51 |
{ |
| 52 |
return static::getQuery()->when(Arr::get($params["params"], 'search'), function ($query) use ($params) { |
| 53 |
return $query->search(Arr::get($params["params"], 'search', '')); |
| 54 |
}) |
| 55 |
->applyCustomFilters(Arr::get($params["params"], 'filters', [])) |
| 56 |
->orderBy( |
| 57 |
sanitize_sql_orderby(Arr::get($params["params"], 'order_by', 'id')), |
| 58 |
sanitize_sql_orderby(Arr::get($params["params"], 'order_type', 'DESC'))) |
| 59 |
->paginate(Arr::get($params["params"], 'per_page', 15), ['*'], 'page', Arr::get($params["params"], 'page')); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Find customer by ID. |
| 64 |
* |
| 65 |
* @param int $id Required. The ID of the customer. |
| 66 |
* @param array $params Optional. Additional parameters for finding a customer. |
| 67 |
* [ |
| 68 |
* 'with' => (array) Optional.Relationship name to be eagerly loaded, |
| 69 |
* ] |
| 70 |
* |
| 71 |
*/ |
| 72 |
public static function find($id, $params = []) |
| 73 |
{ |
| 74 |
$with = Arr::get($params, 'with', []); |
| 75 |
$customer = static::getQuery()->with($with)->findOrFail($id); |
| 76 |
return [ |
| 77 |
'customer' => $customer |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Create a new customer with the given data |
| 83 |
* |
| 84 |
* @param array $data Required. Array containing the necessary parameters |
| 85 |
* [ |
| 86 |
* 'first_name' => (string) Required. The first name of the customer, |
| 87 |
* 'last_name' => (string) Optional. The last name of the customer, |
| 88 |
* 'email' => (string) Required. The email of the customer, |
| 89 |
* 'city' => (string) Optional. The city of the customer, |
| 90 |
* 'state' => (string) Optional. The state of the customer, |
| 91 |
* 'postcode' => (string) Optional. The postal code of the customer, |
| 92 |
* 'country' => (string) Optional. The country of the customer, |
| 93 |
* 'wp_user' => (string) Optional. Create customer as WP user, |
| 94 |
* ] |
| 95 |
* @param array $params Optional. Additional parameters for creating a customer. |
| 96 |
* |
| 97 |
*/ |
| 98 |
public static function create($data, $params = []) |
| 99 |
{ |
| 100 |
$email = Arr::get($data, 'email'); |
| 101 |
|
| 102 |
$customer = static::getQuery()->firstOrCreate( |
| 103 |
['email' => $email], |
| 104 |
$data |
| 105 |
); |
| 106 |
|
| 107 |
if ($customer) { |
| 108 |
return static::makeSuccessResponse( |
| 109 |
$customer, |
| 110 |
__('Customer created successfully!', 'fluent-cart') |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
return static::makeErrorResponse([ |
| 115 |
['code' => 400, 'message' => __('Customer creation failed.', 'fluent-cart')] |
| 116 |
]); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Update customer with the given data |
| 121 |
* |
| 122 |
* @param array $data Required. Array containing the necessary parameters |
| 123 |
* [ |
| 124 |
* 'first_name' => (string) Required. The first name of the customer, |
| 125 |
* 'last_name' => (string) Optional. The last name of the customer, |
| 126 |
* 'email' => (string) Required. The email of the customer, |
| 127 |
* 'city' => (string) Optional. The city of the customer, |
| 128 |
* 'state' => (string) Optional. The state of the customer, |
| 129 |
* 'postcode' => (string) Optional. The postal code of the customer, |
| 130 |
* 'country' => (string) Optional. The country of the customer, |
| 131 |
* ] |
| 132 |
* @param int $id Required. The ID of the customer. |
| 133 |
* @param array $params Optional. Additional parameters for creating a customer. |
| 134 |
* |
| 135 |
*/ |
| 136 |
public static function update($data, $id, $params = []) |
| 137 |
{ |
| 138 |
$customer = static::getQuery()->findOrFail($id)->update($data); |
| 139 |
|
| 140 |
if ($customer) { |
| 141 |
return static::makeSuccessResponse( |
| 142 |
$customer, |
| 143 |
__('Customer updated successfully!', 'fluent-cart') |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
return static::makeErrorResponse([ |
| 148 |
['code' => 400, 'message' => __('Customer update failed.', 'fluent-cart')] |
| 149 |
]); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Delete a customer based on the given ID and parameters. |
| 154 |
* |
| 155 |
* @param int $id Optional. The ID of the customer. |
| 156 |
* @param array $params Optional. Additional parameters for deleting multiple customers. |
| 157 |
* [ |
| 158 |
* 'ids' => (array) Required. The array of customer IDs to be deleted. |
| 159 |
* ] |
| 160 |
* |
| 161 |
*/ |
| 162 |
public static function delete($id, $params = []) |
| 163 |
{ |
| 164 |
$ids = Arr::get($params, 'ids'); |
| 165 |
|
| 166 |
$customers = static::getQuery()->with(['orders'])->whereIn('id', $ids)->get(); |
| 167 |
|
| 168 |
foreach ($customers as $customer) { |
| 169 |
$customer->orders()->delete(); |
| 170 |
$customer->delete(); |
| 171 |
} |
| 172 |
|
| 173 |
if ($customer) { |
| 174 |
return static::makeSuccessResponse( |
| 175 |
'', |
| 176 |
__('Selected Customers has been deleted permanently', 'fluent-cart') |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
return static::makeErrorResponse([ |
| 181 |
['code' => 400, 'message' => __('Customer update failed.', 'fluent-cart')] |
| 182 |
]); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get orders for a specific customer. |
| 187 |
* |
| 188 |
* @param array $data Required. Array containing the necessary parameters for order retrieval. |
| 189 |
* [ |
| 190 |
* 'per_page' => (int) Optional. Number of items for per page, |
| 191 |
* ] |
| 192 |
* @param int $customerId Required. The ID of the customer for whom orders are being retrieved. |
| 193 |
* |
| 194 |
*/ |
| 195 |
public static function getOrders(array $data, $customerId): array |
| 196 |
{ |
| 197 |
return OrderResource::search(['customer_id' => ['column' => 'customer_id', 'value' => $customerId]], function (\FluentCart\Framework\Database\Orm\Builder $query) use ($data) { |
| 198 |
return $query->orderBy('id', 'DESC') |
| 199 |
->paginate(Arr::get($data, 'per_page', 15)); |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Update the status of multiple customers with the given parameters. |
| 205 |
* |
| 206 |
* @param array $params Optional. Array containing the necessary parameters |
| 207 |
* [ |
| 208 |
* 'new_status' => (string) Required. The new status to be set for the customers. |
| 209 |
* 'customer_ids' => (array) Required. Customer IDs whose status will be updated. |
| 210 |
* ] |
| 211 |
* |
| 212 |
*/ |
| 213 |
public static function updateStatus($params = []) |
| 214 |
{ |
| 215 |
$newStatus = Arr::get($params, 'new_status', ''); |
| 216 |
|
| 217 |
if (!$newStatus) { |
| 218 |
return static::makeErrorResponse([ |
| 219 |
['code' => 403, 'message' => __('Please select status', 'fluent-cart')] |
| 220 |
]); |
| 221 |
} |
| 222 |
|
| 223 |
$validStatuses = Status::getEditableCustomerStatuses(); |
| 224 |
if (!isset($validStatuses[$newStatus])) { |
| 225 |
return static::makeErrorResponse([ |
| 226 |
['code' => 403, 'message' => __('Provided customer status is not valid', 'fluent-cart')] |
| 227 |
]); |
| 228 |
} |
| 229 |
|
| 230 |
$customers = static::getQuery()->with(['orders'])->whereIn('id', Arr::get($params, 'customer_ids'))->get(); |
| 231 |
|
| 232 |
foreach ($customers as $customer) { |
| 233 |
$customer->updateCustomerStatus($newStatus); |
| 234 |
} |
| 235 |
|
| 236 |
return static::makeSuccessResponse( |
| 237 |
'', |
| 238 |
__('Customer Status has been changed', 'fluent-cart') |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Manage customers based on the provided action and customer IDs. |
| 244 |
* |
| 245 |
* @param array $params Optional. Array containing the necessary parameters |
| 246 |
* [ |
| 247 |
* 'action' => (string) Required. The action to be performed on the selected customers. |
| 248 |
* (e.g., Possible values: 'delete_customers', 'change_customer_status') |
| 249 |
* 'customer_ids' => (array) Required. Customer IDs whose action will be performed. |
| 250 |
* ] |
| 251 |
* |
| 252 |
*/ |
| 253 |
public static function manageCustomer($params = []) |
| 254 |
{ |
| 255 |
|
| 256 |
$action = Arr::get($params, 'action', ''); |
| 257 |
$customerIds = Arr::get($params, 'customer_ids', []); |
| 258 |
|
| 259 |
$customerIds = array_map(function ($id) { |
| 260 |
return (int)$id; |
| 261 |
}, $customerIds); |
| 262 |
|
| 263 |
|
| 264 |
$customerIds = array_filter($customerIds); |
| 265 |
|
| 266 |
if (!$customerIds) { |
| 267 |
return static::makeErrorResponse([ |
| 268 |
['code' => 403, 'message' => __('Customers selection is required', 'fluent-cart')] |
| 269 |
]); |
| 270 |
} |
| 271 |
|
| 272 |
if ($action == 'delete_customers') { |
| 273 |
return static::deleteAuthorized(null, ['ids' => $customerIds]); |
| 274 |
} |
| 275 |
|
| 276 |
if ($action == 'change_customer_status') { |
| 277 |
return static::updateStatusAuthorized($params); |
| 278 |
} |
| 279 |
|
| 280 |
return static::makeErrorResponse([ |
| 281 |
['code' => 400, 'message' => __('Selected action is invalid', 'fluent-cart')] |
| 282 |
]); |
| 283 |
} |
| 284 |
} |