2018-01-18 07:38:04 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
export default class Controller {
|
2018-02-27 11:32:47 +00:00
|
|
|
constructor($stateParams, $scope, $http, $translate, vnApp) {
|
|
|
|
this.params = $stateParams;
|
2018-02-20 11:25:51 +00:00
|
|
|
this.$scope = $scope;
|
|
|
|
this.$http = $http;
|
|
|
|
this.$translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
|
|
|
|
|
|
|
this.warehouses = [];
|
|
|
|
this.niches = [];
|
|
|
|
this.removedNiches = [];
|
|
|
|
this.oldNiches = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setIconAdd() {
|
|
|
|
if (this.niches.length) {
|
|
|
|
this.niches.map(element => {
|
|
|
|
element.showAddIcon = false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
this.niches[this.niches.length - 1].showAddIcon = true;
|
|
|
|
} else {
|
|
|
|
this.addNiche();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setDirty();
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
_unsetDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setPristine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addNiche() {
|
2018-02-27 11:32:47 +00:00
|
|
|
this.niches.push({code: null, itemFk: this.params.id, showAddIcon: true});
|
2018-02-20 11:25:51 +00:00
|
|
|
this._setIconAdd();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeNiche(index) {
|
|
|
|
let item = this.niches[index];
|
|
|
|
if (item) {
|
|
|
|
this.niches.splice(index, 1);
|
|
|
|
this._setIconAdd();
|
|
|
|
if (item.id) {
|
|
|
|
this.removedNiches.push(item.id);
|
|
|
|
this._setDirtyForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_equalNiches(oldNiche, newNiche) {
|
|
|
|
return oldNiche.id === newNiche.id && oldNiche.code === newNiche.code && oldNiche.warehouseFk === newNiche.warehouseFk;
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:32:47 +00:00
|
|
|
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'}
|
|
|
|
};
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-27 11:32:47 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
submit() {
|
|
|
|
let warehousesDefined = [];
|
|
|
|
let repeatedWarehouse = false;
|
|
|
|
let canSubmit;
|
|
|
|
let nichesObj = {
|
|
|
|
delete: this.removedNiches,
|
|
|
|
create: [],
|
|
|
|
update: []
|
|
|
|
};
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
this.niches.forEach(niche => {
|
|
|
|
let isNewNiche = !niche.id;
|
|
|
|
|
|
|
|
if (warehousesDefined.indexOf(niche.warehouseFk) !== -1) {
|
|
|
|
repeatedWarehouse = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
warehousesDefined.push(niche.warehouseFk);
|
|
|
|
|
|
|
|
if (isNewNiche) {
|
|
|
|
nichesObj.create.push(niche);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNewNiche && !this._equalNiches(this.oldNiches[niche.id], niche)) {
|
|
|
|
nichesObj.update.push(niche);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-23 14:20:24 +00:00
|
|
|
if (this.$scope.form.$invalid) {
|
|
|
|
return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid'));
|
|
|
|
}
|
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
if (repeatedWarehouse) {
|
|
|
|
return this.vnApp.showMessage(this.$translate.instant('The niche must be unique'));
|
|
|
|
}
|
2018-02-27 11:32:47 +00:00
|
|
|
|
2018-02-20 11:25:51 +00:00
|
|
|
canSubmit = nichesObj.update.length > 0 || nichesObj.create.length > 0 || nichesObj.delete.length > 0;
|
|
|
|
|
|
|
|
if (canSubmit) {
|
|
|
|
return this.$http.post(`/item/api/ItemNiches/crudItemNiches`, nichesObj).then(() => {
|
|
|
|
this.getNiches();
|
|
|
|
this._unsetDirtyForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.vnApp.showMessage(this.$translate.instant('No changes to save'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
this.getNiches();
|
|
|
|
this.getWarehouses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:32:47 +00:00
|
|
|
Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp'];
|
2018-02-20 11:25:51 +00:00
|
|
|
|
2018-01-18 07:38:04 +00:00
|
|
|
ngModule.component('vnItemNiche', {
|
2018-04-04 17:59:03 +00:00
|
|
|
template: require('./niche.html'),
|
2018-02-20 11:25:51 +00:00
|
|
|
controller: Controller
|
2018-01-18 07:38:04 +00:00
|
|
|
});
|