import ngModule from '../module'; import Component from 'core/lib/component'; class Controller extends Component { constructor($element, $, aclService, $httpParamSerializer) { super($element, $); this.aclService = aclService; this.$httpParamSerializer = $httpParamSerializer; 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: 'Delete stowaway', callback: this.showDeleteStowaway, show: () => this.shouldShowDeleteStowaway() }, { name: 'Make invoice', acl: 'invoicing', callback: this.showMakeInvoiceDialog, show: () => !this.hasInvoice() }, { name: 'Regenerate invoice', acl: 'invoicing', callback: this.showRegenerateInvoiceDialog, show: () => this.hasInvoice() }, { name: 'Recalculate components', callback: this.comfirmRecalculateComponents, show: () => this.isEditable }, ]; } get ticket() { return this._ticket; } set ticket(value) { this._ticket = value; if (!value) return; if (this.$params.sendSMS) this.showSMSDialog(); this.canStowaway(); 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) { links.btnThree = { icon: 'icon-stowaway', state: `ticket.card.summary({id: ${value.ship.id}})`, tooltip: 'Stowaway' }; } this._quicklinks = links; } get quicklinks() { return this._quicklinks; } set quicklinks(value = {}) { this._quicklinks = Object.assign(value, this._quicklinks); } showChangeShipped() { if (!this.isEditable) { this.vnApp.showError(this.$translate.instant(`This ticket can't be modified`)); return; } this.newShipped = this.ticket.shipped; this.$.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; } 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.$.moreButton.data = options; } get isEditable() { try { return !this.ticket.tracking.state.alertLevel; } catch (e) {} return true; } showAddTurnDialog() { this.$.addTurn.show(); } addTurn(day) { let params = {ticketFk: this.ticket.id, weekDay: day}; this.$http.patch(`TicketWeeklies`, params).then(() => { this.$.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.$.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')); }); } } 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; }); } shouldShowDeleteStowaway() { if (!this._ticket || !this.isTicketModule()) return false; return this._ticket.stowaway || this._ticket.ship; } showAddStowaway() { this.$.addStowaway.show(); } showDeleteStowaway() { this.$.deleteStowaway.show(); } deleteStowaway() { const query = `Tickets/${this.ticket.id}/deleteStowaway`; this.$http.post(query).then(res => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.cardReload(); }); } showDeliveryNote() { const params = { clientId: this.ticket.client.id, ticketId: this.ticket.id }; const serializedParams = this.$httpParamSerializer(params); let url = `api/report/delivery-note?${serializedParams}`; window.open(url); } sendDeliveryNote() { const params = { recipient: this.ticket.client.email, clientId: this.ticket.client.id, ticketId: this.ticket.id }; const serializedParams = this.$httpParamSerializer(params); this.$http.get(`email/delivery-note?${serializedParams}`).then( () => this.vnApp.showMessage(this.$translate.instant('Notification sent!')) ); } showSMSDialog() { const address = this.ticket.address; const phone = this.$params.phone || address.mobile; const message = this.$params.message || this.$translate.instant('SMSPayment'); this.newSMS = { destinationFk: this.ticket.clientFk, destination: phone, message: message }; this.$.sms.open(); } /** * Shows an invoice confirmation */ showMakeInvoiceDialog() { this.$.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.$.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; } /** * Shows a delivery-note send confirmation */ confirmDeliveryNote() { this.$.confirmDeliveryNote.show(); } /** * Shows an invoice confirmation */ comfirmRecalculateComponents() { this.$.recalculateComponentsConfirmation.show(); } recalculateComponents() { const query = `Tickets/${this.ticket.id}/recalculateComponents`; this.$http.post(query).then(res => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } } Controller.$inject = ['$element', '$scope', 'aclService', '$httpParamSerializer']; ngModule.component('vnTicketDescriptor', { template: require('./index.html'), bindings: { ticket: '<', cardReload: '&' }, controller: Controller });