From c07beca6c2c35e271f4c6a59954589311be28d4f Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 21 Jun 2019 09:43:47 +0200 Subject: [PATCH] round worker calendar holidays #1547 --- .../back/methods/worker-calendar/absences.js | 33 ++++++++----------- .../worker-calendar/specs/absences.spec.js | 8 ++--- modules/worker/back/models/department.json | 7 ---- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/modules/worker/back/methods/worker-calendar/absences.js b/modules/worker/back/methods/worker-calendar/absences.js index db7318024..0dc3d9af0 100644 --- a/modules/worker/back/methods/worker-calendar/absences.js +++ b/modules/worker/back/methods/worker-calendar/absences.js @@ -36,7 +36,7 @@ module.exports = Self => { } }); - Self.absences = async(ctx, workerFk, started, ended) => { + Self.absences = async(ctx, workerFk, yearStarted, yearEnded) => { const models = Self.app.models; const conn = Self.dataSource.connector; const myUserId = ctx.req.accessToken.userId; @@ -68,7 +68,7 @@ module.exports = Self => { }, where: { workerFk: workerFk, - dated: {between: [started, ended]} + dated: {between: [yearStarted, yearEnded]} } }); @@ -81,7 +81,7 @@ module.exports = Self => { }); // Get active contracts on current year - const year = started.getFullYear(); + const year = yearStarted.getFullYear(); const contracts = await models.WorkerLabour.find({ include: [{ relation: 'holidays', @@ -102,7 +102,7 @@ module.exports = Self => { relation: 'type' }], where: { - dated: {between: [started, ended]} + dated: {between: [yearStarted, yearEnded]} } } } @@ -112,7 +112,7 @@ module.exports = Self => { and: [ {workerFk: workerFk}, {or: [{ - ended: {gte: [started]} + ended: {gte: [yearStarted]} }, {ended: null}]} ], @@ -121,13 +121,7 @@ module.exports = Self => { // Get number of total holidays contracts.forEach(contract => { - let totalHolidays = contract.holidays().days; - - if (contract.started && contract.ended) - totalHolidays = getHolidaysByContract(started, contract); - - calendar.totalHolidays += totalHolidays; - + calendar.totalHolidays += getHolidaysByContract(contract, yearEnded); let holidayList = contract.workCenter().holidays(); for (let day of holidayList) { @@ -141,19 +135,18 @@ module.exports = Self => { return [calendar, absences, holidays]; }; - function getHolidaysByContract(started, contract) { + function getHolidaysByContract(contract, endOfYear) { const dayTimestamp = 1000 * 60 * 60 * 24; - const endedTime = contract.ended.getTime(); + + 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 = contract.holidays().days * (contractDays + 1) / 365; - let integerPart = parseInt(holidays); - let decimalPart = holidays - integerPart; - let decimal = decimalPart >= 0.5 ? 0.5 : 0; - - holidays = integerPart + decimal; + let holidays = Math.round(2 * contract.holidays().days * (contractDays + 1) / 365) / 2; return holidays; } 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 d5a89c9ab..5627c4303 100644 --- a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js +++ b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js @@ -43,7 +43,7 @@ describe('Worker absences()', () => { expect(sixthType).toEqual('Holidays'); }); - it(`should fire the worker 106 on July and see he/she has 13.75`, async() => { + it(`should fire the worker 106 on July and see he/she has 14`, async() => { const firedWorker = await app.models.WorkerLabour.findById(106); const endedDate = new Date(); @@ -71,7 +71,7 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(15.5); + expect(calendar.totalHolidays).toEqual(14); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name; @@ -81,7 +81,7 @@ describe('Worker absences()', () => { expect(sixthType).toEqual('Holidays'); }); - it(`should fire the worker 106 on March and see he/she has 6.5`, async() => { + it(`should fire the worker 106 on March and see he/she has 7`, async() => { const firedWorker = await app.models.WorkerLabour.findById(106); const endedDate = new Date(); @@ -109,7 +109,7 @@ describe('Worker absences()', () => { let calendar = result[0]; let absences = result[1]; - expect(calendar.totalHolidays).toEqual(6.5); + expect(calendar.totalHolidays).toEqual(7); expect(calendar.holidaysEnjoyed).toEqual(5); let firstType = absences[0].absenceType().name; diff --git a/modules/worker/back/models/department.json b/modules/worker/back/models/department.json index c89a6ac4e..0b978b56c 100644 --- a/modules/worker/back/models/department.json +++ b/modules/worker/back/models/department.json @@ -14,12 +14,5 @@ "name": { "type": "String" } - }, - "relations": { - "father": { - "type": "belongsTo", - "model": "Department", - "foreignKey": "fatherFk" - } } }