From a58dfcfa4a01fd62e8a180c4c99f4cf393c5a9dc Mon Sep 17 00:00:00 2001 From: Simon Franken Date: Tue, 24 Feb 2026 21:27:03 +0100 Subject: [PATCH] fix: clamp ongoing-period corrections to today to prevent future corrections inflating balance A correction dated in the future (within the current period) was being added to the balance immediately, while the corresponding expected hours were not yet counted (elapsed working days only go up to today). Fix: in the ongoing-period branch, sum only corrections whose date is <= today, matching the same window used for elapsed working days and tracked time. --- backend/src/services/clientTarget.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/src/services/clientTarget.service.ts b/backend/src/services/clientTarget.service.ts index 71793a4..db6a160 100644 --- a/backend/src/services/clientTarget.service.ts +++ b/backend/src/services/clientTarget.service.ts @@ -506,8 +506,19 @@ export class ClientTargetService { const elapsedWorkingDays = countWorkingDays(effectiveStart, elapsedEnd, workingDays); const expectedHours = elapsedWorkingDays * dailyRateHours; + // Only count corrections up to and including today — future corrections + // within the ongoing period must not be counted until those days have elapsed, + // otherwise a +8h correction for tomorrow inflates the balance immediately. + const correctionHoursToDate = target.corrections.reduce((sum, c) => { + const d = c.date.toISOString().split('T')[0]; + if (cmpDate(d, effectiveStart) >= 0 && cmpDate(d, today) <= 0) { + return sum + c.hours; + } + return sum; + }, 0); + balanceSeconds = Math.round( - (trackedSeconds + correctionHours * 3600) - expectedHours * 3600, + (trackedSeconds + correctionHoursToDate * 3600) - expectedHours * 3600, ); extra = {