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

115 lines
2.9 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
2020-05-06 12:38:09 +00:00
import Popover from 'core/components/popover';
import './style.scss';
2020-05-06 12:38:09 +00:00
class Controller extends Popover {
constructor(...args) {
super(...args);
this.totalBasquet = 0;
}
2018-09-14 12:20:58 +00:00
set prices(value) {
this._prices = value;
if (value && value[0].grouping)
2021-04-15 12:30:30 +00:00
this.getTotalQuantity();
2018-09-14 12:20:58 +00:00
}
2018-09-14 12:20:58 +00:00
get prices() {
return this._prices;
}
2020-05-06 12:38:09 +00:00
show(parent, item) {
this.item = JSON.parse(JSON.stringify(item));
2020-05-06 12:38:09 +00:00
this.id = item.id;
this.prices = this.item.prices;
2021-04-15 12:30:30 +00:00
2020-05-06 12:38:09 +00:00
super.show(parent);
}
onClose() {
2020-05-06 12:38:09 +00:00
this.id = null;
this.item = {};
this.tags = {};
2018-09-14 12:20:58 +00:00
this._prices = {};
2021-04-15 12:30:30 +00:00
this.totalQuantity = 0;
super.onClose();
2018-09-14 12:20:58 +00:00
}
2021-04-15 12:30:30 +00:00
getTotalQuantity() {
let total = 0;
for (let price of this.prices) {
2018-09-14 12:20:58 +00:00
if (!price.quantity) price.quantity = 0;
2021-04-15 12:30:30 +00:00
total += price.quantity;
}
this.totalQuantity = total;
this.maxQuantity = this.item.available - total;
2018-09-14 12:20:58 +00:00
}
addQuantity(price) {
2021-04-15 12:30:30 +00:00
this.getTotalQuantity();
const quantity = this.totalQuantity + price.grouping;
if (quantity <= this.maxQuantity)
price.quantity += price.grouping;
2018-09-14 12:20:58 +00:00
}
2021-04-15 12:30:30 +00:00
getGroupings() {
const filledRows = [];
for (let price of this.prices) {
2018-09-17 12:30:39 +00:00
if (price.quantity && price.quantity > 0) {
2021-04-15 12:30:30 +00:00
const priceMatch = filledRows.find(row => {
return row.warehouseFk == price.warehouseFk;
2018-09-17 12:30:39 +00:00
});
2021-04-15 12:30:30 +00:00
if (!priceMatch)
filledRows.push(Object.assign({}, price));
else priceMatch.quantity += price.quantity;
2018-09-17 12:30:39 +00:00
}
2021-04-15 12:30:30 +00:00
}
return filledRows;
2018-09-14 12:20:58 +00:00
}
2018-09-14 12:20:58 +00:00
submit() {
2021-04-15 12:30:30 +00:00
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;
2018-09-17 12:30:39 +00:00
}
2021-04-15 12:30:30 +00:00
return true;
}
}
2020-05-06 12:38:09 +00:00
ngModule.vnComponent('vnOrderPricesPopover', {
slotTemplate: require('./index.html'),
2018-09-14 12:20:58 +00:00
controller: Controller,
bindings: {
order: '<'
2019-01-14 10:35:48 +00:00
},
require: {
card: '?^vnOrderCard'
2018-09-14 12:20:58 +00:00
}
});