diff --git a/src/time-and-attendance/domains/services/overtime.service.ts b/src/time-and-attendance/domains/services/overtime.service.ts index 60f4311..fc38f71 100644 --- a/src/time-and-attendance/domains/services/overtime.service.ts +++ b/src/time-and-attendance/domains/services/overtime.service.ts @@ -69,8 +69,8 @@ export class OvertimeService { const day = new Date(week_start.getTime() + i * 24 * 60 * 60 * 1000); days.push(day.toISOString().slice(0, 10)); } - - const week_total_hours = [...day_totals.values()].reduce((a, b) => a + b, 0); + //allows the weekly overtime to be calculate skiping the daily overtime + const week_total_hours = [...day_totals.values()].reduce((a, b) => a + Math.min(b, DAILY_LIMIT_HOURS), 0); const weekly_overtime = Math.max(0, week_total_hours - WEEKLY_LIMIT_HOURS); let running = 0; @@ -93,9 +93,10 @@ export class OvertimeService { }); daily_kept_sum += daily_kept; - running += day_hours; + // Math.min to use only the regular hours in the weekly overtime hours calcul + running += Math.min(day_hours, DAILY_LIMIT_HOURS); } - const total_overtime = weekly_overtime + daily_kept_sum; + const total_overtime = Math.max(0, weekly_overtime - daily_kept_sum); return { success: true, diff --git a/src/time-and-attendance/exports/csv-exports.utils.ts b/src/time-and-attendance/exports/csv-exports.utils.ts index ea30769..19daff4 100644 --- a/src/time-and-attendance/exports/csv-exports.utils.ts +++ b/src/time-and-attendance/exports/csv-exports.utils.ts @@ -2,7 +2,6 @@ import { HolidayService } from "src/time-and-attendance/domains/services/holiday import { OvertimeService } from "src/time-and-attendance/domains/services/overtime.service"; import { CsvRow, InternalCsvRow } from "src/time-and-attendance/exports/export-csv-options.dto"; -// You made a helper to pull bank codes from the db, but omitted to use it here... curious. const REGULAR = 1; const OVERTIME = 43; const VACATION = 109; @@ -62,9 +61,9 @@ export const applyOvertimeRequalifications = async ( overtime_service: OvertimeService, ): Promise => { const result: InternalCsvRow[] = []; - //grouped by timesheet and week number - const grouped_rows = new Map(); + //regroup rows by timesheet and week number + const grouped_rows = new Map(); for (const row of consolidated_rows) { const key = `${row.timesheet_id}|${row.semaine_no}`; if (!grouped_rows.has(key)) { @@ -74,35 +73,40 @@ export const applyOvertimeRequalifications = async ( } for (const [, rows] of grouped_rows) { - //serves only to get the right timesheet_id and a date to find the "week_start of the getWeekOvertimeSummary" + //use the 1st row to determine wich timesheet and wich week number const representative = rows[0]; - const summary = await overtime_service.getWeekOvertimeSummary(representative.timesheet_id, representative.shift_date); - if (!summary.success || summary.data.total_overtime <= 0) { - result.push(...rows); - continue; - } - - const overtime_hours = summary.data.total_overtime; + const summary = await overtime_service.getWeekOvertimeSummary( + representative.timesheet_id, + representative.shift_date + ); + //if it fails, push to rows as is + if (!summary.success) { result.push(...rows); continue; }; + //combine daily and weekly overtime hours + const overtime_hours = summary.data.total_overtime + summary.data.daily_overtime_kept; + //if no overtime, push as is + if (overtime_hours <= 0) { result.push(...rows); continue; }; + + //search for regular hour rows and deduct overtime hours from it const regular_hours = rows.find(r => r.code === REGULAR); - if (!regular_hours || !regular_hours.quantite_hre) { - result.push(...rows); - continue; - } + if (!regular_hours?.quantite_hre) { 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; for (const row of rows) { if (row === regular_hours) { - const remaining = regular_hours.quantite_hre - deducted; + //pushes the regular rows with subtracted overtime hours, if enough hours available if (remaining > 0) { result.push({ ...regular_hours, quantite_hre: remaining }); } } else { + //other rows are left unchanged result.push(row); } } - + //adds a new row with overtime hours deducted from the regular hours result.push({ ...regular_hours, code: OVERTIME,