Skip to content

Commit 9699082

Browse files
authored
Merge pull request #5 from eporsche/code_refactoring
Code refactoring and small bug fixes
2 parents eae388c + 108d444 commit 9699082

24 files changed

+258
-78
lines changed

app/Actions/AddAbsence.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function __construct()
2929
$this->dateFormatter = app(DateFormatter::class);
3030
}
3131

32-
public function add(User $employee, Location $location, array $data): void
32+
public function add(User $employee, array $data): void
3333
{
3434
Gate::forUser($employee)->authorize('addAbsence', [
3535
Absence::class,
36-
$location
36+
$employee->currentLocation
3737
]);
3838

3939
Validator::make($data, [
@@ -64,15 +64,15 @@ public function add(User $employee, Location $location, array $data): void
6464
$calculator = new AbsenceCalculator(
6565
new EmployeeAbsenceCalendar(
6666
$employee,
67-
$location,
67+
$employee->currentLocation,
6868
new CarbonPeriod($startsAt, $endsAt)
6969
),
7070
AbsenceType::findOrFail($data['absence_type_id'])
7171
);
7272

7373
$absence = $employee->absences()->create(
7474
[
75-
'location_id' => $location->id,
75+
'location_id' => $employee->currentLocation->id,
7676
'vacation_days' => $calculator->sumVacationDays(),
7777
'paid_hours' => $calculator->sumPaidHours(),
7878
'starts_at' => $startsAt,
@@ -81,7 +81,7 @@ public function add(User $employee, Location $location, array $data): void
8181
);
8282

8383
$admins = User::all()->filter
84-
->hasLocationRole($location, 'admin');
84+
->hasLocationRole($employee->currentLocation, 'admin');
8585

8686
Mail::to($admins)
8787
->send(new NewAbsenceWaitingForApproval($absence, $employee));

app/Actions/AddTimeTracking.php

-9
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ public function add($employee, array $data, array $pauseTimes)
5151
], Arr::except($data, ['starts_at','ends_at'])));
5252

5353
$trackedTime->pauseTimes()->createMany($pauseTimes);
54-
5554
$trackedTime->updatePauseTime();
5655
});
57-
5856
}
5957

6058
protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $endsAt)
@@ -70,13 +68,6 @@ protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $en
7068
});
7169
}
7270

73-
protected function calculatePauseTimeFromDefaultRestingTimes($employee, $workingTimeInSeconds)
74-
{
75-
return optional(
76-
$employee->defaultRestingTimes()->firstWhere('min_hours','<=',$workingTimeInSeconds)
77-
)->duration->inSeconds();
78-
}
79-
8071
protected function ensureDateIsNotTooFarInTheFuture($endsAt)
8172
{
8273
if ($endsAt->isAfter(Carbon::now()->endOfDay())) {

app/Actions/ApproveAbscence.php

+3-3
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, Location $location, $absenceId)
30+
public function approve(User $user, $absenceId)
3131
{
3232
Validator::make([
3333
'absence_id' => $absenceId
@@ -37,9 +37,9 @@ public function approve(User $user, Location $location, $absenceId)
3737

3838
$absence = Absence::findOrFail($absenceId);
3939

40-
DB::transaction(function () use ($absence, $location) {
40+
DB::transaction(function () use ($absence, $user) {
4141
$this->bookVacationDays($absence);
42-
$this->createAbsenceIndex($absence, $location);
42+
$this->createAbsenceIndex($absence, $user->currentLocation);
4343
$absence->markAsConfirmed();
4444
if (Daybreak::hasCaldavFeature()) {
4545
CreateCaldavEvent::dispatch($absence)

app/Actions/RemoveAbsence.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
namespace App\Actions;
44

55
use App\Models\User;
6+
use Illuminate\Support\Facades\Gate;
67
use App\Mail\AbsenceRemoved;
78
use App\Contracts\RemovesAbsence;
89
use Illuminate\Support\Facades\Mail;
10+
use DB;
911

1012
class RemoveAbsence implements RemovesAbsence
1113
{
12-
public function remove(User $employee, $absenceId)
14+
public function remove($user, $removesAbsenceId)
1315
{
14-
$employee->absences()->whereKey($absenceId)->delete();
16+
Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);
1517

16-
Mail::to($employee)->send(new AbsenceRemoved());
18+
tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {
19+
20+
$absence->delete();
21+
22+
Mail::to($absence->employee)->send(new AbsenceRemoved());
23+
});
1724
}
1825
}

app/Actions/RemoveTimeTracking.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class RemoveTimeTracking implements RemovesTimeTracking
99
{
1010
public function remove(User $user, $timeTrackingId)
1111
{
12-
$user->timeTrackings()->whereKey($timeTrackingId)->delete();
12+
$user->currentLocation->timeTrackings()->whereKey($timeTrackingId)->delete();
1313
}
1414
}

app/Actions/UpdateTimeTracking.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ public function update(User $employee, $timeTrackingId, array $data, array $paus
4141
$this->ensureDateIsNotTooFarInTheFuture($endsAt);
4242
$this->ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt, $timeTrackingId);
4343

44-
//check pause times
45-
4644
$this->validatePauseTimes(
4745
PeriodCalculator::fromTimesArray($pauseTimes),
4846
$startsAt,
4947
$endsAt
5048
);
5149

52-
$trackedTime = $employee->timeTrackings()->whereKey($timeTrackingId)->first();
50+
$trackedTime = $employee->currentLocation->timeTrackings()->whereKey($timeTrackingId)->first();
5351

5452
DB::transaction(function () use ($trackedTime, $startsAt, $endsAt, $data, $pauseTimes) {
5553
$trackedTime->update(array_merge([

app/Contracts/AddsAbsences.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ interface AddsAbsences
1111
* Add a absence for user and location
1212
*
1313
* @param User $employee
14-
* @param Location $location
1514
* @param array $data
1615
* @return void
1716
*/
18-
public function add(User $employee, Location $location, array $data) : void;
17+
public function add(User $employee, array $data) : void;
1918
}

app/Contracts/ApprovesAbsence.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace App\Contracts;
44

55
use App\Models\Absence;
6-
use App\Models\Location;
76
use App\Models\User;
87

98
interface ApprovesAbsence
109
{
11-
public function approve(User $user, Location $location, Absence $absence);
10+
public function approve(User $user, Absence $absence);
1211
}

app/Contracts/RemovesAbsence.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ interface RemovesAbsence
1313
* @param mixed $removesAbsenceId
1414
* @return void
1515
*/
16-
public function remove(User $employee, $removesAbsenceId);
16+
public function remove($user, $removesAbsenceId);
1717
}

app/Formatter/DateFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function formatDateForView($date);
2121

2222
public function formatDateTimeForView($date);
2323

24-
public function generateTimeStr(string $date, string $hours = null, string $minutes = null);
24+
public function generateTimeStr(string $date = null, string $hours = null, string $minutes = null);
2525

2626
public function timeStrToCarbon(string $timeStr) : CarbonImmutable;
2727

app/Formatter/GermanDateFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function formatDateTimeForView($date)
4747
return $date->format('d.m.Y H:i');
4848
}
4949

50-
public function generateTimeStr(string $date, string $hours = null, string $minutes = null)
50+
public function generateTimeStr(string $date = null, string $hours = null, string $minutes = null)
5151
{
5252
return $date.' '.str_pad($hours,2,"0",STR_PAD_LEFT).':'.str_pad($minutes,2,"0",STR_PAD_LEFT);
5353
}

app/Http/Livewire/Absence.php app/Http/Livewire/AbsenceManager.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use App\AbsenceCalendar\AbsenceCalculator;
1616
use App\AbsenceCalendar\EmployeeAbsenceCalendar;
1717

18-
class Absence extends Component
18+
class AbsenceManager extends Component
1919
{
2020
use WithPagination;
2121

@@ -80,7 +80,7 @@ public function updated()
8080
//TODO set to start of day and end of day if full day is activated...
8181
$calendar = new EmployeeAbsenceCalendar(
8282
$this->employee,
83-
$this->location,
83+
$this->employee->currentLocation,
8484
new CarbonPeriod(
8585
$this->startTimeStr(),
8686
$this->endTimeStr()
@@ -99,7 +99,7 @@ public function refreshAbsenceHours()
9999
//TODO set to start of day and end of day if full day is activated...
100100
$calendar = new EmployeeAbsenceCalendar(
101101
$this->employee,
102-
$this->location,
102+
$this->employee->currentLocation,
103103
new CarbonPeriod(
104104
$this->startTimeStr(),
105105
$this->endTimeStr()
@@ -119,8 +119,6 @@ public function mount(User $employee, DateFormatter $dateFormatter)
119119
$this->absenceTypes = $employee->absenceTypesForLocation($employee->currentLocation);
120120
$this->hours = range(0,23);
121121
$this->minutes = range(0,59);
122-
$this->location = $employee->currentLocation;
123-
// $this->employeeSwitcher = $employee->currentLocation->allUsers()->pluck('name','id')->toArray();
124122

125123
$this->employeeOptions = $employee
126124
->currentLocation
@@ -156,7 +154,7 @@ protected function buildVacationInfoPanel($employee, $dateFormatter)
156154

157155
public function switchEmployee()
158156
{
159-
$this->employee = $this->location->allUsers()->first(function ($user) {
157+
$this->employee = $this->emyployee->currentLocation->allUsers()->first(function ($user) {
160158
return $user->id === (int)$this->employeeIdToBeSwitched;
161159
});
162160
}
@@ -199,7 +197,7 @@ public function addAbsence(AddsAbsences $adder)
199197

200198
$this->addAbsenceForm['full_day'] = $this->hideTime;
201199

202-
$adder->add($this->employee, $this->location, $this->addAbsenceForm);
200+
$adder->add($this->employee, $this->addAbsenceForm);
203201

204202
$this->resetFormfields();
205203

@@ -222,7 +220,6 @@ public function approveAbsence($absenceId, ApprovesAbsence $approver)
222220
if (!empty($absenceId)) {
223221
$approver->approve(
224222
$this->employee,
225-
$this->location,
226223
$absenceId
227224
);
228225
}
@@ -247,6 +244,7 @@ public function removeAbsence(RemovesAbsence $remover)
247244
$this->confirmingAbsenceRemoval = false;
248245

249246
$this->absenceIdBeingRemoved = null;
247+
250248
}
251249

252250
public function render()

app/Http/Livewire/TimeTracking.php app/Http/Livewire/TimeTrackingManager.php

+43-13
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
use Livewire\WithPagination;
1111
use App\Formatter\DateFormatter;
1212
use App\Contracts\AddsTimeTrackings;
13-
use Illuminate\Support\Facades\Auth;
1413
use App\Contracts\RemovesTimeTracking;
1514
use App\Contracts\UpdatesTimeTracking;
1615
use Daybreak\Project\Contracts\AddsTimeTrackingWithProjectInfo;
1716
use Daybreak\Project\Contracts\UpdatesTimeTrackingWithProjectInfo;
1817

19-
class TimeTracking extends Component
18+
class TimeTrackingManager extends Component
2019
{
2120
use WithPagination, TrimAndNullEmptyStrings;
2221

@@ -80,7 +79,7 @@ public function mount(User $employee, DateFormatter $dateFormatter)
8079
$this->employeeFilter = collect($this->employeeOptions)
8180
->filterMultipleSelect(fn($item) => $item['id'] === $this->employee->id);
8281

83-
$this->timeTrackingForm = array_merge_when(array_merge($this->timeTrackingForm,[
82+
$this->timeTrackingForm = array_merge_when(array_merge($this->timeTrackingForm, [
8483
'date' => $dateFormatter->formatDateTimeForView(Carbon::today())
8584
]), fn() => $this->projectFormFields(), Daybreak::hasProjectBillingFeature());
8685

@@ -119,21 +118,41 @@ public function confirmAddTimeTracking(DateFormatter $dateFormatter)
119118
{
120119
$this->resetErrorBag();
121120

122-
$generatedPauseTimeArray = $this->generatePauseTimeArray($this->timeTrackingForm['date'], $this->pauseTimeForm, $dateFormatter);
121+
$generatedPauseTimeArray = $this->generatePauseTimeArray(
122+
$this->timeTrackingForm['date'],
123+
$this->pauseTimeForm,
124+
$dateFormatter
125+
);
123126

124127
if (Daybreak::hasProjectBillingFeature()) {
125128
app(AddsTimeTrackingWithProjectInfo::class)->add(
126129
$this->employee, array_merge([
127-
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
128-
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),
130+
'starts_at' => $dateFormatter->generateTimeStr(
131+
$this->timeTrackingForm['date'],
132+
$this->timeTrackingForm['start_hour'],
133+
$this->timeTrackingForm['start_minute']
134+
),
135+
'ends_at' => $dateFormatter->generateTimeStr(
136+
$this->timeTrackingForm['date'],
137+
$this->timeTrackingForm['end_hour'],
138+
$this->timeTrackingForm['end_minute']
139+
),
129140
], $this->filteredTimeTrackingFormFields()),
130141
$generatedPauseTimeArray
131142
);
132143
} else {
133144
app(AddsTimeTrackings::class)->add(
134145
$this->employee, array_merge([
135-
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
136-
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),
146+
'starts_at' => $dateFormatter->generateTimeStr(
147+
$this->timeTrackingForm['date'],
148+
$this->timeTrackingForm['start_hour'],
149+
$this->timeTrackingForm['start_minute']
150+
),
151+
'ends_at' => $dateFormatter->generateTimeStr(
152+
$this->timeTrackingForm['date'],
153+
$this->timeTrackingForm['end_hour'],
154+
$this->timeTrackingForm['end_minute']
155+
),
137156
], $this->filteredTimeTrackingFormFields()),
138157
$generatedPauseTimeArray
139158
);
@@ -235,7 +254,7 @@ public function updateTimeTracking($index)
235254
$this->timeTrackingIdBeingUpdated = $index;
236255

237256
$this->updateTimeTrackingForm(
238-
$this->employee->timeTrackings()
257+
$this->employee->currentLocation->timeTrackings()
239258
->whereKey($index)
240259
->with('pauseTimes')
241260
->first()
@@ -271,7 +290,11 @@ public function confirmUpdateTimeTracking(DateFormatter $dateFormatter)
271290
{
272291
$this->resetErrorBag();
273292

274-
$generatedPauseTimeArray = $this->generatePauseTimeArray($this->timeTrackingForm['date'], $this->pauseTimeForm, $dateFormatter);
293+
$generatedPauseTimeArray = $this->generatePauseTimeArray(
294+
$this->timeTrackingForm['date'],
295+
$this->pauseTimeForm,
296+
$dateFormatter
297+
);
275298

276299
if (Daybreak::hasProjectBillingFeature()) {
277300
app(UpdatesTimeTrackingWithProjectInfo::class)->update(
@@ -296,9 +319,16 @@ public function confirmUpdateTimeTracking(DateFormatter $dateFormatter)
296319
$this->employee,
297320
$this->timeTrackingIdBeingUpdated,
298321
array_merge([
299-
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
300-
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),
301-
322+
'starts_at' => $dateFormatter->generateTimeStr(
323+
$this->timeTrackingForm['date'],
324+
$this->timeTrackingForm['start_hour'],
325+
$this->timeTrackingForm['start_minute']
326+
),
327+
'ends_at' => $dateFormatter->generateTimeStr(
328+
$this->timeTrackingForm['date'],
329+
$this->timeTrackingForm['end_hour'],
330+
$this->timeTrackingForm['end_minute']
331+
),
302332
], $this->filteredTimeTrackingFormFields()),
303333
$generatedPauseTimeArray
304334
);

app/Policies/LocationPolicy.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public function approveAbsence(User $user, Location $location)
1616
return $user->hasLocationPermission($location, 'approveAbsence');
1717
}
1818

19+
public function removeAbsence(User $user, Location $location)
20+
{
21+
22+
return $user->hasLocationPermission($location, 'removeAbsence');
23+
}
24+
1925
/**
2026
* Determine whether the user can add team members.
2127
*

0 commit comments

Comments
 (0)