Refactor progress calculation to avoid mutating instance and improve clarity
This commit is contained in:
@@ -738,44 +738,48 @@ class goals_criteria implements goals_criteria_i
|
||||
|
||||
public function getProgressDetails(?string $timeframe = null): array
|
||||
{
|
||||
if ($timeframe) {
|
||||
$this->setTimeframeByName($timeframe);
|
||||
$criteria = $this;
|
||||
if ($timeframe !== null) {
|
||||
// Avoid mutating the current instance so repeated calls for different
|
||||
// periods (e.g. departmental distribution) always use original goal bounds.
|
||||
$criteria = clone $this;
|
||||
$criteria->setTimeframeByName($timeframe);
|
||||
}
|
||||
|
||||
$target = 0.0;
|
||||
$active_dept_ids = $this->departments->listIDs();
|
||||
$active_dept_ids = $criteria->departments->listIDs();
|
||||
|
||||
if ($timeframe === 'today') {
|
||||
$days = $this->getTimeframeDayCount();
|
||||
$days = $criteria->getTimeframeDayCount();
|
||||
foreach ($active_dept_ids as $id) {
|
||||
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days;
|
||||
$target += (float)($criteria->department_daily_targets[$id] ?? 0) * $days;
|
||||
}
|
||||
} elseif ($timeframe === 'week') {
|
||||
$days = $this->getTimeframeDayCount();
|
||||
$days = $criteria->getTimeframeDayCount();
|
||||
foreach ($active_dept_ids as $id) {
|
||||
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days;
|
||||
$target += (float)($criteria->department_daily_targets[$id] ?? 0) * $days;
|
||||
}
|
||||
} elseif ($timeframe === 'month') {
|
||||
$days = $this->getTimeframeDayCount();
|
||||
$days = $criteria->getTimeframeDayCount();
|
||||
foreach ( $active_dept_ids as $id ) {
|
||||
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days;
|
||||
$target += (float)($criteria->department_daily_targets[$id] ?? 0) * $days;
|
||||
}
|
||||
} elseif ($timeframe === 'year') {
|
||||
$days = $this->getTimeframeDayCount();
|
||||
$days = $criteria->getTimeframeDayCount();
|
||||
foreach ( $active_dept_ids as $id ) {
|
||||
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days;
|
||||
$target += (float)($criteria->department_daily_targets[$id] ?? 0) * $days;
|
||||
}
|
||||
} elseif ($timeframe === 'to_date') {
|
||||
$days = $this->getTimeframeDayCount();
|
||||
$days = $criteria->getTimeframeDayCount();
|
||||
foreach ($active_dept_ids as $id) {
|
||||
$target += (float)($this->department_daily_targets[$id] ?? 0) * $days;
|
||||
$target += (float)($criteria->department_daily_targets[$id] ?? 0) * $days;
|
||||
}
|
||||
} else {
|
||||
$target = (float)$this->target;
|
||||
$target = (float)$criteria->target;
|
||||
}
|
||||
|
||||
return [
|
||||
'count' => $this->getProgress(),
|
||||
'count' => $criteria->getProgress(),
|
||||
'target' => $target
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user