From 16b1ec62866b9a9713c6436f5c63879e11b6aba5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 8 Feb 2023 11:27:38 +1100 Subject: [PATCH] Improve resolution of decimals --- app/Utils/Number.php | 10 +++++----- tests/Feature/TaskApiTest.php | 35 ++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/Utils/Number.php b/app/Utils/Number.php index 06edf8611ab6..e2891b7c54b9 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -223,15 +223,15 @@ class Number /* 08-01-2022 allow increased precision for unit price*/ $v = rtrim(sprintf('%f', $value), '0'); - // $precision = strlen(substr(strrchr($v, $decimal), 1)); - if ($v < 1) { + /* 08-02-2023 special if block to render $0.5 to $0.50*/ + if ($v < 1 && strlen($v) == 3) { + $precision = 2; + } + elseif ($v < 1) { $precision = strlen($v) - strrpos($v, '.') - 1; } - // if($precision == 1) - // $precision = 2; - $value = number_format($v, $precision, $decimal, $thousand); $symbol = $currency->symbol; diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 80c2b3e77097..b5f77674024a 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -43,52 +43,57 @@ class TaskApiTest extends TestCase Model::reguard(); } - private function checkTimeLog($log) + private function checkTimeLog(array $log): bool { + if(count($log) == 0) + return true; + /*Get first value of all arrays*/ $result = array_column($log, 0); + /*Sort the array in ascending order*/ asort($result); $new_array = []; + /*Rebuild the array in order*/ foreach($result as $key => $value) $new_array[] = $log[$key]; + /*Iterate through the array and perform checks*/ foreach($new_array as $key => $array) { + /*Flag which helps us know if there is a NEXT timelog*/ $next = false; - + /* If there are more than 1 time log in the array, ensure the last timestamp is not zero*/ if(count($new_array) >1 && $array[1] == 0) return false; - /* First test is to check if the start time is greater than the end time */ + /* Check if the start time is greater than the end time */ /* Ignore the last value for now, we'll do a separate check for this */ - if($array[0] > $array[1] && $array[1] != 0){ + if($array[0] > $array[1] && $array[1] != 0) return false; - } - - if(array_key_exists($key+1, $new_array)){ + + /* Find the next time log value - if it exists */ + if(array_key_exists($key+1, $new_array)) $next = $new_array[$key+1]; - } /* check the next time log and ensure the start time is GREATER than the end time of the previous record */ - if($next && $next[0] < $array[1]){ + if($next && $next[0] < $array[1]) return false; - } + /* Get the last row of the timelog*/ $last_row = end($new_array); - - if($last_row[1] != 0 && $last_row[0] > $last_row[1]){ - nlog($last_row[0]. " ".$last_row[1]); + + /*If the last value is NOT zero, ensure start time is not GREATER than the endtime */ + if($last_row[1] != 0 && $last_row[0] > $last_row[1]) return false; - } return true; } } - + public function testTimeLogChecker1() {