Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring and small bug fixes #5

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/Actions/AddAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function __construct()
$this->dateFormatter = app(DateFormatter::class);
}

public function add(User $employee, Location $location, array $data): void
public function add(User $employee, array $data): void
{
Gate::forUser($employee)->authorize('addAbsence', [
Absence::class,
$location
$employee->currentLocation
]);

Validator::make($data, [
Expand Down Expand Up @@ -64,15 +64,15 @@ public function add(User $employee, Location $location, array $data): void
$calculator = new AbsenceCalculator(
new EmployeeAbsenceCalendar(
$employee,
$location,
$employee->currentLocation,
new CarbonPeriod($startsAt, $endsAt)
),
AbsenceType::findOrFail($data['absence_type_id'])
);

$absence = $employee->absences()->create(
[
'location_id' => $location->id,
'location_id' => $employee->currentLocation->id,
'vacation_days' => $calculator->sumVacationDays(),
'paid_hours' => $calculator->sumPaidHours(),
'starts_at' => $startsAt,
Expand All @@ -81,7 +81,7 @@ public function add(User $employee, Location $location, array $data): void
);

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

Mail::to($admins)
->send(new NewAbsenceWaitingForApproval($absence, $employee));
Expand Down
9 changes: 0 additions & 9 deletions app/Actions/AddTimeTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public function add($employee, array $data, array $pauseTimes)
], Arr::except($data, ['starts_at','ends_at'])));

$trackedTime->pauseTimes()->createMany($pauseTimes);

$trackedTime->updatePauseTime();
});

}

protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $endsAt)
Expand All @@ -70,13 +68,6 @@ protected function validatePauseTimes($pauseTimePeriodCalculator, $startsAt, $en
});
}

protected function calculatePauseTimeFromDefaultRestingTimes($employee, $workingTimeInSeconds)
{
return optional(
$employee->defaultRestingTimes()->firstWhere('min_hours','<=',$workingTimeInSeconds)
)->duration->inSeconds();
}

protected function ensureDateIsNotTooFarInTheFuture($endsAt)
{
if ($endsAt->isAfter(Carbon::now()->endOfDay())) {
Expand Down
6 changes: 3 additions & 3 deletions app/Actions/ApproveAbscence.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ApproveAbscence implements ApprovesAbsence
* @param int $absenceId
* @return void
*/
public function approve(User $user, Location $location, $absenceId)
public function approve(User $user, $absenceId)
{
Validator::make([
'absence_id' => $absenceId
Expand All @@ -37,9 +37,9 @@ public function approve(User $user, Location $location, $absenceId)

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

DB::transaction(function () use ($absence, $location) {
DB::transaction(function () use ($absence, $user) {
$this->bookVacationDays($absence);
$this->createAbsenceIndex($absence, $location);
$this->createAbsenceIndex($absence, $user->currentLocation);
$absence->markAsConfirmed();
if (Daybreak::hasCaldavFeature()) {
CreateCaldavEvent::dispatch($absence)
Expand Down
13 changes: 10 additions & 3 deletions app/Actions/RemoveAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
namespace App\Actions;

use App\Models\User;
use Illuminate\Support\Facades\Gate;
use App\Mail\AbsenceRemoved;
use App\Contracts\RemovesAbsence;
use Illuminate\Support\Facades\Mail;
use DB;

class RemoveAbsence implements RemovesAbsence
{
public function remove(User $employee, $absenceId)
public function remove($user, $removesAbsenceId)
{
$employee->absences()->whereKey($absenceId)->delete();
Gate::forUser($user)->authorize('removeAbsence', $user->currentLocation);

Mail::to($employee)->send(new AbsenceRemoved());
tap($user->currentLocation->absences()->whereKey($removesAbsenceId)->first(), function ($absence) {

$absence->delete();

Mail::to($absence->employee)->send(new AbsenceRemoved());
});
}
}
2 changes: 1 addition & 1 deletion app/Actions/RemoveTimeTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class RemoveTimeTracking implements RemovesTimeTracking
{
public function remove(User $user, $timeTrackingId)
{
$user->timeTrackings()->whereKey($timeTrackingId)->delete();
$user->currentLocation->timeTrackings()->whereKey($timeTrackingId)->delete();
}
}
4 changes: 1 addition & 3 deletions app/Actions/UpdateTimeTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ public function update(User $employee, $timeTrackingId, array $data, array $paus
$this->ensureDateIsNotTooFarInTheFuture($endsAt);
$this->ensureGivenTimeIsNotOverlappingWithExisting($employee, $startsAt, $endsAt, $timeTrackingId);

//check pause times

$this->validatePauseTimes(
PeriodCalculator::fromTimesArray($pauseTimes),
$startsAt,
$endsAt
);

$trackedTime = $employee->timeTrackings()->whereKey($timeTrackingId)->first();
$trackedTime = $employee->currentLocation->timeTrackings()->whereKey($timeTrackingId)->first();

DB::transaction(function () use ($trackedTime, $startsAt, $endsAt, $data, $pauseTimes) {
$trackedTime->update(array_merge([
Expand Down
3 changes: 1 addition & 2 deletions app/Contracts/AddsAbsences.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ interface AddsAbsences
* Add a absence for user and location
*
* @param User $employee
* @param Location $location
* @param array $data
* @return void
*/
public function add(User $employee, Location $location, array $data) : void;
public function add(User $employee, array $data) : void;
}
3 changes: 1 addition & 2 deletions app/Contracts/ApprovesAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace App\Contracts;

use App\Models\Absence;
use App\Models\Location;
use App\Models\User;

interface ApprovesAbsence
{
public function approve(User $user, Location $location, Absence $absence);
public function approve(User $user, Absence $absence);
}
2 changes: 1 addition & 1 deletion app/Contracts/RemovesAbsence.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface RemovesAbsence
* @param mixed $removesAbsenceId
* @return void
*/
public function remove(User $employee, $removesAbsenceId);
public function remove($user, $removesAbsenceId);
}
2 changes: 1 addition & 1 deletion app/Formatter/DateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function formatDateForView($date);

public function formatDateTimeForView($date);

public function generateTimeStr(string $date, string $hours = null, string $minutes = null);
public function generateTimeStr(string $date = null, string $hours = null, string $minutes = null);

public function timeStrToCarbon(string $timeStr) : CarbonImmutable;

Expand Down
2 changes: 1 addition & 1 deletion app/Formatter/GermanDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function formatDateTimeForView($date)
return $date->format('d.m.Y H:i');
}

public function generateTimeStr(string $date, string $hours = null, string $minutes = null)
public function generateTimeStr(string $date = null, string $hours = null, string $minutes = null)
{
return $date.' '.str_pad($hours,2,"0",STR_PAD_LEFT).':'.str_pad($minutes,2,"0",STR_PAD_LEFT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use App\AbsenceCalendar\AbsenceCalculator;
use App\AbsenceCalendar\EmployeeAbsenceCalendar;

class Absence extends Component
class AbsenceManager extends Component
{
use WithPagination;

Expand Down Expand Up @@ -80,7 +80,7 @@ public function updated()
//TODO set to start of day and end of day if full day is activated...
$calendar = new EmployeeAbsenceCalendar(
$this->employee,
$this->location,
$this->employee->currentLocation,
new CarbonPeriod(
$this->startTimeStr(),
$this->endTimeStr()
Expand All @@ -99,7 +99,7 @@ public function refreshAbsenceHours()
//TODO set to start of day and end of day if full day is activated...
$calendar = new EmployeeAbsenceCalendar(
$this->employee,
$this->location,
$this->employee->currentLocation,
new CarbonPeriod(
$this->startTimeStr(),
$this->endTimeStr()
Expand All @@ -119,8 +119,6 @@ public function mount(User $employee, DateFormatter $dateFormatter)
$this->absenceTypes = $employee->absenceTypesForLocation($employee->currentLocation);
$this->hours = range(0,23);
$this->minutes = range(0,59);
$this->location = $employee->currentLocation;
// $this->employeeSwitcher = $employee->currentLocation->allUsers()->pluck('name','id')->toArray();

$this->employeeOptions = $employee
->currentLocation
Expand Down Expand Up @@ -156,7 +154,7 @@ protected function buildVacationInfoPanel($employee, $dateFormatter)

public function switchEmployee()
{
$this->employee = $this->location->allUsers()->first(function ($user) {
$this->employee = $this->emyployee->currentLocation->allUsers()->first(function ($user) {
return $user->id === (int)$this->employeeIdToBeSwitched;
});
}
Expand Down Expand Up @@ -199,7 +197,7 @@ public function addAbsence(AddsAbsences $adder)

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

$adder->add($this->employee, $this->location, $this->addAbsenceForm);
$adder->add($this->employee, $this->addAbsenceForm);

$this->resetFormfields();

Expand All @@ -222,7 +220,6 @@ public function approveAbsence($absenceId, ApprovesAbsence $approver)
if (!empty($absenceId)) {
$approver->approve(
$this->employee,
$this->location,
$absenceId
);
}
Expand All @@ -247,6 +244,7 @@ public function removeAbsence(RemovesAbsence $remover)
$this->confirmingAbsenceRemoval = false;

$this->absenceIdBeingRemoved = null;

}

public function render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
use Livewire\WithPagination;
use App\Formatter\DateFormatter;
use App\Contracts\AddsTimeTrackings;
use Illuminate\Support\Facades\Auth;
use App\Contracts\RemovesTimeTracking;
use App\Contracts\UpdatesTimeTracking;
use Daybreak\Project\Contracts\AddsTimeTrackingWithProjectInfo;
use Daybreak\Project\Contracts\UpdatesTimeTrackingWithProjectInfo;

class TimeTracking extends Component
class TimeTrackingManager extends Component
{
use WithPagination, TrimAndNullEmptyStrings;

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

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

Expand Down Expand Up @@ -119,21 +118,41 @@ public function confirmAddTimeTracking(DateFormatter $dateFormatter)
{
$this->resetErrorBag();

$generatedPauseTimeArray = $this->generatePauseTimeArray($this->timeTrackingForm['date'], $this->pauseTimeForm, $dateFormatter);
$generatedPauseTimeArray = $this->generatePauseTimeArray(
$this->timeTrackingForm['date'],
$this->pauseTimeForm,
$dateFormatter
);

if (Daybreak::hasProjectBillingFeature()) {
app(AddsTimeTrackingWithProjectInfo::class)->add(
$this->employee, array_merge([
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),
'starts_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['start_hour'],
$this->timeTrackingForm['start_minute']
),
'ends_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['end_hour'],
$this->timeTrackingForm['end_minute']
),
], $this->filteredTimeTrackingFormFields()),
$generatedPauseTimeArray
);
} else {
app(AddsTimeTrackings::class)->add(
$this->employee, array_merge([
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),
'starts_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['start_hour'],
$this->timeTrackingForm['start_minute']
),
'ends_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['end_hour'],
$this->timeTrackingForm['end_minute']
),
], $this->filteredTimeTrackingFormFields()),
$generatedPauseTimeArray
);
Expand Down Expand Up @@ -235,7 +254,7 @@ public function updateTimeTracking($index)
$this->timeTrackingIdBeingUpdated = $index;

$this->updateTimeTrackingForm(
$this->employee->timeTrackings()
$this->employee->currentLocation->timeTrackings()
->whereKey($index)
->with('pauseTimes')
->first()
Expand Down Expand Up @@ -271,7 +290,11 @@ public function confirmUpdateTimeTracking(DateFormatter $dateFormatter)
{
$this->resetErrorBag();

$generatedPauseTimeArray = $this->generatePauseTimeArray($this->timeTrackingForm['date'], $this->pauseTimeForm, $dateFormatter);
$generatedPauseTimeArray = $this->generatePauseTimeArray(
$this->timeTrackingForm['date'],
$this->pauseTimeForm,
$dateFormatter
);

if (Daybreak::hasProjectBillingFeature()) {
app(UpdatesTimeTrackingWithProjectInfo::class)->update(
Expand All @@ -296,9 +319,16 @@ public function confirmUpdateTimeTracking(DateFormatter $dateFormatter)
$this->employee,
$this->timeTrackingIdBeingUpdated,
array_merge([
'starts_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['start_hour'], $this->timeTrackingForm['start_minute']),
'ends_at' => $dateFormatter->generateTimeStr($this->timeTrackingForm['date'], $this->timeTrackingForm['end_hour'], $this->timeTrackingForm['end_minute']),

'starts_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['start_hour'],
$this->timeTrackingForm['start_minute']
),
'ends_at' => $dateFormatter->generateTimeStr(
$this->timeTrackingForm['date'],
$this->timeTrackingForm['end_hour'],
$this->timeTrackingForm['end_minute']
),
], $this->filteredTimeTrackingFormFields()),
$generatedPauseTimeArray
);
Expand Down
6 changes: 6 additions & 0 deletions app/Policies/LocationPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public function approveAbsence(User $user, Location $location)
return $user->hasLocationPermission($location, 'approveAbsence');
}

public function removeAbsence(User $user, Location $location)
{

return $user->hasLocationPermission($location, 'removeAbsence');
}

/**
* Determine whether the user can add team members.
*
Expand Down
Loading