Enhance progress tracking by adding yearly and to-date calculations for departmental targets

This commit is contained in:
Jeppe Bundgaard
2026-02-27 00:23:02 +01:00
parent bf636c0108
commit ece9f5108a
2 changed files with 22 additions and 3 deletions
@@ -478,6 +478,14 @@ class goals_criteria implements goals_criteria_i
$this->start = (clone $now)->modify('first day of this month')->setTime(0, 0, 0);
$this->end = (clone $now)->setTime(23, 59, 59);
break;
case 'year':
$this->start = (clone $now)->modify('first day of January this year')->setTime(0, 0, 0);
$this->end = (clone $now)->setTime(23, 59, 59);
break;
case 'to_date':
$this->start = null; // no start limit
$this->end = (clone $now)->setTime(23, 59, 59);
break;
}
}
@@ -719,9 +727,20 @@ class goals_criteria implements goals_criteria_i
$target += (float)($this->department_daily_targets[$id] ?? 0) * 7;
}
} elseif ($timeframe === 'month') {
foreach ($active_dept_ids as $id) {
foreach ( $active_dept_ids as $id ) {
$target += (float)($this->department_daily_targets[$id] ?? 0) * 30;
}
} elseif ($timeframe === 'year') {
foreach ( $active_dept_ids as $id ) {
$target += (float)($this->department_daily_targets[$id] ?? 0) * 365;
}
} elseif ($timeframe === 'to_date') {
$now = new \DateTime();
$start_of_year = (clone $now)->setDate((int)$now->format('Y'), 1, 1)->setTime(0, 0, 0);
$days_passed = (int)$start_of_year->diff($now)->format('%a') + 1; // +1 to include today
foreach ($active_dept_ids as $id) {
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days_passed;
}
} else {
$target = (float)$this->target;
}
@@ -87,8 +87,8 @@ class department_goals_o extends db
'progress' => [
'all' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value()) : null,
'departmental_distribution' => $include_progress ? goals_criteria::calculateDepartmentalProgressFromArray((array)$this->criteria->value()) : null,
// array of department_id => [ 'all' => [ 'count' => int, 'target' => int ], 'today' => [ 'count' => int, 'target' => int ], 'week' => [ 'count' => int, 'target' => int ], 'month' => [ 'count' => int, 'target' => int ] ]
// Showing progress for different time periods
// array of department_id => [ 'all' => [ 'count' => int, 'target' => int ], 'today' => [ 'count' => int, 'target' => int ], 'week' => [ 'count' => int, 'target' => int ], 'month' => [ 'count' => int, 'target' => int ], 'year' => [ 'count' => int, 'target' => int ], 'to_date' => [ 'count' => int, 'target' => int ] ]
// Showing progress for different time periods (Never starting before the goal start date, and never ending after the goal end date):
'today' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'today') : null,
'week' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'week') : null,
'month' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'month') : null,