Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportingFlyplatesAddedRequest
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
56
 _make_line_chart_data_for_counts
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Actions;
4
5use App\DTO\ReportingFlyplatesAddedRequestDTO;
6use App\Http\Models\Auth\User;
7use App\Http\Models\Shortcut;
8use Carbon\Carbon;
9use MongoDB\BSON\UTCDateTime;
10
11class ReportingFlyplatesAddedRequest
12{
13    /**
14     * Execute the action.
15     *
16     * @return array
17     */
18    public function execute(ReportingFlyplatesAddedRequestDTO $filter)
19    {
20        $users = User::select([
21            'id',
22            'first_name',
23            'last_name',
24            'company_group_id',
25            'company_id',
26            'created_at',
27            'avatar',
28        ]);
29
30        // Parse or set default dates
31        $start_date = $filter->fromDate;
32        $end_date = $filter->toDate;
33
34        if ($start_date && $end_date) {
35            $start_date = Carbon::parse($start_date);
36            $end_date = Carbon::parse($end_date);
37        } else {
38            $start_date = Carbon::now()->subMonths(12)->startOfMonth();
39            $end_date = Carbon::now()->endOfMonth();
40        }
41
42        // Filter users by IDs
43        $user_ids = explode(',', $filter->user_ids);
44        $user_ids = array_filter($user_ids, function ($value) {
45            return $value !== '';
46        });
47        if (count($user_ids) > 0) {
48            $users = $users->whereIn('id', $user_ids);
49        }
50
51        // Filter by group IDs
52        $group_ids = explode(',', $filter->group_ids);
53        $group_ids = array_filter($group_ids, function ($value) {
54            return $value !== '';
55        });
56        if (count($group_ids) > 0) {
57            $users = $users->whereIn('company_group_id', $group_ids);
58        }
59
60        // Filter by subgroup IDs
61        $subgroup_ids = explode(',', $filter->subgroup_ids);
62        $subgroup_ids = array_filter($subgroup_ids, function ($value) {
63            return $value !== '';
64        });
65        if (count($subgroup_ids) > 0) {
66            $users = $users->whereIn('company_group_id', $subgroup_ids);
67        }
68
69        // Fetch all users that are active, ignoring company-specific constraints
70        $users = $users->whereStatus('Active')->get();
71        $ids = $users->pluck('id')->toArray();
72
73        // Fetch flyplates data across all users
74        $flyplates = Shortcut::whereIn('user_id', $ids)
75            ->whereBetween('created_at', [
76                new UTCDateTime($start_date->getTimestamp() * 1000),
77                new UTCDateTime($end_date->getTimestamp() * 1000),
78            ])
79            ->where('user_defined', '=', false)
80            ->get();
81
82        $total_users = $users->count();
83        $months = $end_date->diffInMonths($start_date);
84        $chart = $this->_make_line_chart_data_for_counts($flyplates, $months);
85        $total_flyplate_created = $flyplates->count();
86        $average_flyplate_per_active_user = $total_users > 0 ? $total_flyplate_created / $total_users : 0;
87
88        return [
89            'chart' => $chart,
90            'total_flyplate_created' => number_format($total_flyplate_created, 0, '.', ','),
91            'average_flyplate_per_active_user' => number_format(round($average_flyplate_per_active_user, 2), 2, '.', ','),
92        ];
93    }
94
95    private function _make_line_chart_data_for_counts($flycuts, $months)
96    {
97        // Initialize an array with 12 months set to 0
98        $line_chart_data = [];
99        $current_date = Carbon::now();
100        for ($i = 0; $i < $months; $i++) {
101            $month_year = $current_date->subMonth()->format('M Y');
102            $line_chart_data[$month_year] = ['count' => 0];
103        }
104
105        // Reset the current date to now
106        $current_date = Carbon::now();
107
108        // Aggregate the data by month
109        $aggregated_data = $flycuts->groupBy(function ($date) {
110            return Carbon::parse($date->created_at)->format('Y-m');
111        });
112
113        // Process the aggregated data and populate the line chart data array
114        foreach ($aggregated_data as $month_year => $usages) {
115            $month = Carbon::parse($usages->first()->created_at)->format('M Y');
116            $flycuts_used = $usages->count();
117            $line_chart_data[$month] = ['count' => $flycuts_used];
118        }
119
120        // Ensure the data is sorted by date keys
121        uksort($line_chart_data, function ($a, $b) {
122            return strtotime($b) - strtotime($a);
123        });
124
125        return $line_chart_data;
126    }
127}