2019-03-22 07:28:57 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('absences', {
|
|
|
|
description: 'Returns an array of absences from an specified worker',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'workerFk',
|
|
|
|
type: 'Number',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'started',
|
|
|
|
type: 'Date',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'ended',
|
|
|
|
type: 'Date',
|
|
|
|
required: true,
|
|
|
|
}],
|
|
|
|
returns: [{
|
|
|
|
arg: 'calendar'
|
|
|
|
},
|
|
|
|
{
|
2019-04-23 11:29:52 +00:00
|
|
|
arg: 'absences',
|
|
|
|
type: 'Number'
|
2019-03-22 07:28:57 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-23 11:29:52 +00:00
|
|
|
arg: 'holidays',
|
|
|
|
type: 'Number'
|
2019-03-22 07:28:57 +00:00
|
|
|
}],
|
|
|
|
http: {
|
|
|
|
path: `/absences`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-21 07:43:47 +00:00
|
|
|
Self.absences = async(ctx, workerFk, yearStarted, yearEnded) => {
|
2019-03-22 07:28:57 +00:00
|
|
|
const models = Self.app.models;
|
2019-05-17 11:27:51 +00:00
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk);
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2019-05-17 11:27:51 +00:00
|
|
|
if (!isSubordinate)
|
2019-03-22 07:28:57 +00:00
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2019-05-17 11:27:51 +00:00
|
|
|
const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
|
|
|
|
const holidays = [];
|
2019-03-22 07:28:57 +00:00
|
|
|
|
|
|
|
// Get active contracts on current year
|
2019-06-21 07:43:47 +00:00
|
|
|
const year = yearStarted.getFullYear();
|
2019-03-22 07:28:57 +00:00
|
|
|
const contracts = await models.WorkerLabour.find({
|
|
|
|
include: [{
|
|
|
|
relation: 'holidays',
|
|
|
|
scope: {
|
|
|
|
where: {year}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'workCenter',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'holidays',
|
|
|
|
scope: {
|
|
|
|
include: [{
|
|
|
|
relation: 'detail'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'type'
|
|
|
|
}],
|
|
|
|
where: {
|
2019-06-21 07:43:47 +00:00
|
|
|
dated: {between: [yearStarted, yearEnded]}
|
2019-03-22 07:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{workerFk: workerFk},
|
|
|
|
{or: [{
|
2019-06-21 07:43:47 +00:00
|
|
|
ended: {gte: [yearStarted]}
|
2019-03-22 07:28:57 +00:00
|
|
|
}, {ended: null}]}
|
|
|
|
],
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-10 12:29:25 +00:00
|
|
|
// Contracts ids
|
|
|
|
const contractsId = contracts.map(contract => {
|
|
|
|
return contract.businessFk;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get absences of year
|
|
|
|
let absences = await Self.find({
|
|
|
|
include: {
|
|
|
|
relation: 'absenceType'
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
businessFk: {inq: contractsId},
|
|
|
|
dated: {between: [yearStarted, yearEnded]}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let entitlementRate = 0;
|
|
|
|
absences.forEach(absence => {
|
|
|
|
const absenceType = absence.absenceType();
|
|
|
|
const isHoliday = absenceType.code === 'holiday';
|
|
|
|
const isHalfHoliday = absenceType.code === 'halfHoliday';
|
|
|
|
|
|
|
|
if (isHoliday)
|
|
|
|
calendar.holidaysEnjoyed += 1;
|
|
|
|
if (isHalfHoliday)
|
|
|
|
calendar.holidaysEnjoyed += 0.5;
|
|
|
|
|
|
|
|
entitlementRate += absenceType.holidayEntitlementRate;
|
|
|
|
|
|
|
|
absence.dated = new Date(absence.dated);
|
|
|
|
absence.dated.setHours(0, 0, 0, 0);
|
|
|
|
});
|
|
|
|
|
2019-11-15 09:44:11 +00:00
|
|
|
// Get number of worked days
|
|
|
|
let workedDays = 0;
|
2019-03-22 07:28:57 +00:00
|
|
|
contracts.forEach(contract => {
|
2019-11-15 09:44:11 +00:00
|
|
|
const started = contract.started;
|
|
|
|
const ended = contract.ended;
|
|
|
|
const startedTime = started.getTime();
|
|
|
|
const endedTime = ended && ended.getTime() || yearEnded;
|
|
|
|
const dayTimestamp = 1000 * 60 * 60 * 24;
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2019-11-15 09:44:11 +00:00
|
|
|
workedDays += Math.floor((endedTime - startedTime) / dayTimestamp);
|
|
|
|
|
2020-06-19 07:58:09 +00:00
|
|
|
if (workedDays > daysInYear())
|
|
|
|
workedDays = daysInYear();
|
|
|
|
|
2019-11-15 09:44:11 +00:00
|
|
|
// Workcenter holidays
|
2019-03-22 07:28:57 +00:00
|
|
|
let holidayList = contract.workCenter().holidays();
|
|
|
|
for (let day of holidayList) {
|
|
|
|
day.dated = new Date(day.dated);
|
|
|
|
day.dated.setHours(0, 0, 0, 0);
|
|
|
|
|
|
|
|
holidays.push(day);
|
|
|
|
}
|
|
|
|
});
|
2019-11-15 09:44:11 +00:00
|
|
|
const currentContract = contracts.find(contract => {
|
|
|
|
return contract.started <= new Date()
|
|
|
|
&& (contract.ended >= new Date() || contract.ended == null);
|
|
|
|
});
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2019-11-15 09:44:11 +00:00
|
|
|
if (currentContract) {
|
2019-11-20 13:22:44 +00:00
|
|
|
const maxHolidays = currentContract.holidays().days;
|
|
|
|
calendar.totalHolidays = maxHolidays;
|
2019-04-23 11:29:52 +00:00
|
|
|
|
2020-06-19 07:58:09 +00:00
|
|
|
workedDays -= entitlementRate;
|
|
|
|
|
|
|
|
if (workedDays < daysInYear())
|
|
|
|
calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function daysInYear() {
|
|
|
|
const year = yearStarted.getFullYear();
|
|
|
|
|
|
|
|
return isLeapYear(year) ? 366 : 365;
|
2019-04-23 11:29:52 +00:00
|
|
|
}
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2019-11-15 09:44:11 +00:00
|
|
|
return [calendar, absences, holidays];
|
|
|
|
};
|
2020-06-19 07:58:09 +00:00
|
|
|
|
|
|
|
function isLeapYear(year) {
|
|
|
|
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
|
|
|
|
}
|
2019-03-22 07:28:57 +00:00
|
|
|
};
|