refactor: calculate holidays through db procedures
This commit is contained in:
parent
67a90c356f
commit
4bdc29e283
|
@ -1,4 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('holidays', {
|
||||
|
@ -33,7 +34,8 @@ module.exports = Self => {
|
|||
Self.holidays = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
const args = ctx.args;
|
||||
|
||||
const conn = Self.dataSource.connector;
|
||||
const stmts = [];
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
|
@ -56,42 +58,6 @@ module.exports = Self => {
|
|||
ended.setHours(23, 59, 59, 59);
|
||||
|
||||
const filter = {
|
||||
include: [{
|
||||
relation: 'holidays',
|
||||
scope: {
|
||||
where: {year: args.year}
|
||||
}
|
||||
},
|
||||
{
|
||||
relation: 'absences',
|
||||
scope: {
|
||||
include: {
|
||||
relation: 'absenceType',
|
||||
},
|
||||
where: {
|
||||
dated: {between: [started, ended]}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
relation: 'workCenter',
|
||||
scope: {
|
||||
include: {
|
||||
relation: 'holidays',
|
||||
scope: {
|
||||
include: [{
|
||||
relation: 'detail'
|
||||
},
|
||||
{
|
||||
relation: 'type'
|
||||
}],
|
||||
where: {
|
||||
dated: {between: [started, ended]}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}],
|
||||
where: {
|
||||
and: [
|
||||
{workerFk: id},
|
||||
|
@ -107,76 +73,32 @@ module.exports = Self => {
|
|||
|
||||
}
|
||||
};
|
||||
if (args.businessFk)
|
||||
filter.where.and.push({businessFk: args.businessFk});
|
||||
|
||||
const contracts = await models.WorkerLabour.find(filter, myOptions);
|
||||
|
||||
let [firstContract] = contracts;
|
||||
let payedHolidays;
|
||||
const payedHolidays = firstContract.payedHolidays;
|
||||
|
||||
if (firstContract.payedHolidays)
|
||||
payedHolidays = firstContract.payedHolidays;
|
||||
else payedHolidays = 0;
|
||||
let queryIndex;
|
||||
const year = started.getFullYear();
|
||||
|
||||
let totalHolidays = 0;
|
||||
let holidaysEnjoyed = 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);
|
||||
|
||||
let startedTime;
|
||||
if (contractStarted < started)
|
||||
startedTime = started.getTime();
|
||||
else startedTime = contractStarted.getTime();
|
||||
|
||||
let endedTime;
|
||||
if (!contractEnded || (contractEnded && contractEnded > ended))
|
||||
endedTime = ended.getTime();
|
||||
else endedTime = contractEnded.getTime();
|
||||
|
||||
const dayTimestamp = 1000 * 60 * 60 * 24;
|
||||
|
||||
// Get number of worked days between dates
|
||||
let workedDays = Math.floor((endedTime - startedTime) / dayTimestamp);
|
||||
workedDays += 1; // 1 day inclusion
|
||||
|
||||
// Calculates absences
|
||||
let entitlementRate = 0;
|
||||
for (let absence of contract.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;
|
||||
}
|
||||
|
||||
workedDays -= entitlementRate;
|
||||
|
||||
// Max holidays for the selected year
|
||||
const maxHolidays = contract.holidays() && contract.holidays().days;
|
||||
|
||||
if (workedDays < daysInYear())
|
||||
totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
|
||||
else totalHolidays = maxHolidays;
|
||||
if (args.businessFk) {
|
||||
stmts.push(new ParameterizedSQL('CALL vn.workerCalendar_calculateBusiness(?,?)', [year, args.businessFk]));
|
||||
queryIndex = stmts.push('SELECT * FROM tmp.workerCalendarCalculateBusiness') - 1;
|
||||
stmts.push('DROP TEMPORARY TABLE tmp.workerCalendarCalculateBusiness');
|
||||
} else {
|
||||
stmts.push(new ParameterizedSQL('CALL vn.workerCalendar_calculateYear(?,?)', [year, id]));
|
||||
queryIndex = stmts.push('SELECT * FROM tmp.workerCalendarCalculateYear') - 1;
|
||||
stmts.push('DROP TEMPORARY TABLE tmp.workerCalendarCalculateYear');
|
||||
}
|
||||
|
||||
function daysInYear() {
|
||||
const year = started.getFullYear();
|
||||
const sql = ParameterizedSQL.join(stmts, ';');
|
||||
const result = await conn.executeStmt(sql, myOptions);
|
||||
const [holidays] = result[queryIndex];
|
||||
|
||||
return isLeapYear(year) ? 366 : 365;
|
||||
}
|
||||
return {totalHolidays, holidaysEnjoyed, payedHolidays};
|
||||
const totalHolidays = holidays.days;
|
||||
const holidaysEnjoyed = holidays.daysEnjoyed;
|
||||
const totalHours = holidays.hours;
|
||||
const hoursEnjoyed = holidays.hoursEnjoyed;
|
||||
|
||||
return {totalHolidays, holidaysEnjoyed, totalHours, hoursEnjoyed, payedHolidays};
|
||||
};
|
||||
|
||||
function isLeapYear(year) {
|
||||
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -25,20 +25,28 @@
|
|||
<div class="totalBox vn-mb-sm" style="text-align: center;">
|
||||
<h6>{{'Contract' | translate}} #{{$ctrl.businessId}}</h6>
|
||||
<div>
|
||||
{{'Used' | translate}} {{$ctrl.contractHolidays.holidaysEnjoyed}}
|
||||
{{'Used' | translate}} {{$ctrl.contractHolidays.holidaysEnjoyed || 0}}
|
||||
{{'of' | translate}} {{$ctrl.contractHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||
</div>
|
||||
<div>
|
||||
{{'Paid holidays' | translate}} {{$ctrl.contractHolidays.payedHolidays}} {{'days' | translate}}
|
||||
{{'Use' | translate}} {{$ctrl.contractHolidays.hoursEnjoyed || 0}}
|
||||
{{'of' | translate}} {{$ctrl.contractHolidays.totalHours || 0}} {{'hours' | translate}}
|
||||
</div>
|
||||
<div>
|
||||
{{'Paid holidays' | translate}} {{$ctrl.contractHolidays.payedHolidays || 0}} {{'days' | translate}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="totalBox" style="text-align: center;">
|
||||
<h6>{{'Year' | translate}} {{$ctrl.year}}</h6>
|
||||
<div>
|
||||
{{'Used' | translate}} {{$ctrl.yearHolidays.holidaysEnjoyed}}
|
||||
{{'Used' | translate}} {{$ctrl.yearHolidays.holidaysEnjoyed || 0}}
|
||||
{{'of' | translate}} {{$ctrl.yearHolidays.totalHolidays || 0}} {{'days' | translate}}
|
||||
</div>
|
||||
<div>
|
||||
{{'Use' | translate}} {{$ctrl.yearHolidays.hoursEnjoyed || 0}}
|
||||
{{'of' | translate}} {{$ctrl.yearHolidays.totalHours || 0}} {{'hours' | translate}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="vn-pt-md">
|
||||
|
|
|
@ -2,9 +2,11 @@ Calendar: Calendario
|
|||
Contract: Contrato
|
||||
Festive: Festivo
|
||||
Used: Utilizados
|
||||
Use: Utilizadas
|
||||
Year: Año
|
||||
of: de
|
||||
days: días
|
||||
hours: horas
|
||||
Choose an absence type from the right menu: Elige un tipo de ausencia desde el menú de la derecha
|
||||
To start adding absences, click an absence type from the right menu and then on the day you want to add an absence: Para empezar a añadir ausencias, haz clic en un tipo de ausencia desde el menu de la derecha y después en el día que quieres añadir la ausencia
|
||||
You can just add absences within the current year: Solo puedes añadir ausencias dentro del año actual
|
||||
|
|
Loading…
Reference in New Issue