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 / Report / ReportHelper.php

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

228 lines 7.6 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\Report;
4
5 use FluentCart\App\App;
6 use FluentCart\Api\StoreSettings;
7 use FluentCart\App\Helpers\Status;
8 use FluentCart\Framework\Support\Arr;
9 use FluentCart\Framework\Support\Sanitizer;
10 use FluentCart\App\Services\DateTime\DateTime;
11
12 class ReportHelper
13 {
14
15 /**
16 * Define the group key based on the data density between the start and end dates.
17 *
18 * @param \DateTime $startDate The start date as a DateTime object.
19 * @param \DateTime $endDate The end date as a DateTime object.
20 * @return string The group key, which can be 'daily', 'monthly', or 'yearly'.
21 */
22
23 public static function defineGroupKey($startDate, $endDate)
24 {
25 $diff = $endDate->diff($startDate);
26 $days = $diff->days;
27
28 if ($days <= 91) {
29 return 'daily';
30 } elseif ($days <= 365) {
31 return 'monthly';
32 } else {
33 return 'yearly';
34 }
35 }
36
37 public static function processGroup($startDate, $endDate, $groupKey = null)
38 {
39 if (!$groupKey || $groupKey === 'default') {
40 $groupKey = static::defineGroupKey($startDate, $endDate);
41 }
42
43 switch ($groupKey) {
44 case 'yearly':
45 $format = '%Y';
46 break;
47 case 'monthly':
48 $format = '%Y-%m';
49 break;
50 default:
51 $format = '%Y-%m-%d';
52 break;
53 }
54
55 $groupBy = "DATE_FORMAT(o.created_at, '{$format}')";
56 $selection = "{$groupBy} AS `group`, YEAR(o.created_at) AS year";
57
58 return [
59 'key' => $groupKey,
60 'by' => $groupBy,
61 'field' => $selection,
62 ];
63 }
64
65 public static function processFilters($params = [])
66 {
67 $storeMode = (new StoreSettings())->get('order_mode') ?: 'live';
68 $mode = $params['filterMode'] ?? $storeMode;
69
70 $filters = [
71 'payment_status' => $params['paymentStatus'] ?? null,
72 'status' => $params['orderStatus'] ?? null,
73 'currency' => $params['currency'] ?? null,
74 'mode' => $mode,
75 ];
76
77 return array_filter($filters);
78 }
79
80 public static function processRequest($params = []): array
81 {
82 $storeMode = (new StoreSettings())->get('order_mode') ?: 'live';
83 $mode = $params['filterMode'] ?? $storeMode;
84
85 $filters = [
86 'payment_status' => Arr::get($params, 'paymentStatus'),
87 'status' => Arr::get($params, 'orderStatus'),
88 'currency' => Arr::get($params, 'currency'),
89 'mode' => $mode,
90 'type' => array_filter(Arr::get($params, 'orderTypes', [])),
91 ];
92
93 $groupKey = Arr::get($params, 'groupKey', 'daily');
94
95 return [
96 'filters' => array_filter($filters),
97 'startDate' => Arr::get($params, 'startDate'),
98 'endDate' => Arr::get($params, 'endDate'),
99 'groupKey' => $groupKey,
100 ];
101 }
102
103 public static function processParams($params = [], $additional = []): array
104 {
105 $params = static::sanitizeParams($params);
106
107 if (!App::isProActive()) {
108 $startDate = (new DateTime())->subMonth()->startOfDay();
109 $endDate = (new DateTime())->endOfDay();
110 } else {
111 $startDate = (new DateTime(Arr::get($params, 'startDate')))->startOfDay();
112 $endDate = (new DateTime(Arr::get($params, 'endDate')))->endOfDay();
113 }
114
115 $compareType = Arr::get($params, 'compareType');
116 $compareDate = Arr::get($params, 'compareDate');
117 $comparePeriod = null;
118
119 if ($compareType && $compareDate) {
120 $comparePeriod = static::getCompareRange(
121 $compareType,
122 ['startDate' => $startDate, 'endDate' => $endDate],
123 $compareDate
124 );
125 }
126
127 $attributes = [
128 'startDate' => $startDate,
129 'endDate' => $endDate,
130 'groupKey' => Arr::get($params, 'groupKey'),
131 'currency' => Arr::get($params, 'currency'),
132 'paymentMode' => Arr::get($params, 'filterMode'),
133 'variationIds' => Arr::get($params, 'variation_ids', []),
134 'comparePeriod' => $comparePeriod
135 ];
136
137 foreach ($additional as $key) {
138 $value = Arr::get($params, $key);
139
140 if ($key === 'orderStatus') {
141 if (!$value || in_array('all', $value)) {
142 $value = [Status::ORDER_ON_HOLD, Status::ORDER_FAILED];
143 }
144 } elseif ($key === 'paymentStatus') {
145 $value = Status::getReportStatuses();
146 } elseif ($key === 'subscriptionType') {
147 $value = $value ?: Status::ORDER_TYPE_SUBSCRIPTION;
148 }
149
150 $attributes[$key] = $value;
151 }
152
153 return $attributes;
154 }
155
156 protected static function sanitizeParams($params)
157 {
158 $rules = [
159 'startDate' => 'sanitize_text_field',
160 'endDate' => 'sanitize_text_field',
161 'compareType' => 'sanitize_text_field',
162 'compareDate' => 'sanitize_text_field',
163 'groupKey' => function ($value) {
164 $acceptedValues = ['billing_country', 'shipping_country', 'payment_method', 'payment_status', 'default', 'monthly', 'yearly'];
165 return in_array($value, $acceptedValues) ? $value : 'payment_method';
166 },
167 'currency' => 'sanitize_text_field',
168 'filterMode' => 'sanitize_text_field',
169 'storeMode' => 'sanitize_text_field',
170 'variation_ids.*' => 'intval',
171 'subscriptionType' => 'sanitize_text_field',
172 'orderStatus.*' => 'sanitize_text_field',
173 'orderTypes.*' => 'sanitize_text_field',
174 ];
175
176 return Sanitizer::sanitize($params, $rules);
177 }
178
179 /**
180 * @param string $type
181 * @param array $compareRange
182 * @param array $currentRange
183 * @return array|\DateTime[]|false
184 * @throws \Exception
185 */
186 public static function getCompareRange($type, $currentRange, $compareDate = null)
187 {
188 $currentStartDate = $currentRange['startDate'];
189 $currentendDate = $currentRange['endDate'];
190
191 $diffDays = $currentendDate->diff($currentStartDate)->days;
192
193 if ($type == 'previous_period') {
194 return [
195 $currentStartDate->copy()->subDays($diffDays + 1),
196 $currentStartDate->copy()->subDays(1)->endOfDay()
197 ];
198 } else if ($type == 'previous_month') {
199 $fromDate = $currentStartDate->copy()->subMonths(1);
200
201 return [
202 $fromDate->copy(),
203 $fromDate->addDays($diffDays)->endOfDay()
204 ];
205 } else if ($type == 'previous_quarter') {
206 $fromDate = $currentStartDate->copy()->subMonths(3);
207 return [
208 $fromDate->copy()->startOfMonth(),
209 $fromDate->addDays($diffDays)->endOfDay()
210 ];
211 } else if ($type == 'previous_year') {
212 $fromDate = $currentStartDate->copy()->subYears(1);
213 return [
214 $fromDate->copy(),
215 $fromDate->addDays($diffDays)->endOfDay()
216 ];
217 } else if ($type == 'custom' && $compareDate) {
218 $compareDate = (new DateTime($compareDate))->startOfDay();
219 return [
220 $compareDate->copy(),
221 $compareDate->addDays($diffDays)->endOfDay(),
222 ];
223 } else {
224 return false;
225 }
226 }
227 }
228