| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\Api\Validator\CustomerValidator; |
| 6 |
use FluentCart\App\Helpers\CustomerHelper; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Models\Customer; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\Framework\Validator\ValidationException; |
| 11 |
|
| 12 |
class Customers |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var CustomerHelper $customerHelper An instance of the CustomerHelper service |
| 16 |
*/ |
| 17 |
private $customerHelper; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->customerHelper = (new CustomerHelper()); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @return Boolean |
| 26 |
* |
| 27 |
* Check email is already available or not |
| 28 |
* |
| 29 |
* @param Email $email, Customer email address |
| 30 |
*/ |
| 31 |
public function isExist($email) |
| 32 |
{ |
| 33 |
return Customer::query()->where('email', $email)->exists(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return null|ValidationException |
| 38 |
* |
| 39 |
* @param array $args, which contain customers data like |
| 40 |
* first_name, last_name, email, city, state, postcode, country |
| 41 |
*/ |
| 42 |
public function validate($args) |
| 43 |
{ |
| 44 |
try { |
| 45 |
(new CustomerValidator())->validate($args); |
| 46 |
} catch (ValidationException $e) { |
| 47 |
throw $e; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* @return array |
| 54 |
* |
| 55 |
* create new customer |
| 56 |
* |
| 57 |
* @param array $args will be the customer info |
| 58 |
*/ |
| 59 |
public function create($args) |
| 60 |
{ |
| 61 |
try { |
| 62 |
$this->customerHelper->hasPermission(); |
| 63 |
|
| 64 |
$this->validate($args); |
| 65 |
|
| 66 |
$this->customerHelper->createCustomer($args)->toArray(); |
| 67 |
|
| 68 |
} catch (ValidationException $e) { |
| 69 |
throw $e; |
| 70 |
} |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* @return bool |
| 77 |
* |
| 78 |
* Update any existing customer basic info |
| 79 |
* |
| 80 |
* @param array $id , valid customer id |
| 81 |
* @param array $data , customer data to update |
| 82 |
*/ |
| 83 |
public function update($id, $data) |
| 84 |
{ |
| 85 |
return Customer::query()->findOrFail($id) |
| 86 |
->update( |
| 87 |
$this->customerHelper->sanitizeCustomer($data) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* @return array |
| 94 |
* |
| 95 |
* Delete existing customer or customers |
| 96 |
* |
| 97 |
* @param array $ids , customer id or array of id |
| 98 |
*/ |
| 99 |
public function delete($ids) |
| 100 |
{ |
| 101 |
$customers = Customer::with(['orders'])->whereIn('id', $ids)->get(); |
| 102 |
|
| 103 |
foreach ($customers as $customer) { |
| 104 |
$customer->orders()->delete(); |
| 105 |
$customer->delete(); |
| 106 |
} |
| 107 |
|
| 108 |
return [ |
| 109 |
'message' => __('Selected Customers has been deleted permanently', 'fluent-cart'), |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* @return array |
| 116 |
* |
| 117 |
* Will return info of specific customer |
| 118 |
* |
| 119 |
* @param string|Number $id, get data by this customer id |
| 120 |
* @param array $with, accepts array of 'orders', 'shipping_address', 'billing_address' |
| 121 |
*/ |
| 122 |
public function get($id, $with = []) |
| 123 |
{ |
| 124 |
return Customer::with($with)->findOrFail($id)->toArray(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* @return array |
| 130 |
* |
| 131 |
* will return all customers with pagination |
| 132 |
* |
| 133 |
* @param array $args, arguments of pagination info |
| 134 |
*/ |
| 135 |
public function getAll($args) |
| 136 |
{ |
| 137 |
$customers = Customer::query()->when(Arr::get($args["params"], 'search'), function ($query) use ($args) { |
| 138 |
return $query->search(Arr::get($args["params"], 'search', '')); |
| 139 |
}) |
| 140 |
->applyCustomFilters(Arr::get($args["params"], 'filters', [])) |
| 141 |
->orderBy( |
| 142 |
sanitize_sql_orderby(Arr::get($args["params"], 'order_by', 'id')), |
| 143 |
sanitize_sql_orderby(Arr::get($args["params"], 'order_type', 'DESC'))) |
| 144 |
->paginate(Arr::get($args["params"], 'per_page', 15), ['*'], 'page', Arr::get($args["params"], 'page')); |
| 145 |
|
| 146 |
return $customers; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* @return string |
| 152 |
* |
| 153 |
* Delete existing customer or customers |
| 154 |
* |
| 155 |
* @param array $arg , |
| 156 |
* 'new_status': active|inactive, |
| 157 |
* 'customer_ids': [], |
| 158 |
* 'action': change_customer_status|delete_customers |
| 159 |
*/ |
| 160 |
public function updateStatus($arg) |
| 161 |
{ |
| 162 |
$newStatus = sanitize_text_field(Arr::get($arg, 'new_status', '')); |
| 163 |
if (!$newStatus) { |
| 164 |
throw new \Exception(esc_html__('Please select status', 'fluent-cart')); |
| 165 |
} |
| 166 |
|
| 167 |
$validStatuses = Status::getEditableCustomerStatuses(); |
| 168 |
if (!isset($validStatuses[$newStatus])) { |
| 169 |
throw new \Exception(esc_html__('Provided customer status is not valid', 'fluent-cart')); |
| 170 |
} |
| 171 |
|
| 172 |
$customers = Customer::with(['orders'])->whereIn('id', Arr::get($arg, 'customer_ids'))->get(); |
| 173 |
foreach ($customers as $customer) { |
| 174 |
$customer->updateCustomerStatus($newStatus); |
| 175 |
} |
| 176 |
|
| 177 |
return [ |
| 178 |
'message' => __('Customer Status has been changed', 'fluent-cart') |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|