diff --git a/modules/worker/back/methods/worker-calendar/absences.js b/modules/worker/back/methods/worker-calendar/absences.js index e822291b7..541f95ff5 100644 --- a/modules/worker/back/methods/worker-calendar/absences.js +++ b/modules/worker/back/methods/worker-calendar/absences.js @@ -108,10 +108,18 @@ module.exports = Self => { } }); - // Get number of total holidays + // Get number of worked days + let workedDays = 0; contracts.forEach(contract => { - calendar.totalHolidays += getHolidaysByContract(contract, yearEnded); + const started = contract.started; + const ended = contract.ended; + const startedTime = started.getTime(); + const endedTime = ended && ended.getTime() || yearEnded; + const dayTimestamp = 1000 * 60 * 60 * 24; + workedDays += Math.floor((endedTime - startedTime) / dayTimestamp); + + // Workcenter holidays let holidayList = contract.workCenter().holidays(); for (let day of holidayList) { day.dated = new Date(day.dated); @@ -120,26 +128,19 @@ module.exports = Self => { holidays.push(day); } }); + const currentContract = contracts.find(contract => { + return contract.started <= new Date() + && (contract.ended >= new Date() || contract.ended == null); + }); + + if (currentContract) { + const maxDays = currentContract.holidays().days; + calendar.totalHolidays = maxDays; + + if (workedDays < 365) + calendar.totalHolidays = Math.round(2 * maxDays * (workedDays + 1) / 365) / 2; + } return [calendar, absences, holidays]; }; - - function getHolidaysByContract(contract, endOfYear) { - const dayTimestamp = 1000 * 60 * 60 * 24; - - const started = contract.started; - const ended = contract.ended; - const startedTime = started.getTime(); - const endedTime = ended && ended.getTime() || endOfYear; - - const contractDays = Math.floor((endedTime - startedTime) / dayTimestamp); - - if (contractDays < 365) { - let holidays = Math.round(2 * contract.holidays().days * (contractDays + 1) / 365) / 2; - - return holidays; - } - - return contract.holidays().days; - } }; diff --git a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js index ccafa683d..0a5e92e1f 100644 --- a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js +++ b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js @@ -43,13 +43,13 @@ describe('Worker absences()', () => { expect(sixthType).toEqual('Holidays'); }); - it(`should fire the worker 106 on Juny and see he/she has 14`, async() => { + it(`should see he/she has 26.5`, async() => { let workerFk = 106; const firedWorker = await app.models.WorkerLabour.findById(workerFk); const endedDate = new Date(); - endedDate.setDate(30); - endedDate.setMonth(5); + endedDate.setDate(endedDate.getDate() + 1); + endedDate.setMonth(endedDate.getMonth() + 1); endedDate.setHours(0, 0, 0, 0); await firedWorker.updateAttributes({ended: endedDate}); @@ -71,7 +71,7 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(13.5); + expect(calendar.totalHolidays).toEqual(26.5); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name; @@ -81,13 +81,13 @@ describe('Worker absences()', () => { expect(sixthType).toEqual('Holidays'); }); - it(`should fire the worker 106 on March and see he/she has 7`, async() => { + it(`should see he/she has 26.5`, async() => { const firedWorker = await app.models.WorkerLabour.findById(106); const endedDate = new Date(); + endedDate.setDate(endedDate.getDate() + 1); + endedDate.setMonth(endedDate.getMonth() + 1); endedDate.setHours(0, 0, 0, 0); - endedDate.setMonth(2); - endedDate.setDate(31); await firedWorker.updateAttributes({ended: endedDate}); @@ -109,7 +109,7 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(7); + expect(calendar.totalHolidays).toEqual(26.5); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name; @@ -119,19 +119,13 @@ describe('Worker absences()', () => { expect(sixthType).toEqual('Holidays'); }); - it(`should fire the worker 106 on january and see he/she has x`, async() => { - const firedWorker = await app.models.WorkerLabour.findById(106); - - const endedDate = new Date(); - endedDate.setHours(0, 0, 0, 0); - endedDate.setMonth(0); - endedDate.setDate(28); - - await firedWorker.updateAttributes({ended: endedDate}); - + it(`should see he/she has 27.5`, async() => { let ctx = {req: {accessToken: {userId: 106}}}; let workerFk = 106; + const firedWorker = await app.models.WorkerLabour.findById(106); + await firedWorker.updateAttributes({ended: null}); + const started = new Date(); started.setHours(0, 0, 0, 0); started.setMonth(0); @@ -147,7 +141,7 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(2); + expect(calendar.totalHolidays).toEqual(27.5); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name;