7
7
use App \Models \Location ;
8
8
use Carbon \CarbonPeriod ;
9
9
use App \Models \AbsenceType ;
10
- use Illuminate \Support \Arr ;
11
10
use App \Contracts \AddsAbsences ;
12
11
use App \Formatter \DateFormatter ;
12
+ use Laravel \Jetstream \Jetstream ;
13
13
use Illuminate \Support \Facades \Gate ;
14
14
use Illuminate \Support \Facades \Mail ;
15
15
use Illuminate \Support \Facades \Validator ;
@@ -29,11 +29,12 @@ public function __construct()
29
29
$ this ->dateFormatter = app (DateFormatter::class);
30
30
}
31
31
32
- public function add (User $ employee , array $ data ): void
32
+ public function add (User $ user , Location $ location , int $ managingAbsenceForId , array $ data )
33
33
{
34
- Gate::forUser ($ employee )->authorize ('addAbsence ' , [
34
+ Gate::forUser ($ user )->authorize ('addAbsence ' , [
35
35
Absence::class,
36
- $ employee ->currentLocation
36
+ $ managingAbsenceForId ,
37
+ $ location
37
38
]);
38
39
39
40
Validator::make ($ data , [
@@ -50,40 +51,38 @@ public function add(User $employee, array $data): void
50
51
'full_day ' => ['required ' , 'boolean ' ]
51
52
])->validateWithBag ('addAbsence ' );
52
53
53
-
54
54
$ startsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['starts_at ' ]);
55
55
$ endsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['ends_at ' ]);
56
56
57
57
//ignore given time if calculation is based on full day
58
58
if (isset ($ data ['full_day ' ]) && $ data ['full_day ' ]) {
59
59
$ startsAt = $ startsAt ->copy ()->startOfDay ();
60
60
$ endsAt = $ endsAt ->copy ()->endOfDay ();
61
-
62
61
}
63
62
63
+ $ addingAbsenceFor = Jetstream::findUserByIdOrFail ($ managingAbsenceForId );
64
+
64
65
$ calculator = new AbsenceCalculator (
65
66
new EmployeeAbsenceCalendar (
66
- $ employee ,
67
- $ employee -> currentLocation ,
67
+ $ addingAbsenceFor ,
68
+ $ location ,
68
69
new CarbonPeriod ($ startsAt , $ endsAt )
69
70
),
70
71
AbsenceType::findOrFail ($ data ['absence_type_id ' ])
71
72
);
72
73
73
- $ absence = $ employee ->absences ()->create (
74
+ $ absence = $ addingAbsenceFor ->absences ()->create (
74
75
[
75
- 'location_id ' => $ employee -> currentLocation ->id ,
76
+ 'location_id ' => $ location ->id ,
76
77
'vacation_days ' => $ calculator ->sumVacationDays (),
77
78
'paid_hours ' => $ calculator ->sumPaidHours (),
78
79
'starts_at ' => $ startsAt ,
79
80
'ends_at ' => $ endsAt ,
80
81
] + $ data
81
82
);
82
83
83
- $ admins = User::all ()->filter
84
- ->hasLocationRole ($ employee ->currentLocation , 'admin ' );
85
-
86
- Mail::to ($ admins )
87
- ->send (new NewAbsenceWaitingForApproval ($ absence , $ employee ));
84
+ Mail::to (
85
+ $ location ->allUsers ()->filter ->hasLocationRole ($ location , 'admin ' )
86
+ )->send (new NewAbsenceWaitingForApproval ($ absence , $ addingAbsenceFor ));
88
87
}
89
88
}
0 commit comments