salix/modules/agency/front/location/calendar.js

151 lines
3.9 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
class Controller {
constructor($element, $scope, $stateParams, $http) {
this.$element = $element;
this.$stateParams = $stateParams;
this.$scope = $scope;
this.$http = $http;
this.stMonthDate = new Date();
this.ndMonthDate = new Date();
this.ndMonthDate.setMonth(this.ndMonthDate.getMonth() + 1);
}
$postLink() {
this.stMonth = this.$scope.stMonth;
this.ndMonth = this.$scope.ndMonth;
}
// Disabled until implementation
// of holidays by node
/* get zone() {
return this._zone;
}
set zone(value) {
this._zone = value;
if (!value) return;
let query = '/agency/api/LabourHolidays/getByWarehouse';
this.$http.get(query, {params: {warehouseFk: value.warehouseFk}}).then(res => {
if (!res.data) return;
const events = [];
res.data.forEach(holiday => {
events.push({
date: holiday.dated,
className: 'red',
title: holiday.description || holiday.name,
isRemovable: false
});
});
this.events = this.events.concat(events);
});
} */
get data() {
return this._data;
}
set data(value) {
this._data = value;
if (!value) return;
const events = [];
value.forEach(event => {
events.push({
name: 'Has delivery',
dated: event.delivered,
style: {backgroundColor: '#a3d131'}
});
});
this.events = events;
}
onSelection(values, calendar) {
let totalEvents = 0;
values.forEach(day => {
const exists = calendar.events.findIndex(event => {
return event.dated >= day.dated && event.dated <= day.dated
&& event.isRemovable;
});
if (exists > -1) totalEvents++;
});
if (totalEvents > (values.length / 2))
this.removeEvents(calendar, values);
else
this.insertEvents(calendar, values);
}
insertEvents(calendar, days) {
days.forEach(day => {
const event = calendar.events.find(event => {
return event.dated >= day.dated && event.dated <= day.dated;
});
if (event) return false;
this.$scope.model.insert({
zoneFk: this.zone.id,
delivered: day.dated,
price: this.zone.price,
bonus: this.zone.bonus
});
calendar.addEvent({
name: 'Has delivery',
dated: day.dated,
style: {backgroundColor: '#a3d131'}
});
});
this.$scope.model.save().then(() => {
this.events = calendar.events;
});
}
removeEvents(calendar, days) {
let dates = [];
days.forEach(day => {
const event = calendar.events.find(event => {
return event.dated >= day.dated && event.dated <= day.dated;
});
if (event && !event.isRemovable)
return false;
dates.push(day.dated);
calendar.removeEvent(day.dated);
});
if (dates.length == 0) return;
const params = {zoneFk: this.zone.id, dates};
this.$http.post('/agency/api/zoneCalendars/removeByDate', params).then(() => {
this.events = calendar.events;
});
}
onMoveNext(calendar) {
calendar.moveNext(2);
}
onMovePrevious(calendar) {
calendar.movePrevious(2);
}
}
Controller.$inject = ['$element', '$scope', '$stateParams', '$http'];
ngModule.component('vnZoneLocationCalendar', {
template: require('./calendar.html'),
controller: Controller,
bindings: {
zone: '<'
}
});