import ngModule from '../../module'; import './style.scss'; class Controller { constructor($scope, $http, $translate, vnApp) { this.$scope = $scope; this.$http = $http; this.$translate = $translate; this.vnApp = vnApp; } $onInit() { this.data.registerChild(this); } set ticket(value) { this._ticket = value; if (!value || !value.id) return; this.onChangeAddress(value.clientFk); } get ticket() { return this._ticket; } set clientFk(value) { this.ticket.clientFk = value; this.ticket.addressFk = null; this.onChangeAddress(value); } get clientFk() { if (this.ticket) return this.ticket.clientFk; return null; } set shipped(value) { this.ticket.shipped = value; this.onChangeShipped(value); } get shipped() { if (this.ticket) return this.ticket.shipped; return null; } set landed(value) { this.ticket.landed = value; this.onChangeLanded(value); } get landed() { if (this.ticket) return this.ticket.landed; return null; } onChangeShipped(value) { let params = { shipped: value, addressFk: this.ticket.addressFk, agencyModeFk: this.ticket.agencyModeFk, warehouseFk: this.ticket.warehouseFk }; let query = `/api/Agencies/getLanded`; this.$http.get(query, {params}).then(res => { if (res.data && res.data.landed) this.ticket.landed = res.data.landed; else { return this.vnApp.showError( this.$translate.instant(`There's no available agency for this shipping date`) ); } }); } onChangeLanded(value) { let params = { landed: value, addressFk: this.ticket.addressFk, agencyModeFk: this.ticket.agencyModeFk, warehouseFk: this.ticket.warehouseFk }; let query = `/api/Agencies/getShipped`; this.$http.get(query, {params}).then(res => { if (res.data) this.ticket.shipped = res.data; else { return this.vnApp.showError( this.$translate.instant(`There's no available agency for this landing date`) ); } }); } onChangeAddress(value) { let filter = { include: [ { relation: 'province', scope: { fields: ['name'] } }, { relation: 'agencyMode', scope: { fields: ['name'] } } ] }; filter = encodeURIComponent(JSON.stringify(filter)); let query = `/api/Clients/${value}/addresses?filter=${filter}`; this.$http.get(query).then(res => { if (res.data) this.addresses = res.data; }); } async onStepChange() { if (this.isFormInvalid()) { return this.vnApp.showError( this.$translate.instant('Some fields are invalid') ); } let query = `/ticket/api/sales/${this.ticket.id}/priceDifference`; let data = { landed: this.ticket.landed, addressFk: this.ticket.addressFk, agencyModeFk: this.ticket.agencyModeFk, warehouseFk: this.ticket.warehouseFk }; return this.$http.post(query, data).then(res => { if (res.data) this.ticket.sale = res.data; return true; }, err => { if (err.data.error.message === 'NO_AGENCY_AVAILABLE') { this.vnApp.showMessage( this.$translate.instant(`There's no available agency for this landing date`) ); } else { this.vnApp.showMessage( this.$translate.instant(err.data.error.message) ); } }); } isFormInvalid() { return !this.ticket.clientFk || !this.ticket.addressFk || !this.ticket.agencyModeFk || !this.ticket.companyFk || !this.ticket.shipped || !this.ticket.landed; } } Controller.$inject = ['$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnTicketBasicDataStepOne', { template: require('./index.html'), controller: Controller, bindings: { ticket: '<' }, require: { data: '^vnTicketBasicData' } });