salix/modules/ticket/front/create/card.js

142 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-11-27 12:28:15 +00:00
import ngModule from '../module';
class Controller {
2019-10-09 22:47:29 +00:00
constructor($http, vnApp, $translate, $state, $stateParams, vnConfig) {
2018-11-27 12:28:15 +00:00
this.$stateParams = $stateParams;
this.$http = $http;
this.translate = $translate;
this.vnApp = vnApp;
2019-10-09 22:47:29 +00:00
this.vnConfig = vnConfig;
2018-11-27 12:28:15 +00:00
this.ticket = {};
this.$state = $state;
this.clientFk = $stateParams.clientFk;
}
$onInit() {
if (this.$stateParams && this.$stateParams.clientFk)
2019-12-26 12:29:50 +00:00
this.clientId = this.$stateParams.clientFk;
2019-01-25 10:58:50 +00:00
2019-12-26 12:29:50 +00:00
this.warehouseId = this.vnConfig.warehouseFk;
2018-11-27 12:28:15 +00:00
}
get ticket() {
return this._ticket;
}
2018-11-27 12:28:15 +00:00
set ticket(value) {
if (value)
this._ticket = value;
}
get clientId() {
return this.ticket.clientFk;
2018-11-27 12:28:15 +00:00
}
set clientId(value) {
2018-11-27 12:28:15 +00:00
this.ticket.clientFk = value;
if (value) {
let filter = {
include: {
relation: 'defaultAddress',
scope: {
fields: ['id', 'agencyModeFk']
}
},
where: {id: value}
};
2018-11-27 12:28:15 +00:00
filter = encodeURIComponent(JSON.stringify(filter));
let query = `Clients?filter=${filter}`;
2018-11-27 12:28:15 +00:00
this.$http.get(query).then(res => {
const [client] = res.data;
this.defaultAddress = client.defaultAddress;
this.addressId = this.defaultAddress.id;
2018-11-27 12:28:15 +00:00
});
} else
this.addressId = null;
2019-01-25 10:58:50 +00:00
this.getAvailableAgencies();
2018-11-27 12:28:15 +00:00
}
get addressId() {
return this.ticket.addressFk;
2018-11-27 12:28:15 +00:00
}
set addressId(value) {
2018-11-27 12:28:15 +00:00
this.ticket.addressFk = value;
2019-01-25 10:58:50 +00:00
this.getAvailableAgencies();
2018-11-27 12:28:15 +00:00
}
get landed() {
return this.ticket.landed;
2018-11-27 12:28:15 +00:00
}
set landed(value) {
this.ticket.landed = value;
2019-01-25 10:58:50 +00:00
this.getAvailableAgencies();
2018-11-27 12:28:15 +00:00
}
get warehouseId() {
return this.ticket.warehouseFk;
2018-11-27 12:28:15 +00:00
}
set warehouseId(value) {
2018-11-27 12:28:15 +00:00
this.ticket.warehouseFk = value;
this.getAvailableAgencies();
}
get agencyModeId() {
return this.ticket.agencyModeFk;
}
set agencyModeId(value) {
this.ticket.agencyModeFk = value;
2018-11-27 12:28:15 +00:00
}
getAvailableAgencies() {
2019-11-11 15:32:03 +00:00
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;
this.agencyModeId = this.defaultAddress.agencyModeFk;
});
2018-11-27 12:28:15 +00:00
}
}
onSubmit() {
this.createTicket();
}
createTicket() {
let params = {
clientId: this.ticket.clientFk,
2018-11-27 12:28:15 +00:00
landed: this.ticket.landed,
addressId: this.ticket.addressFk,
agencyModeId: this.ticket.agencyModeFk,
warehouseId: this.ticket.warehouseFk,
2018-11-27 12:28:15 +00:00
};
this.$http.post(`Tickets/new`, params).then(res => {
2018-11-27 12:28:15 +00:00
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
this.$state.go('ticket.card.summary', {id: res.data.id});
});
}
}
2019-10-09 22:47:29 +00:00
Controller.$inject = ['$http', 'vnApp', '$translate', '$state', '$stateParams', 'vnConfig'];
2018-11-27 12:28:15 +00:00
ngModule.component('vnTicketCreateCard', {
template: require('./card.html'),
controller: Controller,
bindings: {
ticket: '<?'
}
});