salix/modules/worker/front/calendar/index.js

301 lines
8.1 KiB
JavaScript
Raw Normal View History

2019-03-22 07:28:57 +00:00
import ngModule from '../module';
2020-03-18 11:55:22 +00:00
import Section from 'salix/components/section';
2019-03-22 07:28:57 +00:00
import './style.scss';
2020-03-18 11:55:22 +00:00
class Controller extends Section {
constructor($element, $) {
super($element, $);
2019-10-23 15:38:35 +00:00
this.date = new Date();
this.events = {};
2020-08-10 12:44:57 +00:00
this.buildYearFilter();
}
get year() {
return this.date.getFullYear();
}
set year(value) {
const newYear = new Date();
newYear.setFullYear(value);
this.date = newYear;
2020-08-18 11:50:08 +00:00
2021-06-04 13:49:48 +00:00
this.refresh()
.then(() => this.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays());
2019-03-22 07:28:57 +00:00
}
2021-06-10 09:24:32 +00:00
get businessId() {
return this._businessId;
}
set businessId(value) {
this._businessId = value;
if (value) {
this.refresh()
.then(() => this.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays());
}
}
2019-10-23 15:38:35 +00:00
get date() {
return this._date;
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
set date(value) {
this._date = value;
value.setHours(0, 0, 0, 0);
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
this.months = new Array(12);
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
for (let i = 0; i < this.months.length; i++) {
const now = new Date(value.getTime());
now.setDate(1);
now.setMonth(i);
this.months[i] = now;
}
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
get worker() {
return this._worker;
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
set worker(value) {
this._worker = value;
2019-03-22 07:28:57 +00:00
2020-07-15 09:35:34 +00:00
if (value) {
this.getIsSubordinate();
2021-06-03 09:00:40 +00:00
this.getActiveContract();
}
}
2021-12-24 10:30:28 +00:00
get payedHolidays() {
return this._businessId;
}
2020-08-10 12:44:57 +00:00
buildYearFilter() {
2021-06-03 09:00:40 +00:00
const now = new Date();
now.setFullYear(now.getFullYear() + 1);
const maxYear = now.getFullYear();
const minRange = maxYear - 5;
2020-08-10 12:44:57 +00:00
const years = [];
2021-06-03 09:00:40 +00:00
for (let i = maxYear; i > minRange; i--)
2020-08-10 12:44:57 +00:00
years.push({year: i});
this.yearFilter = years;
}
2020-07-15 09:35:34 +00:00
getIsSubordinate() {
2021-06-03 09:00:40 +00:00
this.$http.get(`Workers/${this.worker.id}/isSubordinate`)
.then(res => this.isSubordinate = res.data);
}
getActiveContract() {
this.$http.get(`Workers/${this.worker.id}/activeContract`).then(res => {
if (res.data)
this.businessId = res.data.businessFk;
});
2019-03-22 07:28:57 +00:00
}
2021-06-04 13:49:48 +00:00
getContractHolidays() {
this.getHolidays({
businessFk: this.businessId,
year: this.year
}, data => this.contractHolidays = data);
}
getYearHolidays() {
2021-06-10 09:24:32 +00:00
this.getHolidays({
2021-06-04 13:49:48 +00:00
year: this.year
2021-06-10 09:24:32 +00:00
}, data => this.yearHolidays = data);
2021-06-04 13:49:48 +00:00
}
getHolidays(params, cb) {
this.$http.get(`Workers/${this.worker.id}/holidays`, {params})
.then(res => cb(res.data));
}
2019-10-23 15:38:35 +00:00
onData(data) {
this.events = {};
this.calendar = data.calendar;
2019-03-22 07:28:57 +00:00
2020-09-24 08:14:53 +00:00
let addEvent = (day, newEvent) => {
const timestamp = new Date(day).getTime();
const event = this.events[timestamp];
if (event) {
const oldName = event.name;
Object.assign(event, newEvent);
event.name = `${oldName}, ${event.name}`;
} else
this.events[timestamp] = newEvent;
2019-10-23 15:38:35 +00:00
};
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
if (data.holidays) {
data.holidays.forEach(holiday => {
2020-09-07 05:59:27 +00:00
const holidayDetail = holiday.detail && holiday.detail.name;
2019-10-23 15:38:35 +00:00
const holidayType = holiday.type && holiday.type.name;
const holidayName = holidayDetail || holidayType;
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
addEvent(holiday.dated, {
name: holidayName,
2020-09-24 09:15:35 +00:00
className: 'festive'
2019-10-23 15:38:35 +00:00
});
});
}
if (data.absences) {
data.absences.forEach(absence => {
let type = absence.absenceType;
addEvent(absence.dated, {
name: type.name,
2020-07-08 13:05:56 +00:00
color: type.rgb,
type: type.code,
absenceId: absence.id
2019-10-23 15:38:35 +00:00
});
});
2019-03-22 07:28:57 +00:00
}
}
2019-10-23 15:38:35 +00:00
repaint() {
let calendars = this.element.querySelectorAll('vn-calendar');
for (let calendar of calendars)
calendar.$ctrl.repaint();
}
2019-10-23 15:38:35 +00:00
formatDay(day, element) {
let event = this.events[day.getTime()];
if (!event) return;
let dayNumber = element.firstElementChild;
dayNumber.title = event.name;
dayNumber.style.backgroundColor = event.color;
2020-09-24 08:14:53 +00:00
if (event.border)
dayNumber.style.border = event.border;
2020-09-24 09:15:35 +00:00
if (event.className)
dayNumber.classList.add(event.className);
}
2020-07-07 12:29:57 +00:00
pick(absenceType) {
2020-07-15 09:35:34 +00:00
if (!this.isSubordinate) return;
2020-07-07 12:29:57 +00:00
if (absenceType == this.absenceType)
absenceType = null;
this.absenceType = absenceType;
}
2020-07-11 16:30:27 +00:00
onSelection($event, $days) {
2020-07-07 12:29:57 +00:00
if (!this.absenceType)
2020-07-11 16:30:27 +00:00
return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu'));
2020-07-07 12:29:57 +00:00
2020-07-08 13:05:56 +00:00
const day = $days[0];
const stamp = day.getTime();
const event = this.events[stamp];
2020-07-11 16:30:27 +00:00
const calendar = $event.target.closest('vn-calendar').$ctrl;
2020-07-08 13:05:56 +00:00
if (event) {
if (event.type == this.absenceType.code)
2020-07-11 16:30:27 +00:00
this.delete(calendar, day, event);
2020-07-08 13:05:56 +00:00
else
2020-07-11 16:30:27 +00:00
this.edit(calendar, event);
2020-07-08 13:05:56 +00:00
} else
2020-07-11 16:30:27 +00:00
this.create(calendar, day);
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
create(calendar, dated) {
2020-07-08 13:05:56 +00:00
const absenceType = this.absenceType;
const params = {
dated: dated,
2021-06-03 09:00:40 +00:00
absenceTypeId: absenceType.id,
businessFk: this.businessId
2020-07-07 12:29:57 +00:00
};
2020-07-08 13:05:56 +00:00
const path = `Workers/${this.$params.id}/createAbsence`;
this.$http.post(path, params).then(res => {
2020-07-11 16:30:27 +00:00
const newEvent = res.data;
this.events[dated.getTime()] = {
name: absenceType.name,
color: absenceType.rgb,
type: absenceType.code,
absenceId: newEvent.id
};
this.repaintCanceller(() =>
2021-06-10 09:24:32 +00:00
this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
2020-07-11 16:30:27 +00:00
);
2020-07-08 13:05:56 +00:00
});
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
edit(calendar, event) {
2020-07-08 13:05:56 +00:00
const absenceType = this.absenceType;
const params = {
absenceId: event.absenceId,
absenceTypeId: absenceType.id
};
const path = `Workers/${this.$params.id}/updateAbsence`;
2020-07-11 16:30:27 +00:00
this.$http.patch(path, params).then(() => {
event.color = absenceType.rgb;
event.name = absenceType.name;
2020-07-16 08:04:16 +00:00
event.type = absenceType.code;
2020-07-11 16:30:27 +00:00
this.repaintCanceller(() =>
2021-06-10 09:24:32 +00:00
this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
2020-07-11 16:30:27 +00:00
);
});
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
delete(calendar, day, event) {
2020-07-08 13:05:56 +00:00
const params = {absenceId: event.absenceId};
const path = `Workers/${this.$params.id}/deleteAbsence`;
2020-07-11 16:30:27 +00:00
this.$http.delete(path, {params}).then(() => {
delete this.events[day.getTime()];
this.repaintCanceller(() =>
2021-06-10 09:24:32 +00:00
this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
2020-07-11 16:30:27 +00:00
);
});
}
repaintCanceller(cb) {
if (this.canceller) {
clearTimeout(this.canceller);
this.canceller = null;
2020-07-07 12:29:57 +00:00
}
2020-07-08 13:05:56 +00:00
2020-07-11 16:30:27 +00:00
this.canceller = setTimeout(
() => cb(), 500);
2020-07-07 12:29:57 +00:00
}
refresh() {
const params = {
workerFk: this.$params.id,
2021-06-03 09:00:40 +00:00
businessFk: this.businessId,
year: this.year
2020-07-07 12:29:57 +00:00
};
2020-08-10 12:29:25 +00:00
return this.$http.get(`Calendars/absences`, {params})
2020-07-07 12:29:57 +00:00
.then(res => this.onData(res.data));
}
2019-03-22 07:28:57 +00:00
}
ngModule.vnComponent('vnWorkerCalendar', {
2019-03-22 07:28:57 +00:00
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});