PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Services / OrdersQuery.php

OrdersQuery.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Services/OrdersQuery.php

168 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services;
4
5 use FluentCart\App\Models\Order;
6 use FluentCart\Framework\Support\Arr;
7
8 class OrdersQuery
9 {
10 private $args = [];
11
12 private $model = null;
13
14 public function __construct($args = [])
15 {
16 $this->args = wp_parse_args($args, [
17 'with' => ['customer', 'order_items'],
18 'filter_type' => 'simple',
19 'filters_groups' => [],
20 'filters_groups_raw' => [],
21 'search' => '',
22 'sort_by' => 'id',
23 'sort_type' => 'DESC',
24 'payment_statuses' => [], // payment_status
25 'order_statuses' => [], // status
26 'shipping_statuses' => [], // shipping_status
27 'limit' => false,
28 'offset' => false,
29 'payment_status' => '',
30 'active_view' => 'all'
31 ]);
32
33 $this->setupQuery();
34 }
35
36 private function setupQuery()
37 {
38 if ($this->args['filters_groups_raw']) {
39 $this->formatAdvancedFilters();
40 }
41
42 $ordersQuery = Order::with($this->args['with']);
43
44 if ($sortBy = $this->args['sort_by']) {
45 if (in_array($sortBy, (new Order())->getFillable()) || $sortBy == 'id') {
46 $ordersQuery->orderBy($sortBy, $this->args['sort_type']);
47 }
48 }
49
50 if ($this->args['filter_type'] == 'advanced') {
51 $filtersGroups = $this->args['filters_groups'];
52 $ordersQuery->where(function ($queryGroup) use ($filtersGroups) {
53 foreach ($filtersGroups as $groupIndex => $group) {
54 $method = 'orWhere';
55 if ($groupIndex == 0) {
56 $method = 'where';
57 }
58
59 $queryGroup->{$method}(function ($q) use ($group) {
60 foreach ($group as $providerName => $items) {
61 do_action_ref_array('fluent_cart/orders_filter_' . $providerName, [&$q, $items]);
62 }
63 });
64 }
65 });
66 } else {
67 if ($paymentStatuses = $this->args['payment_statuses']) {
68 $ordersQuery->whereIn('payment_status', $paymentStatuses);
69 }
70
71 if ($orderStatuses = $this->args['order_statuses']) {
72 $ordersQuery->whereIn('status', $orderStatuses);
73 }
74
75 if ($shippingStatuses = $this->args['shipping_statuses']) {
76 $ordersQuery->whereIn('shipping_status', $shippingStatuses);
77 }
78
79 if ($search = $this->args['search']) {
80 $ordersQuery->searchBy($search);
81 }
82 }
83
84 $acceptedViewsMaps = [
85 'on-hold' => 'status',
86 'paid' => 'payment_status',
87 'unpaid' => 'payment_status',
88 'completed' => 'status',
89 'processing' => 'status',
90 'renewal' => 'type',
91 'subscription' => 'type',
92 ];
93
94 $activeView = $this->args['active_view'];
95
96 if (isset($acceptedViewsMaps[$activeView])) {
97 $ordersQuery->where($acceptedViewsMaps[$activeView], $activeView);
98 }
99
100 $this->model = $ordersQuery;
101 }
102
103 public function get()
104 {
105 $orderModel = $this->model;
106
107 if ($limit = $this->args['limit']) {
108 $orderModel = $orderModel->limit($limit);
109 }
110
111 if ($offset = $this->args['offset']) {
112 $orderModel = $orderModel->offset($offset);
113 }
114
115 return $this->returnOrders($orderModel->get());
116 }
117
118 public function paginate($perPage = 10)
119 {
120 return $this->returnOrders($this->model->paginate($perPage));
121 }
122
123 public function getModel()
124 {
125 return $this->model;
126 }
127
128 private function returnOrders($orders)
129 {
130 return $orders;
131 }
132
133 private function formatAdvancedFilters()
134 {
135 $filters = $this->args['filters_groups_raw'];
136 $groups = [];
137
138 foreach ($filters as $filterGroup) {
139 $group = [];
140 foreach ($filterGroup as $filterItem) {
141 if (count($filterItem['source']) != 2 || empty($filterItem['source'][0]) || empty($filterItem['source'][1]) || empty($filterItem['operator'])) {
142 continue;
143 }
144 $provider = $filterItem['source'][0];
145
146 if (!isset($group[$provider])) {
147 $group[$provider] = [];
148 }
149
150 $property = $filterItem['source'][1];
151
152 $group[$provider][] = [
153 'property' => $property,
154 'operator' => $filterItem['operator'],
155 'value' => $filterItem['value']
156 ];
157 }
158
159 if ($group) {
160 $groups[] = $group;
161 }
162 }
163
164 $this->args['filters_groups'] = $groups;
165 }
166
167 }
168