feat: enhance previous balance calculation to filter valid entries and adjust for current month
All checks were successful
Build and Push Docker Image / build (push) Successful in 25s
All checks were successful
Build and Push Docker Image / build (push) Successful in 25s
This commit is contained in:
@@ -1477,9 +1477,16 @@ async function calculatePreviousBalance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Filter out entries with invalid dates
|
||||
const validEntries = allEntries.filter(e => e.date && e.date.match(/^\d{4}-\d{2}-\d{2}$/));
|
||||
|
||||
if (validEntries.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find earliest date
|
||||
const earliestDate = allEntries.reduce((earliest, entry) => {
|
||||
const entryDate = new Date(entry.date);
|
||||
const earliestDate = validEntries.reduce((earliest, entry) => {
|
||||
const entryDate = new Date(entry.date + 'T00:00:00');
|
||||
return !earliest || entryDate < earliest ? entryDate : earliest;
|
||||
}, null);
|
||||
|
||||
@@ -1496,6 +1503,9 @@ async function calculatePreviousBalance() {
|
||||
let checkMonth = firstMonth;
|
||||
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0); // Normalize to start of day
|
||||
|
||||
console.log(`calculatePreviousBalance: Calculating from ${firstYear}-${firstMonth+1} to ${currentYear}-${currentMonth} (exclusive)`);
|
||||
|
||||
// Loop through all months from first entry until previous month of displayed month
|
||||
while (checkYear < currentYear || (checkYear === currentYear && checkMonth < currentMonth)) {
|
||||
@@ -1505,9 +1515,9 @@ async function calculatePreviousBalance() {
|
||||
|
||||
const entries = await fetchEntries(firstDay, lastDayStr);
|
||||
|
||||
// For past months, use full month. For current month (if displayed), limit to today
|
||||
const monthEnd = new Date(checkYear, checkMonth + 1, 0);
|
||||
const limitDate = monthEnd; // Always use full month for previous balance calculation
|
||||
// For past months (completed months), count ALL workdays
|
||||
// Only limit to today if this is the current calendar month
|
||||
const isCurrentCalendarMonth = (checkYear === today.getFullYear() && checkMonth === today.getMonth());
|
||||
|
||||
let workdaysPassed = 0;
|
||||
const monthLastDay = new Date(checkYear, checkMonth + 1, 0).getDate();
|
||||
@@ -1526,8 +1536,16 @@ async function calculatePreviousBalance() {
|
||||
.map(e => e.date)
|
||||
);
|
||||
|
||||
// Count workdays (all days for past months, up to today for current month)
|
||||
for (let day = 1; day <= monthLastDay; day++) {
|
||||
const dateObj = new Date(checkYear, checkMonth, day);
|
||||
dateObj.setHours(0, 0, 0, 0);
|
||||
|
||||
// For current calendar month, only count up to today
|
||||
if (isCurrentCalendarMonth && dateObj > today) {
|
||||
break;
|
||||
}
|
||||
|
||||
const year = dateObj.getFullYear();
|
||||
const month = String(dateObj.getMonth() + 1).padStart(2, '0');
|
||||
const dayStr = String(dateObj.getDate()).padStart(2, '0');
|
||||
@@ -1548,6 +1566,8 @@ async function calculatePreviousBalance() {
|
||||
const actualHours = entries.reduce((sum, entry) => sum + entry.netHours, 0);
|
||||
const monthBalance = actualHours - targetHours;
|
||||
|
||||
console.log(`Month ${checkYear}-${String(checkMonth+1).padStart(2, '0')}: ${entries.length} entries, ${workdaysPassed} workdays, ${actualHours.toFixed(1)}h actual, ${targetHours.toFixed(1)}h target, balance: ${monthBalance.toFixed(1)}h`);
|
||||
|
||||
totalBalance += monthBalance;
|
||||
|
||||
// Move to next month
|
||||
@@ -1558,6 +1578,7 @@ async function calculatePreviousBalance() {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Total previous balance: ${totalBalance.toFixed(1)}h`);
|
||||
return totalBalance;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user