2018-01-18 07:38:04 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
|
2018-02-14 13:22:35 +00:00
|
|
|
export default class Controller {
|
|
|
|
constructor($state, $scope, $http, $q, $translate, vnApp) {
|
|
|
|
this.$state = $state;
|
|
|
|
this.$scope = $scope;
|
|
|
|
this.$http = $http;
|
|
|
|
this.$q = $q;
|
|
|
|
this.$translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
|
|
|
|
|
|
|
this.barcodes = [];
|
|
|
|
this.removedBarcodes = [];
|
|
|
|
this.oldBarcodes = {};
|
|
|
|
}
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-14 13:22:35 +00:00
|
|
|
_setIconAdd() {
|
|
|
|
if (this.barcodes.length) {
|
|
|
|
this.barcodes.map(element => {
|
|
|
|
element.showAddIcon = false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
this.barcodes[this.barcodes.length - 1].showAddIcon = true;
|
|
|
|
} else {
|
|
|
|
this.addBarcode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setDirty();
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-14 13:22:35 +00:00
|
|
|
_unsetDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setPristine();
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 14:08:36 +00:00
|
|
|
|
2018-02-14 13:22:35 +00:00
|
|
|
_equalBarcodes(oldBarcode, newBarcode) {
|
|
|
|
return oldBarcode.id === newBarcode.id && oldBarcode.code === newBarcode.code;
|
|
|
|
}
|
|
|
|
|
|
|
|
addBarcode() {
|
|
|
|
this.barcodes.push({code: null, itemFk: this.$state.params.id, showAddIcon: true});
|
|
|
|
this._setIconAdd();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeBarcode(index) {
|
|
|
|
let item = this.barcodes[index];
|
|
|
|
if (item) {
|
|
|
|
this.barcodes.splice(index, 1);
|
|
|
|
this._setIconAdd();
|
|
|
|
if (item.id) {
|
|
|
|
this.removedBarcodes.push(item.id);
|
|
|
|
this._setDirtyForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
submit() {
|
|
|
|
let codes = [];
|
|
|
|
let repeatedBarcodes = false;
|
|
|
|
let canSubmit;
|
|
|
|
let barcodesObj = {
|
|
|
|
delete: this.removedBarcodes,
|
|
|
|
create: [],
|
|
|
|
update: []
|
|
|
|
};
|
|
|
|
for (let i = 0; i < this.barcodes.length; i++) {
|
|
|
|
let barcode = this.barcodes[i];
|
|
|
|
let isNewBarcode = barcode.id === undefined;
|
|
|
|
|
|
|
|
if (barcode.code && codes.indexOf(barcode.code) !== -1) {
|
|
|
|
repeatedBarcodes = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (barcode.code) codes.push(barcode.code);
|
|
|
|
|
|
|
|
if (isNewBarcode && barcode.code) {
|
|
|
|
barcodesObj.create.push(barcode);
|
|
|
|
} else if (!isNewBarcode && !this._equalBarcodes(this.oldBarcodes[barcode.id], barcode)) {
|
|
|
|
barcodesObj.update.push(barcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatedBarcodes) {
|
2018-07-09 11:01:46 +00:00
|
|
|
return this.vnApp.showError(this.$translate.instant('The barcode must be unique'));
|
2018-02-14 13:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canSubmit = barcodesObj.update.length > 0 || barcodesObj.create.length > 0 || barcodesObj.delete.length > 0;
|
|
|
|
|
|
|
|
if (canSubmit) {
|
|
|
|
return this.$http.post(`/item/api/ItemBarcodes/crudItemBarcodes`, barcodesObj).then(() => {
|
|
|
|
this.getBarcodes();
|
|
|
|
this._unsetDirtyForm();
|
2018-05-31 09:52:39 +00:00
|
|
|
this.$scope.watcher.notifySaved();
|
2018-02-14 13:22:35 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-09 11:01:46 +00:00
|
|
|
this.vnApp.showError(this.$translate.instant('No changes to save'));
|
2018-02-14 13:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setOldBarcodes(response) {
|
|
|
|
this._setIconAdd();
|
|
|
|
response.data.forEach(barcode => {
|
|
|
|
this.oldBarcodes[barcode.id] = Object.assign({}, barcode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBarcodes() {
|
|
|
|
let filter = {
|
|
|
|
where: {itemFk: this.$state.params.id}
|
|
|
|
};
|
|
|
|
this.$http.get(`/item/api/ItemBarcodes?filter=${JSON.stringify(filter)}`).then(response => {
|
|
|
|
this.barcodes = response.data;
|
|
|
|
this.setOldBarcodes(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
this.getBarcodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller.$inject = ['$state', '$scope', '$http', '$q', '$translate', 'vnApp'];
|
|
|
|
|
2018-01-18 07:38:04 +00:00
|
|
|
ngModule.component('vnItemBarcode', {
|
2018-05-25 08:03:45 +00:00
|
|
|
template: require('./index.html'),
|
2018-02-14 13:22:35 +00:00
|
|
|
controller: Controller
|
2018-01-18 07:38:04 +00:00
|
|
|
});
|