worker-time-control/js/clockIn.js

242 lines
7.5 KiB
JavaScript

var userData = "";
$(document).ready(function () {
userData = $.parseJSON(localStorage.getItem("userData"));
FastClick.attach(document.body);
setView();
setEvents();
});
function setEvents() {
$(".btnSalir").on("click", function () {
cerrar();
});
$(".in").on("click", function () {
fichar('in');
});
$(".inMiddle").on("click", function () {
fichar('middle');
});
$(".outMiddle").on("click", function () {
fichar('middle');
});
$(".out").on("click", function () {
fichar('out');
});
setTimeout(function () {
cerrar();
}, 5000);
}
function setView() {
$(".footer").hide();
$("#txtNombre").text(userData["name"] + " " + userData["surname"]);
getInfo();
$("." + userData["button1"]).show();
$("." + userData["button2"]).show();
}
function fichar(direction) {
$.post({
urlPath: 'clockIn',
jsonData: [userData['userFk'], direction],
processData: false,
success: function (msg) {
if (msg[0].error){
printErrores(msg);
setTimeout(function () {
cerrar();
}, 3000);
}else {
$(".confirm").fadeIn(200);
$(".txtConfirm").append('FICHADA REGISTRADA');
setTimeout(function () {
cerrar();
}, 1000);
}
}
});
}
function setView() {
$(".footer").hide();
$(".in").hide();
$(".inMiddle").hide();
$(".outMiddle").hide();
$(".out").hide();
$("#txtNombre").text(userData["name"] + " " + userData["surname"]);
getInfo();
if(userData["button1"] === null && userData["button2"] === null ) {
printError ("Contacta con tu responsable")
} else {
$("." + userData["button1"]).show();
$("." + userData["button2"]).show();
}
}
function getInfo() {
$.post({
urlPath: 'getClockIn',
jsonData: [userData['userFk']],
processData: false,
success: function (data) {
$('.footer').show();
printTimetable(data);
}
});
}
function printTimetable(timetable) {
var listWeekName = printWeek();
var dated = new Date();
dated.setDate(dated.getDate() - 6);
for (var i = 0; i < 6; i++) {
$(".listHorario").append('<label>' + listWeekName[dated.getDay()] +'</label>');
dated.setDate(dated.getDate() + 1);
}
$(".listHorario").append('<label class="hoy">HOY</label>');
$(".listHorario").append('<li class="headerLi"></li>');
for (var i = 0; i < timetable.length; i++) {
$(".listHorario").append(
'<li>' +
'<div class="time ' + ifIsEmpty(timetable[i]["6daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["6daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["6daysAgo"]) + '</p>' +
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["5daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["5daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["5daysAgo"]) + '</p>'+
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["4daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["4daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["4daysAgo"]) + '</p>'+
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["3daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["3daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["3daysAgo"]) + '</p>'+
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["2daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["2daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["2daysAgo"]) + '</p>'+
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["1daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["1daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["1daysAgo"]) + '</p>'+
'</div>' +
'<div class="time ' + ifIsEmpty(timetable[i]["0daysAgo"]) + '">' +
'<img src="' + ifIsEmptyImage(timetable[i]["0daysAgoDirection"]) + '" />' +
'<p>' + ifIsEmptyText(timetable[i]["0daysAgo"]) + '</p>'+
'</div>' +
'</li>'
);
}
printTotalHours(timetable);
}
function printTotalHours(timetable) {
if(timetable.length>0) {
$(".listHorario").append('<li class="headerLi"></li>');
$(".listHorario").append('<li><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["6daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["5daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["4daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["3daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["2daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["1daysAgoTotal"]))
+ '</div><div class="time">' + secondsToHm(ifIsEmptyText(timetable[0]["0daysAgoTotal"]))
+ '</div></li>');
$(".total").text('Total: ' +
secondsToHm(Number(timetable[0]["6daysAgoTotal"] == null)+
Number(timetable[0]["5daysAgoTotal"])+
Number(timetable[0]["4daysAgoTotal"]) +
Number(timetable[0]["3daysAgoTotal"]) +
Number(timetable[0]["2daysAgoTotal"]) +
Number(timetable[0]["1daysAgoTotal"]) +
Number(timetable[0]["0daysAgoTotal"])
) + 'h');
} else{
$(".total").text('Total: 0h ');
}
}
function printWeek() {
var listWeekName = [];
var curr = new Date();
for (var i = 0; i < 7; i++) {
curr.setDate(curr.getDate() - 1);
listWeekName.push(weekDays[curr.getDay()]);
}
return listWeekName;
}
var weekDays = [
'Domingo',
'Sábado',
'Viernes',
'Jueves',
'Miércoles',
'Martes',
'Lunes'
];
function printErrores(errores) {
var error ='';
for (var i = 0; i < errores.length; i++) {
if (errores[i].error) {
error += errores[i].error + "<br>";
}
}
printError(error);
}
function ifIsEmpty(value) {
if (value.trim().length === 0) {
return "hide";
} else {
return "show";
}
}
function ifIsEmptyImage(value) {
if (value.trim().length === 0) {
return "img/in.png";
} else {
return "img/" + value + ".png";
}
}
function ifIsEmptyText(value) {
if (value.trim().length === 0) {
return "00:00";
} else {
return value;
}
}
function cerrar(){
$(".loading").fadeIn(200);
localStorage.removeItem("userData");
setTimeout(function () {
window.location='index.html';
}, 200);
}
function secondsToHm(seconds) {
seconds = Number(seconds);
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor(seconds % 3600 / 60);
return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
}