targo-backend/src/time-and-attendance/exports/csv-exports.utils.ts

140 lines
4.7 KiB
TypeScript

import { HolidayService } from "src/time-and-attendance/domains/services/holiday.service";
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;
export const consolidateRowHoursAndAmountByType = (rows: InternalCsvRow[]): InternalCsvRow[] => {
const map = new Map<string, InternalCsvRow>();
for (const row of rows) {
if (row.code === VACATION) {
map.set(`${row.code}|${row.shift_date.toString()}|${row.timesheet_id}`, row);
} else {
const key = `${row.code}|${row.timesheet_id}`;
if (!map.has(key)) {
map.set(key, row);
} else {
const existing = map.get(key)!;
existing.quantite_hre = (existing.quantite_hre ?? 0) + (row.quantite_hre ?? 0);
existing.montant = (existing.montant ?? 0) + (row.montant ?? 0);
}
}
}
return Array.from(map.values());
}
export const applyHolidayRequalifications = async (
rows: InternalCsvRow[],
holiday_service: HolidayService,
holiday_bank_code: string,
): Promise<InternalCsvRow[]> => {
const result: InternalCsvRow[] = [];
const HOLIDAY_BANK_CODE = Number(holiday_bank_code.slice(1,));
for (const row of rows) {
if (row.code !== HOLIDAY_BANK_CODE) {
result.push(row);
continue;
}
if (!row.premier_jour_absence || !row.dernier_jour_absence || !row.employee_matricule || !row.compagnie_no) {
result.push(row);
continue;
}
const calculated = await holiday_service.calculateHolidayPay(row.employee_matricule, row.compagnie_no, new Date(row.premier_jour_absence));
if (!calculated.success) {
result.push({ ...row, quantite_hre: 0 });
continue;
}
result.push({ ...row, quantite_hre: calculated.data });
}
return result;
}
export const applyOvertimeRequalifications = async (
consolidated_rows: InternalCsvRow[],
overtime_service: OvertimeService,
): Promise<CsvRow[]> => {
const result: InternalCsvRow[] = [];
//grouped by timesheet and week number
const grouped_rows = new Map<string, InternalCsvRow[]>();
for (const row of consolidated_rows) {
const key = `${row.timesheet_id}|${row.semaine_no}`;
if (!grouped_rows.has(key)) {
grouped_rows.set(key, []);
}
grouped_rows.get(key)!.push({ ...row });
}
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"
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 regular_hours = rows.find(r => r.code === REGULAR);
if (!regular_hours || !regular_hours.quantite_hre) {
result.push(...rows);
continue;
}
const deducted = Math.min(overtime_hours, regular_hours.quantite_hre);
for (const row of rows) {
if (row === regular_hours) {
const remaining = regular_hours.quantite_hre - deducted;
if (remaining > 0) {
result.push({ ...regular_hours, quantite_hre: remaining });
}
} else {
result.push(row);
}
}
result.push({
...regular_hours,
code: OVERTIME,
quantite_hre: deducted,
});
}
return result;
}
export const resolveCompanyCodes = (companies: { targo: boolean; solucom: boolean; }): number[] => {
const out: number[] = [];
if (companies.targo) {
const code_no = 271583;
out.push(code_no);
}
if (companies.solucom) {
const code_no = 271585;
out.push(code_no);
}
return out;
}
export const computeWeekNumber = (start: Date, date: Date): number => {
const dayMS = 86400000;
const days = Math.floor((toUTC(date).getTime() - toUTC(start).getTime()) / dayMS);
return Math.floor(days / 7) + 1;
}
export const toUTC = (date: Date) => {
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
}
export const formatDate = (d: Date): string => {
return d.toISOString().split('T')[0];
}