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

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

73 lines 1.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
7 class CustomerReportService extends ReportService
8 {
9 public function getCustomerReportData($params = [])
10 {
11 $group = ReportHelper::processGroup(
12 $params['startDate'], $params['endDate'], $params['groupKey']
13 );
14
15 $query = App::db()->table('fct_customers as o')
16 ->selectRaw("
17 {$group['field']},
18 COUNT(*) as count
19 ")
20 ->whereBetween('created_at', [$params['startDate'], $params['endDate']])
21 ->groupByRaw($group['by'])
22 ->orderByRaw($group['by']);
23
24 $results = $query->get();
25
26 $summary = [
27 'customer_count' => 0,
28 ];
29
30 $keys = ['customer_count'];
31 $grouped = $this->getPeriodRange(
32 $params['startDate'], $params['endDate'], $group['key'], $keys
33 );
34
35 foreach ($results as $row) {
36 $item = [
37 'year' => (int) $row->year,
38 'group' => $row->group,
39 'customer_count' => (int) $row->count,
40 ];
41
42 $grouped[$row->group] = $item;
43
44 $summary['customer_count'] += $item['customer_count'];
45 }
46
47 return [
48 'summary' => $summary,
49 'grouped' => array_values($grouped),
50 ];
51 }
52
53 public function calculateFluctuations($currentMetrics, $previousMetrics)
54 {
55 $fluctuations = [];
56
57 $indices = ['customer_count'];
58
59 foreach ($indices as $key) {
60 $previousValue = $previousMetrics[$key] ?? 0;
61 $currentValue = $currentMetrics[$key] ?? 0;
62
63 if ($previousValue > 0) {
64 $fluctuations[$key] = (($currentValue - $previousValue) / $previousValue) * 100;
65 } else {
66 $fluctuations[$key] = $currentValue > 0 ? 100 : 0;
67 }
68 }
69
70 return $fluctuations;
71 }
72 }
73