2018-03-27 13:06:22 +00:00
|
|
|
import ngModule from '../../module';
|
2018-03-15 13:36:09 +00:00
|
|
|
|
|
|
|
class Controller {
|
|
|
|
|
2018-03-21 10:08:59 +00:00
|
|
|
constructor($http, $scope, $translate, vnApp) {
|
2018-03-15 13:36:09 +00:00
|
|
|
this.$http = $http;
|
|
|
|
this.$ = $scope;
|
2018-03-21 10:08:59 +00:00
|
|
|
this.$translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
|
|
|
this.removedPackages = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
submit() {
|
|
|
|
let query = `/ticket/api/TicketPackagings/crudTicketPackaging`;
|
|
|
|
let packagesObj = {
|
|
|
|
delete: this.removedPackages,
|
|
|
|
create: [],
|
|
|
|
update: []
|
|
|
|
};
|
|
|
|
|
|
|
|
this.packages.forEach(item => {
|
|
|
|
if (typeof item.id === 'undefined')
|
|
|
|
packagesObj.create.push(item);
|
|
|
|
|
2018-03-27 13:06:22 +00:00
|
|
|
if (typeof item.id !== 'undefined' && !this.packageEquals(item, this.oldPackages[item.id]))
|
2018-03-21 10:08:59 +00:00
|
|
|
packagesObj.update.push(item);
|
|
|
|
});
|
|
|
|
|
2018-03-27 13:06:22 +00:00
|
|
|
if (this.$.form.$invalid)
|
|
|
|
return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid'));
|
|
|
|
|
|
|
|
if (!this.hasChanges(packagesObj))
|
|
|
|
return this.vnApp.showMessage(this.$translate.instant('No changes to save'));
|
|
|
|
|
2018-03-21 10:08:59 +00:00
|
|
|
this.$http.post(query, packagesObj).then(res => {
|
|
|
|
this.$.index.accept();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
removePackage(index) {
|
|
|
|
if (this.packages[index] && this.packages[index].id)
|
|
|
|
this.removedPackages.push(this.packages[index].id);
|
|
|
|
|
|
|
|
this.packages.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
addPackage() {
|
|
|
|
let data = {
|
|
|
|
packagingFk: null,
|
|
|
|
quantity: null,
|
|
|
|
created: Date.now(),
|
|
|
|
ticketFk: this.ticket.id
|
|
|
|
};
|
|
|
|
this.packages.push(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPackages() {
|
|
|
|
this.packages = this.$.index.model;
|
|
|
|
this.setOldPackages();
|
|
|
|
}
|
|
|
|
|
|
|
|
setOldPackages() {
|
2018-03-27 13:06:22 +00:00
|
|
|
if (this.oldPackages && !this.$.watcher.dataChanged())
|
|
|
|
return;
|
|
|
|
|
2018-03-21 10:08:59 +00:00
|
|
|
this.oldPackages = [];
|
2018-03-27 13:06:22 +00:00
|
|
|
this.removedPackages = [];
|
2018-03-21 10:08:59 +00:00
|
|
|
this.packages.forEach(item => {
|
2018-03-27 13:06:22 +00:00
|
|
|
this.oldPackages[item.id] = Object.assign({}, item);
|
2018-03-21 10:08:59 +00:00
|
|
|
});
|
2018-03-15 13:36:09 +00:00
|
|
|
}
|
2018-03-27 13:06:22 +00:00
|
|
|
|
|
|
|
packageEquals(newPackage, oldPackage) {
|
|
|
|
return newPackage.packagingFk === oldPackage.packagingFk && newPackage.quantity == oldPackage.quantity;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasChanges(packagesObj) {
|
|
|
|
if (packagesObj.create.length || packagesObj.update.length || packagesObj.delete.length)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-15 13:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 13:06:22 +00:00
|
|
|
Controller.$inject = ['$http', '$scope', '$translate', 'vnApp'];
|
2018-03-15 13:36:09 +00:00
|
|
|
|
2018-03-27 13:06:22 +00:00
|
|
|
ngModule.component('vnTicketPackageIndex', {
|
2018-03-21 13:54:12 +00:00
|
|
|
template: require('./package.html'),
|
2018-03-15 13:36:09 +00:00
|
|
|
controller: Controller,
|
|
|
|
bindings: {
|
|
|
|
ticket: '<'
|
|
|
|
}
|
|
|
|
});
|