import ngModule from '../../module'; class Controller { constructor($http, $state, $translate, vnApp) { this.$http = $http; this.$state = $state; this.$translate = $translate; this.vnApp = vnApp; } $onInit() { this.data.registerChild(this); } get ticket() { return this._ticket; } set ticket(value) { this._ticket = value; if (!value) return; this.getTotalPrice(); this.getTotalNewPrice(); this.getTotalDifferenceOfPrice(); this.loadDefaultTicketAction(); } loadDefaultTicketAction() { let filter = {where: {code: 'changePrice'}}; this.$http.get(`TicketUpdateActions`, {filter}).then(response => { return this.ticket.option = response.data[0].id; }); } onStepChange() { return true; } getTotalPrice() { let totalPrice = 0; this.ticket.sale.items.forEach(item => { let itemTotalPrice = item.quantity * item.price; totalPrice += itemTotalPrice; }); this.totalPrice = totalPrice; } getTotalNewPrice() { let totalNewPrice = 0; this.ticket.sale.items.forEach(item => { if (item.component) totalNewPrice += item.quantity * item.component.newPrice; }); this.totalNewPrice = totalNewPrice; } getTotalDifferenceOfPrice() { let totalPriceDifference = 0; this.ticket.sale.items.forEach(item => { if (item.component) totalPriceDifference += item.component.difference; }); this.totalPriceDifference = totalPriceDifference; } onSubmit() { if (!this.ticket.option) { return this.vnApp.showError( this.$translate.instant('Choose an option') ); } let query = `tickets/${this.ticket.id}/componentUpdate`; let params = { clientFk: this.ticket.clientFk, agencyModeFk: this.ticket.agencyModeFk, addressFk: this.ticket.addressFk, zoneFk: this.ticket.zoneFk, warehouseFk: this.ticket.warehouseFk, companyFk: this.ticket.companyFk, shipped: this.ticket.shipped, landed: this.ticket.landed, isDeleted: this.ticket.isDeleted, option: parseInt(this.ticket.option) }; this.$http.post(query, params).then(res => { this.vnApp.showMessage( this.$translate.instant(`The ticket has been unrouted`) ); this.card.reload(); this.$state.go('ticket.card.summary', {id: this.$state.params.id}); }); } } Controller.$inject = ['$http', '$state', '$translate', 'vnApp']; ngModule.component('vnTicketBasicDataStepTwo', { template: require('./index.html'), controller: Controller, bindings: { ticket: '<' }, require: { card: '^vnTicketCard', data: '^vnTicketBasicData' } });