Skip to content

Commit f969829

Browse files
authored
Merge pull request #17 from eporsche/add_absences_for_other_user
Adding the ability to manage absences for other location users
2 parents 9699082 + e3adf3e commit f969829

32 files changed

+449
-324
lines changed

app/AbsenceCalendar/EmployeeAbsenceCalendar.php

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public function __construct(User $employee, Location $location, CarbonPeriod $pe
2929
$period->getStartDate()->startOfDay(),
3030
$period->getEndDate()->endOfDay()
3131
);
32-
3332
$this->calculateDays();
3433
}
3534

@@ -113,7 +112,6 @@ protected function diffHoursToStartOrEndOfDay(Carbon $date)
113112
$diffInHours = BigDecimal::of($date->startOfDay()->diffInMinutes($this->endDay))
114113
->dividedBy('60', 2, RoundingMode::HALF_EVEN);
115114
}
116-
117115
return $diffInHours;
118116
}
119117

app/Actions/AddAbsence.php

+14-15
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use App\Models\Location;
88
use Carbon\CarbonPeriod;
99
use App\Models\AbsenceType;
10-
use Illuminate\Support\Arr;
1110
use App\Contracts\AddsAbsences;
1211
use App\Formatter\DateFormatter;
12+
use Laravel\Jetstream\Jetstream;
1313
use Illuminate\Support\Facades\Gate;
1414
use Illuminate\Support\Facades\Mail;
1515
use 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
}

app/Actions/AddLocationMember.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ protected function rules()
117117
}
118118

119119
/**
120-
* Ensure that the user is not already on the team.
120+
* Ensure that the user is not already on the location.
121121
*
122-
* @param mixed $team
122+
* @param mixed $location
123123
* @param string $email
124124
* @return \Closure
125125
*/
@@ -129,7 +129,7 @@ protected function ensureUserIsNotAlreadyInLocation($location, string $email)
129129
$validator->errors()->addIf(
130130
$location->hasUserWithEmail($email),
131131
'email',
132-
__('This user already belongs to the team.')
132+
__('This user already belongs to the location.')
133133
);
134134
};
135135
}

app/Actions/AddTimeTracking.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ protected function ensureDateIsNotBeforeEmploymentDate($employee, $startsAt)
8888
}
8989
}
9090

91-
protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt) {
92-
91+
protected function ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt)
92+
{
9393
if ($employee->timeTrackings()->where(function ($query) use ($startsAt, $endsAt) {
9494
$query->whereBetween('starts_at', [$startsAt, $endsAt])
9595
->orWhereBetween('ends_at', [$startsAt, $endsAt])

app/Actions/ApproveAbscence.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ApproveAbscence implements ApprovesAbsence
2727
* @param int $absenceId
2828
* @return void
2929
*/
30-
public function approve(User $user, $absenceId)
30+
public function approve(User $user, Location $location, $absenceId)
3131
{
3232
Validator::make([
3333
'absence_id' => $absenceId
@@ -57,6 +57,7 @@ public function bookVacationDays($absence)
5757

5858
//TODO: distribute absence days between available vacation entitlements
5959
$currentVacationEntitlement = $absence->employee->currentVacationEntitlement();
60+
6061
if (!isset($currentVacationEntitlement) || !$currentVacationEntitlement->hasEnoughUnusedVacationDays($absence->vacation_days)) {
6162
throw ValidationException::withMessages([
6263
'error' => [__('Sorry, there is no fitting vacation entitlement for this absence.')],

app/Actions/InviteLocationMember.php

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public function invite($user, $location, string $email, string $role = null)
1818

1919
$this->validate($location, $email, $role);
2020

21-
// InvitinglocationMember::dispatch($location, $email, $role);
22-
2321
$invitation = $location->locationInvitations()->create([
2422
'email' => $email,
2523
'role' => $role,

app/Actions/RemoveAbsence.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33
namespace App\Actions;
44

5+
use DB;
56
use App\Models\User;
6-
use Illuminate\Support\Facades\Gate;
7+
use App\Models\Location;
78
use App\Mail\AbsenceRemoved;
89
use App\Contracts\RemovesAbsence;
10+
use Illuminate\Support\Facades\Gate;
911
use Illuminate\Support\Facades\Mail;
10-
use DB;
1112

1213
class RemoveAbsence implements RemovesAbsence
1314
{
14-
public function remove($user, $removesAbsenceId)
15+
public function remove(User $user, Location $location, $removesAbsenceId)
1516
{
16-
Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);
17+
tap($location->absences()->whereKey($removesAbsenceId)->first(), function ($absence) use ($user, $location) {
1718

18-
tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {
19+
Gate::forUser($user)->authorize('removeAbsence', [
20+
Absence::class,
21+
$absence,
22+
$location
23+
]);
1924

2025
$absence->delete();
2126

app/Contracts/AddsAbsences.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ interface AddsAbsences
1010
/**
1111
* Add a absence for user and location
1212
*
13-
* @param User $employee
13+
* @param User $user
14+
* @param mixed $addingAbsenceForId
1415
* @param array $data
1516
* @return void
1617
*/
17-
public function add(User $employee, array $data) : void;
18+
public function add(User $user, Location $location, int $addingAbsenceForId, array $data);
1819
}

app/Contracts/ApprovesAbsence.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace App\Contracts;
44

5-
use App\Models\Absence;
65
use App\Models\User;
6+
use App\Models\Absence;
7+
use App\Models\Location;
78

89
interface ApprovesAbsence
910
{
10-
public function approve(User $user, Absence $absence);
11+
public function approve(User $user, Location $location, Absence $absence);
1112
}

app/Contracts/RemovesAbsence.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace App\Contracts;
44

55
use App\Models\User;
6+
use App\Models\Location;
67

78
interface RemovesAbsence
89
{
910
/**
1011
* Remove a absence.
1112
*
12-
* @param mixed $user
13+
* @param User $user
14+
* @param Location $location
1315
* @param mixed $removesAbsenceId
1416
* @return void
1517
*/
16-
public function remove($user, $removesAbsenceId);
18+
public function remove(User $user, Location $location, $removesAbsenceId);
1719
}

app/Http/Controllers/AbsenceController.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class AbsenceController extends Controller
88
{
99
public function __invoke(Request $request)
1010
{
11-
return view('absences.index', [
12-
'employee' => $request->user()
13-
]);
11+
return view('absences.index');
1412
}
1513
}

0 commit comments

Comments
 (0)