2018-08-20 07:01:38 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class Controller {
|
2018-10-15 08:48:06 +00:00
|
|
|
constructor($scope, $http, $timeout, $element, $translate, vnApp) {
|
2018-08-20 07:01:38 +00:00
|
|
|
this.$ = $scope;
|
2018-09-27 06:42:36 +00:00
|
|
|
this.$timeout = $timeout;
|
2018-08-20 07:01:38 +00:00
|
|
|
this.$http = $http;
|
2018-09-14 12:20:58 +00:00
|
|
|
this.$element = $element;
|
2018-10-15 08:48:06 +00:00
|
|
|
this.$translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
2018-08-20 07:01:38 +00:00
|
|
|
this.totalBasquet = 0;
|
|
|
|
}
|
2018-09-14 12:20:58 +00:00
|
|
|
set prices(value) {
|
|
|
|
this._prices = value;
|
|
|
|
if (value && value[0].grouping)
|
|
|
|
this.calculateTotal();
|
|
|
|
}
|
|
|
|
get prices() {
|
|
|
|
return this._prices;
|
|
|
|
}
|
2018-08-20 07:01:38 +00:00
|
|
|
getTags() {
|
|
|
|
let filter = {
|
|
|
|
where: {itemFk: this.item.id,
|
|
|
|
priority: {gte: 4}},
|
|
|
|
order: 'priority ASC',
|
|
|
|
include: {relation: 'tag'}
|
|
|
|
};
|
|
|
|
this.$http.get(`/item/api/ItemTags?filter=${JSON.stringify(filter)}`).then(response => {
|
|
|
|
this.tags = response.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
show(event, item) {
|
|
|
|
this.item = JSON.parse(JSON.stringify(item));
|
|
|
|
this.prices = this.item.prices;
|
|
|
|
this.getTags();
|
|
|
|
this.$.popover.parent = event.target;
|
|
|
|
this.$.popover.relocate();
|
|
|
|
this.$.popover.show();
|
|
|
|
}
|
|
|
|
clear() {
|
|
|
|
this.item = {};
|
|
|
|
this.tags = {};
|
2018-09-14 12:20:58 +00:00
|
|
|
this._prices = {};
|
|
|
|
this.total = 0;
|
|
|
|
}
|
|
|
|
calculateMax() {
|
|
|
|
this.max = this.item.available - this.total;
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
2018-09-14 12:20:58 +00:00
|
|
|
calculateTotal() {
|
|
|
|
this.total = 0;
|
|
|
|
|
|
|
|
this.prices.forEach(price => {
|
|
|
|
if (!price.quantity) price.quantity = 0;
|
|
|
|
this.total += price.quantity;
|
|
|
|
});
|
|
|
|
this.calculateMax();
|
|
|
|
}
|
|
|
|
|
2018-08-20 07:01:38 +00:00
|
|
|
addQuantity(price) {
|
2019-01-14 10:35:48 +00:00
|
|
|
if (this.total + price.grouping <= this.max)
|
2018-08-20 07:01:38 +00:00
|
|
|
price.quantity += price.grouping;
|
2019-01-14 10:35:48 +00:00
|
|
|
|
2018-09-14 12:20:58 +00:00
|
|
|
this.validate();
|
2018-08-20 07:01:38 +00:00
|
|
|
}
|
2018-09-14 12:20:58 +00:00
|
|
|
validate() {
|
2018-09-27 06:42:36 +00:00
|
|
|
this.$timeout(() => {
|
2018-09-14 12:20:58 +00:00
|
|
|
this.calculateTotal();
|
|
|
|
let inputs = this.$element[0].querySelectorAll('vn-input-number[name="quantity"] div.infix:not(.validated)');
|
|
|
|
|
|
|
|
inputs.forEach(input => {
|
|
|
|
if (this.total > this.item.available)
|
|
|
|
input.classList.add('invalid');
|
|
|
|
else
|
|
|
|
input.classList.remove('invalid');
|
|
|
|
});
|
|
|
|
let wrongInputs = this.$element[0].querySelectorAll('vn-input-number[name="quantity"] div.infix.invalid');
|
|
|
|
|
2019-01-14 10:35:48 +00:00
|
|
|
if (wrongInputs.length > 0)
|
2018-09-14 12:20:58 +00:00
|
|
|
this.$element[0].querySelector('vn-vertical.prices').classList.add('invalid');
|
2019-01-14 10:35:48 +00:00
|
|
|
else
|
2018-09-14 12:20:58 +00:00
|
|
|
this.$element[0].querySelector('vn-vertical.prices').classList.remove('invalid');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getFilledLines() {
|
|
|
|
let filledLines = [];
|
2018-09-17 12:30:39 +00:00
|
|
|
let match;
|
2018-09-14 12:20:58 +00:00
|
|
|
this.prices.forEach(price => {
|
2018-09-17 12:30:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-09-14 12:20:58 +00:00
|
|
|
});
|
|
|
|
return filledLines;
|
|
|
|
}
|
|
|
|
submit() {
|
|
|
|
this.calculateTotal();
|
2018-09-17 12:30:39 +00:00
|
|
|
let filledLines = this.getFilledLines();
|
2018-09-14 12:20:58 +00:00
|
|
|
|
2018-09-17 12:30:39 +00:00
|
|
|
if (filledLines.length <= 0) {
|
|
|
|
this.vnApp.showError('First you must add some quantity');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-14 12:20:58 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
let params = {
|
|
|
|
orderFk: this.order.id,
|
2018-09-17 12:30:39 +00:00
|
|
|
items: filledLines
|
2018-09-14 12:20:58 +00:00
|
|
|
};
|
|
|
|
|
2018-09-17 12:30:39 +00:00
|
|
|
this.$http.post(`/order/api/OrderRows/addToOrder`, params).then(res => {
|
2018-10-15 08:48:06 +00:00
|
|
|
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
|
2018-09-17 12:30:39 +00:00
|
|
|
this.$.popover.hide();
|
2019-01-14 10:35:48 +00:00
|
|
|
this.card.reload();
|
2018-09-14 12:20:58 +00:00
|
|
|
});
|
2018-08-20 07:01:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 08:48:06 +00:00
|
|
|
Controller.$inject = ['$scope', '$http', '$timeout', '$element', '$translate', 'vnApp'];
|
2018-08-20 07:01:38 +00:00
|
|
|
|
|
|
|
ngModule.component('vnOrderPricesPopover', {
|
|
|
|
template: 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
|
|
|
}
|
2018-08-20 07:01:38 +00:00
|
|
|
});
|