Merge branch 'main' of https://git.targo.ca/Targo/targo_backend
This commit is contained in:
commit
9f0ce738c2
|
|
@ -30,23 +30,16 @@ export class HolidayService {
|
||||||
holiday_date: Date
|
holiday_date: Date
|
||||||
): Promise<Result<number, string>> {
|
): Promise<Result<number, string>> {
|
||||||
try {
|
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 holiday_week_start = getWeekStart(holiday_date);
|
||||||
const window_start = new Date(holiday_week_start.getTime() - 4 * MS_PER_WEEK);
|
const window_start = new Date(holiday_week_start.getTime() - 4 * MS_PER_WEEK);
|
||||||
const window_end = new Date(holiday_week_start.getTime() - 1);
|
const window_end = new Date(holiday_week_start.getTime() - 1);
|
||||||
|
|
||||||
const employee = await this.prisma.employees.findFirst({
|
const employee = await this.prisma.employees.findFirst({
|
||||||
where: {
|
where: { external_payroll_id, company_code }
|
||||||
external_payroll_id,
|
|
||||||
company_code,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!employee)
|
if (!employee) return { success: false, error: 'EMPLOYEE_NOT_FOUND' };
|
||||||
return { success: false, error: 'EMPLOYEE_NOT_FOUND' };
|
|
||||||
|
|
||||||
const shifts = await this.prisma.shifts.findMany({
|
const shifts = await this.prisma.shifts.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
|
@ -61,8 +54,7 @@ export class HolidayService {
|
||||||
for (const shift of shifts) {
|
for (const shift of shifts) {
|
||||||
const hours = computeHours(shift.start_time, shift.end_time);
|
const hours = computeHours(shift.start_time, shift.end_time);
|
||||||
|
|
||||||
if (hours <= 0)
|
if (hours <= 0) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
const shift_week_start = getWeekStart(shift.date);
|
const shift_week_start = getWeekStart(shift.date);
|
||||||
const key = shift_week_start.getTime();
|
const key = shift_week_start.getTime();
|
||||||
|
|
|
||||||
|
|
@ -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";
|
import { CsvRow, InternalCsvRow } from "src/time-and-attendance/exports/export-csv-options.dto";
|
||||||
|
|
||||||
const REGULAR = 1;
|
const REGULAR = 1;
|
||||||
|
const EVENING = 140;
|
||||||
|
const EMERGENCY = 48;
|
||||||
|
const HOLIDAY = 104;
|
||||||
const OVERTIME = 43;
|
const OVERTIME = 43;
|
||||||
const VACATION = 109;
|
const VACATION = 109;
|
||||||
const SICK = 105;
|
const SICK = 105;
|
||||||
|
|
@ -73,26 +76,46 @@ export const applyOvertimeRequalifications = (
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [, rows] of grouped_rows) {
|
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 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 no regular hours row, push as is
|
||||||
if (!regular_hours?.quantite_hre) { result.push(...rows); continue; }
|
if (!regular_hours?.quantite_hre) { result.push(...rows); continue; }
|
||||||
|
|
||||||
// calculate overtime directly from consolidated regular hours
|
const total_hours = (
|
||||||
const overtime_hours = Math.max(0, regular_hours.quantite_hre - WEEKLY_LIMIT_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 no overtime, push as is
|
||||||
if (overtime_hours <= 0) { result.push(...rows); continue; }
|
if (overtime_hours <= 0) { result.push(...rows); continue; }
|
||||||
|
|
||||||
// ensures that its not possible to deduct more hours than the amount of regular hours
|
// ensures that its not possible to deduct more hours than the amount of regular or evening hours
|
||||||
const deducted = Math.min(overtime_hours, regular_hours.quantite_hre);
|
const deducted_regular = Math.min(overtime_hours, regular_hours.quantite_hre);
|
||||||
const remaining = regular_hours.quantite_hre - deducted;
|
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) {
|
for (const row of rows) {
|
||||||
if (row === regular_hours) {
|
if (row === regular_hours) {
|
||||||
// pushes the regular row with subtracted overtime hours, if enough hours remaining
|
// pushes the regular row with subtracted overtime hours, if enough hours remaining
|
||||||
if (remaining > 0) {
|
if (remaining_regular > 0) {
|
||||||
result.push({ ...regular_hours, quantite_hre: remaining });
|
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 {
|
} else {
|
||||||
// other rows are left unchanged
|
// other rows are left unchanged
|
||||||
|
|
@ -100,12 +123,14 @@ export const applyOvertimeRequalifications = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//adds a new row with overtime hours deducted from the regular hours
|
//adds a new row with overtime hours deducted from the regular hours
|
||||||
|
if (deducted_regular + deducted_evening > 0) {
|
||||||
result.push({
|
result.push({
|
||||||
...regular_hours,
|
...regular_hours,
|
||||||
code: OVERTIME,
|
code: OVERTIME,
|
||||||
quantite_hre: deducted,
|
quantite_hre: deducted_regular + deducted_evening,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user