salix/modules/worker/front/time-control/index.js

266 lines
7.5 KiB
JavaScript
Raw Normal View History

2019-11-05 10:57:05 +00:00
import Component from 'core/lib/component';
2019-05-17 11:27:51 +00:00
import ngModule from '../module';
import './style.scss';
2019-11-05 10:57:05 +00:00
class Controller extends Component {
constructor($element, $, vnWeekDays) {
super($element, $);
2019-05-17 11:27:51 +00:00
this.weekDays = [];
2019-10-23 15:38:35 +00:00
this.weekdayNames = vnWeekDays.locales;
2019-05-17 11:27:51 +00:00
}
2019-10-23 15:38:35 +00:00
$postLink() {
this.date = new Date();
}
2019-05-17 11:27:51 +00:00
/**
2019-10-23 15:38:35 +00:00
* The current selected date
2019-05-17 11:27:51 +00:00
*/
2019-10-23 15:38:35 +00:00
get date() {
return this._date;
2019-05-17 11:27:51 +00:00
}
2019-10-23 15:38:35 +00:00
set date(value) {
this._date = value;
value.setHours(0, 0, 0, 0);
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
let weekOffset = value.getDay() - 1;
if (weekOffset < 0) weekOffset = 6;
let started = new Date(value.getTime());
started.setDate(started.getDate() - weekOffset);
this.started = started;
let ended = new Date(started.getTime());
2019-11-05 07:59:48 +00:00
ended.setHours(23, 59, 59, 59);
ended.setDate(ended.getDate() + 6);
2019-10-23 15:38:35 +00:00
this.ended = ended;
2019-11-05 07:59:48 +00:00
this.weekDays = [];
let dayIndex = new Date(started.getTime());
while (dayIndex < ended) {
this.weekDays.push({
dated: new Date(dayIndex.getTime())
});
dayIndex.setDate(dayIndex.getDate() + 1);
}
2019-10-23 15:38:35 +00:00
this.fetchHours();
2019-05-17 11:27:51 +00:00
}
/**
2019-10-23 15:38:35 +00:00
* Worker hours data
2019-05-17 11:27:51 +00:00
*/
get hours() {
return this._hours;
}
set hours(value) {
this._hours = value;
2019-11-05 07:59:48 +00:00
for (const weekDay of this.weekDays) {
if (value) {
let day = weekDay.dated.getDay();
weekDay.hours = value
.filter(hour => new Date(hour.timed).getDay() == day)
.sort((a, b) => new Date(a.timed) - new Date(b.timed));
} else
weekDay.hours = null;
2019-05-17 11:27:51 +00:00
}
}
2019-10-23 15:38:35 +00:00
fetchHours() {
2019-11-05 10:57:05 +00:00
const params = {workerFk: this.$params.id};
2019-10-23 15:38:35 +00:00
const filter = {
where: {and: [
{timed: {gte: this.started}},
2019-11-05 07:59:48 +00:00
{timed: {lte: this.ended}}
2019-10-23 15:38:35 +00:00
]}
};
this.$.model.applyFilter(filter, params);
2019-11-05 07:59:48 +00:00
this.getAbsences();
this.getWorkedHours(this.started, this.ended);
2019-05-17 11:27:51 +00:00
}
2019-10-23 15:38:35 +00:00
hasEvents(day) {
return day >= this.started && day < this.ended;
2019-05-17 11:27:51 +00:00
}
2019-11-05 07:59:48 +00:00
getWorkedHours(from, to) {
2019-11-14 13:20:05 +00:00
this.weekTotalHours = null;
let weekTotalHours = 0;
2019-11-05 07:59:48 +00:00
let params = {
2019-11-05 10:57:05 +00:00
id: this.$params.id,
2019-11-05 07:59:48 +00:00
from: from,
to: to
};
2019-05-17 11:27:51 +00:00
2019-11-05 10:57:05 +00:00
const query = `Workers/${this.$params.id}/getWorkedHours`;
2019-11-05 07:59:48 +00:00
return this.$http.get(query, {params}).then(res => {
const workDays = res.data;
const map = new Map();
for (const workDay of workDays) {
workDay.dated = new Date(workDay.dated);
map.set(workDay.dated, workDay);
2019-11-14 13:20:05 +00:00
weekTotalHours += workDay.workedHours;
2019-11-05 07:59:48 +00:00
}
for (const weekDay of this.weekDays) {
const workDay = workDays.find(day => {
let from = new Date(day.dated);
from.setHours(0, 0, 0, 0);
let to = new Date(day.dated);
to.setHours(23, 59, 59, 59);
return weekDay.dated >= from && weekDay.dated <= to;
});
2019-11-14 13:20:05 +00:00
if (workDay) {
weekDay.expectedHours = workDay.expectedHours;
weekDay.workedHours = workDay.workedHours;
}
2019-11-05 07:59:48 +00:00
}
2019-11-14 13:20:05 +00:00
this.weekTotalHours = weekTotalHours;
2019-11-05 07:59:48 +00:00
});
}
2019-05-17 11:27:51 +00:00
2019-11-05 07:59:48 +00:00
getFinishTime() {
2019-11-05 10:57:05 +00:00
if (!this.weekDays) return;
let today = new Date();
today.setHours(0, 0, 0, 0);
2019-05-17 11:27:51 +00:00
2019-11-05 10:57:05 +00:00
let todayInWeek = this.weekDays.find(day => day.dated.getTime() === today.getTime());
2019-11-14 13:20:05 +00:00
if (todayInWeek && todayInWeek.hours && todayInWeek.hours.length) {
const remainingTime = todayInWeek.workedHours ? ((todayInWeek.expectedHours - todayInWeek.workedHours) * 1000) : null;
const lastKnownEntry = todayInWeek.hours[todayInWeek.hours.length - 1];
const lastKnownTime = new Date(lastKnownEntry.timed).getTime();
const finishTimeStamp = lastKnownTime && remainingTime ? lastKnownTime + remainingTime : null;
2019-05-17 11:27:51 +00:00
2019-11-14 13:20:05 +00:00
if (finishTimeStamp) {
let finishDate = new Date(finishTimeStamp);
let hour = finishDate.getHours();
let minute = finishDate.getMinutes();
2019-05-17 11:27:51 +00:00
2019-11-14 13:20:05 +00:00
if (hour < 10) hour = `0${hour}`;
if (minute < 10) minute = `0${minute}`;
2019-05-17 11:27:51 +00:00
2019-11-14 13:20:05 +00:00
return `${hour}:${minute} h.`;
}
2019-11-05 07:59:48 +00:00
}
2019-05-17 11:27:51 +00:00
}
2019-11-14 13:20:05 +00:00
set weekTotalHours(totalHours) {
if (!totalHours) return this._weekTotalHours = this.formatHours(0);
2019-05-17 11:27:51 +00:00
2019-11-14 13:20:05 +00:00
this._weekTotalHours = this.formatHours(totalHours);
}
2019-11-05 07:59:48 +00:00
2019-11-14 13:20:05 +00:00
get weekTotalHours() {
return this._weekTotalHours;
2019-05-17 11:27:51 +00:00
}
formatHours(timestamp) {
2019-11-05 07:59:48 +00:00
timestamp = timestamp || 0;
let hour = Math.floor(timestamp / 3600);
let min = Math.floor(timestamp / 60 - 60 * hour);
2019-05-17 11:27:51 +00:00
if (hour < 10) hour = `0${hour}`;
if (min < 10) min = `0${min}`;
return `${hour}:${min}`;
}
showAddTimeDialog(weekday) {
const timed = new Date(weekday.dated.getTime());
timed.setHours(0, 0, 0, 0);
2019-05-21 10:56:29 +00:00
this.newTime = timed;
2019-05-17 11:27:51 +00:00
this.selectedWeekday = weekday;
this.$.addTimeDialog.show();
}
addTime(response) {
2019-10-30 15:57:14 +00:00
if (response !== 'accept') return;
2019-10-23 15:38:35 +00:00
let data = {
2019-11-05 10:57:05 +00:00
workerFk: this.$params.id,
2019-10-23 15:38:35 +00:00
timed: this.newTime
};
2019-11-05 08:05:00 +00:00
this.$http.post(`WorkerTimeControls/addTimeEntry`, data)
2019-10-23 15:38:35 +00:00
.then(() => this.fetchHours());
2019-05-17 11:27:51 +00:00
}
2019-11-05 07:59:48 +00:00
showDeleteDialog(hour) {
this.timeEntryToDelete = hour;
this.$.deleteEntryDialog.show();
}
2019-11-05 10:57:05 +00:00
deleteTimeEntry() {
2019-11-05 07:59:48 +00:00
const entryId = this.timeEntryToDelete.id;
2019-11-05 10:57:05 +00:00
this.$http.post(`WorkerTimeControls/${entryId}/deleteTimeEntry`).then(() => {
2019-11-05 07:59:48 +00:00
this.fetchHours();
2019-11-05 10:57:05 +00:00
this.vnApp.showSuccess(this.$t('Entry removed'));
2019-11-05 07:59:48 +00:00
});
}
getAbsences() {
let params = {
2019-11-05 10:57:05 +00:00
workerFk: this.$params.id,
2019-11-05 07:59:48 +00:00
started: this.started,
ended: this.ended
};
2019-11-05 10:57:05 +00:00
return this.$http.get(`WorkerCalendars/absences`, {params})
2019-11-05 07:59:48 +00:00
.then(res => this.onData(res.data));
}
onData(data) {
const events = {};
let addEvent = (day, event) => {
events[new Date(day).getTime()] = event;
};
if (data.holidays) {
data.holidays.forEach(holiday => {
const holidayDetail = holiday.detail && holiday.detail.description;
const holidayType = holiday.type && holiday.type.name;
const holidayName = holidayDetail || holidayType;
addEvent(holiday.dated, {
name: holidayName,
color: '#ff0'
});
});
}
if (data.absences) {
data.absences.forEach(absence => {
const type = absence.absenceType;
addEvent(absence.dated, {
name: type.name,
color: type.rgb
});
});
}
this.weekDays.forEach(day => {
const timestamp = day.dated.getTime();
if (events[timestamp])
day.event = events[timestamp];
});
}
2019-05-17 11:27:51 +00:00
}
2019-11-05 10:57:05 +00:00
Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
2019-05-17 11:27:51 +00:00
ngModule.component('vnWorkerTimeControl', {
template: require('./index.html'),
2019-10-23 15:38:35 +00:00
controller: Controller
2019-05-17 11:27:51 +00:00
});