diff --git a/client/item/src/niche/item-niche.html b/client/item/src/niche/item-niche.html index 88206a4a4..f282644e9 100644 --- a/client/item/src/niche/item-niche.html +++ b/client/item/src/niche/item-niche.html @@ -1,49 +1,55 @@ + +
- Item Niches - - - - - - - - - - - - - + Item Niches + + + + + + + + + + + + + -
+ \ No newline at end of file diff --git a/client/item/src/niche/item-niche.js b/client/item/src/niche/item-niche.js index a6d25be5e..e762bcf90 100644 --- a/client/item/src/niche/item-niche.js +++ b/client/item/src/niche/item-niche.js @@ -1,8 +1,8 @@ import ngModule from '../module'; export default class Controller { - constructor($state, $scope, $http, $translate, vnApp) { - this.$state = $state; + constructor($stateParams, $scope, $http, $translate, vnApp) { + this.params = $stateParams; this.$scope = $scope; this.$http = $http; this.$translate = $translate; @@ -38,7 +38,7 @@ export default class Controller { } addNiche() { - this.niches.push({code: null, itemFk: this.$state.params.id, showAddIcon: true}); + this.niches.push({code: null, itemFk: this.params.id, showAddIcon: true}); this._setIconAdd(); } @@ -58,6 +58,34 @@ export default class Controller { return oldNiche.id === newNiche.id && oldNiche.code === newNiche.code && oldNiche.warehouseFk === newNiche.warehouseFk; } + setOldNiches(response) { + this._setIconAdd(); + response.data.forEach(niche => { + this.oldNiches[niche.id] = Object.assign({}, niche); + }); + } + + getNiches() { + let filter = { + where: {itemFk: this.params.id}, + include: {relation: 'warehouse'} + }; + this.$http.get(`/item/api/ItemNiches?filter=${JSON.stringify(filter)}`).then(response => { + this.niches = response.data; + this.setOldNiches(response); + }); + } + + getWarehouse(id, warehouses) { + return warehouses.find(warehouse => warehouse.id === id); + } + + getWarehouses() { + this.$http.get(`/item/api/Warehouses`).then(response => { + this.warehouses = response.data; + }); + } + submit() { let warehousesDefined = []; let repeatedWarehouse = false; @@ -92,6 +120,7 @@ export default class Controller { if (repeatedWarehouse) { return this.vnApp.showMessage(this.$translate.instant('The niche must be unique')); } + canSubmit = nichesObj.update.length > 0 || nichesObj.create.length > 0 || nichesObj.delete.length > 0; if (canSubmit) { @@ -103,41 +132,13 @@ export default class Controller { this.vnApp.showMessage(this.$translate.instant('No changes to save')); } - setOldNiches(response) { - this._setIconAdd(); - response.data.forEach(niche => { - this.oldNiches[niche.id] = Object.assign({}, niche); - }); - } - - getWarehouse(id, warehouses) { - return warehouses.find(warehouse => warehouse.id === id); - } - - getNiches() { - let filter = { - where: {itemFk: this.$state.params.id}, - include: {relation: 'warehouse'} - }; - this.$http.get(`/item/api/ItemNiches?filter=${JSON.stringify(filter)}`).then(response => { - this.niches = response.data; - this.setOldNiches(response); - }); - } - - getWarehouses() { - this.$http.get(`/item/api/Warehouses`).then(response => { - this.warehouses = response.data; - }); - } - $onInit() { this.getNiches(); this.getWarehouses(); } } -Controller.$inject = ['$state', '$scope', '$http', '$translate', 'vnApp']; +Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnItemNiche', { template: require('./item-niche.html'), diff --git a/client/item/src/tags/item-tags.html b/client/item/src/tags/item-tags.html index 17369ca48..80ee4cbd1 100644 --- a/client/item/src/tags/item-tags.html +++ b/client/item/src/tags/item-tags.html @@ -1,40 +1,50 @@
- Item tags - - - - - - - - - - - - + + Item tags + + + + + + + + + + + + + diff --git a/client/item/src/tags/item-tags.js b/client/item/src/tags/item-tags.js index 424b81e11..6209b6f32 100644 --- a/client/item/src/tags/item-tags.js +++ b/client/item/src/tags/item-tags.js @@ -1,14 +1,16 @@ import ngModule from '../module'; class ItemTags { - constructor($http, $scope, $translate, vnApp, params) { - this.$http = $http; + constructor($stateParams, $scope, $http, $translate, vnApp) { + this.params = $stateParams; this.$scope = $scope; + this.$http = $http; this.$translate = $translate; this.vnApp = vnApp; - this.itemId = params.id; + + this.itemTagTypes = []; this.itemTags = []; - this.itemTagsRemoved = []; + this.removedItemTags = []; this.oldItemTags = {}; } @@ -34,81 +36,88 @@ class ItemTags { } } + addItemTag() { + this.itemTags.push({value: null, itemFk: this.params.id, showAddIcon: true}); + this._setIconAdd(); + } + removeItemTag(index) { let item = this.itemTags[index]; if (item) { this.itemTags.splice(index, 1); this._setIconAdd(); if (item.id) { - this.itemTagsRemoved.push(item.id); + this.removedItemTags.push(item.id); this._setDirtyForm(); } } } - addItemTag() { - this.itemTags.push({value: null, itemFk: this.itemId, tagFk: null, showAddIcon: true}); - this._setIconAdd(); + _equalItemTags(oldTag, newTag) { + return oldTag.id === newTag.id && oldTag.value === newTag.value && oldTag.tagFk === newTag.tagFk; } - _setOlTags(itemTags) { - itemTags.map(tag => { - this.oldItemTags[tag.id] = Object.assign({}, tag); - return tag; - }); + _setOlTags(response) { this._setIconAdd(); + response.data.forEach(tag => { + this.oldItemTags[tag.id] = Object.assign({}, tag); + }); } _getItemtags() { let filter = { - where: {itemFk: this.itemId}, + where: {itemFk: this.params.id}, order: "priority ASC", include: {relation: "tag"} }; this.$http.get(`/item/api/ItemTags?filter=${JSON.stringify(filter)}`).then(response => { this.itemTags = response.data; - this._setOlTags(response.data); + this._setOlTags(response); }); } - _equalItemTags(tagOld, tagNew) { - return tagOld.tagFk === tagNew.tagFk && tagOld.value === tagNew.value; - } - submit() { - let codes = []; + let itemTagsDefined = []; let repeatedItemTags = false; let canSubmit; - let submitObj = { - delete: this.itemTagsRemoved, + let tagsObj = { + delete: this.removedItemTags, create: [], update: [] }; - for (let i = 0; i < this.itemTags.length; i++) { - let itemTag = this.itemTags[i]; - let isNewItemTag = itemTag.id === undefined; + this.itemTags.forEach(tag => { + let isNewTag = !tag.id; - if (itemTag.tagFk && codes.indexOf(itemTag.tagFk) !== -1) { + if (itemTagsDefined.indexOf(tag.tagFk) !== -1) { repeatedItemTags = true; - break; + return; } - if (itemTag.tagFk) codes.push(itemTag.tagFk); + itemTagsDefined.push(tag.tagFk); - if (isNewItemTag && itemTag.tagFk) { - submitObj.create.push(itemTag); - } else if (!isNewItemTag && !this._equalItemTags(this.oldItemTags[itemTag.id], itemTag)) { - submitObj.update.push(itemTag); + if (isNewTag) { + tagsObj.create.push(tag); } + + if (!isNewTag && !this._equalItemTags(this.oldItemTags[tag.id], tag)) { + let tagToUpdate = Object.assign({}, tag); + delete tagToUpdate.tag; + delete tagToUpdate.showAddIcon; + tagsObj.update.push(tagToUpdate); + } + }); + + if (this.$scope.form.$invalid) { + return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid')); } if (repeatedItemTags) { return this.vnApp.showMessage(this.$translate.instant('The tag must be unique')); } - canSubmit = submitObj.update.length > 0 || submitObj.create.length > 0 || submitObj.delete.length > 0; + canSubmit = tagsObj.update.length > 0 || tagsObj.create.length > 0 || tagsObj.delete.length > 0; if (canSubmit) { - return this.$http.post(`/item/api/ItemTags/crudItemTags`, submitObj).then(() => { + return this.$http.post(`/item/api/ItemTags/crudItemTags`, tagsObj).then(() => { this._getItemtags(); this._unsetDirtyForm(); }); @@ -120,7 +129,7 @@ class ItemTags { this._getItemtags(); } } -ItemTags.$inject = ['$http', '$scope', '$translate', 'vnApp', '$stateParams']; +ItemTags.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnItemTags', { template: require('./item-tags.html'),