Files
JobcardSystem/app/Domain/SLA/SlaCalculator.php
T

15 lines
755 B
PHP

<?php
declare(strict_types=1);
function calculate_sla_usage(float $allocatedHours, array $hours): array
{
if ($allocatedHours < 0) {
throw new InvalidArgumentException('Allocated hours must not be negative.');
}
$used = round(array_sum(array_map(static fn ($value): float => max(0.0, (float)$value), $hours)), 2);
$remaining = round(max(0.0, $allocatedHours - $used), 2);
$percentage = $allocatedHours > 0 ? round(($used / $allocatedHours) * 100, 2) : ($used > 0 ? 100.0 : 0.0);
$status = $used > $allocatedHours ? 'exceeded' : ($percentage >= 90 ? 'critical' : ($percentage >= 75 ? 'warning' : 'within_limit'));
return ['used' => $used, 'remaining' => $remaining, 'percentage' => $percentage, 'status' => $status];
}