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

120 lines
3.0 KiB
JavaScript

import ngModule from '../module';
class Controller {
constructor($http, vnApp, $translate, $state, $stateParams) {
this.$stateParams = $stateParams;
this.$http = $http;
this.translate = $translate;
this.vnApp = vnApp;
this.order = {};
this.$state = $state;
this.clientFk = $stateParams.clientFk;
}
$onInit() {
if (this.$stateParams && this.$stateParams.clientFk)
this.clientFk = this.$stateParams.clientFk;
}
set order(value) {
if (value)
this._order = value;
}
get order() {
return this._order;
}
set clientFk(value) {
this.order.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;
}
get clientFk() {
return this.order.clientFk;
}
set addressFk(value) {
this.order.addressFk = value;
this.getAvailableAgencies();
}
get addressFk() {
return this.order.addressFk;
}
set landed(value) {
this.order.landed = value;
this.getAvailableAgencies();
}
get landed() {
return this.order.landed;
}
get warehouseFk() {
return this.order.warehouseFk;
}
getAvailableAgencies() {
this.order.agencyModeFk = null;
if (this.order.landed && this.order.addressFk) {
let filter = {
addressFk: this.order.addressFk,
landed: this.order.landed
};
this.$http.get(`Agencies/landsThatDay`, {params: {filter}})
.then(res => {
this._availableAgencies = res.data;
});
}
}
onSubmit() {
this.createOrder();
}
createOrder() {
let params = {
landed: this.order.landed,
addressId: this.order.addressFk,
agencyModeId: this.order.agencyModeFk
};
this.$http.post(`Orders/new`, params).then(res => {
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
this.$state.go('order.card.catalog', {id: res.data});
});
}
}
Controller.$inject = ['$http', 'vnApp', '$translate', '$state', '$stateParams'];
ngModule.component('vnOrderCreateCard', {
template: require('./card.html'),
controller: Controller,
bindings: {
order: '<?'
}
});