Skip to content

Commit

Permalink
Merge pull request #82 from reload/fix-division-by-zero-error
Browse files Browse the repository at this point in the history
Fix division by zero error
  • Loading branch information
kasperg authored Aug 29, 2023
2 parents 2dd8652 + c48d345 commit 54e14e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
4 changes: 0 additions & 4 deletions docker/opcache-config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ opcache.memory_consumption=256
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=20000

; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
opcache.validate_timestamps=0
34 changes: 24 additions & 10 deletions src/reloaddk/HarvesterBundle/Entity/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,16 @@ public function determineRankingGroup($hours_registered, $hours_goal)
*
* @param $user_entries
* @param int $workingdays_in_range
* @param float $user_working_hours
* @param float $user_working_hours_per_day
* @param string $token
* @return array
*/
public function parseUser(Array $user_entries, $workingdays_in_range, $user_working_hours = null, $token = null)
{
public function parseUser(
Array $user_entries,
$workingdays_in_range,
$user_working_hours_per_day = null,
$token = null
) {
$hours = $billable_hours = $education = $holiday = $vacation = $off_hours = 0;
$illness['normal'] = $illness['child'] = 0;
$time_off['normal'] = $time_off['paternity_leave'] = 0;
Expand Down Expand Up @@ -437,11 +441,11 @@ public function parseUser(Array $user_entries, $workingdays_in_range, $user_work

// Get default working hours per day for the user.
if ($entry_user->getWorkingHours() > 0) {
$user_working_hours = $entry_user->getWorkingHours();
$user_working_hours_per_day = $entry_user->getWorkingHours();
}

// Get the normed hours for this month.
$hours_goal = $workingdays_in_range * $user_working_hours;
$hours_goal = $workingdays_in_range * $user_working_hours_per_day;

// Split user id, used for creating path to user avatar from Harvest!
$user_id = str_pad($entry_user->getId(), 9, 0, STR_PAD_LEFT);
Expand Down Expand Up @@ -493,12 +497,22 @@ public function parseUser(Array $user_entries, $workingdays_in_range, $user_work
}

// Calculate normalized hours_pr_day
//'hours_pr_day_normalized' => round($billable_hours / $workingdays_in_range - ((hours_registered - working_hours) / user_working_hours), 2),
$hours_pr_day_normalized = ($billable_hours / ($workingdays_in_range - (($hours-$working_hours)/$user_working_hours)));
$total_excess_hours_in_range = $hours - $working_hours;
$excess_workings_days_in_range = $total_excess_hours_in_range / $user_working_hours_per_day;
$normalized_working_days_in_range = $workingdays_in_range - $excess_workings_days_in_range;
// Only try to calculate the normalized number of hours per day if there were actually any normalized
// working days in the range. If the user is on maternity leave or vacation for the entire range, there
// will be no normalized working days in that range, and we would get a division by zero error.
if ($normalized_working_days_in_range > 0) {
$billable_hours_per_day_normalized = $billable_hours / $normalized_working_days_in_range;
$hours_pr_day_normalized = $billable_hours_per_day_normalized;
} else {
$hours_pr_day_normalized = 0;
}

// Extra information for admins and logged in users.
$extra = array(
'billable_hours' => $billable_hours,
'billable_hours' => round($billable_hours, 2),
'billability' => array(
'of_total_hours' => $billability['raw'],
'of_working_hours' => $billability['calculated'],
Expand All @@ -510,7 +524,7 @@ public function parseUser(Array $user_entries, $workingdays_in_range, $user_work
'education' => $education,
'vacation' => $vacation,
'illness' => $illness,
'working_hours' => $working_hours,
'working_hours' => round($working_hours, 2),
'working_days' => $workingdays_in_range,
'off_hours' => $off_hours ? $off_hours : false,
);
Expand All @@ -523,7 +537,7 @@ public function parseUser(Array $user_entries, $workingdays_in_range, $user_work
'full_name' => $entry_user->getFirstName() . ' ' . $entry_user->getLastName(),
'email' => $entry_user->getEmail(),
'hours_goal' => $hours_goal,
'hours_registered' => $hours,
'hours_registered' => round($hours, 2),
'extra' => $extra,
'admin' => $admin,
);
Expand Down

0 comments on commit 54e14e9

Please sign in to comment.