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

130 lines
3.7 KiB
JavaScript

import ngModule from '../../module';
import Component from 'core/lib/component';
import './style.scss';
class Controller extends Component {
$onInit() {
this.data.registerChild(this);
}
get ticket() {
return this._ticket;
}
set ticket(value) {
this._ticket = value;
if (!value) return;
this.getTotalPrice();
this.getTotalNewPrice();
this.getTotalDifferenceOfPrice();
this.loadDefaultTicketAction();
this.ticketHaveNegatives();
}
loadDefaultTicketAction() {
const isSalesAssistant = this.aclService.hasAny(['salesAssistant']);
const defaultOption = isSalesAssistant ? 'turnInMana' : 'changePrice';
const filter = {where: {code: defaultOption}};
this.$http.get(`TicketUpdateActions`, {filter}).then(response => {
return this.ticket.option = response.data[0].id;
});
}
onStepChange() {
return true;
}
getTotalPrice() {
let totalPrice = 0;
this.ticket.sale.items.forEach(item => {
let itemTotalPrice = item.quantity * item.price;
totalPrice += itemTotalPrice;
});
this.totalPrice = totalPrice;
}
getTotalNewPrice() {
let totalNewPrice = 0;
this.ticket.sale.items.forEach(item => {
if (item.component)
totalNewPrice += item.quantity * item.component.newPrice;
});
this.totalNewPrice = totalNewPrice;
}
getTotalDifferenceOfPrice() {
let totalPriceDifference = 0;
this.ticket.sale.items.forEach(item => {
if (item.component)
totalPriceDifference += item.component.difference;
});
this.totalPriceDifference = totalPriceDifference;
}
ticketHaveNegatives() {
let haveNegatives = false;
let haveNotNegatives = false;
const haveDifferences = this.ticket.sale.haveDifferences;
this.ticket.sale.items.forEach(item => {
if (item.quantity > item.movable)
haveNegatives = true;
else
haveNotNegatives = true;
});
this.ticket.withoutNegatives = false;
this.haveNegatives = (haveNegatives && haveNotNegatives && haveDifferences);
}
onSubmit() {
if (!this.ticket.option) {
return this.vnApp.showError(
this.$t('Choose an option')
);
}
const query = `tickets/${this.ticket.id}/componentUpdate`;
const params = {
clientFk: this.ticket.clientFk,
nickname: this.ticket.nickname,
agencyModeFk: this.ticket.agencyModeFk,
addressFk: this.ticket.addressFk,
zoneFk: this.ticket.zoneFk,
warehouseFk: this.ticket.warehouseFk,
companyFk: this.ticket.companyFk,
shipped: this.ticket.shipped,
landed: this.ticket.landed,
isDeleted: this.ticket.isDeleted,
option: parseInt(this.ticket.option),
isWithoutNegatives: this.ticket.withoutNegatives
};
this.$http.post(query, params)
.then(res => {
this.ticketToMove = res.data.id;
this.vnApp.showMessage(
this.$t(`The ticket has been unrouted`)
);
})
.finally(() => {
this.$state.go('ticket.card.summary', {id: this.ticketToMove});
});
}
}
ngModule.vnComponent('vnTicketBasicDataStepTwo', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
},
require: {
card: '^vnTicketCard',
data: '^vnTicketBasicData'
}
});