salix/modules/ticket/front/dms/create/index.js

99 lines
2.7 KiB
JavaScript

import ngModule from '../../module';
import './style.scss';
class Controller {
constructor($scope, $http, $state, $translate, vnApp) {
this.$ = $scope;
this.$http = $http;
this.$state = $state;
this.$translate = $translate;
this.vnApp = vnApp;
this.dms = {
files: [],
hasFile: false,
hasAttachedFile: false
};
}
get ticket() {
return this._ticket;
}
set ticket(value) {
this._ticket = value;
if (value)
this.setDefaultParams();
}
setDefaultParams() {
const params = {filter: {
where: {code: 'ticket'}
}};
this.$http.get('/api/DmsTypes/findOne', {params}).then(res => {
const dmsTypeId = res.data && res.data.id;
const defaultParams = {
reference: this.ticket.id,
warehouseId: this.ticket.warehouseFk,
companyId: this.ticket.companyFk,
dmsTypeId: dmsTypeId,
description: this.$translate.instant('FileDescription', {
ticketId: this.ticket.id,
clientId: this.ticket.client.id,
clientName: this.ticket.client.name
}).toUpperCase()
};
this.dms = Object.assign(this.dms, defaultParams);
});
}
onSubmit() {
const query = `/api/tickets/${this.ticket.id}/uploadFile`;
const options = {
method: 'POST',
url: query,
params: this.dms,
headers: {
'Content-Type': undefined
},
transformRequest: files => {
const formData = new FormData();
for (let i = 0; i < files.length; i++)
formData.append(files[i].name, files[i]);
return formData;
},
data: this.dms.files
};
this.$http(options).then(res => {
if (res) {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.watcher.updateOriginalData();
this.$state.go('ticket.card.dms.index');
}
});
}
onFileChange(files) {
let hasAttachedFile = false;
if (files.length > 0)
hasAttachedFile = true;
this.$.$applyAsync(() => {
this.dms.hasAttachedFile = hasAttachedFile;
});
}
}
Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp'];
ngModule.component('vnTicketDmsCreate', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<'
}
});