77use App \Models \Location ;
88use Carbon \CarbonPeriod ;
99use App \Models \AbsenceType ;
10- use Illuminate \Support \Arr ;
1110use App \Contracts \AddsAbsences ;
1211use App \Formatter \DateFormatter ;
12+ use Laravel \Jetstream \Jetstream ;
1313use Illuminate \Support \Facades \Gate ;
1414use Illuminate \Support \Facades \Mail ;
1515use Illuminate \Support \Facades \Validator ;
@@ -29,11 +29,12 @@ public function __construct()
2929 $ this ->dateFormatter = app (DateFormatter::class);
3030 }
3131
32- public function add (User $ employee , array $ data ): void
32+ public function add (User $ user , Location $ location , int $ managingAbsenceForId , array $ data )
3333 {
34- Gate::forUser ($ employee )->authorize ('addAbsence ' , [
34+ Gate::forUser ($ user )->authorize ('addAbsence ' , [
3535 Absence::class,
36- $ employee ->currentLocation
36+ $ managingAbsenceForId ,
37+ $ location
3738 ]);
3839
3940 Validator::make ($ data , [
@@ -50,40 +51,38 @@ public function add(User $employee, array $data): void
5051 'full_day ' => ['required ' , 'boolean ' ]
5152 ])->validateWithBag ('addAbsence ' );
5253
53-
5454 $ startsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['starts_at ' ]);
5555 $ endsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['ends_at ' ]);
5656
5757 //ignore given time if calculation is based on full day
5858 if (isset ($ data ['full_day ' ]) && $ data ['full_day ' ]) {
5959 $ startsAt = $ startsAt ->copy ()->startOfDay ();
6060 $ endsAt = $ endsAt ->copy ()->endOfDay ();
61-
6261 }
6362
63+ $ addingAbsenceFor = Jetstream::findUserByIdOrFail ($ managingAbsenceForId );
64+
6465 $ calculator = new AbsenceCalculator (
6566 new EmployeeAbsenceCalendar (
66- $ employee ,
67- $ employee -> currentLocation ,
67+ $ addingAbsenceFor ,
68+ $ location ,
6869 new CarbonPeriod ($ startsAt , $ endsAt )
6970 ),
7071 AbsenceType::findOrFail ($ data ['absence_type_id ' ])
7172 );
7273
73- $ absence = $ employee ->absences ()->create (
74+ $ absence = $ addingAbsenceFor ->absences ()->create (
7475 [
75- 'location_id ' => $ employee -> currentLocation ->id ,
76+ 'location_id ' => $ location ->id ,
7677 'vacation_days ' => $ calculator ->sumVacationDays (),
7778 'paid_hours ' => $ calculator ->sumPaidHours (),
7879 'starts_at ' => $ startsAt ,
7980 'ends_at ' => $ endsAt ,
8081 ] + $ data
8182 );
8283
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 ));
8887 }
8988}
0 commit comments