PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.5 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 All 48 releases
fluent-cart / app / Services / OrdersQuery.php

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

173 lines 5.3 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 $oldHook = 'fluentcart/orders_filter_' . $providerName;
62 if (has_action($oldHook)) {
63 _deprecated_hook($oldHook, '1.3.16', 'fluent_cart/orders_filter_' . $providerName, 'Use fluent_cart/orders_filter_' . $providerName . ' instead of ' . $oldHook . '.');
64 do_action_ref_array($oldHook, [&$q, $items]);
65 }
66 do_action_ref_array('fluent_cart/orders_filter_' . $providerName, [&$q, $items]);
67 }
68 });
69 }
70 });
71 } else {
72 if ($paymentStatuses = $this->args['payment_statuses']) {
73 $ordersQuery->whereIn('payment_status', $paymentStatuses);
74 }
75
76 if ($orderStatuses = $this->args['order_statuses']) {
77 $ordersQuery->whereIn('status', $orderStatuses);
78 }
79
80 if ($shippingStatuses = $this->args['shipping_statuses']) {
81 $ordersQuery->whereIn('shipping_status', $shippingStatuses);
82 }
83
84 if ($search = $this->args['search']) {
85 $ordersQuery->searchBy($search);
86 }
87 }
88
89 $acceptedViewsMaps = [
90 'on-hold' => 'status',
91 'paid' => 'payment_status',
92 'unpaid' => 'payment_status',
93 'completed' => 'status',
94 'processing' => 'status',
95 'renewal' => 'type',
96 'subscription' => 'type',
97 ];
98
99 $activeView = $this->args['active_view'];
100
101 if (isset($acceptedViewsMaps[$activeView])) {
102 $ordersQuery->where($acceptedViewsMaps[$activeView], $activeView);
103 }
104
105 $this->model = $ordersQuery;
106 }
107
108 public function get()
109 {
110 $orderModel = $this->model;
111
112 if ($limit = $this->args['limit']) {
113 $orderModel = $orderModel->limit($limit);
114 }
115
116 if ($offset = $this->args['offset']) {
117 $orderModel = $orderModel->offset($offset);
118 }
119
120 return $this->returnOrders($orderModel->get());
121 }
122
123 public function paginate($perPage = 10)
124 {
125 return $this->returnOrders($this->model->paginate($perPage));
126 }
127
128 public function getModel()
129 {
130 return $this->model;
131 }
132
133 private function returnOrders($orders)
134 {
135 return $orders;
136 }
137
138 private function formatAdvancedFilters()
139 {
140 $filters = $this->args['filters_groups_raw'];
141 $groups = [];
142
143 foreach ($filters as $filterGroup) {
144 $group = [];
145 foreach ($filterGroup as $filterItem) {
146 if (count($filterItem['source']) != 2 || empty($filterItem['source'][0]) || empty($filterItem['source'][1]) || empty($filterItem['operator'])) {
147 continue;
148 }
149 $provider = $filterItem['source'][0];
150
151 if (!isset($group[$provider])) {
152 $group[$provider] = [];
153 }
154
155 $property = $filterItem['source'][1];
156
157 $group[$provider][] = [
158 'property' => $property,
159 'operator' => $filterItem['operator'],
160 'value' => $filterItem['value']
161 ];
162 }
163
164 if ($group) {
165 $groups[] = $group;
166 }
167 }
168
169 $this->args['filters_groups'] = $groups;
170 }
171
172 }
173