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: 'Send Delivery Note', callback: this.confirmDeliveryNote}, {name: 'Delete ticket', callback: this.showDeleteTicketDialog}, {name: 'Change shipped hour', callback: this.showChangeShipped}, {name: 'Send SMS', callback: this.showSMSDialog}, { name: 'Add stowaway', callback: this.showAddStowaway, show: () => this.canShowStowaway }, { 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 data = {shipped: this.newShipped}; let query = `Tickets/${this.ticket.id}/updateEditableTicket`; this.$http.post(query, data).then(() => { this.vnApp.showSuccess(this.$translate.instant('Shipped hour updated')); this.cardReload(); }); } } isTicketModule() { let path = this.$state.getCurrentPath(); const isTicket = path[1].state.name === 'ticket'; if (isTicket) return true; return false; } canStowaway() { if (!this.isTicketModule()) return; this.$http.get(`Tickets/${this.ticket.id}/canHaveStowaway`).then(response => { if (response.data === true) return this.canShowStowaway = true; return this.canShowStowaway = 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(`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') { const query = `Tickets/${this.ticket.id}/setDeleted`; this.$http.post(query).then(() => { this.$state.go('ticket.index'); this.vnApp.showSuccess(this.$translate.instant('Ticket deleted')); }); } } showAddStowaway() { this.$scope.addStowaway.show(); } showRemoveStowaway() { this.$scope.removeStowaway.show(); } get ticket() { return this._ticket; } set ticket(value) { this._ticket = value; if (value) this.canStowaway(); 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 stowaways' }; } 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 = `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 = `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 = `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; } confirmDeliveryNote() { this.$scope.confirmDeliveryNote.show(); } sendDeliveryNote(response) { if (response === 'accept') { this.$http.post(`email/delivery-note`, {ticketFk: this.ticket.id}).then( () => this.vnApp.showMessage(this.$translate.instant('Notification sent!')) ); } } } Controller.$inject = ['$state', '$scope', '$http', 'vnApp', '$translate', 'aclService']; ngModule.component('vnTicketDescriptor', { template: require('./index.html'), bindings: { ticket: '<', cardReload: '&' }, controller: Controller });