-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUser.php
321 lines (283 loc) · 7.48 KB
/
User.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
use \Spatie\Tags\HasTags;
// public $dates = [
// 'dob', 'joining_date', 'leaving_date'
// ];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'middle_name', 'username', 'email', 'personal_email', 'password', 'dob', 'avatar',
'address', 'designation', 'about', 'mobile_no', 'skype', 'trello', 'slack', 'github', 'twitter', 'linkedin',
'weekly_hours_credit', 'base_salary', 'joining_date', 'leaving_date', 'is_active', 'use_icon_sidebar',
'exclude_from_salary', 'exclude_from_attendance', 'role', 'remarks', 'tds_amount', 'professional_tax_amount',
];
public function getNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* Get avatar url.
*
* @return string
*/
public function getAvatarUrlAttribute()
{
return \Storage::url($this->avatar);
}
/**
* @param Builder $query
* @param \Carbon\Carbon $date
*
* @return mixed
*/
public function scopeDoesntOnLeave(Builder $query, Carbon $date)
{
return $query->whereDoesntHave('leaves', function ($query) use ($date) {
$query->betweenDate($date)
->where('is_approved', true);
});
}
/**
* Admin users only.
*
* @param Builder $query
* @return Builder
*/
public function scopeAdmin(Builder $query)
{
return $query->whereRole('admin');
}
/**
* @param Builder $query
* @return mixed
*/
public function scopeSalaryExclude(Builder $query)
{
return $query->whereExcludeFromSalary(true);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function attendance()
{
return $this->hasMany(UserAttendance::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function projects()
{
return $this->belongsToMany(Project::class, 'project_user');
}
/**
* @return UserAttendance|object|null
*/
public function getTodayAttendanceAttribute()
{
return $this->attendanceOfTheDay(today());
}
/**
* @param $startDate
* @param $endDate
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\HasMany|null|object
*/
public function attendanceFromDates($startDate, $endDate)
{
return $this->attendance()
->whereBetween('date', [$startDate, $endDate])
->get();
}
/**
* @param $date
* @return UserAttendance|null|object
*/
public function attendanceOfTheDay($date)
{
return $this->attendance()
->where('date', $date)
->first();
}
/**
* @param $startDate
* @param $endDate
* @return \Illuminate\Support\Collection
*/
public function dateRangeAttendances($startDate, $endDate)
{
$days = generateDateRange($startDate, $endDate, '1 day');
$attendances = $this->attendanceFromDates($startDate->toDateString(), $endDate->toDateString());
return collect($days)->map(function (Carbon $day) use ($attendances) {
$attendance = $attendances->where('date', $day->format('Y-m-d'))->first();
return [
'id' => $attendance ? $attendance->getKey() : null,
'date' => $day->format('Y-m-d'),
'day' => $day->format('D'),
'hours' => $attendance ? $attendance->hours : 0,
'second' => $attendance ? $attendance->total_times : 0,
'is_on_leave' => $attendance && $attendance->is_on_leave,
];
});
}
/**
* @return UserAttendance|object|null
*/
public function getWeekAttendancesAttribute()
{
return $this->dateRangeAttendances(today()->startOfWeek(), today()->endOfWeek());
}
/**
* @return int
*/
public function getWeeklyWorksHrsPercentage()
{
$weekSeconds = $this->week_attendances->sum('second');
if (! $weekSeconds) {
return 0;
} elseif ($weekSeconds >= $this->weekly_hours_credit * 3600) {
return 100;
}
return number_format(($weekSeconds * 100) / ($this->weekly_hours_credit * 3600), 2);
}
/**
* @return UserAttendance|\Illuminate\Database\Eloquent\Model
*/
public function firstOrCreateAttendance()
{
if ($attendance = $this->today_attendance) {
return $attendance;
}
return $this->createAttendance(today());
}
/**
* @param \Carbon\Carbon $date
* @param array $data
*
* @return \App\UserAttendance
*/
public function createAttendance(Carbon $date, array $data = [])
{
return $this->attendance()->create(array_merge([
'date' => $date,
], $data));
}
/**
* @param \Carbon\Carbon $date
*
* @return \App\UserAttendance
*/
public function createAbsent(Carbon $date)
{
return $this->createAttendance($date, [
'status' => UserAttendance::$ABSENT,
]);
}
/**
* Get recent notifications.
*
* @return mixed
*/
public function getRecentNotifications()
{
return $this->notifications()->take(5)->get();
}
/**
* Is user excluded from attendance.
*
* @return mixed
*/
public function attendanceExcluded()
{
return $this->exclude_from_attendance == true;
}
/**
* Is user excluded from salary.
*
* @return mixed
*/
public function salaryExcluded()
{
return $this->exclude_from_salary == true;
}
/**
* Detect is user object is mine.
*/
public function isMe()
{
return $this->id == auth()->id();
}
/**
* Determine if user is admin.
*
* @return bool
*/
public function isAdmin()
{
return $this->role === 'admin';
}
/**
* Determine if user is employee.
*
* @return bool
*/
public function isEmployee()
{
return $this->role === 'employee';
}
/**
* Determine if user is accountant.
*
* @return bool
*/
public function isAccountant()
{
return $this->role === 'accountant';
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function designations()
{
return $this->belongsToMany(Designation::class)
->withTimestamps()
->orderByDesc('pivot_created_at');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function leaves()
{
return $this->hasMany(Leave::class);
}
/**
* Get user's current designation.
*
* @return string
*/
public function getDesignationAttribute()
{
return $this->designations->first();
}
public function account()
{
return $this->hasOne(Account::class, 'user_id');
}
}