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

115 lines
2.9 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.getTotalQuantity();
}
get prices() {
return this._prices;
}
show(parent, item) {
this.item = JSON.parse(JSON.stringify(item));
this.id = item.id;
this.prices = this.item.prices;
super.show(parent);
}
onClose() {
this.id = null;
this.item = {};
this.tags = {};
this._prices = {};
this.totalQuantity = 0;
super.onClose();
}
getTotalQuantity() {
let total = 0;
for (let price of this.prices) {
if (!price.quantity) price.quantity = 0;
total += price.quantity;
}
this.totalQuantity = total;
this.maxQuantity = this.item.available - total;
}
addQuantity(price) {
this.getTotalQuantity();
const quantity = this.totalQuantity + price.grouping;
if (quantity <= this.maxQuantity)
price.quantity += price.grouping;
}
getGroupings() {
const filledRows = [];
for (let price of this.prices) {
if (price.quantity && price.quantity > 0) {
const priceMatch = filledRows.find(row => {
return row.warehouseFk == price.warehouseFk;
});
if (!priceMatch)
filledRows.push(Object.assign({}, price));
else priceMatch.quantity += price.quantity;
}
}
return filledRows;
}
submit() {
const filledRows = this.getGroupings();
try {
const hasValidGropings = filledRows.some(row =>
row.quantity % row.grouping == 0
);
if (filledRows.length <= 0)
throw new Error('First you must add some quantity');
if (!hasValidGropings)
throw new Error(`The amounts doesn't match with the grouping`);
const params = {
orderFk: this.order.id,
items: filledRows
};
this.$http.post(`OrderRows/addToOrder`, params)
.then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.hide();
if (this.card) this.card.reload();
});
} catch (e) {
this.vnApp.showError(this.$t(e.message));
return false;
}
return true;
}
}
ngModule.vnComponent('vnOrderPricesPopover', {
slotTemplate: require('./index.html'),
controller: Controller,
bindings: {
order: '<'
},
require: {
card: '?^vnOrderCard'
}
});