-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompany.php
255 lines (232 loc) · 6.95 KB
/
Company.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace App\Models;
use App\Actions\Log\ApprovalHandler;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Permission\Models\Role;
class Company extends Model
{
use HasFactory;
use LogsActivity;
protected $fillable = ['name', 'description', 'website', 'postcode', 'is_approved', 'motivation',
'house_number', 'street', 'city', 'logo_path', 'phone_number', 'sponsorship_id', 'is_sponsorship_approved',
'dark_logo_path'];
/**
* Establishes a relationship between the company and
* the sponsorship/sponsor tier
* @return BelongsTo
*/
public function sponsorship(): BelongsTo
{
return $this->belongsTo(Sponsorship::class);
}
/**
* Establishes a relationship between the company and its booth
* @return HasOne
*/
public function booth(): HasOne
{
return $this->hasOne(Booth::class);
}
/**
* Establishes a relationship between the company and the invitation
* for users to join it
* @return HasMany
*/
public function invitations(): HasMany
{
return $this->hasMany(Invitation::class);
}
/**
* Establishes a relationship between the company and the employees
* @return HasMany
*/
public function users(): HasMany
{
return $this->hasMany(User::class);
}
/**
* Establishes relationship between a company and their presentations
* NOTE 1: Not all companies have presentations
* NOTE 2: All companies can only have one presentation except gold sponsor (they have two)
* @return HasMany
*/
public function presentations(): HasMany
{
return $this->hasMany(Presentation::class);
}
/**
* Establishes relationship between a company and the attributes of the internships they provide
* @return HasMany
*/
public function internshipAttributes(): HasMany
{
return $this->hasMany(InternshipAttribute::class);
}
/**
* Settings for the log system for this model
* @return LogOptions
*/
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logAll()
->setDescriptionForEvent(fn(string $eventName)
=> "{$this->name} has been {$eventName}" . (Auth::user() ? " by " . Auth::user()->name : ''))
->logOnlyDirty()
->dontLogIfAttributesChangedOnly(['is_approved', 'is_sponsorship_approved', 'sponsorship_id']);
}
/**
* Returns the status of the company based on the approval status
* @return Attribute
*/
public function status(): Attribute
{
return Attribute::make(
get: fn() => $this->is_approved ? 'Approved' : 'Awaiting approval'
);
}
/**
* Returns the status of the company's sponsorship
* @return Attribute
*/
public function sponsorshipStatus(): Attribute
{
return Attribute::make(
get: function () {
if (!$this->sponsorship) {
return 'Not requested';
}
return $this->sponsorship->is_approved ? 'Approved' : 'Awaiting approval';
}
);
}
/**
* Returns the status of the company's booth
* @return Attribute
*/
public function boothStatus(): Attribute
{
return Attribute::make(
get: function () {
if (!$this->booth) {
return 'Not requested';
}
return $this->booth->is_approved ? 'Approved' : 'Awaiting approval';
}
);
}
/**
* Returns the representative of the company
* @return Attribute
*/
public function representative(): Attribute
{
return Attribute::make(
get: function () {
return $this->users()
->with('roles')
->role('company representative')
->first();
}
);
}
/**
* Returns the booth owners of the company
*
* @return Attribute
*/
public function boothOwners(): Attribute
{
return Attribute::make(
get: function () {
return $this->users()
->with('roles')
->role('booth owner')
->get();
}
);
}
/**
* Checks if the company is HZ University of Applied Sciences
* @return Attribute
*/
public function isHz(): Attribute
{
return Attribute::make(
get: function () {
return $this->name == 'HZ University of Applied Sciences';
}
);
}
/**
* Checks if the team is the gold sponsor
* @return Attribute
*/
public function isGoldSponsor(): Attribute
{
return Attribute::make(
get: fn() => $this->sponsorship ? $this->sponsorship->name === 'gold' && $this->is_sponsorship_approved : 0
);
}
/**
* Returns if the company is approved sponsor in general
* @return Attribute
*/
public function isSponsor(): Attribute
{
return Attribute::make(
get: fn() => $this->sponsorship_id && $this->is_sponsorship_approved ? $this->sponsorship : null
);
}
/**
* Calculates how many presentations does the company have left
*
* @return Attribute
*/
public function hasPresentationsLeft(): Attribute
{
return Attribute::make(
get: function () {
$max_presentations = $this->is_gold_sponsor ? 2 : 1;
return $this->is_approved && $this->presentations->count() < $max_presentations
|| $this->isHz;
}
);
}
/**
* Handle a (dis)approval of this Teams request to join the conference.
*
* @param bool $isApproved
* @return void
*/
public function handleCompanyApproval(bool $isApproved): void
{
(new ApprovalHandler())->execute($this, $isApproved);
}
/**
* Handle a (dis)approval of the company's request for a sponsorship.
*
* @param bool $isApproved
* @return void
*/
public function handleSponsorshipApproval(bool $isApproved): void
{
(new ApprovalHandler())->execute($this, $isApproved, 'is_sponsorship_approved');
if (!$isApproved) {
$this->disableLogging();
$this->is_sponsorship_approved = null;
$this->sponsorship_id = null;
$this->save();
$this->enableLogging();
}
}
}