fixed item tags plus added watcher to niches
This commit is contained in:
parent
c22d1b0e72
commit
33f19949d8
|
@ -1,49 +1,55 @@
|
|||
<vn-watcher
|
||||
vn-id="watcher"
|
||||
data="$ctrl.item"
|
||||
form = "form">
|
||||
</vn-watcher>
|
||||
<form name="form" ng-submit="$ctrl.submit()">
|
||||
<vn-card>
|
||||
<vn-vertical pad-large>
|
||||
<vn-one margin-medium-top>
|
||||
<vn-title>Item Niches</vn-title>
|
||||
<vn-horizontal ng-repeat="itemNiche in $ctrl.niches track by $index">
|
||||
<vn-autocomplete
|
||||
vn-three
|
||||
initial-data = "itemNiche.warehouse"
|
||||
field = "itemNiche.warehouseFk"
|
||||
data = "$ctrl.warehouses"
|
||||
show-field = "name"
|
||||
value-field = "id"
|
||||
label = "Warehouse"
|
||||
order = "name ASC"
|
||||
vn-acl="buyer,replenisher">
|
||||
</vn-autocomplete>
|
||||
<vn-textfield
|
||||
vn-three label="code"
|
||||
model="itemNiche.code"
|
||||
rule="itemNiche.code"
|
||||
vn-acl="buyer,replenisher">
|
||||
</vn-textfield>
|
||||
<vn-one pad-medium-top>
|
||||
<vn-icon
|
||||
vn-acl="buyer,replenisher"
|
||||
pointer
|
||||
medium-grey
|
||||
icon="remove_circle_outline"
|
||||
ng-click="$ctrl.removeNiche($index)">
|
||||
</vn-icon>
|
||||
<vn-icon
|
||||
vn-acl="buyer, replenisher"
|
||||
pointer
|
||||
margin-medium-left
|
||||
orange
|
||||
icon="add_circle"
|
||||
ng-if = "itemNiche.showAddIcon"
|
||||
ng-click="$ctrl.addNiche()">
|
||||
</vn-icon>
|
||||
</vn-one>
|
||||
</vn-horizontal>
|
||||
</vn-one>
|
||||
<vn-title>Item Niches</vn-title>
|
||||
<vn-horizontal ng-repeat="itemNiche in $ctrl.niches track by $index">
|
||||
<vn-autocomplete
|
||||
vn-three
|
||||
initial-data = "itemNiche.warehouse"
|
||||
field = "itemNiche.warehouseFk"
|
||||
data = "$ctrl.warehouses"
|
||||
show-field = "name"
|
||||
value-field = "id"
|
||||
label = "Warehouse"
|
||||
order = "name ASC"
|
||||
vn-acl="buyer,replenisher">
|
||||
</vn-autocomplete>
|
||||
<vn-textfield
|
||||
vn-three
|
||||
label="code"
|
||||
model="itemNiche.code"
|
||||
rule="itemNiche.code"
|
||||
vn-acl="buyer,replenisher">
|
||||
</vn-textfield>
|
||||
<vn-one pad-medium-top>
|
||||
<vn-icon
|
||||
vn-acl="buyer,replenisher"
|
||||
pointer
|
||||
medium-grey
|
||||
icon="remove_circle_outline"
|
||||
ng-click="$ctrl.removeNiche($index)">
|
||||
</vn-icon>
|
||||
<vn-icon
|
||||
vn-acl="buyer, replenisher"
|
||||
pointer
|
||||
margin-medium-left
|
||||
orange
|
||||
icon="add_circle"
|
||||
ng-if = "itemNiche.showAddIcon"
|
||||
ng-click="$ctrl.addNiche()">
|
||||
</vn-icon>
|
||||
</vn-one>
|
||||
</vn-horizontal>
|
||||
</vn-one>
|
||||
</vn-vertical>
|
||||
</vn-card>
|
||||
<vn-button-bar>
|
||||
<vn-submit label="Save"></vn-submit>
|
||||
</vn-button-bar>
|
||||
</form>
|
||||
</form>
|
|
@ -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'),
|
||||
|
|
|
@ -1,40 +1,50 @@
|
|||
<vn-watcher
|
||||
vn-id="watcher"
|
||||
data="$ctrl.item"
|
||||
form = "form">
|
||||
</vn-watcher>
|
||||
<form name="form" ng-submit="$ctrl.submit()">
|
||||
<vn-card>
|
||||
<vn-vertical pad-large>
|
||||
<vn-title>Item tags</vn-title>
|
||||
<mg-ajax path="/item/api/Tags" options="mgIndex as tags"></mg-ajax>
|
||||
<vn-horizontal ng-repeat="itemTag in $ctrl.itemTags track by $index">
|
||||
<vn-autocomplete
|
||||
vn-one
|
||||
initial-data = "itemTag.tag"
|
||||
field = "itemTag.tagFk"
|
||||
data = "tags.model"
|
||||
show-field = "name"
|
||||
label = "Tag">
|
||||
</vn-autocomplete>
|
||||
<vn-textfield vn-three label="Value" model="itemTag.value"></vn-textfield>
|
||||
<vn-one pad-medium-top>
|
||||
<vn-icon
|
||||
pointer
|
||||
medium-grey
|
||||
icon="remove_circle_outline"
|
||||
ng-click="$ctrl.removeItemTag($index)">
|
||||
</vn-icon>
|
||||
<vn-icon
|
||||
pointer
|
||||
margin-medium-left
|
||||
orange
|
||||
icon="add_circle"
|
||||
ng-if = "itemTag.showAddIcon && tags.model.length > $ctrl.itemTags.length"
|
||||
ng-click="$ctrl.addItemTag()">
|
||||
</vn-icon>
|
||||
</vn-one>
|
||||
</vn-horizontal>
|
||||
<vn-one margin-medium-top>
|
||||
<vn-title>Item tags</vn-title>
|
||||
<mg-ajax path="/item/api/Tags" options="mgIndex as tags"></mg-ajax>
|
||||
<vn-horizontal ng-repeat="itemTag in $ctrl.itemTags track by $index">
|
||||
<vn-autocomplete
|
||||
vn-three
|
||||
initial-data = "itemTag.tag"
|
||||
field = "itemTag.tagFk"
|
||||
data = "tags.model"
|
||||
show-field = "name"
|
||||
label = "Tag"
|
||||
order = "name ASC"
|
||||
vn-acl="buyer">
|
||||
</vn-autocomplete>
|
||||
<vn-textfield
|
||||
vn-three
|
||||
label="Value"
|
||||
model="itemTag.value"
|
||||
rule="itemTag.value"
|
||||
vn-acl="buyer">
|
||||
</vn-textfield>
|
||||
<vn-one pad-medium-top>
|
||||
<vn-icon
|
||||
pointer
|
||||
medium-grey
|
||||
icon="remove_circle_outline"
|
||||
ng-click="$ctrl.removeItemTag($index)"
|
||||
vn-acl="buyer">
|
||||
</vn-icon>
|
||||
<vn-icon
|
||||
pointer
|
||||
margin-medium-left
|
||||
orange
|
||||
icon="add_circle"
|
||||
ng-if = "itemTag.showAddIcon && tags.model.length > $ctrl.itemTags.length"
|
||||
ng-click="$ctrl.addItemTag()"
|
||||
vn-acl="buyer">
|
||||
</vn-icon>
|
||||
</vn-one>
|
||||
</vn-horizontal>
|
||||
</vn-vertical>
|
||||
</vn-card>
|
||||
<vn-button-bar>
|
||||
|
|
|
@ -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'),
|
||||
|
|
Loading…
Reference in New Issue