163 lines
5.8 KiB
TypeScript
163 lines
5.8 KiB
TypeScript
import { error } from 'console';
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { DynamicPicker, EnumDynamicpickerType } from '../../picker/dynamic-picker/Dynamic-Picker';
|
|
import { SearchEmployee } from 'src/app/_models/Employee/searchEmployee';
|
|
import { Router } from '@angular/router';
|
|
import { ApiService } from 'src/app/app.api.service';
|
|
import { HRMNotificationService } from 'src/app/app.notification.service';
|
|
import { loadingPanelService } from 'src/app/hrm-loding panel/loding.panel.service';
|
|
import { BasicService } from 'src/app/_services/Basic/basic.service';
|
|
import { Department } from 'src/app/_models/Basic/department';
|
|
import { Grade } from 'src/app/_models/Basic/grade';
|
|
import { EnumStatus } from '../../_models/enums';
|
|
import { Employee } from 'src/app/_models/Employee/employee';
|
|
import { LetterRequestService } from 'src/app/_services/letter-request/letter-request.service';
|
|
import { saveAs } from 'file-saver';
|
|
|
|
@Component({
|
|
selector: 'app-exception-letter-generate',
|
|
templateUrl: './exception-letter-generate.component.html',
|
|
styleUrls: ['./exception-letter-generate.component.scss']
|
|
})
|
|
export class ExceptionLetterGenerateComponent implements OnInit {
|
|
|
|
public selectedEmps: SearchEmployee[] = [];
|
|
public mySelection: number[] = [];
|
|
|
|
public selectedreportType: EnumExceptionLetterTemplateType;
|
|
public reportTypes = Object.keys(EnumExceptionLetterTemplateType)
|
|
.filter(key => !isNaN(Number(EnumExceptionLetterTemplateType[key])))
|
|
.map(key => ({
|
|
text: key.replace(/_/g, ' '),
|
|
value: EnumExceptionLetterTemplateType[key]
|
|
}));
|
|
public allDepartments: Department[];
|
|
public allGrades: Grade[];
|
|
|
|
constructor(
|
|
public router: Router, public loadingPanel: loadingPanelService,
|
|
public notificationService: HRMNotificationService,
|
|
public apiService: ApiService,
|
|
public basicService: BasicService, public letterRequestService: LetterRequestService) {
|
|
this.apiService.selectedMenuName = 'Employee Letter Generation';
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
// this.basicService.getAllDepartment(EnumStatus.Active).subscribe(
|
|
// (resp) => {
|
|
// this.allDepartments = resp;
|
|
// },
|
|
// (err: any) => {
|
|
// this.notificationService.showError(err.error);
|
|
// }
|
|
// );
|
|
// this.basicService.getAllGrade(EnumStatus.Active).subscribe(
|
|
// (resp) => {
|
|
// this.allGrades = resp;
|
|
// },
|
|
// (err: any) => {
|
|
// this.notificationService.showError(err.error);
|
|
// }
|
|
// );
|
|
}
|
|
public GetSelectedEmployee(childData) {
|
|
this.selectedEmps = childData;
|
|
// this.selectedEmps.forEach(element => {
|
|
// element.departmentName = this.allDepartments.find(d => d.id == element.departmentID).name;
|
|
// element.gradeName = this.allDepartments.find(g => g.id == element.gradeID).name;
|
|
// });
|
|
}
|
|
|
|
public onSelectReport(value: any) {
|
|
debugger;
|
|
}
|
|
generateLetter() {
|
|
let employeeDataToGenerate: SearchEmployee[] = [];
|
|
this.selectedEmps.forEach(element => {
|
|
this.mySelection.forEach(item => {
|
|
if (element.employeeID == item) employeeDataToGenerate.push(element);
|
|
});
|
|
});
|
|
this.selectedreportType;
|
|
if (this.selectedreportType == undefined || this.selectedreportType['value'] == null) {
|
|
this.notificationService.showWarning("Please select a Letter to Generate");
|
|
return;
|
|
}
|
|
if (employeeDataToGenerate.length <= 0) {
|
|
this.notificationService.showWarning("Please select Employee to Generate Letter");
|
|
return;
|
|
}
|
|
this.loadingPanel.ShowLoadingPanel = true;
|
|
this.letterRequestService.generatedExceptiinLetter(this.selectedreportType['value'], employeeDataToGenerate).subscribe(
|
|
(resp: any[]) => {
|
|
debugger
|
|
if (resp.length > 0) {
|
|
resp.forEach(fileData => {
|
|
// this.downloadBlob(new Blob([fileData.fileContents], { type: 'application/msword' }), 'application/msword', fileData.fileDownloadName);
|
|
this.downloadFileWord(fileData.fileContents, fileData.fileDownloadName);
|
|
});
|
|
}
|
|
|
|
this.loadingPanel.ShowLoadingPanel = false;
|
|
},
|
|
(err: any) => {
|
|
this.notificationService.showError(err.error);
|
|
this.loadingPanel.ShowLoadingPanel = false;
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
private downloadBlob(data: any, type: string, fileName: string): void {
|
|
const blob: Blob = new Blob([data], { type: type });
|
|
// const fileName: string = this.workOrderBillReceive.UploadAttachment[0].OriginalFileName;
|
|
// const fileName: string = fileName;
|
|
const objectUrl: string = URL.createObjectURL(blob);
|
|
const a: HTMLAnchorElement = document.createElement('a') as HTMLAnchorElement;
|
|
|
|
a.href = objectUrl;
|
|
a.download = fileName;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(objectUrl);
|
|
}
|
|
|
|
|
|
|
|
downloadFileWord(blobContent, fileName) {
|
|
// const blob = new Blob([this.b64toBlob(blobContent, 'application/data:application/vnd.openxmlformats-officedocument.wordprocessingml.document', 1024)], {});
|
|
// saveAs(blob, fileName + '.docx');
|
|
const blob = new Blob([this.b64toBlob(blobContent, 'application/msword', 1024)], {});
|
|
saveAs(blob, fileName);
|
|
}
|
|
|
|
b64toBlob(b64Data, contentType, sliceSize) {
|
|
const byteCharacters = atob(b64Data);
|
|
const byteArrays = [];
|
|
|
|
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
|
|
const slice = byteCharacters.slice(offset, offset + sliceSize);
|
|
|
|
const byteNumbers = new Array(slice.length);
|
|
for (let i = 0; i < slice.length; i++) {
|
|
byteNumbers[i] = slice.charCodeAt(i);
|
|
}
|
|
|
|
const byteArray = new Uint8Array(byteNumbers);
|
|
byteArrays.push(byteArray);
|
|
}
|
|
|
|
const blob = new Blob(byteArrays, { type: contentType });
|
|
return blob;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
export enum EnumExceptionLetterTemplateType {
|
|
Letter_Template_Staff = 1,
|
|
Letter_Template_Worker = 2
|
|
}
|