diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index ef167afe..9f444357 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -1,6 +1,7 @@ time()) { return; } @@ -85,18 +84,9 @@ function UserWorkLog_from_shift($shift) $type = AngelType($entry['TID']); - $nightShiftMultiplier = 1; $shiftStart = Carbon::createFromTimestamp($shift['start']); $shiftEnd = Carbon::createFromTimestamp($shift['end']); - if ( - $nightShifts['enabled'] - && ( - $shiftStart->hour >= $nightShifts['start'] && $shiftStart->hour < $nightShifts['end'] - || $shiftEnd->hour >= $nightShifts['start'] && $shiftEnd->hour < $nightShifts['end'] - ) - ) { - $nightShiftMultiplier = $nightShifts['multiplier']; - } + $nightShiftMultiplier = Shifts::getNightShiftMultiplier($shiftStart, $shiftEnd); $worklog = UserWorkLog_new($entry['UID']); $worklog->hours = (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier; diff --git a/src/Helpers/Shifts.php b/src/Helpers/Shifts.php new file mode 100644 index 00000000..2eb9dce5 --- /dev/null +++ b/src/Helpers/Shifts.php @@ -0,0 +1,42 @@ +hour >= $config['start'] && $start->hour < $config['end'] + || $end->hour >= $config['start'] && $end->hour < $config['end'] + ); + } + + /** + * Calculate a shifts night multiplier + * + * @param Carbon $start + * @param Carbon $end + * @return float + */ + public static function getNightShiftMultiplier(Carbon $start, Carbon $end): float + { + if (!self::isNightShift($start, $end)) { + return 1; + } + + return config('night_shifts')['multiplier']; + } +} diff --git a/tests/Unit/Helpers/ShiftsTest.php b/tests/Unit/Helpers/ShiftsTest.php new file mode 100644 index 00000000..3c9a6310 --- /dev/null +++ b/tests/Unit/Helpers/ShiftsTest.php @@ -0,0 +1,90 @@ + [ + 'enabled' => false, + 'start' => 2, + 'end' => 6, + 'multiplier' => 2, + ]]); + $this->app->instance('config', $config); + + // At night but disabled + $this->assertFalse(Shifts::isNightShift( + new Carbon('2042-01-01 04:00'), + new Carbon('2042-01-01 05:00') + )); + + $config->set('night_shifts', array_merge($config->get('night_shifts'), ['enabled' => true])); + + // Is night shift + $this->assertTrue(Shifts::isNightShift( + new Carbon('2042-01-01 04:00'), + new Carbon('2042-01-01 05:00') + )); + + // Starts as night shift + $this->assertTrue(Shifts::isNightShift( + new Carbon('2042-01-01 05:45'), + new Carbon('2042-01-01 07:00') + )); + + // Ends as night shift + $this->assertTrue(Shifts::isNightShift( + new Carbon('2042-01-01 00:00'), + new Carbon('2042-01-01 02:15') + )); + + // Too early + $this->assertFalse(Shifts::isNightShift( + new Carbon('2042-01-01 00:00'), + new Carbon('2042-01-01 01:59') + )); + + // Too late + $this->assertFalse(Shifts::isNightShift( + new Carbon('2042-01-01 06:00'), + new Carbon('2042-01-01 09:59') + )); + } + + /** + * @covers \Engelsystem\Helpers\Shifts::getNightShiftMultiplier + */ + public function testGetNightShiftMultiplier() + { + $config = new Config(['night_shifts' => [ + 'enabled' => true, + 'start' => 2, + 'end' => 6, + 'multiplier' => 2, + ]]); + $this->app->instance('config', $config); + + $this->assertEquals(2, Shifts::getNightShiftMultiplier( + new Carbon('2042-01-01 02:00'), + new Carbon('2042-01-01 04:00') + )); + + $config->set('night_shifts', array_merge($config->get('night_shifts'), ['enabled' => false])); + $this->assertEquals(1, Shifts::getNightShiftMultiplier( + new Carbon('2042-01-01 02:00'), + new Carbon('2042-01-01 04:00') + )); + } +}