Move NightShiftMultiplier calculation to Shifts helper

This commit is contained in:
Igor Scheller 2022-07-31 16:50:35 +02:00
parent 20e389fccd
commit 870a92efd5
3 changed files with 134 additions and 12 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Helpers\Shifts;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Worklog; use Engelsystem\Models\Worklog;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -71,8 +72,6 @@ function UserWorkLog_create(Worklog $worklog)
function UserWorkLog_from_shift($shift) function UserWorkLog_from_shift($shift)
{ {
$shift = is_array($shift) ? $shift : Shift($shift); $shift = is_array($shift) ? $shift : Shift($shift);
$nightShifts = config('night_shifts');
if ($shift['start'] > time()) { if ($shift['start'] > time()) {
return; return;
} }
@ -85,18 +84,9 @@ function UserWorkLog_from_shift($shift)
$type = AngelType($entry['TID']); $type = AngelType($entry['TID']);
$nightShiftMultiplier = 1;
$shiftStart = Carbon::createFromTimestamp($shift['start']); $shiftStart = Carbon::createFromTimestamp($shift['start']);
$shiftEnd = Carbon::createFromTimestamp($shift['end']); $shiftEnd = Carbon::createFromTimestamp($shift['end']);
if ( $nightShiftMultiplier = Shifts::getNightShiftMultiplier($shiftStart, $shiftEnd);
$nightShifts['enabled']
&& (
$shiftStart->hour >= $nightShifts['start'] && $shiftStart->hour < $nightShifts['end']
|| $shiftEnd->hour >= $nightShifts['start'] && $shiftEnd->hour < $nightShifts['end']
)
) {
$nightShiftMultiplier = $nightShifts['multiplier'];
}
$worklog = UserWorkLog_new($entry['UID']); $worklog = UserWorkLog_new($entry['UID']);
$worklog->hours = (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier; $worklog->hours = (($shift['end'] - $shift['start']) / 60 / 60) * $nightShiftMultiplier;

42
src/Helpers/Shifts.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace Engelsystem\Helpers;
use \Carbon\Carbon;
// Should be moved to the shift model if it's available
class Shifts
{
/**
* Check if a time range is a night shift
*
* @param Carbon $start
* @param Carbon $end
* @return bool
*/
public static function isNightShift(Carbon $start, Carbon $end): bool
{
$config = config('night_shifts');
return $config['enabled'] && (
$start->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'];
}
}

View File

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Helpers\Shifts;
use Engelsystem\Test\Unit\TestCase;
class ShiftsTest extends TestCase
{
/**
* @covers \Engelsystem\Helpers\Shifts::isNightShift
*/
public function testIsNightShift()
{
$config = new Config(['night_shifts' => [
'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')
));
}
}