Add custom monthly target calculation in goals_progress_alert_renderer for specific periods.

This commit is contained in:
Jeppe Bundgaard
2026-03-09 09:58:48 +01:00
parent dd4c9da86c
commit 16bcc3a00c
@@ -219,6 +219,7 @@ class goals_progress_alert_renderer
];
foreach ($periods as $idx => $period) {
$isMonthPeriod = ($idx === 2);
$label = (string)$period['label'];
[$ps, $pe] = $period['range'];
$range = self::clampToCriteriaWindow($criteria, $ps, $pe);
@@ -246,13 +247,17 @@ class goals_progress_alert_renderer
$total_period_count += $count;
if ($includeTargets && isset($criteria->target)) {
// Set the target to (operating days in period) * (daily target)
$target = self::getTargetForDepartmentInTimeframe(
$criteria,
$rs,
$re,
(int)$deptId
);
if ($isMonthPeriod) {
$target = self::getMonthlyTargetForDepartment($criteria, (int)$deptId, $departments);
} else {
// Set the target to (operating days in period) * (daily target)
$target = self::getTargetForDepartmentInTimeframe(
$criteria,
$rs,
$re,
(int)$deptId
);
}
$percent = $target > 0 ? round(($count / max(1, $target)) * 100, 2) : 0.0;
// If the $department is set and matches the current department, make the related line stand out (*text here*)
$line = $deptData['highlight']
@@ -269,14 +274,18 @@ class goals_progress_alert_renderer
// Total line for the period
if ($includeTargets && isset($criteria->target)) {
// Sum per-department targets to account for overrides
$total_target = 0;
foreach ($departments as $deptData) {
$total_target += self::getTargetForDepartmentInTimeframe(
$criteria,
$rs,
$re,
(int)$deptData['id']
);
if ($isMonthPeriod) {
$total_target = max(0, (int)round((float)($criteria->target ?? 0)));
} else {
$total_target = 0;
foreach ($departments as $deptData) {
$total_target += self::getTargetForDepartmentInTimeframe(
$criteria,
$rs,
$re,
(int)$deptData['id']
);
}
}
$total_percent = $total_target > 0 ? round(($total_period_count / max(1, $total_target)) * 100, 2) : 0.0;
$outLines[] = sprintf($label . ': %d ud af %d (%.2f%%)',
@@ -382,6 +391,31 @@ class goals_progress_alert_renderer
return (int)round($dailyTarget * $operatingDays);
}
private static function getMonthlyTargetForDepartment(goals_criteria $criteria, int $departmentId, array $departments): int
{
$totalMonthlyTarget = max(0, (int)round((float)($criteria->target ?? 0)));
if ($totalMonthlyTarget <= 0) {
return 0;
}
$deptIds = array_map(fn($deptData) => (int)$deptData['id'], $departments);
sort($deptIds);
$countDepts = max(1, count($deptIds));
if ($countDepts === 1) {
return $totalMonthlyTarget;
}
$base = intdiv($totalMonthlyTarget, $countDepts);
$remainder = $totalMonthlyTarget % $countDepts;
$index = array_search($departmentId, $deptIds, true);
if ($index === false) {
return $base;
}
return $base + ($index < $remainder ? 1 : 0);
}
private static function getDailyTargetForDepartment(goals_criteria $criteria, ?int $departmentId): float
{
// If explicit per-department daily targets are provided, honor them