From 2930b60adc18f256201a80d8d47ad3565ede03a9 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 19 Jun 2020 09:58:09 +0200 Subject: [PATCH] Entitlement rate --- .../back/methods/worker-calendar/absences.js | 27 ++++++++++++++++--- modules/worker/back/models/absence-type.json | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/worker/back/methods/worker-calendar/absences.js b/modules/worker/back/methods/worker-calendar/absences.js index 2cef07ec5..7ea80509b 100644 --- a/modules/worker/back/methods/worker-calendar/absences.js +++ b/modules/worker/back/methods/worker-calendar/absences.js @@ -56,15 +56,19 @@ module.exports = Self => { } }); + let entitlementRate = 0; absences.forEach(absence => { - const isHoliday = absence.absenceType().code === 'holiday'; - const isHalfHoliday = absence.absenceType().code === 'halfHoliday'; + const absenceType = absence.absenceType(); + const isHoliday = absenceType.code === 'holiday'; + const isHalfHoliday = absenceType.code === 'halfHoliday'; if (isHoliday) calendar.holidaysEnjoyed += 1; if (isHalfHoliday) calendar.holidaysEnjoyed += 0.5; + entitlementRate += absenceType.holidayEntitlementRate; + absence.dated = new Date(absence.dated); absence.dated.setHours(0, 0, 0, 0); }); @@ -119,6 +123,9 @@ module.exports = Self => { workedDays += Math.floor((endedTime - startedTime) / dayTimestamp); + if (workedDays > daysInYear()) + workedDays = daysInYear(); + // Workcenter holidays let holidayList = contract.workCenter().holidays(); for (let day of holidayList) { @@ -137,10 +144,22 @@ module.exports = Self => { const maxHolidays = currentContract.holidays().days; calendar.totalHolidays = maxHolidays; - if (workedDays < 365) - calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / 365) / 2; + workedDays -= entitlementRate; + + if (workedDays < daysInYear()) + calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2; + } + + function daysInYear() { + const year = yearStarted.getFullYear(); + + return isLeapYear(year) ? 366 : 365; } return [calendar, absences, holidays]; }; + + function isLeapYear(year) { + return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); + } }; diff --git a/modules/worker/back/models/absence-type.json b/modules/worker/back/models/absence-type.json index a2fc2469d..f4e9aed18 100644 --- a/modules/worker/back/models/absence-type.json +++ b/modules/worker/back/models/absence-type.json @@ -19,6 +19,9 @@ }, "code": { "type": "String" + }, + "holidayEntitlementRate": { + "type": "Number" } }, "acls": [