holidays fix #1853
gitea/salix/master This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-11-15 10:44:11 +01:00
parent dd17bb0386
commit e056184337
2 changed files with 35 additions and 40 deletions

View File

@ -108,10 +108,18 @@ module.exports = Self => {
} }
}); });
// Get number of total holidays // Get number of worked days
let workedDays = 0;
contracts.forEach(contract => { 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(); let holidayList = contract.workCenter().holidays();
for (let day of holidayList) { for (let day of holidayList) {
day.dated = new Date(day.dated); day.dated = new Date(day.dated);
@ -120,26 +128,19 @@ module.exports = Self => {
holidays.push(day); 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]; 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;
}
}; };

View File

@ -43,13 +43,13 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays'); 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; let workerFk = 106;
const firedWorker = await app.models.WorkerLabour.findById(workerFk); const firedWorker = await app.models.WorkerLabour.findById(workerFk);
const endedDate = new Date(); const endedDate = new Date();
endedDate.setDate(30); endedDate.setDate(endedDate.getDate() + 1);
endedDate.setMonth(5); endedDate.setMonth(endedDate.getMonth() + 1);
endedDate.setHours(0, 0, 0, 0); endedDate.setHours(0, 0, 0, 0);
await firedWorker.updateAttributes({ended: endedDate}); await firedWorker.updateAttributes({ended: endedDate});
@ -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(26.5);
expect(calendar.holidaysEnjoyed).toEqual(5); expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;
@ -81,13 +81,13 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays'); 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 firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date(); const endedDate = new Date();
endedDate.setDate(endedDate.getDate() + 1);
endedDate.setMonth(endedDate.getMonth() + 1);
endedDate.setHours(0, 0, 0, 0); endedDate.setHours(0, 0, 0, 0);
endedDate.setMonth(2);
endedDate.setDate(31);
await firedWorker.updateAttributes({ended: endedDate}); await firedWorker.updateAttributes({ended: endedDate});
@ -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(7); expect(calendar.totalHolidays).toEqual(26.5);
expect(calendar.holidaysEnjoyed).toEqual(5); expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;
@ -119,19 +119,13 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays'); expect(sixthType).toEqual('Holidays');
}); });
it(`should fire the worker 106 on january and see he/she has x`, async() => { it(`should see he/she has 27.5`, 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});
let ctx = {req: {accessToken: {userId: 106}}}; let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106; let workerFk = 106;
const firedWorker = await app.models.WorkerLabour.findById(106);
await firedWorker.updateAttributes({ended: null});
const started = new Date(); const started = new Date();
started.setHours(0, 0, 0, 0); started.setHours(0, 0, 0, 0);
started.setMonth(0); started.setMonth(0);
@ -147,7 +141,7 @@ describe('Worker absences()', () => {
let calendar = result[0]; let calendar = result[0];
let absences = result[1]; let absences = result[1];
expect(calendar.totalHolidays).toEqual(2); expect(calendar.totalHolidays).toEqual(27.5);
expect(calendar.holidaysEnjoyed).toEqual(5); expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;