salix/modules/route/front/tickets/index.js

190 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-04-04 11:06:41 +00:00
import ngModule from '../module';
import Section from 'salix/components/section';
2019-04-04 11:06:41 +00:00
import './style.scss';
class Controller extends Section {
get route() {
return this._route;
}
set route(value) {
this._route = value;
if (value)
this.buildPossibleTicketsFilter();
}
2019-04-04 11:06:41 +00:00
get isChecked() {
if (this.tickets) {
for (let instance of this.tickets)
if (instance.checked) return true;
}
return false;
}
buildPossibleTicketsFilter() {
let minDate = new Date(this.route.finished);
minDate.setHours(0, 0, 0, 0);
let maxDate = new Date(this.route.finished);
maxDate.setHours(23, 59, 59, 59);
this.possibleTicketsFilter = {
where: {
zoneFk: this.route.zoneFk,
routeFk: null,
landed: {between: [minDate, maxDate]},
},
include: [
{
relation: 'warehouse',
scope: {
fields: ['name']
},
}, {
relation: 'address'
}
]
};
}
2019-04-04 11:06:41 +00:00
getHighestPriority() {
let highestPriority = Math.max(...this.$.model.data.map(tag => {
return tag.priority;
}));
return highestPriority + 1;
2019-04-04 11:06:41 +00:00
}
setPriority(id, priority) {
let params = {priority: priority};
let query = `Tickets/${id}/`;
2019-04-04 11:06:41 +00:00
this.$http.patch(query, params).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
2019-04-04 11:06:41 +00:00
this.$.model.refresh();
});
}
getSelectedItems(items) {
const selectedItems = [];
if (items) {
for (let i = 0; i < items.length; i++) {
if (items[i].checked)
selectedItems.push(items[i]);
2019-04-04 11:06:41 +00:00
}
}
return selectedItems;
2019-04-04 11:06:41 +00:00
}
goToBuscaman() {
let query = `Routes/${this.route.vehicleFk}/getDeliveryPoint`;
let deliveryPointAddress;
let addresses;
this.$http.get(query).then(response => {
deliveryPointAddress = response.data;
}).then(() => {
addresses = deliveryPointAddress;
let lines = this.getSelectedItems(this.tickets);
let url = 'http://gps.buscalia.com/usuario/localizar.aspx?bmi=true&addr=';
lines.forEach(line => {
addresses = addresses + '+to:' + line.address.postalCode + ' ' + line.address.city + ' ' + line.address.street;
});
window.open(url + addresses, '_blank');
2019-04-04 11:06:41 +00:00
});
}
showDeleteConfirm(id) {
this.selectedTicket = id;
2019-04-04 11:06:41 +00:00
this.$.confirm.show();
}
2020-07-29 08:47:48 +00:00
removeTicketFromRoute() {
let params = {routeFk: null};
let query = `Tickets/${this.selectedTicket}/`;
this.$http.patch(query, params).then(() => {
this.vnApp.showSuccess(this.$t('Ticket removed from route'));
this.updateVolume();
});
2019-04-04 11:06:41 +00:00
}
2019-06-06 11:50:12 +00:00
updateVolume() {
let url = `Routes/${this.$params.id}/updateVolume`;
2019-06-06 11:50:12 +00:00
this.$http.post(url).then(() => {
this.card.reload();
this.$.model.refresh();
});
}
2019-04-04 11:06:41 +00:00
guessPriority() {
let query = `Routes/${this.$params.id}/guessPriority/`;
2019-04-04 11:06:41 +00:00
this.$http.get(query).then(() => {
this.vnApp.showSuccess(this.$t('Order changed'));
2019-04-04 11:06:41 +00:00
this.$.model.refresh();
});
}
openPossibleTicketsDialog() {
this.$.possibleTicketsModel.refresh();
this.$.possibleTicketsDialog.show();
}
2020-07-29 08:47:48 +00:00
setTicketsRoute() {
let tickets = this.getSelectedItems(this.possibleTickets);
if (tickets.length === 0) return;
for (let i = 0; i < tickets.length; i++) {
delete tickets[i].checked;
tickets[i].routeFk = this.route.id;
}
2020-07-29 08:47:48 +00:00
return this.$.possibleTicketsModel.save().then(() => {
this.$.model.data = this.$.model.data.concat(tickets);
});
}
onDrop($event) {
const ticketId = $event.dataTransfer.getData('Text');
if (isNaN(ticketId)) {
const regexp = new RegExp(/\/ticket\/([0-9]+)\//i);
const matches = ticketId.match(regexp);
if (matches && matches.length)
this.insert(matches[1]);
else
this.vnApp.showError(this.$t('Ticket not found'));
}
if (!isNaN(ticketId))
this.insert(ticketId);
}
insert(id) {
2020-11-20 11:12:06 +00:00
const params = {routeId: this.route.id};
const query = `Routes/${id}/ticketToRoute`;
return this.$http.patch(query, params).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.model.refresh();
this.card.reload();
}).catch(error => {
if (error.status == 404)
return this.vnApp.showError(this.$t('Ticket not found'));
throw error;
});
}
2019-04-04 11:06:41 +00:00
}
ngModule.vnComponent('vnRouteTickets', {
2019-04-04 11:06:41 +00:00
template: require('./index.html'),
controller: Controller,
2019-06-06 11:50:12 +00:00
require: {
card: '^vnRouteCard'
},
bindings: {
route: '<'
}
2019-04-04 11:06:41 +00:00
});