round worker calendar holidays #1547
gitea/salix/master This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-06-21 09:43:47 +02:00
parent da01d52756
commit c07beca6c2
3 changed files with 17 additions and 31 deletions

View File

@ -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;
}

View File

@ -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;

View File

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