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