diff --git a/src/services/attendance.service.ts b/src/services/attendance.service.ts index 4fefd1b..848c3b6 100644 --- a/src/services/attendance.service.ts +++ b/src/services/attendance.service.ts @@ -356,15 +356,24 @@ export async function getWorkfund(year: number) { orderBy: { last_name: 'asc' }, }); + const now = new Date(); + const currentYear = now.getFullYear(); + const currentMonth = now.getMonth(); // 0-based + const maxMonth = year < currentYear ? 11 : (year === currentYear ? currentMonth : -1); + + if (maxMonth < 0) { + return { months: {}, users: users.map(u => ({ id: u.id, name: `${u.first_name} ${u.last_name}`.trim() })), balances: {} }; + } + const yearStart = new Date(year, 0, 1); - const yearEnd = new Date(year, 11, 31, 23, 59, 59); + const yearEnd = new Date(year, maxMonth + 1, 0, 23, 59, 59); const allRecords = await prisma.attendance.findMany({ where: { shift_date: { gte: yearStart, lte: yearEnd } }, }); const months: Record }> = {}; - for (let m = 0; m < 12; m++) { + for (let m = 0; m <= maxMonth; m++) { const bizDays = countWorkingDays(year, m); const fund = bizDays * 8; const monthStart = new Date(year, m, 1);