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

173 lines
4.3 KiB
JavaScript

import ngModule from '../module';
import './style.scss';
class Controller {
constructor($scope, $http, $stateParams, $element, vnWeekDays) {
this.$stateParams = $stateParams;
this.$ = $scope;
this.$http = $http;
this.$element = $element;
this.weekDays = [];
this.weekdayNames = vnWeekDays.locales;
}
$postLink() {
this.date = new Date();
}
/**
* The current selected date
*/
get date() {
return this._date;
}
set date(value) {
this._date = value;
value.setHours(0, 0, 0, 0);
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());
ended.setDate(ended.getDate() + 7);
this.ended = ended;
this.fetchHours();
}
/**
* Worker hours data
*/
get hours() {
return this._hours;
}
set hours(value) {
this._hours = value;
this.weekDays = [];
if (!this.hours) return;
let dayIndex = new Date(this.started.getTime());
while (dayIndex < this.ended) {
let weekDay = dayIndex.getDay();
let hours = this.hours
.filter(hour => new Date(hour.timed).getDay() == weekDay)
.sort((a, b) => new Date(a.timed) - new Date(b.timed));
this.weekDays.push({
dated: new Date(dayIndex.getTime()),
hours
});
dayIndex.setDate(dayIndex.getDate() + 1);
}
}
fetchHours() {
const params = {workerFk: this.$stateParams.id};
const filter = {
where: {and: [
{timed: {gte: this.started}},
{timed: {lt: this.ended}}
]}
};
this.$.model.applyFilter(filter, params);
}
hasEvents(day) {
return day >= this.started && day < this.ended;
}
hourColor(weekDay) {
return weekDay.manual ? 'alert' : 'warning';
}
getWeekdayTotalHours(weekday) {
if (weekday.hours.length == 0) return 0;
const hours = weekday.hours;
let totalStamp = 0;
hours.forEach((hour, index) => {
let currentHour = new Date(hour.timed);
let previousHour = new Date(hour.timed);
if (index > 0 && (index % 2 == 1))
previousHour = new Date(hours[index - 1].timed);
const dif = Math.abs(previousHour - currentHour);
totalStamp += dif;
});
if (totalStamp / 3600 / 1000 > 5)
totalStamp += (20 * 60 * 1000);
weekday.total = totalStamp;
return this.formatHours(totalStamp);
}
get weekTotalHours() {
let total = 0;
this.weekDays.forEach(weekday => {
if (weekday.total)
total += weekday.total;
});
return this.formatHours(total);
}
formatHours(timestamp) {
let hour = Math.floor(timestamp / 3600 / 1000);
let min = Math.floor(timestamp / 60 / 1000 - 60 * hour);
if (hour < 10) hour = `0${hour}`;
if (min < 10) min = `0${min}`;
return `${hour}:${min}`;
}
showAddTimeDialog(weekday) {
const timed = new Date(weekday.dated);
const now = new Date();
now.setMonth(timed.getMonth());
now.setDate(timed.getDate());
now.setHours(0, 0, 0, 0);
this.newTime = now;
this.selectedWeekday = weekday;
this.$.addTimeDialog.show();
const selector = '[vn-id=addTimeDialog] input[type=time]';
const input = this.$element[0].querySelector(selector);
input.focus();
}
addTime(response) {
if (response !== 'ACCEPT') return;
let data = {
workerFk: this.$stateParams.id,
timed: this.newTime
};
this.$http.post(`api/WorkerTimeControls/addTime`, data)
.then(() => this.fetchHours());
}
}
Controller.$inject = ['$scope', '$http', '$stateParams', '$element', 'vnWeekDays'];
ngModule.component('vnWorkerTimeControl', {
template: require('./index.html'),
controller: Controller
});