Refactor departmental progress target calculations to use consistent timeframe references and improve clarity in goal tracking

This commit is contained in:
Jeppe Bundgaard
2026-02-27 00:44:58 +01:00
parent adb2b90e2a
commit 6989e38e14
@@ -18,7 +18,7 @@ const toast = useToast();
const timeframe = ref('all');
const currentTimeframe = computed(() => timeframe.value === 'until_now' ? 'all' : timeframe.value);
const currentTimeframe = computed(() => timeframe.value === 'until_now' ? 'to_date' : timeframe.value);
const fetchGoals = async () => {
loading.value = true;
@@ -88,7 +88,7 @@ const getGoalProgress = (goal, deptId = null) => {
// Use the .value to divided by .target and multiply by 100
if (deptId && goal.progress?.departmental_distribution?.[deptId]?.[key]) {
const deptValue = parseFloat(goal.progress.departmental_distribution[deptId][key].count);
const deptTarget = getDailyGoalDepartmentTarget(goal, deptId, true);
const deptTarget = getDepartmentProgressTarget(goal, deptId);
if (isNaN(deptValue) || isNaN(deptTarget) || deptTarget === 0) return 0;
const deptProgress = (deptValue / deptTarget) * 100;
return Math.min(Math.round(deptProgress), 100);
@@ -174,13 +174,13 @@ const getDailyGoalDepartmentTarget = (goal, deptId, progressUntilNow = false) =>
return isNaN(x) ? 0 : x;
};
const getEvenSplitPerDay = () => {
if (daysTotal === 0) return goal.progress?.departmental_distribution?.[deptId]?.[currentTimeframe]?.target || 0;
if (daysTotal === 0) return goal.progress?.departmental_distribution?.[deptId]?.[currentTimeframe.value]?.target || 0;
const deptCount = Math.max(1, goal.departments.length || 1);
// If the department has a custom daily target, use that, otherwise use the even split
if (hasOverride) {
return normalizeInt(overrides[String(deptId)]);
}
return (normalizeInt(goal.progress?.departmental_distribution?.[deptId]?.[currentTimeframe]?.target) || (normalizeInt(goal.criteria.target) / deptCount)) / daysTotal;
return (normalizeInt(goal.progress?.departmental_distribution?.[deptId]?.[currentTimeframe.value]?.target) || (normalizeInt(goal.criteria.target) / deptCount)) / daysTotal;
};
const dailyTarget = hasOverride ? normalizeInt(overrides[String(deptId)]) : getEvenSplitPerDay();
@@ -198,6 +198,12 @@ const getDailyGoalDepartmentTarget = (goal, deptId, progressUntilNow = false) =>
return Math.round(dailyTarget);
};
const getDepartmentProgressTarget = (goal, deptId) => {
const apiTarget = parseFloat(goal.progress?.departmental_distribution?.[deptId]?.[currentTimeframe.value]?.target);
if (!isNaN(apiTarget) && apiTarget > 0) return apiTarget;
return getDailyGoalDepartmentTarget(goal, deptId, currentTimeframe.value === 'to_date');
};
const getGoalApplicableDaysInPeriod = (goal, fromDate, toDate) => {
const weekdays = goal.criteria.progress_alert_weekdays || ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'];
return SessionUser.functions.getBusinessDaysBetweenDates(
@@ -304,6 +310,12 @@ const getGoalApplicableDaysInPeriod = (goal, fromDate, toDate) => {
<span>Denne måned</span>
</a>
</li>
<li :class="{ 'is-active': timeframe === 'year' }" @click="timeframe = 'year'">
<a>
<span class="icon is-small"><i class="fas fa-calendar"></i></span>
<span>År</span>
</a>
</li>
</ul>
</div>
@@ -405,16 +417,16 @@ const getGoalApplicableDaysInPeriod = (goal, fromDate, toDate) => {
<div class="divider mt-0">Valgt tidsramme</div>
<!-- Departmental progress and then % with 2 decimal places -->
<template v-if="goal.departments.length > 1" v-for="tmpDeptId in goal.departments" :key="tmpDeptId">
<!-- The target for this department until today -->
<!-- The target for this department in selected timeframe -->
<div class="is-flex is-justify-content-space-between mb-1">
<strong :class="{'has-text-warning': tmpDeptId === deptId, 'has-text-grey': tmpDeptId !== deptId}">{{ getDepartmentName(tmpDeptId) }}</strong>
<span :class="{'has-text-warning': tmpDeptId === deptId, 'has-text-grey': tmpDeptId !== deptId}">{{ goal.progress.departmental_distribution[tmpDeptId]?.[currentTimeframe]?.count || 0 }} / {{ getDailyGoalDepartmentTarget(goal, tmpDeptId, true) }} - <strong :class="{'has-text-warning': tmpDeptId === deptId, 'has-text-grey': tmpDeptId !== deptId}">{{ Math.min(Math.round(((goal.progress.departmental_distribution[tmpDeptId]?.[currentTimeframe]?.count || 0) / getDailyGoalDepartmentTarget(goal, tmpDeptId, true)) * 100, true), 100) }}%</strong></span>
<span :class="{'has-text-warning': tmpDeptId === deptId, 'has-text-grey': tmpDeptId !== deptId}">{{ goal.progress.departmental_distribution[tmpDeptId]?.[currentTimeframe]?.count || 0 }} / {{ getDepartmentProgressTarget(goal, tmpDeptId) }} - <strong :class="{'has-text-warning': tmpDeptId === deptId, 'has-text-grey': tmpDeptId !== deptId}">{{ Math.min(Math.round(((goal.progress.departmental_distribution[tmpDeptId]?.[currentTimeframe]?.count || 0) / getDepartmentProgressTarget(goal, tmpDeptId)) * 100, true), 100) }}%</strong></span>
</div>
</template>
<!-- Total progress until now for all departments -->
<!-- Total progress in selected timeframe for all departments -->
<div class="is-flex is-justify-content-space-between mb-1">
<strong class="has-text-white">I alt</strong>
<span>{{ goal.progress[currentTimeframe]?.count || 0 }} / {{ goal.progress[currentTimeframe]?.target || (getDailyGoalDepartmentTarget(goal, deptId, true) * goal.departments.length) }} - <strong class="has-text-white">{{ getGoalProgress(goal) }}%</strong></span>
<span>{{ goal.progress[currentTimeframe]?.count || 0 }} / {{ goal.progress[currentTimeframe]?.target || goal.criteria.target }} - <strong class="has-text-white">{{ getGoalProgress(goal) }}%</strong></span>
</div>
<!-- Total divider -->
<div class="divider">I alt</div>
@@ -437,8 +449,8 @@ const getGoalApplicableDaysInPeriod = (goal, fromDate, toDate) => {
</template>
<template v-slot:default>
<span>
{{ Math.round(goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) }} / {{ Math.round(goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.target || getDailyGoalDepartmentTarget(goal, deptId, true)) }} -
<strong>{{ Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.target) * 100, true), 100) }}%</strong>
{{ Math.round(goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) }} / {{ Math.round(getDepartmentProgressTarget(goal, deptId)) }} -
<strong>{{ Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / getDepartmentProgressTarget(goal, deptId)) * 100, true), 100) }}%</strong>
</span>
</template>
</b-tooltip>
@@ -446,10 +458,10 @@ const getGoalApplicableDaysInPeriod = (goal, fromDate, toDate) => {
<progress
class="progress is-small mb-0"
:value="goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0"
:max="goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.target || getDailyGoalDepartmentTarget(goal, deptId, true)"
:class="getProgressClass(getDailyGoalDepartmentTarget(goal, deptId, true) === 0 ? 0 : Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.target) * 100, true), 100))"
:max="getDepartmentProgressTarget(goal, deptId)"
:class="getProgressClass(getDepartmentProgressTarget(goal, deptId) === 0 ? 0 : Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / getDepartmentProgressTarget(goal, deptId)) * 100, true), 100))"
>
{{ Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.target) * 100, true), 100) }}%
{{ Math.min(Math.round(((goal.progress.departmental_distribution[deptId]?.[currentTimeframe]?.count || 0) / getDepartmentProgressTarget(goal, deptId)) * 100, true), 100) }}%
</progress>
</div>
</div>