import ngModule from '../module'; import Section from 'salix/components/section'; import './style.scss'; import UserError from 'core/lib/user-error'; class Controller extends Section { constructor($element, $, vnReport) { super($element, $); this.droppableElement = 'a.vn-tr'; } get isChecked() { if (this.tickets) { for (let instance of this.tickets) if (instance.checked) return true; } return false; } getHighestPriority() { let highestPriority = Math.max(...this.$.model.data.map(tag => { return tag.priority; })); return highestPriority + 1; } setHighestPriority(id) { const highestPriority = this.getHighestPriority(); const params = {priority: highestPriority}; const query = `Tickets/${id}/`; this.$http.patch(query, params).then(() => { this.vnApp.showSuccess(this.$t('Data saved!')); this.$.model.refresh(); }); } setPriority(id, priority) { let params = {priority: priority}; let query = `Tickets/${id}/`; this.$http.patch(query, params).then(() => { this.vnApp.showSuccess(this.$t('Data saved!')); this.$.model.refresh(); }); } 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(); }); } } getSelectedItems(items) { const selectedItems = []; if (items) { for (let i = 0; i < items.length; i++) { if (items[i].checked) selectedItems.push(items[i]); } } return selectedItems; } goToBuscaman() { if (!this.route.vehicleFk) throw new UserError(`The route doesn't have a vehicle`); let query = `Routes/${this.route.vehicleFk}/getDeliveryPoint`; this.$http.get(query).then(response => { if (!response.data) throw new UserError(`The route's vehicle doesn't have a delivery point`); return response.data; }).then(address => { let addresses; if (address) addresses = address; let lines = this.getSelectedItems(this.tickets); let url = 'http://gps.buscalia.com/usuario/localizar.aspx?bmi=true&addr='; lines.forEach((line, index) => { const previusLine = lines[index - 1] ? lines[index - 1].street : null; if (previusLine != line.street) addresses = addresses + '+to:' + line.postalCode + ' ' + line.city + ' ' + line.street; }); window.open(url + addresses, '_blank'); }); } showDeleteConfirm(id) { this.selectedTicket = id; this.$.confirm.show(); } removeTicketFromRoute($index) { let params = {routeFk: null}; let query = `Tickets/${this.selectedTicket}/`; this.$http.patch(query, params).then(() => { this.$.model.remove($index); this.vnApp.showSuccess(this.$t('Ticket removed from route')); this.updateVolume(); }); } updateVolume() { let url = `Routes/${this.$params.id}/updateVolume`; this.$http.post(url).then(() => { this.card.reload(); this.$.model.refresh(); }); } guessPriority() { let query = `Routes/${this.$params.id}/guessPriority/`; this.$http.get(query).then(() => { this.vnApp.showSuccess(this.$t('Order changed')); this.$.model.refresh(); }); } onDrop($event) { const target = $event.target; const droppable = target.closest(this.droppableElement); const ticketId = droppable.id; console.log($event, target, droppable, ticketId); } insert(ticketId) { ticketId = parseInt(ticketId); 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; }); } } ngModule.vnComponent('vnRouteTickets', { template: require('./index.html'), controller: Controller, require: { card: '^vnRouteCard' }, bindings: { route: '<' } });