salix/modules/worker/back/methods/worker-time-control/getHoursWorked.js

122 lines
5.0 KiB
JavaScript

/*
Author : Enrique Blasco BLanquer
Date: 28 de mayo de 2019
*/
module.exports = Self => {
Self.remoteMethodCtx('getHoursWorked', {
description: 'Get worked hours in current week, month and year',
accessType: 'WRITE',
returns: [{
type: 'Object',
root: true
}],
http: {
path: `/getHoursWorked`,
verb: 'GET'
}
});
Self.getHoursWorked = async(ctx, data) => {
let totalHours = 0; // total hours worked in one year
let totalMinutes = 0; // total minutes worked in one year
let totalHoursMonth = 0; // total hours worked in one month
let totalMinutesMonth = 0; // total minutes worked in one month
let totalHoursWeek = 0; // total hours worked in one week
let totalMinutesWeek = 0; // total minutes worked in one week
const myUserId = ctx.req.accessToken.userId; // user id
let today = new Date(); // needed to calculate total hours worked to current date
let fromDate = today.getFullYear() + '-01-01'; // from date, current year
let toDate = today.getFullYear() + '-12-31'; // to date, current year
// 1 hours worked in a year
let hoursYear = await Self.rawSql(`SELECT wtc.userFk, DATE(wtc.timed) dated,
UNIX_TIMESTAMP(MIN(timed))timedStart,
SEC_TO_TIME(SUM(if( mod(wtc.order,2)=1,
UNIX_TIMESTAMP(timed) *-1,
UNIX_TIMESTAMP(timed)))) timeWorkDay
FROM vn.workerTimeControl wtc
WHERE wtc.timed BETWEEN ? AND ? AND userFk = ?
GROUP BY wtc.userFk,dated ORDER BY dated DESC`, [fromDate, toDate, myUserId]);
// 2 Get days of week
let week = [];
// Starting Monday not Sunday
let current = new Date();
current.setDate((current.getDate() - current.getDay() + 1));
for (let i = 0; i < 7; i++) {
week.push(
new Date(current)
);
current.setDate(current.getDate() + 1);
}
// 3 I have all timed control for one year... NOW I CALCULATE TOTAL HOURS IN YEAR, MONTH, WEEK, Let's GO!
for (hour of hoursYear) {
if (parseInt(hour.timeWorkDay.split(':')[0]) > 0) {
// YEAR
totalHours += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutes += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutes += 20;
// MONTH
if ((new Date(hour.dated)).getMonth() == today.getMonth()) {
totalHoursMonth += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutesMonth += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutesMonth += 20;
}
// WEEK
for (day of week) {
let dayOfWeek = new Date(day);
let dayOfCurrentWeek = new Date(hour.dated);
if (dayOfWeek.getMonth() == dayOfCurrentWeek.getMonth() && dayOfWeek.getDate() == dayOfCurrentWeek.getDate()) {
totalHoursWeek += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutesWeek += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutesWeek += 20;
break;
}
}
}
}
// TOTAL WORKED HOURS IN THE YEAR
totalHours += totalMinutes / 60;
totalHours = decimalToHour(totalHours);
// TOTAL WORKED HOURS IN THE MONTH
totalHoursMonth += totalMinutesMonth / 60;
totalHoursMonth = decimalToHour(totalHoursMonth);
// TOTAL WORKED HOURS IN THE WEEK
totalHoursWeek += totalMinutesWeek / 60;
totalHoursWeek = decimalToHour(totalHoursWeek);
return {
'totalWorekdYear': totalHours,
'totalWorekdMonth': totalHoursMonth,
'totalWorkedWeek': totalHoursWeek
};
};
};
/*
function to calculate hours and minutes from decimal value
*/
function decimalToHour(value) {
let decimalTime = parseFloat(value);
decimalTime = decimalTime * 60 * 60;
let hoursDay = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hoursDay * 60 * 60);
let minutesDay = Math.floor((decimalTime / 60));
return hoursDay + ':' + minutesDay;
}