| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Helpers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\FrontendResource\OrderDownloadPermissionResource; |
| 6 |
use FluentCart\Api\Resource\OrderResource; |
| 7 |
use FluentCart\Api\Resource\ProductDownloadResource; |
| 8 |
use FluentCart\App\Models\Customer; |
| 9 |
use FluentCart\App\Models\OrderDownloadPermission; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
use FluentCart\Framework\Support\DateTime; |
| 12 |
|
| 13 |
class CustomerHelper |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @return array |
| 17 |
* @param array $customer with details info like email, name |
| 18 |
*/ |
| 19 |
public function sanitizeCustomer($customer) |
| 20 |
{ |
| 21 |
$email = sanitize_email(Arr::get($customer, 'email', '')); |
| 22 |
foreach ($customer as $key => $val) { |
| 23 |
$customer[$key] = sanitize_text_field($val); |
| 24 |
} |
| 25 |
$customer['email'] = $email; |
| 26 |
return $customer; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* @return Boolean |
| 32 |
* |
| 33 |
* Check email is already available or not |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* @return Boolean Check the request owner has right permission |
| 38 |
* or throw new \Exception('You don\'t have permission!') |
| 39 |
*/ |
| 40 |
|
| 41 |
|
| 42 |
public function updateCustomer($id, $data) |
| 43 |
{ |
| 44 |
return Customer::findOrFail($id) |
| 45 |
->update( |
| 46 |
$this->sanitizeCustomer($data) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
public function createWpUser($newCustomer) |
| 51 |
{ |
| 52 |
$userId = wp_create_user( |
| 53 |
Arr::get($newCustomer, 'first_name'), |
| 54 |
wp_generate_password(8), |
| 55 |
Arr::get($newCustomer, 'email') |
| 56 |
); |
| 57 |
|
| 58 |
if (is_wp_error($userId)) { |
| 59 |
$error = $userId->get_error_message(); |
| 60 |
throw new \Exception(esc_html($error)); |
| 61 |
} else { |
| 62 |
return $userId; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
//Using Resource Api |
| 67 |
/*public function manageCustomer($selected) |
| 68 |
{ |
| 69 |
sleep(1); |
| 70 |
$action = sanitize_text_field(Arr::get($selected, 'action', '')); |
| 71 |
$customerIds = Arr::get($selected, 'customer_ids', []); |
| 72 |
|
| 73 |
$customerIds = array_map(function ($id) { |
| 74 |
return (int)$id; |
| 75 |
}, $customerIds); |
| 76 |
|
| 77 |
$customerIds = array_filter($customerIds); |
| 78 |
if (!$customerIds) { |
| 79 |
throw new \Exception(__('Customers selection is required', 'fluent-cart')); |
| 80 |
} |
| 81 |
|
| 82 |
$customersApi = new Customers(); |
| 83 |
|
| 84 |
if ($action == 'delete_customers') { |
| 85 |
return $customersApi->delete($customerIds); |
| 86 |
} |
| 87 |
|
| 88 |
if ($action == 'change_customer_status') { |
| 89 |
return $customersApi->updateStatus($selected); |
| 90 |
} |
| 91 |
|
| 92 |
throw new \Exception(__('Selected action is invalid', 'fluent-cart')); |
| 93 |
}*/ |
| 94 |
|
| 95 |
public static function getRepeatCustomerBySearch($params) |
| 96 |
{ |
| 97 |
$params["status"] = [ "column" => "status", "operator" => "in", "value" => [Arr::get($params, 'order_status')] ]; |
| 98 |
// $params["payment_status"] = [ "column" => "payment_status", "operator" => "in", "value" => Status::getTransactionSuccessStatuses() ]; |
| 99 |
$param = Arr::only($params, ['created_at', 'status', 'payment_status']); |
| 100 |
|
| 101 |
return Customer::query() |
| 102 |
->when($params["search"], function ($query) use ($params) { |
| 103 |
return $query->search($params["search"]); |
| 104 |
}) |
| 105 |
->whereHas('orders', function ($query) use($param){ |
| 106 |
$query->when($param, function ($query) use ($param) { |
| 107 |
return $query->search($param); |
| 108 |
})->havingRaw('COUNT(*) > 1'); |
| 109 |
}) |
| 110 |
->with(['orders' => function ($query) use($param){ |
| 111 |
$query->select('id', 'customer_id', 'status') |
| 112 |
->when($param, function ($query) use ($param) { |
| 113 |
return $query->search($param); |
| 114 |
}); |
| 115 |
}]); |
| 116 |
} |
| 117 |
|
| 118 |
public static function checkDownloadPermissionAndStoreLog($params) |
| 119 |
{ |
| 120 |
if (!is_user_logged_in()) { |
| 121 |
wp_send_json(['message' => __('You are not logged in', 'fluent-cart')], 422); |
| 122 |
} |
| 123 |
|
| 124 |
$orderUuid = Arr::get($params, 'order'); |
| 125 |
$downloadId = Arr::get($params, 'download_id'); |
| 126 |
$variationId = Arr::get($params, 'variation_id'); |
| 127 |
$order = OrderResource::find($orderUuid); |
| 128 |
|
| 129 |
if (!empty($order)) { |
| 130 |
|
| 131 |
if (empty($order->customer) || wp_get_current_user()->ID != $order->customer->user_id) { |
| 132 |
wp_send_json(['message' => __('Sorry, This is not your order', 'fluent-cart')], 422); |
| 133 |
} |
| 134 |
|
| 135 |
$orderId = Arr::get($order, 'id'); |
| 136 |
|
| 137 |
$productDownload = ProductDownloadResource::find($downloadId); |
| 138 |
|
| 139 |
if (!empty($productDownload)) { |
| 140 |
|
| 141 |
$productDownloadId = Arr::get($productDownload, 'id'); |
| 142 |
$productDownloadLimit = Arr::get($productDownload, 'settings.download_limit', ''); |
| 143 |
$productDownloadExpiry = Arr::get($productDownload, 'settings.download_expiry', ''); |
| 144 |
|
| 145 |
$hasDownloaded = OrderDownloadPermissionResource::find($downloadId, ['order_id' => $orderId, 'variation_id' => $variationId]); |
| 146 |
|
| 147 |
$totalDownloads = Arr::get($hasDownloaded, 'download_count', 0); |
| 148 |
|
| 149 |
$downloadExpiryDate = ''; |
| 150 |
|
| 151 |
if($productDownloadExpiry !== '') { |
| 152 |
$downloadExpiryDate = new DateTime($order->created_at); |
| 153 |
|
| 154 |
$downloadExpiryDate->modify('+'. $productDownloadExpiry .' months')->format('Y-m-d H:i:s'); |
| 155 |
} |
| 156 |
$currentDate = gmdate('Y-m-d H:i:s', current_time('timestamp')); |
| 157 |
|
| 158 |
if ($productDownloadExpiry !== '' && $currentDate > $downloadExpiryDate) { |
| 159 |
wp_send_json(['message' => __('Download date has been expired', 'fluent-cart')], 422); |
| 160 |
} |
| 161 |
|
| 162 |
if($productDownloadLimit !== '' && $totalDownloads >= $productDownloadLimit) { |
| 163 |
wp_send_json(['message' => __('You have crossed download limit', 'fluent-cart')], 422); |
| 164 |
} |
| 165 |
|
| 166 |
if(($totalDownloads < $productDownloadLimit && $currentDate <= $downloadExpiryDate) || ($productDownloadLimit === '' && $productDownloadExpiry === '') || ($productDownloadLimit === '' && $currentDate <= $downloadExpiryDate) || (($totalDownloads < $productDownloadLimit && $productDownloadExpiry === ''))) { |
| 167 |
$data= [ |
| 168 |
'order_id' => $orderId, |
| 169 |
'variation_id' => $variationId, |
| 170 |
'customer_id' => Arr::get($order, 'customer.id'), |
| 171 |
'download_id' => $downloadId, |
| 172 |
'download_count' => $totalDownloads += 1, |
| 173 |
'download_limit' => $productDownloadLimit, |
| 174 |
'access_expires' => $downloadExpiryDate, |
| 175 |
]; |
| 176 |
return self::storeDownloadLog($data, $hasDownloaded); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
wp_send_json(['message' => __('File not found', 'fluent-cart')], 422); |
| 181 |
} |
| 182 |
wp_send_json(['message' => __('Order not found', 'fluent-cart')], 422); |
| 183 |
} |
| 184 |
|
| 185 |
private static function storeDownloadLog($data, ?OrderDownloadPermission $orderDownloadPermission = null) |
| 186 |
{ |
| 187 |
if (empty($orderDownloadPermission)) { |
| 188 |
return OrderDownloadPermissionResource::create($data); |
| 189 |
} |
| 190 |
return OrderDownloadPermissionResource::update($data, Arr::get($orderDownloadPermission, 'id')); |
| 191 |
} |
| 192 |
|
| 193 |
// public static function storeDownloadLog($params) |
| 194 |
// { |
| 195 |
// if (!is_user_logged_in()) { |
| 196 |
// wp_send_json_error(['message' => __('You are not logged in', 'fluent-cart')], 423); |
| 197 |
// } |
| 198 |
|
| 199 |
// $order = Arr::get($params, 'order'); |
| 200 |
// $downloadId = Arr::get($params, 'download_id'); |
| 201 |
|
| 202 |
// $order = OrderResource::find($order, [ |
| 203 |
// 'with' => [ |
| 204 |
// 'order_items' => function ($query) use ($downloadId) { |
| 205 |
// return $query->with(['product_downloads']) |
| 206 |
// ->whereHas('product_downloads', function ($query) use ($downloadId) { |
| 207 |
// $query->where('id', $downloadId); |
| 208 |
// }); |
| 209 |
// } |
| 210 |
// ] |
| 211 |
// ]); |
| 212 |
|
| 213 |
// if ($order) { |
| 214 |
|
| 215 |
// if (empty($order->customer) || wp_get_current_user()->ID != $order->customer->user_id) { |
| 216 |
// wp_send_json_error(['message' => __('Sorry, This is not your order', 'fluent-cart')], 423); |
| 217 |
// } |
| 218 |
// $orderId = Arr::get($order, 'id'); |
| 219 |
// $orderItem = $order->order_items->first(); |
| 220 |
// if ($orderItem) { |
| 221 |
// $productDownloadId = Arr::get($orderItem, 'product_downloads.id'); |
| 222 |
// $hasDownload = OrderDownloadPermissionResource::find($productDownloadId, ['order_id' => $orderId ]); |
| 223 |
// $downloadCount = Arr::get($hasDownload, 'download_count', 0) + 1; |
| 224 |
|
| 225 |
// $data= [ |
| 226 |
// 'order_id' => $orderId, |
| 227 |
// 'customer_id' => Arr::get($order, 'customer.id'), |
| 228 |
// 'download_id' => $productDownloadId, |
| 229 |
// 'download_count' => $downloadCount, |
| 230 |
// 'download_limit' => Arr::get($orderItem, 'product_downloads.settings.download_limit'), |
| 231 |
// 'access_expires' => Arr::get($orderItem, 'product_downloads.settings.download_expiry'), |
| 232 |
// ]; |
| 233 |
|
| 234 |
// if (empty($hasDownload)) { |
| 235 |
// $isCreatedOrUpdated = OrderDownloadPermissionResource::create($data); |
| 236 |
// } |
| 237 |
// else { |
| 238 |
// $isCreatedOrUpdated = OrderDownloadPermissionResource::update($data, Arr::get($hasDownload, 'id')); |
| 239 |
// } |
| 240 |
|
| 241 |
// if (is_wp_error($isCreatedOrUpdated)) { |
| 242 |
// return $isCreatedOrUpdated; |
| 243 |
// } |
| 244 |
|
| 245 |
// wp_send_json($isCreatedOrUpdated, 200); |
| 246 |
// } |
| 247 |
// } |
| 248 |
// } |
| 249 |
|
| 250 |
public function calculateCustomerStats(array $customerIds = []) |
| 251 |
{ |
| 252 |
$customers = []; |
| 253 |
if(count($customerIds) > 0) { |
| 254 |
$customers = Customer::query()->whereIn('id', $customerIds)->with('orders.transactions')->get(); |
| 255 |
} else { |
| 256 |
$customers = Customer::query()->with('orders.transactions')->get(); |
| 257 |
} |
| 258 |
|
| 259 |
$updateableData = []; |
| 260 |
|
| 261 |
foreach ($customers as $customer){ |
| 262 |
$orders = $customer->orders; |
| 263 |
$totalPayments = []; |
| 264 |
foreach ($orders as $order){ |
| 265 |
foreach ($order->transactions as $transaction){ |
| 266 |
if($transaction->status == Status::TRANSACTION_SUCCEEDED){ |
| 267 |
if(empty($totalPayments[$order['currency']])){ |
| 268 |
$totalPayments[$order['currency']]=$transaction['total']; |
| 269 |
}else{ |
| 270 |
$totalPayments[$order['currency']]+=$transaction['total']; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
$updateableData[] = [ |
| 276 |
'id' => $customer->id, |
| 277 |
'purchase_value' => json_encode($totalPayments), |
| 278 |
'purchase_count' => $orders->count(), |
| 279 |
'first_purchase_date' => $orders->min('created_at').'', |
| 280 |
'last_purchase_date' => $orders->max('created_at').'' |
| 281 |
]; |
| 282 |
} |
| 283 |
if(!empty($customers)) { |
| 284 |
return Customer::query()->batchUpdate($updateableData); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
|