Move NightShiftMultiplier calculation to Shifts helper
This commit is contained in:
parent
20e389fccd
commit
870a92efd5
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Engelsystem\Helpers\Shifts;
|
||||
use Engelsystem\Models\Room;
|
||||
use Engelsystem\Models\Worklog;
|
||||
use Illuminate\Support\Collection;
|
||||
|
@ -71,8 +72,6 @@ function UserWorkLog_create(Worklog $worklog)
|
|||
function UserWorkLog_from_shift($shift)
|
||||
{
|
||||
$shift = is_array($shift) ? $shift : Shift($shift);
|
||||
$nightShifts = config('night_shifts');
|
||||
|
||||
if ($shift['start'] > 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;
|
||||
|
|
|
@ -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'];
|
||||
}
|
||||
}
|
|
@ -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')
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue