PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.4
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.5.4, at app/Services/Report/ReportHelper.php

240 lines 7.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\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 /**
157 * Whitelist a groupKey value used to build raw SQL (SELECT/GROUP BY clauses)
158 * so an unrecognized or missing value never reaches the query builder.
159 *
160 * @param mixed $value
161 * @return string
162 */
163 public static function sanitizeGroupKey($value)
164 {
165 $acceptedValues = ['billing_country', 'shipping_country', 'payment_method', 'payment_status', 'default', 'monthly', 'yearly'];
166 return in_array($value, $acceptedValues) ? $value : 'payment_method';
167 }
168
169 protected static function sanitizeParams($params)
170 {
171 $rules = [
172 'startDate' => 'sanitize_text_field',
173 'endDate' => 'sanitize_text_field',
174 'compareType' => 'sanitize_text_field',
175 'compareDate' => 'sanitize_text_field',
176 'groupKey' => function ($value) {
177 return static::sanitizeGroupKey($value);
178 },
179 'currency' => 'sanitize_text_field',
180 'filterMode' => 'sanitize_text_field',
181 'storeMode' => 'sanitize_text_field',
182 'variation_ids.*' => 'intval',
183 'subscriptionType' => 'sanitize_text_field',
184 'orderStatus.*' => 'sanitize_text_field',
185 'orderTypes.*' => 'sanitize_text_field',
186 ];
187
188 return Sanitizer::sanitize($params, $rules);
189 }
190
191 /**
192 * @param string $type
193 * @param array $compareRange
194 * @param array $currentRange
195 * @return array|\DateTime[]|false
196 * @throws \Exception
197 */
198 public static function getCompareRange($type, $currentRange, $compareDate = null)
199 {
200 $currentStartDate = $currentRange['startDate'];
201 $currentendDate = $currentRange['endDate'];
202
203 $diffDays = $currentendDate->diff($currentStartDate)->days;
204
205 if ($type == 'previous_period') {
206 return [
207 $currentStartDate->copy()->subDays($diffDays + 1),
208 $currentStartDate->copy()->subDays(1)->endOfDay()
209 ];
210 } else if ($type == 'previous_month') {
211 $fromDate = $currentStartDate->copy()->subMonths(1);
212
213 return [
214 $fromDate->copy(),
215 $fromDate->addDays($diffDays)->endOfDay()
216 ];
217 } else if ($type == 'previous_quarter') {
218 $fromDate = $currentStartDate->copy()->subMonths(3);
219 return [
220 $fromDate->copy()->startOfMonth(),
221 $fromDate->addDays($diffDays)->endOfDay()
222 ];
223 } else if ($type == 'previous_year') {
224 $fromDate = $currentStartDate->copy()->subYears(1);
225 return [
226 $fromDate->copy(),
227 $fromDate->addDays($diffDays)->endOfDay()
228 ];
229 } else if ($type == 'custom' && $compareDate) {
230 $compareDate = (new DateTime($compareDate))->startOfDay();
231 return [
232 $compareDate->copy(),
233 $compareDate->addDays($diffDays)->endOfDay(),
234 ];
235 } else {
236 return false;
237 }
238 }
239 }
240