From 8b4828f2367593ec4943975552e56b67b0ad5c65 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 24 Oct 2022 16:21:57 +0200 Subject: [PATCH] allow 4 decimals for rounded rates (#3596) --- src/Timesheet/Util.php | 2 +- tests/API/TimesheetControllerTest.php | 2 +- .../Calculator/RateCalculatorTest.php | 6 +-- tests/Timesheet/RateServiceTest.php | 6 +-- tests/Timesheet/UtilTest.php | 48 +++++++++++++++---- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/Timesheet/Util.php b/src/Timesheet/Util.php index 90371266..4df8a800 100644 --- a/src/Timesheet/Util.php +++ b/src/Timesheet/Util.php @@ -24,7 +24,7 @@ class Util public static function calculateRate(float $hourlyRate, int $seconds): float { $rate = (float) ($hourlyRate * ($seconds / 3600)); - $rate = round($rate, 2); + $rate = round($rate, 4); return $rate; } diff --git a/tests/API/TimesheetControllerTest.php b/tests/API/TimesheetControllerTest.php index 3b77304f..d190260c 100644 --- a/tests/API/TimesheetControllerTest.php +++ b/tests/API/TimesheetControllerTest.php @@ -347,7 +347,7 @@ class TimesheetControllerTest extends APIControllerBaseTest 'exported' => true, 'metaFields' => [], 'hourlyRate' => 137.21, - 'rate' => 1772.3, + 'rate' => 1772.2958, 'internalRate' => 0.0, ]; diff --git a/tests/Timesheet/Calculator/RateCalculatorTest.php b/tests/Timesheet/Calculator/RateCalculatorTest.php index fde21935..33c7ba5e 100644 --- a/tests/Timesheet/Calculator/RateCalculatorTest.php +++ b/tests/Timesheet/Calculator/RateCalculatorTest.php @@ -249,7 +249,7 @@ class RateCalculatorTest extends TestCase [ 31837, [], - 663.27 + 663.2708 ], [ 31837, @@ -263,7 +263,7 @@ class RateCalculatorTest extends TestCase 'factor' => 1.5 ], ], - 1326.54 + 1326.5417 ], [ 31837, @@ -277,7 +277,7 @@ class RateCalculatorTest extends TestCase 'factor' => 1.5 ], ], - 2321.45 + 2321.4479 ], ]; } diff --git a/tests/Timesheet/RateServiceTest.php b/tests/Timesheet/RateServiceTest.php index 5a7aae65..36b13745 100644 --- a/tests/Timesheet/RateServiceTest.php +++ b/tests/Timesheet/RateServiceTest.php @@ -248,7 +248,7 @@ class RateServiceTest extends TestCase [ 31837, [], - 663.27 + 663.2708 ], [ 31837, @@ -262,7 +262,7 @@ class RateServiceTest extends TestCase 'factor' => 1.5 ], ], - 1326.54 + 1326.5417 ], [ 31837, @@ -276,7 +276,7 @@ class RateServiceTest extends TestCase 'factor' => 1.5 ], ], - 2321.45 + 2321.4479 ], ]; } diff --git a/tests/Timesheet/UtilTest.php b/tests/Timesheet/UtilTest.php index 030fd25b..93efca20 100644 --- a/tests/Timesheet/UtilTest.php +++ b/tests/Timesheet/UtilTest.php @@ -28,16 +28,46 @@ class UtilTest extends TestCase public function getRateCalculationData() { yield [0, 0, 0]; - yield [1, 100, 0.03]; + yield [1, 100, 0.0278]; yield [1, 900, 0.25]; yield [1, 1800, 0.5]; - yield [10000, 1, 2.78]; - yield [736, 123.45, 25.15]; - yield [736, 123, 25.15]; - yield [7360, 1234.99, 2522.84]; - yield [7360, 1234, 2522.84]; - yield [7360.34, 1234, 2522.96]; - yield [7360.01, 1234, 2522.85]; - yield [7360.99, 1234, 2523.18]; + yield [10000, 1, 2.7778]; + yield [736, 123, 25.1467]; + yield [7360, 1234, 2522.8444]; + yield [7360.34, 1234, 2522.961]; + yield [7360.01, 1234, 2522.8479]; + yield [7360.99, 1234, 2523.1838]; + } + + public function testCalculateRateWithRounding() + { + $total = 0.00; + $seconds = 0; + $repeat = 130; + + for ($a = 0; $a < $repeat; $a++) { + $inputs = [ + 900, + 1600, + 4200, + 8763, + 3300, + 600, + 1300, + 1837, + 4217, + 5400, + 3283, + 600, + ]; + + foreach ($inputs as $i) { + $seconds += $i; + $total += Util::calculateRate(114.75, $i); + } + } + + self::assertEquals(36000 * $repeat, $seconds); + self::assertEquals(1147.50 * $repeat, $total); } }