| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Writer\Common\Creator\WriterEntityFactory; |
| 6 |
use FluentCart\Api\Resource\ProductResource; |
| 7 |
use FluentCart\App\App; |
| 8 |
use FluentCart\App\Helpers\CustomerHelper; |
| 9 |
use FluentCart\App\Helpers\Helper; |
| 10 |
use FluentCart\App\Helpers\Status; |
| 11 |
use FluentCart\App\Models\Order; |
| 12 |
use FluentCart\Framework\Support\Arr; |
| 13 |
|
| 14 |
class ExportHandler |
| 15 |
{ |
| 16 |
public function register() |
| 17 |
{ |
| 18 |
add_action('wp_ajax_fluent_cart_admin_ajax', array($this, 'mapAjaxRoute')); |
| 19 |
} |
| 20 |
|
| 21 |
public function mapAjaxRoute() |
| 22 |
{ |
| 23 |
$route = App::getInstance()->request->get('route'); |
| 24 |
$route = sanitize_text_field($route); |
| 25 |
|
| 26 |
$validRoutes = [ |
| 27 |
'export_orders' => 'exportOrders', |
| 28 |
'export_repeat_customers' => 'exportRepeatCustomers', |
| 29 |
'export_products' => 'exportProducts', |
| 30 |
]; |
| 31 |
|
| 32 |
if (isset($validRoutes[$route])) { |
| 33 |
return $this->{$validRoutes[$route]}(); |
| 34 |
} |
| 35 |
|
| 36 |
die('No Route Found'); |
| 37 |
} |
| 38 |
|
| 39 |
public function exportOrders() |
| 40 |
{ |
| 41 |
if (!current_user_can('manage_options')) { |
| 42 |
die('No Access'); |
| 43 |
} |
| 44 |
|
| 45 |
Helper::loadSpoutLib(); |
| 46 |
|
| 47 |
$writer = WriterEntityFactory::createCSVWriter(); |
| 48 |
|
| 49 |
$writer->openToBrowser('orders-export-' . gmdate('Y-m-d_H:i') . '.csv'); |
| 50 |
|
| 51 |
$values = [ |
| 52 |
'Order ID', |
| 53 |
'Order Status', |
| 54 |
// 'Shipping Status', |
| 55 |
'Currency', |
| 56 |
'Total Amount', |
| 57 |
'Items Count', |
| 58 |
'Order Type', |
| 59 |
'Payment Method', |
| 60 |
'Order Date', |
| 61 |
'Customer ID', |
| 62 |
'Customer Name', |
| 63 |
'Customer Email', |
| 64 |
]; |
| 65 |
|
| 66 |
$rowFromValues = WriterEntityFactory::createRowFromArray($values); |
| 67 |
$writer->addRow($rowFromValues); |
| 68 |
|
| 69 |
$request = App::getInstance()->request->all(); |
| 70 |
|
| 71 |
$ordersQuery = Order::with(['order_items', 'customer']) |
| 72 |
->whereHas('customer', function ($query) use ($request) { |
| 73 |
$query->when(Arr::get($request, 'search'), function ($search) use ($request) { |
| 74 |
return $search->search(Arr::get($request, 'search', '')); |
| 75 |
}); |
| 76 |
}) |
| 77 |
->orderBy( |
| 78 |
sanitize_text_field(Arr::get($request, 'order_by', 'id')), |
| 79 |
sanitize_text_field(Arr::get($request, 'order_type', 'DESC')) |
| 80 |
); |
| 81 |
|
| 82 |
if (isset($request['customer_id'])) { |
| 83 |
$customerId = (int)$request['customer_id']; |
| 84 |
$ordersQuery = $ordersQuery->search(["customer_id" => ["column" => "customer_id", "value" => $customerId]]); |
| 85 |
} |
| 86 |
|
| 87 |
if (!empty($request['filters'])) { |
| 88 |
$ordersQuery = $ordersQuery->applyCustomFilters(Arr::get($request, 'filters', [])); |
| 89 |
} |
| 90 |
|
| 91 |
if (!empty($request['paginate'])) { |
| 92 |
$ordersQuery = $ordersQuery->paginate(Arr::get($request['paginate'], 'per_page'), ['*'], 'page', Arr::get($request['paginate'], 'current_page')); |
| 93 |
} |
| 94 |
if (empty($request['paginate'])) { |
| 95 |
$ordersQuery = $ordersQuery->get(); |
| 96 |
} |
| 97 |
|
| 98 |
$orders = $ordersQuery; |
| 99 |
|
| 100 |
foreach ($orders as $order) { |
| 101 |
$rowData = [ |
| 102 |
$order->id, |
| 103 |
$order->status, |
| 104 |
// $order->shipping_status, |
| 105 |
$order->currency, |
| 106 |
Helper::toDecimal($order->total_amount, false), |
| 107 |
count($order->order_items), |
| 108 |
$order->type, |
| 109 |
$order->payment_method, |
| 110 |
$order->created_at, |
| 111 |
$order->customer_id, |
| 112 |
$order->customer ? $order->customer->full_name : '', |
| 113 |
$order->customer ? $order->customer->email : '' |
| 114 |
]; |
| 115 |
$writer->addRow(WriterEntityFactory::createRowFromArray($rowData)); |
| 116 |
} |
| 117 |
$writer->close(); |
| 118 |
die(); |
| 119 |
} |
| 120 |
|
| 121 |
public function exportRepeatCustomers() |
| 122 |
{ |
| 123 |
if (!current_user_can('manage_options')) { |
| 124 |
die('No Access'); |
| 125 |
} |
| 126 |
|
| 127 |
Helper::loadSpoutLib(); |
| 128 |
|
| 129 |
$writer = WriterEntityFactory::createCSVWriter(); |
| 130 |
|
| 131 |
$writer->openToBrowser('repeat-customers-export-' . gmdate('Y-m-d_H:i') . '.csv'); |
| 132 |
|
| 133 |
$values = [ |
| 134 |
'Customer ID', |
| 135 |
'Customer Name', |
| 136 |
'Customer Email', |
| 137 |
'Total Orders', |
| 138 |
'Order Status' |
| 139 |
]; |
| 140 |
|
| 141 |
$rowFromValues = WriterEntityFactory::createRowFromArray($values); |
| 142 |
$writer->addRow($rowFromValues); |
| 143 |
|
| 144 |
$params = App::getInstance()->request->get('params', []); |
| 145 |
|
| 146 |
$repeatCustomers = CustomerHelper::getRepeatCustomerBySearch($params)->paginate(Arr::get($params, 'per_page'), ['*'], 'page', Arr::get($params, 'current_page')); |
| 147 |
|
| 148 |
foreach ($repeatCustomers as $customer) { |
| 149 |
$rowData = [ |
| 150 |
$customer->id, |
| 151 |
$customer->full_name ? $customer->full_name : '', |
| 152 |
$customer->email ? $customer->email : '', |
| 153 |
count($customer->orders), |
| 154 |
Arr::get($customer, 'orders.0.status', '') |
| 155 |
]; |
| 156 |
$writer->addRow(WriterEntityFactory::createRowFromArray($rowData)); |
| 157 |
} |
| 158 |
$writer->close(); |
| 159 |
die(); |
| 160 |
} |
| 161 |
|
| 162 |
public function exportProducts() |
| 163 |
{ |
| 164 |
if (!current_user_can('manage_options')) { |
| 165 |
die('No Access'); |
| 166 |
} |
| 167 |
|
| 168 |
Helper::loadSpoutLib(); |
| 169 |
|
| 170 |
$writer = WriterEntityFactory::createCSVWriter(); |
| 171 |
|
| 172 |
$writer->openToBrowser('products-export-' . gmdate('Y-m-d_H:i') . '.csv'); |
| 173 |
|
| 174 |
$values = [ |
| 175 |
'ID', |
| 176 |
'Image', |
| 177 |
'Product Name', |
| 178 |
'Type', |
| 179 |
'Variation', |
| 180 |
'Min Price', |
| 181 |
'Max Price', |
| 182 |
'Stock', |
| 183 |
'Status', |
| 184 |
'Created Date', |
| 185 |
]; |
| 186 |
|
| 187 |
$rowFromValues = WriterEntityFactory::createRowFromArray($values); |
| 188 |
$writer->addRow($rowFromValues); |
| 189 |
|
| 190 |
$request = App::getInstance()->request->all(); |
| 191 |
|
| 192 |
$params = [ |
| 193 |
"select" => ['ID', 'post_title', 'post_date', 'post_status', 'guid'], |
| 194 |
"with" => ['detail'], |
| 195 |
"admin_all_statuses" => ["post_status" => ["column" => "post_status", "operator" => "in", "value" => Status::productAdminAllStatuses()]], |
| 196 |
"admin_search" => Arr::get($request, 'search'), |
| 197 |
"admin_filters" => Arr::get($request, 'filters', []), |
| 198 |
"order_by" => Arr::get($request, 'order_by', 'ID'), |
| 199 |
"order_type" => Arr::get($request, 'order_type', 'DESC'), |
| 200 |
"per_page" => Arr::get($request, 'paginate.per_page', 10), |
| 201 |
"page" => Arr::get($request, 'paginate.current_page'), |
| 202 |
]; |
| 203 |
|
| 204 |
$products = ProductResource::get($params)['products']; |
| 205 |
|
| 206 |
foreach ($products as $product) { |
| 207 |
|
| 208 |
$rowData = [ |
| 209 |
$product->ID, |
| 210 |
$product->thumbnail, |
| 211 |
//Image Check Test |
| 212 |
//(new ProductAdminHelper())->getFeaturedMedia($product->detail->featured_media), |
| 213 |
$product->post_title, |
| 214 |
ucfirst($product->detail->fulfillment_type), |
| 215 |
$product->detail->variation_type, |
| 216 |
Helper::toDecimal($product->detail->min_price), |
| 217 |
Helper::toDecimal($product->detail->max_price), |
| 218 |
$product->detail->stock_availability, |
| 219 |
ucfirst($product->post_status), |
| 220 |
gmdate('Y-m-d', strtotime($product->post_date)), |
| 221 |
]; |
| 222 |
$writer->addRow(WriterEntityFactory::createRowFromArray($rowData)); |
| 223 |
} |
| 224 |
$writer->close(); |
| 225 |
die(); |
| 226 |
} |
| 227 |
} |
| 228 |
|