salix/modules/ticket/front/data/step-one/index.js

168 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-04-04 11:56:16 +00:00
import ngModule from '../../module';
2019-02-05 10:25:47 +00:00
import './style.scss';
2018-04-04 11:56:16 +00:00
class Controller {
2018-05-22 06:14:16 +00:00
constructor($scope, $http, $translate, vnApp) {
2018-04-04 11:56:16 +00:00
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;
}
2018-07-02 11:14:14 +00:00
set clientFk(value) {
this.ticket.clientFk = value;
this.ticket.addressFk = null;
this.onChangeAddress(value);
}
2018-07-02 11:14:14 +00:00
get clientFk() {
2018-05-22 06:14:16 +00:00
if (this.ticket)
2018-07-02 11:14:14 +00:00
return this.ticket.clientFk;
2019-01-24 14:41:48 +00:00
return null;
2018-07-02 11:14:14 +00:00
}
2018-08-16 11:12:18 +00:00
set shipped(value) {
this.ticket.shipped = value;
this.onChangeShipped(value);
}
get shipped() {
if (this.ticket)
return this.ticket.shipped;
2019-01-24 14:41:48 +00:00
return null;
2018-08-16 11:12:18 +00:00
}
set landed(value) {
this.ticket.landed = value;
this.onChangeLanded(value);
}
get landed() {
if (this.ticket)
return this.ticket.landed;
2019-01-24 14:41:48 +00:00
return null;
}
2018-08-16 11:12:18 +00:00
onChangeShipped(value) {
let data = {
shipped: value,
2018-08-16 11:12:18 +00:00
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
let query = `/api/Tickets/getLanded`;
this.$http.post(query, data).then(res => {
if (res.data && res.data.landed)
this.ticket.landed = res.data.landed;
});
}
onChangeLanded(value) {
let data = {
landed: value,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk
};
let query = `/api/Tickets/getShipped`;
this.$http.post(query, data).then(res => {
if (res.data && res.data.shipped)
this.ticket.shipped = res.data.shipped;
});
}
onChangeAddress(value) {
let filter = {
include: [
{
relation: 'province',
scope: {
fields: ['name']
}
2019-01-24 14:41:48 +00:00
},
{
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(state) {
2019-01-24 14:41:48 +00:00
if (this.isFormInvalid()) {
return this.vnApp.showError(
this.$translate.instant('Some fields are invalid')
);
2019-01-24 14:41:48 +00:00
}
let query = `/ticket/api/sales/${this.ticket.id}/priceDifference`;
let data = {
landed: this.ticket.landed,
2018-05-22 06:14:16 +00:00
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
return this.$http.post(query, data).then(res => {
2018-05-22 06:14:16 +00:00
if (res.data)
this.ticket.sale = res.data;
2018-05-22 06:14:16 +00:00
return true;
}, res => {
2019-01-24 14:41:48 +00:00
if (res.data.error.message === 'NO_AGENCY_AVAILABLE') {
2018-08-06 12:25:11 +00:00
this.vnApp.showMessage(
this.$translate.instant(`There's no available agency for this landing date`)
);
2019-01-24 14:41:48 +00:00
}
});
}
isFormInvalid() {
2018-05-22 06:14:16 +00:00
return !this.ticket.clientFk || !this.ticket.addressFk || !this.ticket.agencyModeFk
|| !this.ticket.companyFk || !this.ticket.shipped || !this.ticket.landed;
2018-04-04 11:56:16 +00:00
}
}
2018-05-22 06:14:16 +00:00
Controller.$inject = ['$scope', '$http', '$translate', 'vnApp'];
2018-04-04 11:56:16 +00:00
ngModule.component('vnTicketDataStepOne', {
template: require('./index.html'),
controller: Controller,
2018-04-04 11:56:16 +00:00
bindings: {
ticket: '<'
},
require: {
data: '^vnTicketData'
}
2018-04-04 11:56:16 +00:00
});