2018-08-20 07:01:38 +00:00
|
|
|
import ngModule from '../module';
|
2020-05-06 12:38:09 +00:00
|
|
|
import Popover from 'core/components/popover';
|
2018-08-20 07:01:38 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
class Controller extends Popover {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2018-08-20 07:01:38 +00:00
|
|
|
this.totalBasquet = 0;
|
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
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
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
2018-09-14 12:20:58 +00:00
|
|
|
get prices() {
|
|
|
|
return this._prices;
|
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
show(parent, item) {
|
2018-08-20 07:01:38 +00:00
|
|
|
this.item = JSON.parse(JSON.stringify(item));
|
2020-05-06 12:38:09 +00:00
|
|
|
this.id = item.id;
|
2018-08-20 07:01:38 +00:00
|
|
|
this.prices = this.item.prices;
|
2021-04-15 12:30:30 +00:00
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
super.show(parent);
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
2020-05-08 11:23:06 +00:00
|
|
|
onClose() {
|
2020-05-06 12:38:09 +00:00
|
|
|
this.id = null;
|
2018-08-20 07:01:38 +00:00
|
|
|
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;
|
2020-05-08 11:23:06 +00:00
|
|
|
super.onClose();
|
2018-09-14 12:20:58 +00:00
|
|
|
}
|
2019-10-28 16:31:33 +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
|
|
|
}
|
|
|
|
|
2018-08-20 07:01:38 +00:00
|
|
|
addQuantity(price) {
|
2021-04-15 12:30:30 +00:00
|
|
|
this.getTotalQuantity();
|
|
|
|
const quantity = this.totalQuantity + price.grouping;
|
|
|
|
if (quantity <= this.maxQuantity)
|
2018-08-20 07:01:38 +00:00
|
|
|
price.quantity += price.grouping;
|
2018-09-14 12:20:58 +00:00
|
|
|
}
|
2019-10-28 16:31:33 +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
|
|
|
}
|
2019-10-28 16:31:33 +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;
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: {
|
2019-10-28 16:31:33 +00:00
|
|
|
card: '?^vnOrderCard'
|
2018-09-14 12:20:58 +00:00
|
|
|
}
|
2018-08-20 07:01:38 +00:00
|
|
|
});
|