diff --git a/src/time-and-attendance/domains/services/holiday.service.ts b/src/time-and-attendance/domains/services/holiday.service.ts index d800510..925a38e 100644 --- a/src/time-and-attendance/domains/services/holiday.service.ts +++ b/src/time-and-attendance/domains/services/holiday.service.ts @@ -30,23 +30,16 @@ export class HolidayService { holiday_date: Date ): Promise> { try { - const valid_codes = ['G1', 'G43', 'G140', 'G104', 'G105', 'G305', 'G700', 'G720']; + const valid_codes = ['G1', 'G43', 'G48', 'G104', 'G105', 'G109', 'G140', 'G720']; const holiday_week_start = getWeekStart(holiday_date); const window_start = new Date(holiday_week_start.getTime() - 4 * MS_PER_WEEK); const window_end = new Date(holiday_week_start.getTime() - 1); const employee = await this.prisma.employees.findFirst({ - where: { - external_payroll_id, - company_code, - }, - select: { - id: true, - } + where: { external_payroll_id, company_code } }); - if (!employee) - return { success: false, error: 'EMPLOYEE_NOT_FOUND' }; + if (!employee) return { success: false, error: 'EMPLOYEE_NOT_FOUND' }; const shifts = await this.prisma.shifts.findMany({ where: { @@ -61,8 +54,7 @@ export class HolidayService { for (const shift of shifts) { const hours = computeHours(shift.start_time, shift.end_time); - if (hours <= 0) - continue; + if (hours <= 0) continue; const shift_week_start = getWeekStart(shift.date); const key = shift_week_start.getTime(); diff --git a/src/time-and-attendance/exports/csv-exports.utils.ts b/src/time-and-attendance/exports/csv-exports.utils.ts index c931396..f24661d 100644 --- a/src/time-and-attendance/exports/csv-exports.utils.ts +++ b/src/time-and-attendance/exports/csv-exports.utils.ts @@ -3,6 +3,9 @@ import { HolidayService } from "src/time-and-attendance/domains/services/holiday import { CsvRow, InternalCsvRow } from "src/time-and-attendance/exports/export-csv-options.dto"; const REGULAR = 1; +const EVENING = 140; +const EMERGENCY = 48; +const HOLIDAY = 104; const OVERTIME = 43; const VACATION = 109; const SICK = 105; @@ -73,26 +76,46 @@ export const applyOvertimeRequalifications = ( } for (const [, rows] of grouped_rows) { + const evening_hours = rows.find(r => r.code === EVENING); + const emergency_hours = rows.find(r => r.code === EMERGENCY); + const holiday_hours = rows.find(r => r.code === HOLIDAY); const regular_hours = rows.find(r => r.code === REGULAR); + const vacation_hours = rows.find(r => r.code === VACATION); // if no regular hours row, push as is if (!regular_hours?.quantite_hre) { result.push(...rows); continue; } - // calculate overtime directly from consolidated regular hours - const overtime_hours = Math.max(0, regular_hours.quantite_hre - WEEKLY_LIMIT_HOURS); + const total_hours = ( + regular_hours.quantite_hre + + (evening_hours?.quantite_hre ?? 0) + + (emergency_hours?.quantite_hre ?? 0) + + (holiday_hours?.quantite_hre ?? 0) + + (vacation_hours?.quantite_hre ?? 0) + ) + // calculate overtime directly from consolidated hours + const overtime_hours = Math.max(0, total_hours - WEEKLY_LIMIT_HOURS); // if no overtime, push as is if (overtime_hours <= 0) { result.push(...rows); continue; } - // ensures that its not possible to deduct more hours than the amount of regular hours - const deducted = Math.min(overtime_hours, regular_hours.quantite_hre); - const remaining = regular_hours.quantite_hre - deducted; + // ensures that its not possible to deduct more hours than the amount of regular or evening hours + const deducted_regular = Math.min(overtime_hours, regular_hours.quantite_hre); + const remaining_overtime = overtime_hours - deducted_regular; + const deducted_evening = Math.min(remaining_overtime, evening_hours?.quantite_hre ?? 0); + + const remaining_regular = (regular_hours.quantite_hre ?? 0) - deducted_regular; + const remaining_evening = (evening_hours?.quantite_hre ?? 0) - deducted_evening; for (const row of rows) { if (row === regular_hours) { // pushes the regular row with subtracted overtime hours, if enough hours remaining - if (remaining > 0) { - result.push({ ...regular_hours, quantite_hre: remaining }); + if (remaining_regular > 0) { + result.push({ ...regular_hours, quantite_hre: remaining_regular }); + } + } else if (row === evening_hours) { + // pushes the evening row with subtracted overtime hours, if enough not enough regular hours remaining + if (remaining_evening > 0) { + result.push({ ...evening_hours, quantite_hre: remaining_evening }); } } else { // other rows are left unchanged @@ -100,11 +123,13 @@ export const applyOvertimeRequalifications = ( } } //adds a new row with overtime hours deducted from the regular hours - result.push({ - ...regular_hours, - code: OVERTIME, - quantite_hre: deducted, - }); + if (deducted_regular + deducted_evening > 0) { + result.push({ + ...regular_hours, + code: OVERTIME, + quantite_hre: deducted_regular + deducted_evening, + }); + } } return result; }