Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
UsersExport | |
0.00% |
0 / 84 |
|
0.00% |
0 / 4 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
collection | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
30 | |||
headings | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
map | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | |
3 | namespace App\Exports; |
4 | |
5 | use Carbon\Carbon; |
6 | use App\Http\Models\Admin\AdminUserInvitation; |
7 | use App\Http\Models\Auth\Role; |
8 | use App\Http\Models\Auth\User; |
9 | use App\Http\Models\FakeInvitationUser; |
10 | use Vitorccs\LaravelCsv\Concerns\FromCollection; |
11 | use Vitorccs\LaravelCsv\Concerns\WithMapping; |
12 | use Vitorccs\LaravelCsv\Concerns\WithHeadings; |
13 | use Illuminate\Support\Facades\Log; |
14 | |
15 | class UsersExport implements FromCollection, WithHeadings, WithMapping |
16 | { |
17 | use \Vitorccs\LaravelCsv\Concerns\Exportable; |
18 | |
19 | public $only_deactivated; |
20 | public $only_selected_user_ids; |
21 | |
22 | public function __construct($only_deactivated, $only_selected_user_ids = []) |
23 | { |
24 | $this->only_deactivated = $only_deactivated; |
25 | $this->only_selected_user_ids = $only_selected_user_ids; |
26 | } |
27 | |
28 | public function collection() |
29 | { |
30 | $admin = auth()->user(); |
31 | $role = role($admin->role); |
32 | |
33 | $userQuery = User::select([ |
34 | "first_name", |
35 | "last_name", |
36 | "email", |
37 | "role", |
38 | "status", |
39 | "activation_date", |
40 | "created_at", |
41 | "deleted_at", |
42 | "deactivated_at", |
43 | "avatar" |
44 | ]); |
45 | |
46 | if (!empty($this->only_selected_user_ids)) { |
47 | $userQuery->whereIn("_id", $this->only_selected_user_ids); |
48 | } |
49 | |
50 | if ($role !== Role::GLOBAL_ADMIN && $role !== Role::VENGRESO_ADMIN) { |
51 | $user_group = $admin->company_group; |
52 | $userQuery->where("company_group_id", $user_group->id); |
53 | } |
54 | |
55 | if ($this->only_deactivated) { |
56 | $userQuery->onlyTrashed(); |
57 | } |
58 | |
59 | $users = $userQuery->get(); |
60 | |
61 | // Fetch invitations and convert to fake models |
62 | $invitations = AdminUserInvitation::when(!empty($this->only_selected_user_ids), function ($q) { |
63 | return $q->whereIn("_id", $this->only_selected_user_ids); |
64 | }) |
65 | ->get() |
66 | ->map(function ($invite) { |
67 | return new FakeInvitationUser([ |
68 | 'first_name' => null, |
69 | 'last_name' => null, |
70 | 'email' => $invite->email, |
71 | 'role' => 'invited', |
72 | 'status' => 'Invited', |
73 | 'activation_date' => null, |
74 | 'created_at' => $invite->created_at ?? now(), |
75 | 'deleted_at' => null, |
76 | 'deactivated_at' => null, |
77 | 'avatar' => null, |
78 | ]); |
79 | }); |
80 | |
81 | return $users->concat($invitations); |
82 | } |
83 | |
84 | public function headings(): array |
85 | { |
86 | return [ |
87 | 'First Name', |
88 | 'Last Name', |
89 | 'Email', |
90 | 'Group/Subgroup', |
91 | 'License Type', |
92 | 'Account Status', |
93 | 'Account Status Date', |
94 | 'Role', |
95 | ]; |
96 | } |
97 | |
98 | public function map($user): array |
99 | { |
100 | // Group/subgroup name |
101 | $group_name = "Not Assigned"; |
102 | if (isset($user->company_group) && $user->company_group) { |
103 | if ($user->company_group->parent_id) { |
104 | $group_name = $user->company_group->parent->name ?? $group_name; |
105 | } else { |
106 | $group_name = $user->company_group->name ?? $group_name; |
107 | } |
108 | } |
109 | |
110 | // Subscription/license type (only for real users) |
111 | $license_type = "Not Assigned"; |
112 | if (method_exists($user, 'subscription')) { |
113 | $subscription = $user->subscription("main"); |
114 | $license_type = $subscription && $subscription->plan ? $subscription->plan->title : "Not Assigned"; |
115 | } |
116 | |
117 | // Role handling |
118 | $role = isset($user->role) ? role($user->role) : ''; |
119 | |
120 | // Account status date |
121 | $account_status_date = match (true) { |
122 | !empty($user->deactivated_at) => $user->deactivated_at->toFormattedDateString(), |
123 | !empty($user->activation_date) => Carbon::parse($user->activation_date)->toFormattedDateString(), |
124 | !empty($user->created_at) => Carbon::parse($user->created_at)->toFormattedDateString(), |
125 | default => '', |
126 | }; |
127 | |
128 | // Handle role and date override for invited users |
129 | if (isset($user->status) && $user->status == "Invited") { |
130 | $invitation = AdminUserInvitation::where("email", $user->email)->first(); |
131 | if ($invitation) { |
132 | $account_status_date = $invitation->updated_at?->toFormattedDateString() ?? $account_status_date; |
133 | $role = $invitation->role_name ?? $role; |
134 | } |
135 | } |
136 | |
137 | return [ |
138 | $user->first_name ?? '', |
139 | $user->last_name ?? '', |
140 | $user->email ?? '', |
141 | $group_name, |
142 | $license_type, |
143 | $user->status ?? '', |
144 | $account_status_date, |
145 | $role |
146 | ]; |
147 | } |
148 | } |