158 lines
4.7 KiB
JavaScript
158 lines
4.7 KiB
JavaScript
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: 'businessFk',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'year',
|
|
type: 'date',
|
|
required: true,
|
|
}],
|
|
returns: [{
|
|
arg: 'calendar'
|
|
},
|
|
{
|
|
arg: 'absences',
|
|
type: 'number'
|
|
},
|
|
{
|
|
arg: 'holidays',
|
|
type: 'number'
|
|
}],
|
|
http: {
|
|
path: `/absences`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.absences = async(ctx, businessFk, year) => {
|
|
const models = Self.app.models;
|
|
const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
|
|
const holidays = [];
|
|
|
|
// Get active contracts on current year
|
|
// const year = yearStarted.getFullYear();
|
|
|
|
const started = new Date();
|
|
started.setFullYear(year);
|
|
started.setMonth(0);
|
|
started.setDate(1);
|
|
|
|
const ended = new Date();
|
|
ended.setFullYear(year);
|
|
ended.setMonth(12);
|
|
ended.setDate(0);
|
|
|
|
const contract = await models.WorkerLabour.findOne({
|
|
include: [{
|
|
relation: 'holidays',
|
|
scope: {
|
|
where: {year}
|
|
}
|
|
},
|
|
{
|
|
relation: 'workCenter',
|
|
scope: {
|
|
include: {
|
|
relation: 'holidays',
|
|
scope: {
|
|
include: [{
|
|
relation: 'detail'
|
|
},
|
|
{
|
|
relation: 'type'
|
|
}],
|
|
where: {
|
|
dated: {between: [started, ended]}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}],
|
|
where: {businessFk}
|
|
});
|
|
|
|
if (!contract) return;
|
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk);
|
|
if (!isSubordinate)
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
// Get absences of year
|
|
let absences = await Self.find({
|
|
include: {
|
|
relation: 'absenceType'
|
|
},
|
|
where: {
|
|
businessFk: contract.businessFk,
|
|
dated: {between: [started, ended]}
|
|
}
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
// Get number of worked days
|
|
let workedDays = 0;
|
|
const contractStarted = contract.started;
|
|
const contractEnded = contract.ended;
|
|
// esta mal, la fecha de inicio puede ser un año anterior...
|
|
const startedTime = contractStarted < started ? started.getTime() : contractStarted.getTime();
|
|
const endedTime = contractEnded && contractEnded.getTime() || ended;
|
|
const dayTimestamp = 1000 * 60 * 60 * 24;
|
|
|
|
workedDays += Math.floor((endedTime - startedTime) / dayTimestamp);
|
|
|
|
if (workedDays > daysInYear())
|
|
workedDays = daysInYear();
|
|
|
|
// Workcenter holidays
|
|
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);
|
|
}
|
|
|
|
const maxHolidays = contract.holidays() && contract.holidays().days;
|
|
calendar.totalHolidays = maxHolidays;
|
|
|
|
workedDays -= entitlementRate;
|
|
|
|
if (workedDays < daysInYear())
|
|
calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
|
|
|
function daysInYear() {
|
|
const year = started.getFullYear();
|
|
|
|
return isLeapYear(year) ? 366 : 365;
|
|
}
|
|
|
|
return [calendar, absences, holidays];
|
|
};
|
|
|
|
function isLeapYear(year) {
|
|
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
|
|
}
|
|
};
|