2018-07-09 11:54:43 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
|
|
|
|
class Controller {
|
2018-11-23 06:59:51 +00:00
|
|
|
constructor($http, vnApp, $translate, $state, $stateParams) {
|
|
|
|
this.$stateParams = $stateParams;
|
2018-07-09 11:54:43 +00:00
|
|
|
this.$http = $http;
|
|
|
|
this.translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
2018-07-24 11:48:31 +00:00
|
|
|
this.order = {};
|
|
|
|
this.$state = $state;
|
2018-11-23 06:59:51 +00:00
|
|
|
this.clientFk = $stateParams.clientFk;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
if (this.$stateParams && this.$stateParams.clientFk)
|
|
|
|
this.clientFk = this.$stateParams.clientFk;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 11:48:31 +00:00
|
|
|
set order(value) {
|
2018-11-22 12:56:35 +00:00
|
|
|
if (value)
|
2018-07-24 11:48:31 +00:00
|
|
|
this._order = value;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 11:48:31 +00:00
|
|
|
get order() {
|
|
|
|
return this._order;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set clientFk(value) {
|
2018-07-24 11:48:31 +00:00
|
|
|
this.order.clientFk = value;
|
2018-11-22 12:56:35 +00:00
|
|
|
|
|
|
|
if (value) {
|
2019-02-28 07:55:34 +00:00
|
|
|
let filter = {
|
|
|
|
include: {
|
|
|
|
relation: 'defaultAddress',
|
|
|
|
scope: {
|
|
|
|
fields: 'id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
where: {id: value}
|
|
|
|
};
|
2018-11-22 12:56:35 +00:00
|
|
|
filter = encodeURIComponent(JSON.stringify(filter));
|
2019-10-24 22:53:53 +00:00
|
|
|
let query = `Clients?filter=${filter}`;
|
2018-11-22 12:56:35 +00:00
|
|
|
this.$http.get(query).then(res => {
|
2019-02-28 07:55:34 +00:00
|
|
|
if (res.data) {
|
|
|
|
let client = res.data[0];
|
|
|
|
let defaultAddress = client.defaultAddress;
|
|
|
|
this.addressFk = defaultAddress.id;
|
|
|
|
}
|
2018-11-22 12:56:35 +00:00
|
|
|
});
|
|
|
|
} else
|
|
|
|
this.addressFk = null;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get clientFk() {
|
2018-07-24 11:48:31 +00:00
|
|
|
return this.order.clientFk;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set addressFk(value) {
|
2018-07-24 11:48:31 +00:00
|
|
|
this.order.addressFk = value;
|
2018-07-17 11:04:24 +00:00
|
|
|
this.getAvailableAgencies();
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get addressFk() {
|
2018-07-24 11:48:31 +00:00
|
|
|
return this.order.addressFk;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set landed(value) {
|
2018-07-24 11:48:31 +00:00
|
|
|
this.order.landed = value;
|
2018-07-17 11:04:24 +00:00
|
|
|
this.getAvailableAgencies();
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get landed() {
|
2018-07-24 11:48:31 +00:00
|
|
|
return this.order.landed;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get warehouseFk() {
|
2018-07-24 11:48:31 +00:00
|
|
|
return this.order.warehouseFk;
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 11:04:24 +00:00
|
|
|
getAvailableAgencies() {
|
2019-11-11 15:32:03 +00:00
|
|
|
let order = this.order;
|
|
|
|
order.agencyModeFk = null;
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
addressFk: order.addressFk,
|
|
|
|
landed: order.landed
|
|
|
|
};
|
|
|
|
if (params.landed && params.addressFk) {
|
|
|
|
this.$http.get(`Agencies/landsThatDay`, {params})
|
|
|
|
.then(res => this._availableAgencies = res.data);
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
this.createOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
createOrder() {
|
|
|
|
let params = {
|
2018-07-24 11:48:31 +00:00
|
|
|
landed: this.order.landed,
|
2019-06-03 08:05:22 +00:00
|
|
|
addressId: this.order.addressFk,
|
|
|
|
agencyModeId: this.order.agencyModeFk
|
2018-07-09 11:54:43 +00:00
|
|
|
};
|
2019-10-24 22:53:53 +00:00
|
|
|
this.$http.post(`Orders/new`, params).then(res => {
|
2018-07-09 11:54:43 +00:00
|
|
|
this.vnApp.showSuccess(this.translate.instant('Data saved!'));
|
2018-11-22 12:56:35 +00:00
|
|
|
this.$state.go('order.card.catalog', {id: res.data});
|
2018-07-09 11:54:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:59:51 +00:00
|
|
|
Controller.$inject = ['$http', 'vnApp', '$translate', '$state', '$stateParams'];
|
2018-07-09 11:54:43 +00:00
|
|
|
|
2018-07-24 11:48:31 +00:00
|
|
|
ngModule.component('vnOrderCreateCard', {
|
2018-07-09 11:54:43 +00:00
|
|
|
template: require('./card.html'),
|
|
|
|
controller: Controller,
|
|
|
|
bindings: {
|
2018-07-24 11:48:31 +00:00
|
|
|
order: '<?'
|
2018-07-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
});
|