import ngModule from '../module'; class Controller { constructor($state, $scope, $http, vnApp, $translate, aclService) { this.$scope = $scope; this.$state = $state; this.$http = $http; this.vnApp = vnApp; this.$translate = $translate; this.aclService = aclService; this.moreOptions = [ {name: 'Add turn', callback: this.showAddTurnDialog}, {name: 'Show Delivery Note', callback: this.showDeliveryNote}, {name: 'Delete ticket', callback: this.showDeleteTicketDialog}, {name: 'Change shipped hour', callback: this.showChangeShipped}, {name: 'Send SMS', callback: this.showSMSDialog}, {name: 'Show pallet report', callback: this.openRptRoute}, { name: 'Add stowaway', callback: this.showAddStowaway, show: () => this.isTicketModule() }, { name: 'Remove stowaway', callback: this.showRemoveStowaway, show: () => this.shouldShowRemoveStowaway() }, { name: 'Make invoice', acl: 'invoicing', callback: this.showMakeInvoiceDialog, show: () => !this.hasInvoice() }, { name: 'Regenerate invoice', acl: 'invoicing', callback: this.showRegenerateInvoiceDialog, show: () => this.hasInvoice() }, ]; } showChangeShipped() { if (!this.isEditable) { this.vnApp.showError(this.$translate.instant('This ticket can\'t be modified')); return; } this.newShipped = this.ticket.shipped; this.$scope.changeShippedDialog.show(); } changeShipped(response) { if (response === 'ACCEPT') { let params = {shipped: this.newShipped}; this.$http.patch(`/ticket/api/Tickets/${this.ticket.id}`, params).then(() => { this.vnApp.showSuccess(this.$translate.instant('Shipped hour updated')); this.cardReload(); }); } } isTicketModule() { let path = this.$state.getCurrentPath(); if (path[1].state.name === 'ticket') return true; return false; } shouldShowRemoveStowaway() { if (!this._ticket || !this.isTicketModule()) return false; return (this._ticket.stowaway || (this._ticket.ship && this._ticket.ship.length > 0)); } onMoreChange(callback) { callback.call(this); } goToTicket(ticketID) { this.$state.go('ticket.card.sale', {id: ticketID}, {absolute: true}); } onMoreOpen() { let options = this.moreOptions.filter(option => { const hasShowProperty = Object.hasOwnProperty.call(option, 'show'); const hasAclProperty = Object.hasOwnProperty.call(option, 'acl'); const hasAcl = !hasAclProperty || (hasAclProperty && this.aclService.hasAny([option.acl])); return (!hasShowProperty || option.show === true || typeof option.show === 'function' && option.show()) && hasAcl; }); this.$scope.moreButton.data = options; } get isEditable() { try { return !this.ticket.tracking.state.alertLevel; } catch (e) {} return true; } showAddTurnDialog() { this.$scope.addTurn.show(); } addTurn(day) { let params = {ticketFk: this.ticket.id, weekDay: day}; this.$http.patch(`/ticket/api/TicketWeeklies`, params).then(() => { this.$scope.addTurn.hide(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } showDeleteTicketDialog() { if (!this.isEditable) { this.vnApp.showError(this.$translate.instant('This ticket cant be deleted')); return; } this.$scope.deleteConfirmation.show(); } deleteTicket(response) { if (response === 'ACCEPT') { let params = {id: this.ticket.id}; this.$http.post(`/ticket/api/Tickets/deleted`, params).then(() => { this.$state.go('ticket.index'); this.vnApp.showSuccess(this.$translate.instant('Ticket deleted')); }); } } openRptRoute() { let url = `/api/report/rpt-route?routeFk=${this.ticket.routeFk}`; window.open(url); } showAddStowaway() { this.$scope.addStowaway.show(); } showRemoveStowaway() { this.$scope.removeStowaway.show(); } get ticket() { return this._ticket; } set ticket(value) { this._ticket = value; if (!value) return; let links = { btnOne: { icon: 'person', state: `client.card.summary({id: ${value.clientFk}})`, tooltip: 'Client card' }}; if (value.stowaway) { links.btnTwo = { icon: 'icon-stowaway', state: `ticket.card.summary({id: ${value.stowaway.shipFk}})`, tooltip: 'Ship' }; } if (value.ship && value.ship.length == 1) { links.btnThree = { icon: 'icon-stowaway', state: `ticket.card.summary({id: ${value.ship[0].id}})`, tooltip: 'Stowaway' }; } else if (value.ship && value.ship.length > 1) this.shipStowaways = value.ship; this._quicklinks = links; } set quicklinks(value = {}) { this._quicklinks = Object.assign(value, this._quicklinks); } get quicklinks() { return this._quicklinks; } showDeliveryNote() { let url = `/api/report/rpt-delivery-note?ticketFk=${this.ticket.id}`; window.open(url); } showSMSDialog() { const address = this.ticket.address; this.newSMS = { destinationFk: this.ticket.clientFk, destination: address.mobile || null, message: this.$translate.instant('SMSPayment') }; this.$scope.sms.open(); } /** * Shows an invoice confirmation */ showMakeInvoiceDialog() { this.$scope.makeInvoiceConfirmation.show(); } /** * Makes an invoice * from current ticket * * @param {String} response - Response result */ makeInvoice(response) { if (response === 'ACCEPT') { const query = `/ticket/api/Tickets/${this.ticket.id}/makeInvoice`; this.$http.post(query).then(() => { this.vnApp.showSuccess(this.$translate.instant('Ticket invoiced')); this.$state.reload(); }); } } /** * Shows an invoice confirmation */ showRegenerateInvoiceDialog() { this.$scope.regenerateInvoiceConfirmation.show(); } /** * Sends an invoice to a regeneration queue * for the current ticket * * @param {String} response - Response result */ regenerateInvoice(response) { if (response === 'ACCEPT') { const invoiceId = this.ticket.invoiceOut.id; const query = `/invoiceOut/api/InvoiceOuts/${invoiceId}/regenerate`; this.$http.post(query).then(() => { const snackbarMessage = this.$translate.instant( `Invoice sent for a regeneration, will be available in a few minutes`); this.vnApp.showSuccess(snackbarMessage); }); } } /** * Returns if the current ticket * is already invoiced * @return {Boolean} - True if invoiced */ hasInvoice() { return this.ticket.refFk !== null; } } Controller.$inject = ['$state', '$scope', '$http', 'vnApp', '$translate', 'aclService']; ngModule.component('vnTicketDescriptor', { template: require('./index.html'), bindings: { ticket: '<', cardReload: '&' }, controller: Controller });