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

262 lines
6.5 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.basicData.registerChild(this);
}
get ticket() {
return this._ticket;
}
set ticket(value) {
this._ticket = value;
if (!value || !value.id) return;
this.onChangeClient(value.clientFk);
}
get clientId() {
if (this.ticket)
return this.ticket.clientFk;
return null;
}
set clientId(value) {
2018-07-02 11:14:14 +00:00
this.ticket.clientFk = value;
this.ticket.addressFk = null;
this.onChangeClient(value);
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
}
get landed() {
if (this.ticket)
return this.ticket.landed;
return null;
}
set landed(value) {
this.ticket.landed = value;
this.onChangeLanded(value);
}
get agencyModeId() {
if (this.ticket)
return this.ticket.agencyModeFk;
return null;
}
set agencyModeId(value) {
this.ticket.agencyModeFk = value;
if (value)
this.onChangeAgencyMode(value);
}
get zoneId() {
if (this.ticket)
return this.ticket.zoneFk;
2019-01-24 14:41:48 +00:00
return null;
}
set zoneId(value) {
this.ticket.zoneFk = value;
if (value)
this.onChangeZone(value);
}
/*
* Autocompletes address on client change
*/
onChangeClient(value) {
let filter = {
include: [
{
relation: 'province',
scope: {
fields: ['name']
}
},
{
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;
});
}
/*
* Returns a landing date
*/
getLanded(params) {
let query = `/api/Agencies/getLanded`;
return this.$http.get(query, {params});
}
/*
* Returns a shipment date
*/
getShipped(params) {
let query = `/api/Agencies/getShipped`;
return this.$http.get(query, {params});
}
onChangeShipped(shipped) {
2019-05-30 06:41:08 +00:00
let params = {
shipped: shipped,
2018-08-16 11:12:18 +00:00
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
2019-05-30 06:41:08 +00:00
let query = `/api/Agencies/getLanded`;
this.$http.get(query, {params}).then(res => {
if (res.data && res.data.landed) {
this.ticket.zoneFk = res.data.zoneFk;
2018-08-16 11:12:18 +00:00
this.ticket.landed = res.data.landed;
} else {
2019-03-26 11:02:11 +00:00
return this.vnApp.showError(
this.$translate.instant(`No delivery zone available for this shipping date`)
2019-03-26 11:02:11 +00:00
);
}
2018-08-16 11:12:18 +00:00
});
}
onChangeLanded(landed) {
2019-05-30 06:41:08 +00:00
let params = {
landed: landed,
addressFk: this.ticket.addressFk,
2019-03-14 09:43:14 +00:00
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
2019-05-30 06:41:08 +00:00
let query = `/api/Agencies/getShipped`;
this.$http.get(query, {params}).then(res => {
if (res.data) {
this.ticket.zoneFk = res.data.id;
this.ticket.shipped = res.data.shipped;
} else {
2019-03-26 11:02:11 +00:00
return this.vnApp.showError(
this.$translate.instant(`No delivery zone available for this landing date`)
2019-03-26 11:02:11 +00:00
);
}
});
}
/*
* Gets an agency from an specified zone
*/
onChangeZone(zoneId) {
const query = `/api/Zones/${zoneId}`;
this.$http.get(query).then(res => {
if (res.data)
this.ticket.agencyModeFk = res.data.agencyModeFk;
});
}
/*
* Gets a zone from an agency
*/
onChangeAgencyMode(agencyModeId) {
let params = {
landed: this.ticket.landed,
addressFk: this.ticket.addressFk,
agencyModeFk: agencyModeId,
warehouseFk: this.ticket.warehouseFk
};
this.ticket.zoneFk = null;
let query = `/api/Agencies/getShipped`;
this.$http.get(query, {params}).then(res => {
if (res.data)
this.ticket.zoneFk = res.data.id;
else {
return this.vnApp.showMessage(
this.$translate.instant('No delivery zone available for this parameters')
);
}
});
}
async onStepChange() {
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 = `/api/tickets/${this.ticket.id}/priceDifference`;
let params = {
landed: this.ticket.landed,
addressId: this.ticket.addressFk,
agencyModeId: this.ticket.agencyModeFk,
zoneId: this.ticket.zoneFk,
warehouseId: this.ticket.warehouseFk
};
return this.$http.post(query, params).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;
}, err => {
this.vnApp.showError(
this.$translate.instant(err.data.error.message)
);
});
}
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
|| !this.ticket.zoneFk;
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('vnTicketBasicDataStepOne', {
template: require('./index.html'),
controller: Controller,
2018-04-04 11:56:16 +00:00
bindings: {
ticket: '<'
},
require: {
basicData: '^vnTicketBasicData'
}
2018-04-04 11:56:16 +00:00
});