2023-11-30 08:25:32 +00:00
|
|
|
let userData = "";
|
2024-06-18 15:00:09 +00:00
|
|
|
const footer = document.querySelector("footer");
|
2024-06-18 14:39:32 +00:00
|
|
|
const txtName = document.querySelector("#txtNombre");
|
|
|
|
const inBtn = document.querySelector(".in");
|
|
|
|
const inMiddleBtn = document.querySelector(".inMiddle");
|
|
|
|
const outMiddleBtn = document.querySelector(".outMiddle");
|
|
|
|
const outBtn = document.querySelector(".out");
|
|
|
|
const timetableList = document.querySelector(".listHorario");
|
|
|
|
const weekDays = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
2023-11-28 13:50:11 +00:00
|
|
|
userData = JSON.parse(localStorage.getItem("userData"));
|
2021-10-19 08:52:45 +00:00
|
|
|
setView();
|
|
|
|
setEvents();
|
|
|
|
});
|
|
|
|
|
|
|
|
function setEvents() {
|
2024-06-18 14:39:32 +00:00
|
|
|
document.querySelector(".btnSalir").addEventListener("click", close);
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
inBtn.addEventListener("click", () => clockIn("in"));
|
|
|
|
inMiddleBtn.addEventListener("click", () => clockIn("middle"));
|
|
|
|
outMiddleBtn.addEventListener("click", () => clockIn("middle"));
|
|
|
|
outBtn.addEventListener("click", () => clockIn("out"));
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-19 09:40:30 +00:00
|
|
|
setTimeout(close, 5000);
|
2024-06-18 14:39:32 +00:00
|
|
|
}
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
async function clockIn(direction) {
|
|
|
|
let timeout = 1000;
|
|
|
|
const data = await call("WorkerTimeControls/clockIn", {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
workerFk: userData["userFk"],
|
|
|
|
direction,
|
|
|
|
device: localStorage.getItem("device"),
|
|
|
|
},
|
2021-10-19 08:52:45 +00:00
|
|
|
});
|
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
if (data.error) {
|
|
|
|
let msg = "";
|
|
|
|
for (const val of data) if (val.error) msg += `${val.error}\n`;
|
|
|
|
printError(msg);
|
|
|
|
} else {
|
|
|
|
document.querySelector(".confirm").classList.add("fade-in");
|
|
|
|
txtConfirm.textContent = "Fichada registrada correctamente";
|
|
|
|
timeout = 2000;
|
|
|
|
}
|
2022-05-24 06:01:06 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
setTimeout(close, timeout);
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setView() {
|
2024-06-18 15:00:09 +00:00
|
|
|
footer.style.hidden = true;
|
2024-06-18 14:39:32 +00:00
|
|
|
inBtn.style.display = "none";
|
|
|
|
inMiddleBtn.style.display = "none";
|
|
|
|
outMiddleBtn.style.display = "none";
|
|
|
|
outBtn.style.display = "none";
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
txtName.textContent = `${userData.name} ${userData.surname}`;
|
|
|
|
getInfo();
|
2023-11-28 13:50:11 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
if (userData.button1 === null && userData.button2 === null) return printError("Contacta con tu responsable");
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
document.querySelector(`.${userData.button1}`).style.display = "inline-block";
|
|
|
|
if (userData.button2) document.querySelector(`.${userData.button2}`).style.display = "inline-block";
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
async function getInfo() {
|
|
|
|
const data = await call("WorkerTimeControls/getClockIn", { params: { workerFk: userData["userFk"] } });
|
2024-06-18 15:00:09 +00:00
|
|
|
footer.style.hidden = false;
|
2024-06-18 14:39:32 +00:00
|
|
|
printTimetable(data);
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 13:28:03 +00:00
|
|
|
function printTimetable(timetable) {
|
2024-06-18 14:39:32 +00:00
|
|
|
const dated = new Date();
|
2022-05-23 13:28:03 +00:00
|
|
|
dated.setDate(dated.getDate() - 6);
|
|
|
|
|
2024-06-18 15:00:09 +00:00
|
|
|
const table = document.createDocumentFragment();
|
2024-06-18 14:39:32 +00:00
|
|
|
const totalEl = document.querySelector(".total");
|
|
|
|
let totalHr = 0;
|
|
|
|
|
|
|
|
for (let day = 0; day < weekDays.length - 1; day++) {
|
|
|
|
const label = createElement("label", { text: weekDays[dated.getDay()] });
|
2024-06-18 15:00:09 +00:00
|
|
|
table.append(label);
|
2022-05-23 13:28:03 +00:00
|
|
|
dated.setDate(dated.getDate() + 1);
|
|
|
|
}
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 15:00:09 +00:00
|
|
|
table.append(createElement("label", { text: "HOY", classes: ["hoy"] }), createElement("li", { classes: ["hrTop"] }));
|
2022-05-23 13:28:03 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
const liContent = createElement("li", {});
|
|
|
|
const liTotals = createElement("li", {});
|
|
|
|
timetable.forEach((row, index) => {
|
|
|
|
for (let day = weekDays.length - 1; day >= 0; day--) {
|
|
|
|
const img = createElement("img", { attrs: { src: ifIsEmptyImage(row[`${day}daysAgoDirection`]) } });
|
|
|
|
const p = createElement("p", { text: row[`${day}daysAgo`] });
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
const innerDiv = createElement("div", { childs: [img, p] });
|
|
|
|
const outerDiv = createElement("div", { classes: ["time", ifIsEmpty(row[`${day}daysAgo`])], childs: [innerDiv] });
|
|
|
|
liContent.append(outerDiv);
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
const div = createElement("div", { classes: ["time"], text: secondsToHm(timetable[0][`${day}daysAgoTotal`]) });
|
|
|
|
liTotals.append(div);
|
|
|
|
totalHr += timetable[0][`${day}daysAgoTotal`] || 0;
|
|
|
|
}
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
2024-06-18 15:00:09 +00:00
|
|
|
table.append(liContent);
|
|
|
|
if (index === timetable.length - 1) table.append(createElement("hr", {}), liTotals);
|
2024-06-18 14:39:32 +00:00
|
|
|
});
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
totalEl.innerText = `Total: ${totalHr ? secondsToHm(totalHr) : 0} h`;
|
2024-06-18 15:00:09 +00:00
|
|
|
timetableList.append(table);
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
const ifIsEmpty = (value) => (value?.trim() ? "show" : "hide");
|
2022-05-23 13:28:03 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
const ifIsEmptyImage = (value) => (value?.trim() ? `img/${value}.svg` : "img/in.svg");
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-18 14:39:32 +00:00
|
|
|
function close() {
|
2021-10-19 08:52:45 +00:00
|
|
|
localStorage.removeItem("userData");
|
2024-06-18 14:39:32 +00:00
|
|
|
setTimeout(() => (window.location = "index.html"), 200);
|
2022-05-23 13:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function secondsToHm(seconds) {
|
2024-06-18 14:39:32 +00:00
|
|
|
seconds = +seconds;
|
2022-05-23 13:28:03 +00:00
|
|
|
let hours = Math.floor(seconds / 3600);
|
2024-06-18 14:39:32 +00:00
|
|
|
let minutes = Math.floor((seconds % 3600) / 60);
|
|
|
|
return hours.toString().padStart(2, "0") + ":" + minutes.toString().padStart(2, "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
function createElement(tag, { classes = [], text, attrs = {}, childs = [] }) {
|
|
|
|
const el = document.createElement(tag);
|
|
|
|
if (classes) el.classList.add(...classes);
|
|
|
|
if (text) el.textContent = text;
|
|
|
|
for (const [key, value] of Object.entries(attrs)) el.setAttribute(key, value);
|
|
|
|
if (childs) el.append(...childs);
|
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|