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

169 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-05-17 11:27:51 +00:00
import ngModule from '../module';
import './style.scss';
class Controller {
2019-10-23 15:38:35 +00:00
constructor($scope, $http, $stateParams, $element, vnWeekDays) {
2019-05-17 11:27:51 +00:00
this.$stateParams = $stateParams;
this.$ = $scope;
this.$http = $http;
2019-05-21 10:56:29 +00:00
this.$element = $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());
ended.setDate(ended.getDate() + 7);
this.ended = ended;
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-10-23 15:38:35 +00:00
this.weekDays = [];
if (!this.hours) return;
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
let dayIndex = new Date(this.started.getTime());
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
while (dayIndex < this.ended) {
let weekDay = dayIndex.getDay();
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
let hours = this.hours
.filter(hour => new Date(hour.timed).getDay() == weekDay)
.sort((a, b) => new Date(a.timed) - new Date(b.timed));
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
this.weekDays.push({
dated: new Date(dayIndex.getTime()),
hours
2019-05-17 11:27:51 +00:00
});
2019-10-23 15:38:35 +00:00
dayIndex.setDate(dayIndex.getDate() + 1);
2019-05-17 11:27:51 +00:00
}
}
2019-10-23 15:38:35 +00:00
fetchHours() {
const params = {workerFk: this.$stateParams.id};
const filter = {
where: {and: [
{timed: {gte: this.started}},
{timed: {lt: this.ended}}
]}
};
2019-05-17 11:27:51 +00:00
2019-10-23 15:38:35 +00:00
this.$.model.applyFilter(filter, params);
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-10-23 15:38:35 +00:00
hourColor(weekDay) {
return weekDay.manual ? 'alert' : 'warning';
2019-05-17 11:27:51 +00:00
}
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();
2019-05-21 10:56:29 +00:00
2019-05-17 11:27:51 +00:00
now.setMonth(timed.getMonth());
now.setDate(timed.getDate());
2019-10-04 11:12:53 +00:00
now.setHours(0, 0, 0, 0);
2019-05-17 11:27:51 +00:00
this.newTime = now;
this.selectedWeekday = weekday;
this.$.addTimeDialog.show();
}
addTime(response) {
2019-10-23 15:38:35 +00:00
if (response !== 'ACCEPT') return;
let data = {
workerFk: this.$stateParams.id,
timed: this.newTime
};
this.$http.post(`WorkerTimeControls/addTime`, data)
2019-10-23 15:38:35 +00:00
.then(() => this.fetchHours());
2019-05-17 11:27:51 +00:00
}
}
2019-10-23 15:38:35 +00:00
Controller.$inject = ['$scope', '$http', '$stateParams', '$element', '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
});