salix/client/item/src/barcode/index.js

132 lines
3.7 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
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
_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
_unsetDirtyForm() {
if (this.$scope.form) {
this.$scope.form.$setPristine();
}
}
2018-03-16 14:08:36 +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) {
return this.vnApp.showError(this.$translate.instant('The barcode must be unique'));
}
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();
this.$scope.watcher.notifySaved();
});
}
this.vnApp.showError(this.$translate.instant('No changes to save'));
}
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'];
ngModule.component('vnItemBarcode', {
template: require('./index.html'),
controller: Controller
});