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

202 lines
6.0 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';
import UserError from 'core/lib/user-error';
2019-04-04 11:06:41 +00:00
class Controller extends Section {
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;
}
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
}
setHighestPriority(ticket) {
const highestPriority = this.getHighestPriority();
if (highestPriority - 1 != ticket.priority) {
const params = {priority: highestPriority};
const query = `Tickets/${ticket.id}/`;
this.$http.patch(query, params).then(res => {
ticket.priority = res.data.priority;
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
}
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
});
}
deletePriority() {
const lines = this.getSelectedItems(this.tickets);
for (const line of lines) {
this.$http.patch(`Tickets/${line.id}/`, {priority: null}).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.model.refresh();
});
}
}
setOrderedPriority(lines) {
let priority = 1;
for (const line of lines) {
this.$http.patch(`Tickets/${line.id}/`, {priority: priority}).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.model.refresh();
});
priority++;
}
}
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
}
2023-03-01 13:22:25 +00:00
goToBuscaman(ticket) {
if (!this.route.vehicleFk)
throw new UserError(`The route doesn't have a vehicle`);
2023-03-01 13:22:25 +00:00
this.$http.get(`Routes/${this.route.vehicleFk}/getDeliveryPoint`).then(res => {
if (!res.data)
throw new UserError(`The route's vehicle doesn't have a delivery point`);
2023-03-01 13:22:25 +00:00
let addresses = res.data;
const lines = ticket ? [ticket] : this.getSelectedItems(this.tickets);
lines.forEach((line, index) => {
2023-03-01 13:22:25 +00:00
const previousLine = lines[index - 1] ? lines[index - 1].street : null;
if (previousLine != line.street)
addresses = addresses + '+to:' + line.postalCode + ' ' + line.city + ' ' + line.street;
});
2023-03-01 13:22:25 +00:00
const url = 'http://gps.buscalia.com/usuario/localizar.aspx?bmi=true&addr=';
window.open(url + encodeURI(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();
}
removeTicketFromRoute($index) {
2020-07-29 08:47:48 +00:00
let params = {routeFk: null};
let query = `Tickets/${this.selectedTicket}/`;
this.$http.patch(query, params).then(() => {
this.$.model.remove($index);
2020-07-29 08:47:48 +00:00
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();
});
}
2022-06-09 11:48:57 +00:00
onDrop($event) {
2022-06-14 11:52:32 +00:00
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(ticketId) {
ticketId = parseInt(ticketId);
2020-11-20 11:12:06 +00:00
const query = `Routes/${this.route.id}/insertTicket`;
return this.$http.patch(query, {ticketId}).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.updateVolume();
}).catch(error => {
if (error.status == 404)
return this.vnApp.showError(this.$t('Ticket not found'));
throw error;
});
}
2022-10-20 13:04:59 +00:00
async sendSms() {
try {
const clientsFk = [];
const clientsName = [];
const clients = [];
const selectedTickets = this.getSelectedItems(this.$.$ctrl.tickets);
for (let ticket of selectedTickets) {
clientsFk.push(ticket.clientFk);
let userContact = await this.$http.get(`Clients/${ticket.clientFk}`);
clientsName.push(userContact.data.name);
clients.push(userContact.data.phone);
}
const destinationFk = String(clientsFk);
const destination = String(clients);
this.newSMS = Object.assign({
destinationFk: destinationFk,
destination: destination
});
this.$.sms.open();
return true;
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
}
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
});