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

259 lines
6.6 KiB
JavaScript

import ngModule from '../../module';
import './style.scss';
class Controller {
constructor($scope, $http, $translate, vnApp) {
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) {
this.ticket.clientFk = value;
this.ticket.addressFk = null;
this.onChangeClient(value);
}
get warehouseId() {
if (this.ticket)
return this.ticket.warehouseFk;
return null;
}
set warehouseId(value) {
if (value != this.ticket.warehouseFk) {
this.ticket.warehouseFk = value;
this.getShipped({
landed: this.ticket.landed,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: value
});
}
}
get shipped() {
if (this.ticket)
return this.ticket.shipped;
return null;
}
set shipped(value) {
this.ticket.shipped = value;
this.getLanded({
shipped: value,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
});
}
get landed() {
if (this.ticket)
return this.ticket.landed;
return null;
}
set landed(value) {
this.ticket.landed = value;
this.getShipped({
landed: value,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
});
}
get agencyModeId() {
if (this.ticket)
return this.ticket.agencyModeFk;
return null;
}
set agencyModeId(value) {
if (value != this.ticket.agencyModeFk) {
this.ticket.agencyModeFk = value;
this.getShipped({
landed: this.ticket.landed,
addressFk: this.ticket.addressFk,
agencyModeFk: value,
warehouseFk: this.ticket.warehouseFk
});
}
}
get zoneId() {
if (this.ticket)
return this.ticket.zoneFk;
return null;
}
set zoneId(value) {
if (value != this.ticket.zoneFk) {
this.ticket.zoneFk = 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;
});
}
/*
* Gets an agency from an specified zone
*/
onChangeZone(zoneId) {
this.ticket.agencyModeFk = null;
const query = `/api/Zones/${zoneId}`;
this.$http.get(query).then(res => {
if (res.data) {
this.ticket.agencyModeFk = res.data.agencyModeFk;
this.ticket.warehouseFk = res.data.warehouseFk;
}
});
}
async onStepChange() {
if (this.isFormInvalid()) {
return this.vnApp.showError(
this.$translate.instant('Some fields are invalid')
);
}
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 => {
if (res.data)
this.ticket.sale = res.data;
return true;
}, err => {
this.vnApp.showError(
this.$translate.instant(err.data.error.message)
);
});
}
/*
* Returns a landing date
*/
getLanded(params) {
this.ticket.zoneFk = null;
const query = `/api/Agencies/getLanded`;
this.$http.get(query, {params}).then(res => {
if (res.data) {
this.ticket.zoneFk = res.data.zoneFk;
this.ticket.landed = res.data.landed;
this.ticket.agencyModeFk = res.data.agencyModeFk;
this.ticket.warehouseFk = res.data.warehouseFk;
} else {
return this.vnApp.showError(
this.$translate.instant(`No delivery zone available for this landing date`)
);
}
});
}
/*
* Returns a shipment date
*/
getShipped(params) {
this.ticket.zoneFk = null;
const query = `/api/Agencies/getShipped`;
this.$http.get(query, {params}).then(res => {
if (res.data) {
this.ticket.zoneFk = res.data.zoneFk;
this.ticket.shipped = res.data.shipped;
this.ticket.agencyModeFk = res.data.agencyModeFk;
this.ticket.warehouseFk = res.data.warehouseFk;
} else {
return this.vnApp.showError(
this.$translate.instant(`No delivery zone available for this landing date`)
);
}
});
}
isFormInvalid() {
return !this.ticket.clientFk || !this.ticket.addressFk || !this.ticket.agencyModeFk
|| !this.ticket.companyFk || !this.ticket.shipped || !this.ticket.landed
|| !this.ticket.zoneFk;
}
}
Controller.$inject = ['$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnTicketBasicDataStepOne', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
},
require: {
basicData: '^vnTicketBasicData'
}
});