132 lines
3.4 KiB
JavaScript
132 lines
3.4 KiB
JavaScript
import ngModule from '../module';
|
|
|
|
class Controller {
|
|
constructor($http, vnApp, $translate, $state, $stateParams, vnConfig) {
|
|
this.$stateParams = $stateParams;
|
|
this.$http = $http;
|
|
this.translate = $translate;
|
|
this.vnApp = vnApp;
|
|
this.vnConfig = vnConfig;
|
|
this.ticket = {};
|
|
this.$state = $state;
|
|
this.clientFk = $stateParams.clientFk;
|
|
}
|
|
|
|
$onInit() {
|
|
if (this.$stateParams && this.$stateParams.clientFk)
|
|
this.clientFk = this.$stateParams.clientFk;
|
|
|
|
this.warehouseFk = this.vnConfig.warehouseFk;
|
|
}
|
|
|
|
set ticket(value) {
|
|
if (value)
|
|
this._ticket = value;
|
|
}
|
|
|
|
get ticket() {
|
|
return this._ticket;
|
|
}
|
|
|
|
set clientFk(value) {
|
|
this.ticket.clientFk = value;
|
|
|
|
if (value) {
|
|
let filter = {
|
|
include: {
|
|
relation: 'defaultAddress',
|
|
scope: {
|
|
fields: 'id'
|
|
}
|
|
},
|
|
where: {id: value}
|
|
};
|
|
filter = encodeURIComponent(JSON.stringify(filter));
|
|
let query = `Clients?filter=${filter}`;
|
|
this.$http.get(query).then(res => {
|
|
if (res.data) {
|
|
let client = res.data[0];
|
|
let defaultAddress = client.defaultAddress;
|
|
this.addressFk = defaultAddress.id;
|
|
}
|
|
});
|
|
} else
|
|
this.addressFk = null;
|
|
this.getAvailableAgencies();
|
|
}
|
|
|
|
get clientFk() {
|
|
return this.ticket.clientFk;
|
|
}
|
|
|
|
set addressFk(value) {
|
|
this.ticket.addressFk = value;
|
|
this.getAvailableAgencies();
|
|
}
|
|
|
|
get addressFk() {
|
|
return this.ticket.addressFk;
|
|
}
|
|
|
|
set landed(value) {
|
|
this.ticket.landed = value;
|
|
this.getAvailableAgencies();
|
|
}
|
|
|
|
get landed() {
|
|
return this.ticket.landed;
|
|
}
|
|
|
|
set warehouseFk(value) {
|
|
this.ticket.warehouseFk = value;
|
|
this.getAvailableAgencies();
|
|
}
|
|
get warehouseFk() {
|
|
return this.ticket.warehouseFk;
|
|
}
|
|
|
|
getAvailableAgencies() {
|
|
let ticket = this.ticket;
|
|
let params = {
|
|
warehouseFk: ticket.warehouseFk,
|
|
addressFk: ticket.addressFk,
|
|
landed: ticket.landed
|
|
};
|
|
|
|
if (params.warehouseFk && params.addressFk && params.landed) {
|
|
ticket.agencyModeFk = null;
|
|
|
|
this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params})
|
|
.then(res => this._availableAgencies = res.data);
|
|
}
|
|
}
|
|
|
|
onSubmit() {
|
|
this.createTicket();
|
|
}
|
|
|
|
createTicket() {
|
|
let params = {
|
|
clientFk: this.ticket.clientFk,
|
|
landed: this.ticket.landed,
|
|
addressFk: this.ticket.addressFk,
|
|
agencyModeFk: this.ticket.agencyModeFk,
|
|
warehouseFk: this.ticket.warehouseFk,
|
|
};
|
|
this.$http.post(`Tickets/new`, params).then(res => {
|
|
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
|
|
this.$state.go('ticket.card.summary', {id: res.data.id});
|
|
});
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$http', 'vnApp', '$translate', '$state', '$stateParams', 'vnConfig'];
|
|
|
|
ngModule.component('vnTicketCreateCard', {
|
|
template: require('./card.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
ticket: '<?'
|
|
}
|
|
});
|