round worker calendar holidays #1547
gitea/salix/dev This commit has test failures Details

This commit is contained in:
Joan Sanchez 2019-06-21 09:43:47 +02:00
parent dae43bb9b0
commit 340e7b8495
3 changed files with 17 additions and 32 deletions

View File

@ -1,5 +1,4 @@
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('absences', { Self.remoteMethodCtx('absences', {
@ -36,7 +35,7 @@ module.exports = Self => {
} }
}); });
Self.absences = async(ctx, workerFk, started, ended) => { Self.absences = async(ctx, workerFk, yearStarted, yearEnded) => {
const models = Self.app.models; const models = Self.app.models;
const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk); const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk);
@ -53,7 +52,7 @@ module.exports = Self => {
}, },
where: { where: {
workerFk: workerFk, workerFk: workerFk,
dated: {between: [started, ended]} dated: {between: [yearStarted, yearEnded]}
} }
}); });
@ -66,7 +65,7 @@ module.exports = Self => {
}); });
// Get active contracts on current year // Get active contracts on current year
const year = started.getFullYear(); const year = yearStarted.getFullYear();
const contracts = await models.WorkerLabour.find({ const contracts = await models.WorkerLabour.find({
include: [{ include: [{
relation: 'holidays', relation: 'holidays',
@ -87,7 +86,7 @@ module.exports = Self => {
relation: 'type' relation: 'type'
}], }],
where: { where: {
dated: {between: [started, ended]} dated: {between: [yearStarted, yearEnded]}
} }
} }
} }
@ -97,7 +96,7 @@ module.exports = Self => {
and: [ and: [
{workerFk: workerFk}, {workerFk: workerFk},
{or: [{ {or: [{
ended: {gte: [started]} ended: {gte: [yearStarted]}
}, {ended: null}]} }, {ended: null}]}
], ],
@ -106,13 +105,7 @@ module.exports = Self => {
// Get number of total holidays // Get number of total holidays
contracts.forEach(contract => { contracts.forEach(contract => {
let totalHolidays = contract.holidays().days; calendar.totalHolidays += getHolidaysByContract(contract, yearEnded);
if (contract.started && contract.ended)
totalHolidays = getHolidaysByContract(started, contract);
calendar.totalHolidays += totalHolidays;
let holidayList = contract.workCenter().holidays(); let holidayList = contract.workCenter().holidays();
for (let day of holidayList) { for (let day of holidayList) {
@ -126,19 +119,18 @@ module.exports = Self => {
return [calendar, absences, holidays]; return [calendar, absences, holidays];
}; };
function getHolidaysByContract(started, contract) { function getHolidaysByContract(contract, endOfYear) {
const dayTimestamp = 1000 * 60 * 60 * 24; const dayTimestamp = 1000 * 60 * 60 * 24;
const endedTime = contract.ended.getTime();
const started = contract.started;
const ended = contract.ended;
const startedTime = started.getTime(); const startedTime = started.getTime();
const endedTime = ended && ended.getTime() || endOfYear;
const contractDays = Math.floor((endedTime - startedTime) / dayTimestamp); const contractDays = Math.floor((endedTime - startedTime) / dayTimestamp);
if (contractDays < 365) { if (contractDays < 365) {
let holidays = contract.holidays().days * (contractDays + 1) / 365; let holidays = Math.round(2 * contract.holidays().days * (contractDays + 1) / 365) / 2;
let integerPart = parseInt(holidays);
let decimalPart = holidays - integerPart;
let decimal = decimalPart >= 0.5 ? 0.5 : 0;
holidays = integerPart + decimal;
return holidays; return holidays;
} }

View File

@ -43,7 +43,7 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays'); expect(sixthType).toEqual('Holidays');
}); });
it(`should fire the worker 106 on July and see he/she has 13.5`, 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 firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date(); const endedDate = new Date();
@ -71,7 +71,7 @@ describe('Worker absences()', () => {
let calendar = result[0]; let calendar = result[0];
let absences = result[1]; let absences = result[1];
expect(calendar.totalHolidays).toEqual(13.5); expect(calendar.totalHolidays).toEqual(14);
expect(calendar.holidaysEnjoyed).toEqual(5); expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;
@ -81,7 +81,7 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays'); 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 firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date(); const endedDate = new Date();
@ -109,7 +109,7 @@ describe('Worker absences()', () => {
let calendar = result[0]; let calendar = result[0];
let absences = result[1]; let absences = result[1];
expect(calendar.totalHolidays).toEqual(6.5); expect(calendar.totalHolidays).toEqual(7);
expect(calendar.holidaysEnjoyed).toEqual(5); expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;

View File

@ -14,12 +14,5 @@
"name": { "name": {
"type": "String" "type": "String"
} }
},
"relations": {
"father": {
"type": "belongsTo",
"model": "Department",
"foreignKey": "fatherFk"
}
} }
} }