From 569cd60d7ba824277b044769821c94fef7df8f6f Mon Sep 17 00:00:00 2001 From: gerard Date: Fri, 14 Sep 2018 14:20:58 +0200 Subject: [PATCH] =?UTF-8?q?Tarea=20#601=20dialogo=20de=20a=C3=B1adir=20can?= =?UTF-8?q?tidad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/order/src/prices-popover/index.html | 22 ++-- client/order/src/prices-popover/index.js | 112 ++++++++++++++---- client/order/src/prices-popover/locale/es.yml | 3 +- client/order/src/prices-popover/style.scss | 17 ++- 4 files changed, 124 insertions(+), 30 deletions(-) diff --git a/client/order/src/prices-popover/index.html b/client/order/src/prices-popover/index.html index 132058e7e..6a6957edd 100644 --- a/client/order/src/prices-popover/index.html +++ b/client/order/src/prices-popover/index.html @@ -34,21 +34,28 @@ ng-click="$ctrl.saveQuantity($ctrl.prices)"> +
{{::price.warehouse}} - {{::price.grouping}} x {{::price.price | currency: ' €': 2}} + {{::price.grouping}} x {{::price.price | currency: ' €': 2}} - - + min="0" + step="price.grouping" + on-change="$ctrl.validate()"> + +
+ + Wrong quantity +
@@ -59,9 +66,10 @@ - + ng-click="$ctrl.submit()"> + diff --git a/client/order/src/prices-popover/index.js b/client/order/src/prices-popover/index.js index 010fd94fa..0354529ae 100644 --- a/client/order/src/prices-popover/index.js +++ b/client/order/src/prices-popover/index.js @@ -2,12 +2,20 @@ import ngModule from '../module'; import './style.scss'; class Controller { - constructor($scope, $http) { + constructor($scope, $http, $element) { this.$ = $scope; this.$http = $http; + this.$element = $element; this.totalBasquet = 0; } - + set prices(value) { + this._prices = value; + if (value && value[0].grouping) + this.calculateTotal(); + } + get prices() { + return this._prices; + } getTags() { let filter = { where: {itemFk: this.item.id, @@ -19,7 +27,6 @@ class Controller { this.tags = response.data; }); } - show(event, item) { this.item = JSON.parse(JSON.stringify(item)); this.prices = this.item.prices; @@ -28,35 +35,98 @@ class Controller { this.$.popover.relocate(); this.$.popover.show(); } - clear() { this.item = {}; this.tags = {}; - this.prices = {}; - this.totalBasquet = 0; + this._prices = {}; + this.total = 0; } - addQuantity(price) { - if (this.totalBasquet + price.grouping <= this.item.disponible) { - price.quantity += price.grouping; - this.totalBasquet = this.totalBasquet + price.grouping; - } + calculateMax() { + this.max = this.item.available - this.total; } - save() { - let params = { - warehouseFk: warehouseFk, - itemFk: itemFk, - quantity: quantity - }; + calculateTotal() { + this.total = 0; - this.$http.post(`/order/api/OrderRows/new`, params).then(response => { - this.tags = response.data; + 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; + } + this.validate(); + } + + validate() { + setTimeout(() => { + 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'); + + if (wrongInputs.length > 0) { + this.$element[0].querySelector('vn-vertical.prices').classList.add('invalid'); + } else { + this.$element[0].querySelector('vn-vertical.prices').classList.remove('invalid'); + } + }); + } + + focusFirstInput() { + let inputs = document.querySelectorAll('input[name="quantity"]'); + inputs[inputs.length - 1].querySelector("input").focus(); + this.calculateTotals(); + } + + getFilledLines() { + let filledLines = []; + this.prices.forEach(price => { + if (price.quantity && price.quantity > 0) + filledLines.push(price); + }); + return filledLines; + } + + submit() { + this.calculateTotal(); + let filledLines = getFilledLines(); + + if (filledLines.length <= 0) return; + + setTimeout(() => { + let params = { + orderFk: this.order.id, + lines: filledLines, + itemFk: this.item.id, + landed: this.order.landed, + addressFk: this.order.address_id, + agencyFk: this.order.agency_id + }; + + this.$http.post(`/order/api/OrderRows/new`, params).then(res => { + console.log(res.data); + this.tags = res.data; + }); }); } } -Controller.$inject = ['$scope', '$http']; +Controller.$inject = ['$scope', '$http', '$element']; ngModule.component('vnOrderPricesPopover', { template: require('./index.html'), - controller: Controller + controller: Controller, + bindings: { + order: '<' + } }); diff --git a/client/order/src/prices-popover/locale/es.yml b/client/order/src/prices-popover/locale/es.yml index c151537ba..0fc7767f2 100644 --- a/client/order/src/prices-popover/locale/es.yml +++ b/client/order/src/prices-popover/locale/es.yml @@ -1,2 +1,3 @@ Last entries: Últimas entradas -Qty.: Cant. \ No newline at end of file +Qty.: Cant. +Wrong quantity: Cantidad errónea \ No newline at end of file diff --git a/client/order/src/prices-popover/style.scss b/client/order/src/prices-popover/style.scss index d9d590661..80bc98b57 100644 --- a/client/order/src/prices-popover/style.scss +++ b/client/order/src/prices-popover/style.scss @@ -19,10 +19,22 @@ vn-order-prices-popover { vn-vertical.prices{ padding-left: 16px; + &.invalid { + vn-one.error { + display: block; + padding-top: 10px; + color: #d50000; + text-align: center!important; + } + } } - vn-textfield{ + vn-input-number{ margin: 0!important; + + label { + display: none; + } } vn-one.number { @@ -37,4 +49,7 @@ vn-order-prices-popover { padding-top: 16px; text-align: center; } + vn-one.error{ + display: none; + } } \ No newline at end of file