Refactor
This commit is contained in:
parent
b77ecc2c06
commit
c853cfc86f
|
@ -1,148 +0,0 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
|
||||||
|
|
||||||
module.exports = Self => {
|
|
||||||
Self.remoteMethodCtx('holidays', {
|
|
||||||
description: 'Returns data - Change me',
|
|
||||||
accepts: [{
|
|
||||||
arg: 'businessFk',
|
|
||||||
type: 'number',
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
arg: 'year',
|
|
||||||
type: 'date',
|
|
||||||
required: true,
|
|
||||||
}],
|
|
||||||
returns: [{
|
|
||||||
type: 'object',
|
|
||||||
root: true
|
|
||||||
}],
|
|
||||||
http: {
|
|
||||||
path: `/holidays`,
|
|
||||||
verb: 'GET'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Self.holidays = 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
|
|
||||||
const 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);
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -0,0 +1,213 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('holidays', {
|
||||||
|
description: 'Returns data - Change me',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'The worker id',
|
||||||
|
http: {source: 'path'}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'year',
|
||||||
|
type: 'date',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'businessFk',
|
||||||
|
type: 'number',
|
||||||
|
required: false
|
||||||
|
}],
|
||||||
|
returns: [{
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
}],
|
||||||
|
http: {
|
||||||
|
path: `/:id/holidays`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.holidays = async(ctx, id, year, businessFk) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
/* const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
|
||||||
|
const holidays = []; */
|
||||||
|
|
||||||
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
|
||||||
|
if (!isSubordinate)
|
||||||
|
throw new UserError(`You don't have enough privileges`);
|
||||||
|
|
||||||
|
// Get active contracts on current year
|
||||||
|
// const year = yearStarted.getFullYear();
|
||||||
|
|
||||||
|
const started = new Date();
|
||||||
|
started.setFullYear(year);
|
||||||
|
started.setMonth(0);
|
||||||
|
started.setDate(1);
|
||||||
|
started.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
const ended = new Date();
|
||||||
|
ended.setFullYear(year);
|
||||||
|
ended.setMonth(12);
|
||||||
|
ended.setDate(0);
|
||||||
|
ended.setHours(23, 59, 59, 59);
|
||||||
|
|
||||||
|
const filter = {
|
||||||
|
include: [{
|
||||||
|
relation: 'holidays',
|
||||||
|
scope: {
|
||||||
|
where: {year}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relation: 'workCenter',
|
||||||
|
scope: {
|
||||||
|
include: {
|
||||||
|
relation: 'holidays',
|
||||||
|
scope: {
|
||||||
|
include: [{
|
||||||
|
relation: 'detail'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relation: 'type'
|
||||||
|
}],
|
||||||
|
where: {
|
||||||
|
dated: {between: [started, ended]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
where: {
|
||||||
|
and: [
|
||||||
|
{workerFk: id},
|
||||||
|
{or: [{
|
||||||
|
ended: {gte: [started]}
|
||||||
|
}, {ended: null}]}
|
||||||
|
],
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (businessFk)
|
||||||
|
filter.where = {businessFk};
|
||||||
|
const contracts = await models.WorkerLabour.find(filter);
|
||||||
|
|
||||||
|
if (!contracts.length) return;
|
||||||
|
|
||||||
|
// Contracts ids
|
||||||
|
const contractsId = contracts.map(contract => contract.businessFk);
|
||||||
|
|
||||||
|
// Get absences of year
|
||||||
|
const absences = await models.Calendar.find({
|
||||||
|
include: {
|
||||||
|
relation: 'absenceType'
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
businessFk: {inq: contractsId},
|
||||||
|
dated: {between: [started, ended]}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let totalHolidays = 0;
|
||||||
|
let holidaysEnjoyed = 0;
|
||||||
|
let entitlementRate = 0;
|
||||||
|
for (let absence of absences) {
|
||||||
|
const absenceType = absence.absenceType();
|
||||||
|
const isHoliday = absenceType.code === 'holiday';
|
||||||
|
const isHalfHoliday = absenceType.code === 'halfHoliday';
|
||||||
|
|
||||||
|
if (isHoliday) holidaysEnjoyed += 1;
|
||||||
|
if (isHalfHoliday) holidaysEnjoyed += 0.5;
|
||||||
|
|
||||||
|
entitlementRate += absenceType.holidayEntitlementRate;
|
||||||
|
|
||||||
|
absence.dated = new Date(absence.dated); // not needed
|
||||||
|
absence.dated.setHours(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get number of worked days
|
||||||
|
// let totalWorkedDays = 0;
|
||||||
|
for (let contract of contracts) {
|
||||||
|
const contractStarted = contract.started;
|
||||||
|
contractStarted.setHours(0, 0, 0, 0);
|
||||||
|
const contractEnded = contract.ended;
|
||||||
|
if (contractEnded)
|
||||||
|
contractEnded.setHours(23, 59, 59, 59);
|
||||||
|
|
||||||
|
// 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.getTime();
|
||||||
|
const dayTimestamp = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
|
let workedDays = Math.floor((endedTime - startedTime) / dayTimestamp); // debería dar un computo de 366 dias
|
||||||
|
workedDays += 1; // 1 day inclusion
|
||||||
|
// workedDays -= entitlementRate;
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
// Set max holidays
|
||||||
|
// const maxHolidays = contract.holidays() && contract.holidays().days;
|
||||||
|
const maxHolidays = contract.holidays() && contract.holidays().days;
|
||||||
|
|
||||||
|
if (workedDays < daysInYear())
|
||||||
|
totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
||||||
|
|
||||||
|
// totalWorkedDays += workedDays;
|
||||||
|
}
|
||||||
|
|
||||||
|
function daysInYear() {
|
||||||
|
const year = started.getFullYear();
|
||||||
|
|
||||||
|
return isLeapYear(year) ? 366 : 365;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {totalHolidays, holidaysEnjoyed};
|
||||||
|
};
|
||||||
|
|
||||||
|
function isLeapYear(year) {
|
||||||
|
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 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}
|
||||||
|
}); */
|
||||||
|
};
|
|
@ -11,5 +11,6 @@ module.exports = Self => {
|
||||||
require('../methods/worker/activeWithRole')(Self);
|
require('../methods/worker/activeWithRole')(Self);
|
||||||
require('../methods/worker/activeWithInheritedRole')(Self);
|
require('../methods/worker/activeWithInheritedRole')(Self);
|
||||||
require('../methods/worker/contracts')(Self);
|
require('../methods/worker/contracts')(Self);
|
||||||
|
require('../methods/worker/holidays')(Self);
|
||||||
require('../methods/worker/activeContract')(Self);
|
require('../methods/worker/activeContract')(Self);
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,18 +23,18 @@
|
||||||
<vn-side-menu side="right">
|
<vn-side-menu side="right">
|
||||||
<div class="vn-pa-md">
|
<div class="vn-pa-md">
|
||||||
<div class="totalBox vn-mb-sm" style="text-align: center;">
|
<div class="totalBox vn-mb-sm" style="text-align: center;">
|
||||||
<h6 translate>Current contract</h6>
|
<h6>{{'Contract' | translate}} ID: {{$ctrl.businessId}}</h6>
|
||||||
<div>
|
<div>
|
||||||
{{'Used' | translate}} {{$ctrl.calendar.holidaysEnjoyed}}
|
{{'Used' | translate}} {{$ctrl.contractHolidays.holidaysEnjoyed}}
|
||||||
{{'of' | translate}} {{$ctrl.calendar.totalHolidays || 0}} {{'days' | translate}}
|
{{'of' | translate}} {{$ctrl.contractHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="totalBox" style="text-align: center;">
|
<div class="totalBox" style="text-align: center;">
|
||||||
<h6>{{'Year' | translate}} {{$ctrl.year}}</h6>
|
<h6>{{'Year' | translate}} {{$ctrl.year}}</h6>
|
||||||
<div>
|
<div>
|
||||||
{{'Used' | translate}} {{$ctrl.calendar.holidaysEnjoyed}}
|
{{'Used' | translate}} {{$ctrl.yearHolidays.holidaysEnjoyed}}
|
||||||
{{'of' | translate}} {{$ctrl.calendar.totalHolidays || 0}} {{'days' | translate}}
|
{{'of' | translate}} {{$ctrl.yearHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,10 @@ class Controller extends Section {
|
||||||
|
|
||||||
this.date = newYear;
|
this.date = newYear;
|
||||||
|
|
||||||
this.refresh().then(() => this.repaint());
|
this.refresh()
|
||||||
|
.then(() => this.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays());
|
||||||
}
|
}
|
||||||
|
|
||||||
get date() {
|
get date() {
|
||||||
|
@ -73,7 +76,9 @@ class Controller extends Section {
|
||||||
this._businessId = value;
|
this._businessId = value;
|
||||||
if (value) {
|
if (value) {
|
||||||
this.refresh()
|
this.refresh()
|
||||||
.then(() => this.repaint());
|
.then(() => this.repaint())
|
||||||
|
.then(() => this.getContractHolidays())
|
||||||
|
.then(() => this.getYearHolidays());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +106,24 @@ class Controller extends Section {
|
||||||
.then(res => this.businessId = res.data.businessFk);
|
.then(res => this.businessId = res.data.businessFk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getContractHolidays() {
|
||||||
|
this.getHolidays({
|
||||||
|
businessFk: this.businessId,
|
||||||
|
year: this.year
|
||||||
|
}, data => this.contractHolidays = data);
|
||||||
|
}
|
||||||
|
|
||||||
|
getYearHolidays() {
|
||||||
|
this.getHolidays({
|
||||||
|
year: this.year
|
||||||
|
}, data => this.yearHolidays = data);
|
||||||
|
}
|
||||||
|
|
||||||
|
getHolidays(params, cb) {
|
||||||
|
this.$http.get(`Workers/${this.worker.id}/holidays`, {params})
|
||||||
|
.then(res => cb(res.data));
|
||||||
|
}
|
||||||
|
|
||||||
onData(data) {
|
onData(data) {
|
||||||
this.events = {};
|
this.events = {};
|
||||||
this.calendar = data.calendar;
|
this.calendar = data.calendar;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Calendar: Calendario
|
Calendar: Calendario
|
||||||
Holidays: Vacaciones
|
Contract: Contrato
|
||||||
Festive: Festivo
|
Festive: Festivo
|
||||||
Used: Utilizados
|
Used: Utilizados
|
||||||
Year: Año
|
Year: Año
|
||||||
|
|
Loading…
Reference in New Issue