Skip to content

Commit

Permalink
Merge pull request #31 from eporsche/fix_vacation_allocation
Browse files Browse the repository at this point in the history
fixed an issue where multiple vacation allowances could not be approved
  • Loading branch information
eporsche authored Dec 2, 2021
2 parents 8390089 + 6469816 commit 07517c6
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 6 deletions.
0 .php_cs → .php-cs-fixer.php
100644 → 100755
File renamed without changes.
Empty file modified .styleci.yml
100644 → 100755
Empty file.
11 changes: 8 additions & 3 deletions app/Models/VacationEntitlement.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function isExpired()
return $this->status === 'expired';
}

public function notExpired()
{
return $this->status != 'expired';
}

public function expire()
{
return $this->update([
Expand Down Expand Up @@ -151,15 +156,15 @@ public function useVacationDays(Absence $absence)
return;
}

if ($this->isUsed()) {
if ($this->isUsed() || $this->isExpired()) {
throw ValidationException::withMessages([
'error' => [__('Vacation entitlement has wrong status.')],
]);
}

$this->usedVacationDays()->attach($absence->id, [
tap($this, fn($model) => $model->usedVacationDays()->attach($absence->id, [
'used_days' => $absence->vacation_days
]);
]))->load('usedVacationDays');

if ($this->used_days->isGreaterThanOrEqualTo($this->available_days)) {
$this->markAsUsed();
Expand Down
14 changes: 11 additions & 3 deletions app/Traits/HasVacations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ public function availableVacationEntitlements()
public function currentVacationEntitlement()
{
$today = now()->startOfDay();
return $this->availableVacationEntitlements()
$available = $this->availableVacationEntitlements()
->where('starts_at','<=',$today)
->where('ends_at','>=',$today)
->orderBy('ends_at','ASC')
->limit(1)
->first();
->get();

$feasibleEntitlements = $available->filter(function (VacationEntitlement $entitlement) {
if(!$entitlement->isExpired() && !$entitlement->isUsed()) {
return $entitlement;
}
});

return $feasibleEntitlements->first();

}

public function latestVacationEntitlement()
Expand Down
162 changes: 162 additions & 0 deletions tests/Feature/EvaluationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\Location;
use App\Models\AbsenceType;
use App\Contracts\AddsAbsences;
use App\Contracts\AddsVacationEntitlements;
use App\Contracts\ApprovesAbsence;
use Illuminate\Support\Facades\Bus;
use App\Contracts\FiltersEvaluation;
Expand Down Expand Up @@ -135,6 +136,167 @@ public function test_can_submit_fullday_illness()
//Überstunden

//Urlaub
public function test_can_use_vacation_entitlement()
{
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Urlaub',
'affect_vacation_times' => true,
'affect_evaluations' => true,
'evaluation_calculation_setting' => 'absent_to_target'
]);

$this->user->absenceTypes()->sync($absenceType);

/**
* @var AddsVacationEntitlements
*/
$action = app(AddsVacationEntitlements::class);
$action->add($this->user, [
'name' => "yearly allowance",
'starts_at' => "01.01.2021",
'ends_at' => "31.12.2021",
'days' => 2,
'expires' => false,
'transfer_remaining' => false
]);

/**
* @var AddsAbsences
*/
$action = app(AddsAbsences::class);
$action->add($this->user, $this->location, $this->user->id, [
'absence_type_id' => $absenceType->id,
'starts_at' => '29.11.2021 00:00',
'ends_at' => '30.11.2021 00:00',
'full_day' => true,
'status' => 'confirmed'
]);

Bus::fake();

/**
* @var ApprovesAbsence
*/
$approver = app(ApprovesAbsence::class);

$approver->approve(
$this->user,
$this->location,
Absence::first()->id
);

$this->assertDatabaseHas("absences",[
'id' => Absence::first()->id,
'status' => 'confirmed',
'vacation_days' => 2,
'paid_hours' => 16
]);

$this->assertDatabaseHas("vacation_entitlements",[
'name' => 'yearly allowance',
'status' => 'used',
]);
}

public function test_can_submit_vacation()
{
$absenceType = AbsenceType::forceCreate([
'location_id' => $this->location->id,
'title' => 'Urlaub',
'affect_vacation_times' => true,
'affect_evaluations' => true,
'evaluation_calculation_setting' => 'absent_to_target'
]);

$this->user->absenceTypes()->sync($absenceType);

/**
* @var AddsVacationEntitlements
*/
$action = app(AddsVacationEntitlements::class);
$action->add($this->user, [
'name' => "yearly allowance",
'starts_at' => "01.01.2021",
'ends_at' => "31.12.2021",
'days' => 2,
'expires' => false,
'transfer_remaining' => false
]);

/**
* @var AddsAbsences
*/
$action = app(AddsAbsences::class);

$action->add($this->user, $this->location, $this->user->id, [
'absence_type_id' => $absenceType->id,
'starts_at' => '29.11.2021 00:00',
'ends_at' => '30.11.2021 00:00',
'full_day' => true,
'status' => 'confirmed'
]);

Bus::fake();

/**
* @var ApprovesAbsence
*/
$approver = app(ApprovesAbsence::class);

$approver->approve(
$this->user,
$this->location,
Absence::first()->id
);

$this->assertDatabaseHas("absence_index",[
'date' => '2021-11-29 00:00:00',
'hours' => 8,
]);

$this->assertDatabaseHas("absence_index",[
'date' => '2021-11-30 00:00:00',
'hours' => 8,
]);

/**
* @var AddsVacationEntitlements
*/
$action = app(AddsVacationEntitlements::class);
$action->add($this->user, [
'name' => "additional allowance",
'starts_at' => "01.01.2021",
'ends_at' => "31.12.2021",
'days' => 2,
'expires' => false,
'transfer_remaining' => false
]);

/**
* @var AddsAbsences
*/
$action = app(AddsAbsences::class);

$action->add($this->user, $this->location, $this->user->id, [
'absence_type_id' => $absenceType->id,
'starts_at' => '06.12.2021 00:00',
'ends_at' => '07.12.2021 00:00',
'full_day' => true,
'status' => 'confirmed'
]);

/**
* @var ApprovesAbsence
*/
$approver = app(ApprovesAbsence::class);

$approver->approve(
$this->user,
$this->location,
Absence::where('starts_at','2021-12-06 00:00:00')->first()->id
);
}

//Wunschfrei
}

0 comments on commit 07517c6

Please sign in to comment.