Skip to content

Commit

Permalink
Fixed issue regarding averages, and revised rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppekroghitk committed Dec 18, 2024
1 parent 39b23c8 commit 4ecbd8e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/Service/InvoicingRateReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function getInvoicingRateReport(PeriodTypeEnum $viewPeriodType = PeriodTy
$year = (int) (new \DateTime())->format('Y');
$workers = $this->workerRepository->findAllIncludedInReports();
$periods = $this->getPeriods($viewPeriodType, $year);
$periodSums = [];
$periodCounts = [];

foreach ($periods as $period) {
$readablePeriod = $this->getReadablePeriod($period, $viewPeriodType);
Expand All @@ -48,8 +50,6 @@ public function getInvoicingRateReport(PeriodTypeEnum $viewPeriodType = PeriodTy
$currentPeriodReached = false;
$loggedBilledHoursSum = 0;
$loggedHoursSum = 0;
$periodSums = [];
$periodCounts = [];

foreach ($periods as $period) {
// Add current period match-point (current week-number, month-number etc.)
Expand Down Expand Up @@ -94,7 +94,7 @@ public function getInvoicingRateReport(PeriodTypeEnum $viewPeriodType = PeriodTy
$loggedHoursSum += $loggedHours;
}

$loggedBilledPercentage = $loggedHours > 0 ? round($loggedBilledHours / $loggedHours * 100, 1) : 0;
$loggedBilledPercentage = $loggedHours > 0 ? round($loggedBilledHours / $loggedHours * 100, 4) : 0;

// Add percentage result to worker for current period.
$invoicingRateReportWorker->dataByPeriod->set($period, [
Expand All @@ -107,11 +107,13 @@ public function getInvoicingRateReport(PeriodTypeEnum $viewPeriodType = PeriodTy
$periodCounts[$period] = ($periodCounts[$period] ?? 0) + 1;

// Calculate and set the average for this period
$average = round($periodSums[$period] / $periodCounts[$period], 2);
$average = round($periodSums[$period] / $periodCounts[$period], 4);

$invoicingRateReportData->periodAverages->set($period, $average);
}

$invoicingRateReportWorker->average = $loggedHoursSum > 0 ? round($loggedBilledHoursSum / $loggedHoursSum * 100, 1) : 0;

$invoicingRateReportWorker->average = $loggedHoursSum > 0 ? round($loggedBilledHoursSum / $loggedHoursSum * 100, 4) : 0;

$invoicingRateReportData->workers->add($invoicingRateReportWorker);
}
Expand All @@ -126,7 +128,7 @@ public function getInvoicingRateReport(PeriodTypeEnum $viewPeriodType = PeriodTy

// Calculate the total average of averages
if ($numberOfPeriods > 0) {
$invoicingRateReportData->totalAverage = round($averageSum / $numberOfPeriods, 1);
$invoicingRateReportData->totalAverage = round($averageSum / $numberOfPeriods, 4);
}

return $invoicingRateReportData;
Expand Down
8 changes: 4 additions & 4 deletions templates/reports/invoicing_rate_report.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
">
<div class="flex justify-between w-32 double-number-container">
<span class="flex-1 text-right">{{ periodData.loggedBillableHours }}</span>
<span class="flex-auto text-right">{{ periodData.loggedBilledPercentage ~ '%' }}</span>
<span class="flex-auto text-right">{{ periodData.loggedBilledPercentage|round(1) ~ '%' }}</span>
</div>

</td>
{% endfor %}
<td class="text-center px-3 border border-slate-600 bg-slate-300 dark:bg-slate-800">
{{ worker.average ~ '%' }}
{{ worker.average|round(1) ~ '%' }}
</td>
</tr>
</tbody>
Expand All @@ -80,10 +80,10 @@
{% for periodNumeric, period in data.period %}
<td class="text-center px-3 border border-slate-600 bg-slate-300 dark:bg-slate-800
">
{{ data.periodAverages[periodNumeric] ~ '%' }}
~ {{ data.periodAverages[periodNumeric]|round(1) ~ '%' }}
</td>
{% endfor %}
<td class="text-center px-3 border border-slate-600 bg-slate-300 dark:bg-slate-800"> {{ data.totalAverage ~ '%' }}</td>
<td class="text-center px-3 border border-slate-600 bg-slate-300 dark:bg-slate-800"> {{ data.totalAverage|round(1) ~ '%' }}</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 4ecbd8e

Please sign in to comment.