salix/modules/order/front/prices-popover/index.js

124 lines
3.0 KiB
JavaScript

import ngModule from '../module';
import Popover from 'core/components/popover';
import './style.scss';
class Controller extends Popover {
constructor(...args) {
super(...args);
this.totalBasquet = 0;
}
set prices(value) {
this._prices = value;
if (value && value[0].grouping)
this.calculateTotal();
}
get prices() {
return this._prices;
}
getTags() {
const filter = {
where: {
itemFk: this.id,
priority: {gte: 4}
},
order: 'priority ASC',
include: {relation: 'tag'}
};
this.$http.get(`ItemTags`, {filter})
.then(res => {
this.tags = res.data;
this.$.$applyAsync(() => this.relocate());
});
}
show(parent, item) {
this.item = JSON.parse(JSON.stringify(item));
this.id = item.id;
this.prices = this.item.prices;
this.getTags();
super.show(parent);
}
onClose() {
this.id = null;
this.item = {};
this.tags = {};
this._prices = {};
this.total = 0;
super.onClose();
}
calculateMax() {
this.max = this.item.available - this.total;
}
calculateTotal() {
this.total = 0;
this.prices.forEach(price => {
if (!price.quantity) price.quantity = 0;
this.total += price.quantity;
});
this.calculateMax();
}
addQuantity(price) {
if (this.total + price.grouping <= this.max)
price.quantity += price.grouping;
}
getFilledLines() {
const filledLines = [];
let match;
this.prices.forEach(price => {
if (price.quantity && price.quantity > 0) {
match = filledLines.find(element => {
return element.warehouseFk == price.warehouseFk;
});
if (!match) {
filledLines.push(Object.assign({}, price));
return;
}
match.quantity += price.quantity;
}
});
return filledLines;
}
submit() {
this.calculateTotal();
const filledLines = this.getFilledLines();
if (filledLines.length <= 0) {
this.vnApp.showError(this.$t('First you must add some quantity'));
return;
}
const params = {
orderFk: this.order.id,
items: filledLines
};
this.$http.post(`OrderRows/addToOrder`, params)
.then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.hide();
if (this.card) this.card.reload();
});
}
}
ngModule.vnComponent('vnOrderPricesPopover', {
slotTemplate: require('./index.html'),
controller: Controller,
bindings: {
order: '<'
},
require: {
card: '?^vnOrderCard'
}
});