EchoTex_Payroll/HRM.UI/ClientApp/src/app/reports/emp-leave-balance/emp-leave-balance.component.ts

201 lines
7.1 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { SearchEmployee } from '../../_models/Employee/searchEmployee';
import { LeaveYear } from '../../_models/Leave/leaveYear';
import { Leave } from '../../_models/Leave/leave';
import { LeaveService } from '../../_services/leave/leave.service';
import { HRMNotificationService } from '../../app.notification.service';
import { loadingPanelService } from '../../hrm-loding panel/loding.panel.service';
import { ApiService } from '../../app.api.service';
import { EnumStatus } from '../../_models/enums';
import { saveAs } from 'file-saver';
import { ReportServices } from '../../_services/reports/report.service';
import { GlobalfunctionExtension } from 'src/app/_models/globalFunctions';
import { Router } from '@angular/router';
import { EmployeeServices } from 'src/app/_services/employee/employee.service';
import { AuthorizedPerson } from 'src/app/adhoc-feature/authorized-persons/authorizedPerson';
@Component({
selector: 'app-emp-leave-balance',
templateUrl: './emp-leave-balance.component.html',
styleUrls: ['./emp-leave-balance.component.scss']
})
export class EmpLeaveBalanceComponent implements OnInit {
selectedEmployee: SearchEmployee;
leaveYearList: LeaveYear[] = [];
leaveList: Leave[] = [];
leaveYearId: number;
PDFTitle = null;
showPopUp = false;
data: Array<Object>;
src: any;
base64Data: any;
leaveId: number;
fromDate: Date = GlobalfunctionExtension.getFirstDateofYear(new Date());
toDate: Date = GlobalfunctionExtension.getLastDateOfYear(new Date());
authPersons: AuthorizedPerson[];
personID: number;
reportRoute: any;
reportID: number;
constructor(public leaveService: LeaveService,
public notificationService: HRMNotificationService,
public reportService: ReportServices,
public loadingPanel: loadingPanelService,
public apiService: ApiService,
public router: Router,
public employeeService: EmployeeServices) {
this.reportRoute = this.router.url.split('/').pop();
debugger
if (this.reportRoute == 'emp-leave-balance') {
this.apiService.selectedMenuName = 'Employee Leave Balance';
this.PDFTitle = 'Employee Leave Balance';
this.reportID = 724;
}
else if (this.reportRoute == 'leave-register') {
this.apiService.selectedMenuName = 'Leave Register Report';
this.PDFTitle = 'Leave Register Bangla';
this.reportID = 727;
}
}
ngOnInit(): void {
this.loadLeaveYear();
this.loadAuthPersons();
}
public GetSelectedEmployee(childData) {
this.selectedEmployee = childData;
}
loadLeaveYear() {
this.leaveService.getAllLeaveYear().subscribe(
(resp) => {
this.leaveYearList = resp;
},
(err) => {
this.notificationService.showError(err.error);
this.loadingPanel.ShowLoadingPanel = false;
},
() => {
// console.log(this.leaveYearList);
}
);
this.leaveService.getAllLeave(EnumStatus.Active, '', '').subscribe(
(resp) => {
this.leaveList = resp;
},
(err) => {
this.notificationService.showError(err.error);
this.loadingPanel.ShowLoadingPanel = false;
},
() => {
// console.log(this.leaveList);
/*this.leaveList.forEach(x => {
x.code = x.code.toLowerCase() + x.id;
});*/
},
);
}
loadAuthPersons() {
this.employeeService.getAuthorizedPerson().subscribe(
(resp) => {
this.authPersons = resp;
debugger;
},
(err) => {
this.notificationService.showError(err.error);
this.loadingPanel.ShowLoadingPanel = false;
}
);
}
preview(reportType: string) {
// const leave = this.leaveYearList.find(l => l.id === this.leaveYearId);
// if (leave === null) {
// this.notificationService.showError('Please select leave year!');
// return;
// }
let data;
if (this.reportID == 724)
data = {
reportid: this.reportID,
itemid: this.leaveId,
bankId: null,
empIds: this.selectedEmployee.employeeID.toString(),
reportType: reportType,
fromDate: this.fromDate,
toDate: this.toDate
};
else if (this.reportID == 727)
data = {
reportid: this.reportID,
itemid: this.personID,
empIds: this.selectedEmployee.employeeID.toString(),
reportType: reportType,
};
this.loadingPanel.ShowLoadingPanel = true;
if (reportType === 'PDF')
this.showPopUp = true;
this.reportService.getCommonReportData(data).subscribe(
(resp: any) => {
// if (data.reportType === 'PDF') {
// this.src = URL.createObjectURL(this.b64toBlob(resp, 'data:application/pdf;base64', 1024));
// this.showPopUp = true;
// } else if (data.reportType === 'EXCEL') {
// this.downloadFile(resp);
// }
if (reportType === 'PDF') {
this.src = URL.createObjectURL(this.b64toBlob(resp, 'application/pdf', 1024));
var element = <HTMLIFrameElement>(document.getElementById("pdf-viewer-report"));
element.src = this.src;
} else if (reportType === 'EXCEL') {
this.downloadFile(resp);
}
},
(err) => {
console.log(err);
this.notificationService.showError(err.error);
this.loadingPanel.ShowLoadingPanel = false;
this.showPopUp = false;
},
() => {
this.loadingPanel.ShowLoadingPanel = false;
// this.loadGrid();
}
);
}
downloadFile(blobContent) {
const blob = new Blob([this.b64toBlob(blobContent, 'application/data:application/vnd.ms-excel', 1024)], {});
saveAs(blob, this.PDFTitle + '.xls');
}
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;
}
closeForm(): void {
this.showPopUp = false;
}
}