Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 117 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AllUsersReportOverviewExport | |
0.00% |
0 / 117 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
array | |
0.00% |
0 / 102 |
|
0.00% |
0 / 1 |
72 | |||
headings | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Exports; |
4 | |
5 | use Carbon\Carbon; |
6 | use Carbon\CarbonPeriod; |
7 | use App\Http\Models\Chart; |
8 | use MongoDB\BSON\UTCDateTime; |
9 | use App\Http\Models\Auth\User; |
10 | use App\Http\Models\HubspotProperties; |
11 | use App\Http\Models\Admin\CompanyLicenses; |
12 | use Vitorccs\LaravelCsv\Concerns\FromArray; |
13 | use Vitorccs\LaravelCsv\Concerns\Exportable; |
14 | use Vitorccs\LaravelCsv\Concerns\WithHeadings; |
15 | use App\Traits\AccountCenter\Reporting\ChartTrait; |
16 | |
17 | class AllUsersReportOverviewExport implements FromArray, WithHeadings |
18 | { |
19 | use Exportable; |
20 | use ChartTrait; |
21 | |
22 | public function array(): array |
23 | { |
24 | $users = User::select([ |
25 | "id", |
26 | "first_name", |
27 | "last_name", |
28 | "company_group_id", |
29 | "company_id", |
30 | "deleted_at", |
31 | "avatar" |
32 | ])->get(); |
33 | |
34 | $ids = $users->pluck("id")->toArray(); |
35 | |
36 | $period = CarbonPeriod::create(Carbon::now()->subMonths(12)->toDateString(), '1 month', Carbon::now()->toDateString()); |
37 | $data = []; |
38 | |
39 | foreach ($period as $date) { |
40 | $month_year = $date->format('M Y'); |
41 | $flychart = Chart::whereIn("user_id", $ids)->where("month_year", $month_year)->get(); |
42 | |
43 | $purchased_licenses = 0; |
44 | $assigned_licenses = 0; |
45 | $activate_licenses = 0; |
46 | $extension_installed = 0; |
47 | |
48 | $company_license = CompanyLicenses::active() |
49 | ->whereBetween("created_at", [ |
50 | new UTCDateTime(strtotime($date->startOfMonth()->toDateString()) * 1000), |
51 | new UTCDateTime(strtotime($date->endOfMonth()->toDateString()) * 1000), |
52 | ]) |
53 | ->first(); |
54 | |
55 | if ($company_license) { |
56 | $purchased_licenses = $company_license->total_number_of_licenses_available; |
57 | $assigned_licenses = $company_license->total_number_of_licenses_available - $company_license->total_number_of_licenses_remaining; |
58 | } |
59 | |
60 | $user_ids = $ids; |
61 | |
62 | $extension_installed = HubspotProperties::whereIn("flymsg_id", $user_ids) |
63 | ->where(function ($query) { |
64 | $query->where(function ($subQuery) { |
65 | $subQuery->where("flymsg_chrome_extension_installed", "Yes") |
66 | ->where("flymsg_chrome_extension_uninstalled", "No"); |
67 | })->orWhere(function ($subQuery) { |
68 | $subQuery->where("flymsg_edge_extension_installed", "Yes") |
69 | ->where("flymsg_edge_extension_uninstalled", "No"); |
70 | }); |
71 | }) |
72 | ->whereBetween("created_at", [ |
73 | new UTCDateTime(strtotime($date->startOfMonth()->toDateString()) * 1000), |
74 | new UTCDateTime(strtotime($date->endOfMonth()->toDateString()) * 1000), |
75 | ]) |
76 | ->count(); |
77 | |
78 | $user_active_ids = User::select(['id']) |
79 | ->whereStatus("Active") |
80 | ->whereBetween("updated_at", [ |
81 | new UTCDateTime(strtotime($date->startOfMonth()->toDateString()) * 1000), |
82 | new UTCDateTime(strtotime($date->endOfMonth()->toDateString()) * 1000), |
83 | ]) |
84 | ->pluck("id"); |
85 | $activate_licenses = count($user_active_ids); |
86 | |
87 | $roi_spotlight = number_format($flychart->sum("cost_saved"), 2); |
88 | $productivity_spotlight = number_format($flychart->sum("time_saved"), 2); |
89 | $total_characters_typed = number_format($flychart->sum("characters_saved"), 0); |
90 | |
91 | $coach_level_beginner = 0; |
92 | $coach_level_intermediate = 0; |
93 | $coach_level_proficient = 0; |
94 | $coach_level_advanced = 0; |
95 | $coach_level_expert = 0; |
96 | |
97 | $start = new UTCDateTime(strtotime($date->startOfMonth()->toDateString()) * 1000); |
98 | $end = new UTCDateTime(strtotime($date->endOfMonth()->toDateString()) * 1000); |
99 | |
100 | $characterUsages = $this->getTotalCharactersByUsersInPeriod($ids, $start, $end); |
101 | |
102 | // Step 2: Categorize the users based on the total characters saved |
103 | $categories = [ |
104 | 'beginner' => 0, |
105 | 'intermediate' => 0, |
106 | 'proficient' => 0, |
107 | 'advanced' => 0, |
108 | 'expert' => 0, |
109 | ]; |
110 | |
111 | foreach ($characterUsages as $usage) { |
112 | $total_characters = $usage->total_characters; |
113 | |
114 | if ($total_characters < 10000) { |
115 | $categories['beginner']++; |
116 | } elseif ($total_characters < 25000) { |
117 | $categories['intermediate']++; |
118 | } elseif ($total_characters < 50000) { |
119 | $categories['proficient']++; |
120 | } elseif ($total_characters < 100000) { |
121 | $categories['advanced']++; |
122 | } else { |
123 | $categories['expert']++; |
124 | } |
125 | } |
126 | |
127 | $coach_level_beginner = $categories['beginner']; |
128 | $coach_level_intermediate = $categories['intermediate']; |
129 | $coach_level_proficient = $categories['proficient']; |
130 | $coach_level_advanced = $categories['advanced']; |
131 | $coach_level_expert = $categories['expert']; |
132 | |
133 | $data[$month_year] = [ |
134 | 'Month' => $month_year, |
135 | 'Purchased Licenses' => $purchased_licenses, |
136 | 'Assigned Licenses' => $assigned_licenses, |
137 | 'Activate Licenses' => $activate_licenses, |
138 | 'Extension Installed' => $extension_installed, |
139 | 'ROI Spotlight' => $roi_spotlight, |
140 | 'Productivity Spotlight' => $productivity_spotlight, |
141 | 'Total Characters Typed' => $total_characters_typed, |
142 | 'Coach Level Beginner' => $coach_level_beginner, |
143 | 'Coach Level Intermediate' => $coach_level_intermediate, |
144 | 'Coach Level Proficient' => $coach_level_proficient, |
145 | 'Coach Level Advanced' => $coach_level_advanced, |
146 | 'Coach Level Expert' => $coach_level_expert, |
147 | ]; |
148 | } |
149 | |
150 | return $data; |
151 | } |
152 | |
153 | public function headings(): array |
154 | { |
155 | return [ |
156 | 'Months', |
157 | 'Purchased Licenses', |
158 | 'Assigned Licenses', |
159 | 'Activate Licenses', |
160 | 'Extension Installed', |
161 | 'ROI Spotlight', |
162 | 'Productivity Spotlight', |
163 | 'Total Characters Typed', |
164 | 'Coach Level Beginner', |
165 | 'Coach Level Intermediate', |
166 | 'Coach Level Proficient', |
167 | 'Coach Level Advanced', |
168 | 'Coach Level Expert', |
169 | ]; |
170 | } |
171 | } |