From accd302ef415dcd6b3165aaa859cb7defbc78f98 Mon Sep 17 00:00:00 2001 From: mashfiq Date: Thu, 16 Jan 2025 16:18:21 +0600 Subject: [PATCH] msword, profile letter --- HRM.Report/Class/rptEmployee.cs | 34 +++++- HRM.Report/HRM.Report.csproj | 12 ++ .../app/_services/reports/report.service.ts | 3 + .../employee-profile-reports.component.html | 4 +- .../employee-profile-reports.component.ts | 83 +++++++++---- HRM.UI/Controllers/Report/ReportController.cs | 115 +++++++++++++++++- 6 files changed, 220 insertions(+), 31 deletions(-) diff --git a/HRM.Report/Class/rptEmployee.cs b/HRM.Report/Class/rptEmployee.cs index a7088d8..c83ba55 100644 --- a/HRM.Report/Class/rptEmployee.cs +++ b/HRM.Report/Class/rptEmployee.cs @@ -21,6 +21,7 @@ using Microsoft.Extensions.Configuration; using NPOI.SS.Formula.Functions; using System.Collections; using Microsoft.AspNetCore.Http; +using NPOI.HPSF; namespace HRM.Report { @@ -3448,7 +3449,7 @@ namespace HRM.Report { try { - + string pdfFilePath = string.Empty; PayrollType payrollType = new PayrollTypeService().Get(payrollTypeID); string[] sBody = { }; string sFilePath = string.Empty; @@ -3457,7 +3458,7 @@ namespace HRM.Report // Commented for testing in development phase //LetterTemplte letterTemplte = new LetterTemplteService().Get(oLetterTemplate.ID); //PhotoPath oPhotoPath = new PhotoPathService().Get(ID.FromInteger(1)); - PhotoPath oPhotoPath = new PhotoPathService().Get(1); + //PhotoPath oPhotoPath = new PhotoPathService().Get(1); #region Expandable Objects @@ -3629,14 +3630,41 @@ namespace HRM.Report file.ReplaceWords(sFilePath, table); file.CloseWordApplication(); + pdfFilePath = System.IO.Path.ChangeExtension(sFilePath, ".pdf"); + ConvertDocToPdf(sFilePath, pdfFilePath); } - return sFilePath; + return pdfFilePath; } catch (Exception ex) { throw new Exception(ex.Message); } } + public void ConvertDocToPdf(string wordFilePath, string pdfFilePath) + { + Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); + Microsoft.Office.Interop.Word.Document wordDocument = null; + try + { + // Open the Word document + wordDocument = wordApp.Documents.Open(wordFilePath); + + // Save as PDF + wordDocument.SaveAs(pdfFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + finally + { + // Clean up + wordDocument?.Close(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument); + wordApp.Quit(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); + } + } } } diff --git a/HRM.Report/HRM.Report.csproj b/HRM.Report/HRM.Report.csproj index 0423cb1..192df9a 100644 --- a/HRM.Report/HRM.Report.csproj +++ b/HRM.Report/HRM.Report.csproj @@ -296,6 +296,18 @@ + + + tlbimp + 7 + 8 + 00020905-0000-0000-c000-000000000046 + 0 + false + true + + + diff --git a/HRM.UI/ClientApp/src/app/_services/reports/report.service.ts b/HRM.UI/ClientApp/src/app/_services/reports/report.service.ts index f1e6a43..3bf3ce3 100644 --- a/HRM.UI/ClientApp/src/app/_services/reports/report.service.ts +++ b/HRM.UI/ClientApp/src/app/_services/reports/report.service.ts @@ -133,4 +133,7 @@ export class ReportServices { getProfileReportData(param: any): Observable> { return this.apiService.httpPost('/Report/getProfileReportData/', param); } + getGeneratedProfileReportData(param: any) { + return this.apiService.httpDownloadFile('/Report/getGeneratedProfileReport/', param); + } } diff --git a/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.html b/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.html index c62ccb2..80054c8 100644 --- a/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.html +++ b/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.html @@ -47,8 +47,8 @@
- +
diff --git a/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.ts b/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.ts index 91963a3..6cbe30c 100644 --- a/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.ts +++ b/HRM.UI/ClientApp/src/app/reports/employee-profile-reports/employee-profile-reports.component.ts @@ -45,6 +45,7 @@ import { encodeBase64 } from '@progress/kendo-file-saver'; import { AuthorizedPerson } from 'src/app/adhoc-feature/authorized-persons/authorizedPerson'; import { Employee } from 'src/app/_models/Employee/employee'; import { HrEmployee } from 'src/app/_models/HREmployee/hrEmployee'; +import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-employee-profile-reports', @@ -97,42 +98,73 @@ export class EmployeeProfileReportsComponent implements OnInit { preview(reportType: string) { debugger; const data = { - reportID: this.selectedreportType['value'], - personID: this.authorizedPerson.id, - employeeID: this.selectedEmployee.employeeID, - reportType: reportType + reportID: this.selectedreportType['value'], + personID: this.authorizedPerson.id, + employeeID: this.selectedEmployee.employeeID, + reportType: reportType == 'Download' ? 'PDF' : reportType }; + switch(this.selectedreportType['value']){ + case EnumProfileReportType.Print_CV: + this.PDFTitle = EnumExtension.getName(EnumProfileReportType, EnumProfileReportType.Print_CV); + break; + case EnumProfileReportType.Appointment_Letter_Officer: + this.PDFTitle = EnumExtension.getName(EnumProfileReportType, EnumProfileReportType.Appointment_Letter_Officer); + break; + case EnumProfileReportType.Appointment_Letter_Staff: + this.PDFTitle = EnumExtension.getName(EnumProfileReportType, EnumProfileReportType.Appointment_Letter_Staff); + break; + case EnumProfileReportType.Appointment_Letter_Worker: + this.PDFTitle = EnumExtension.getName(EnumProfileReportType, EnumProfileReportType.Appointment_Letter_Worker); + break; + } this.loadingPanel.ShowLoadingPanel = true; if (reportType === 'PDF') - this.showPopUp = true; - this.reportService.getProfileReportData(data).subscribe( + this.showPopUp = true; + if (this.selectedreportType['value'] == EnumProfileReportType.Print_CV || this.selectedreportType['value'] == EnumProfileReportType.Appointment_Letter_Officer) { + this.reportService.getProfileReportData(data).subscribe( (resp: any) => { - if (reportType === 'PDF') { + if (reportType === 'PDF') { - this.src = URL.createObjectURL(this.b64toBlob(resp, 'application/pdf', 1024)); - var element = (document.getElementById("pdf-viewer-report")); - element.src = this.src; - } else if (reportType === 'EXCEL') { - this.downloadFile(resp); - } + this.src = URL.createObjectURL(this.b64toBlob(resp, 'application/pdf', 1024)); + var element = (document.getElementById("pdf-viewer-report")); + element.src = this.src; + } else if (reportType === 'Download') { + this.downloadFile(resp); + } + this.loadingPanel.ShowLoadingPanel = false; }, (err) => { - this.closeForm(); - console.log(err); - this.notificationService.showError(err.error); - this.loadingPanel.ShowLoadingPanel = false; - }, - () => { - - this.loadingPanel.ShowLoadingPanel = false; - // this.loadGrid(); + this.closeForm(); + console.log(err); + this.notificationService.showError(err.error); + this.loadingPanel.ShowLoadingPanel = false; } - ); + ); + } + else { + this.reportService.getGeneratedProfileReportData(data).subscribe(fileData => { + if (reportType == 'PDF') { + var element = (document.getElementById("pdf-viewer-report")); + element.src = URL.createObjectURL(new Blob([fileData], { type: 'application/pdf' })); + } + this.loadingPanel.ShowLoadingPanel = false; + + // if (reportType == 'download') + // this.downloadBlob(new Blob([fileData], { type: 'application/pdf' }), 'application/pdf', dataItem.employeeNo + "-" + dataItem.name + "_" + dataItem.year.toString() + "_" + 'Promotion Letter'); + // this.loadingPanelService.ShowLoadingPanel = false; + }, + (error) => { + this.closeForm(); + this.notificationService.showError(error.error); + }); + } } downloadFile(blobContent) { - let blob = new Blob([this.b64toBlob(blobContent, 'application/data:application/vnd.ms-excel', 1024)], {}); - saveAs(blob, this.PDFTitle + '.xls'); + // let blob = new Blob([this.b64toBlob(blobContent, 'application/data:application/vnd.ms-excel', 1024)], {}); + // saveAs(blob, this.PDFTitle + '.xls'); + let blob = new Blob([this.b64toBlob(blobContent, 'application/pdf', 1024)], {}); + saveAs(blob, this.PDFTitle + '.pdf'); } b64toBlob(b64Data, contentType, sliceSize) { const byteCharacters = atob(b64Data); @@ -183,6 +215,7 @@ export class EmployeeProfileReportsComponent implements OnInit { // } // ); } + } export enum EnumProfileReportType { diff --git a/HRM.UI/Controllers/Report/ReportController.cs b/HRM.UI/Controllers/Report/ReportController.cs index 2711a5a..9afabc5 100644 --- a/HRM.UI/Controllers/Report/ReportController.cs +++ b/HRM.UI/Controllers/Report/ReportController.cs @@ -20,6 +20,7 @@ using NPOI.SS.Formula.Functions; using HRM.BO.Report; using HRM.Report.PayrollDataSet; using Newtonsoft.Json; +using Microsoft.AspNetCore.StaticFiles; namespace HRM.UI.Controllers.Report { @@ -1740,7 +1741,21 @@ namespace HRM.UI.Controllers.Report ltemplate.Type = EnumDocType.Desktop_Letter; ltemplate.TypeID = (int)EnumDocType.Desktop_Letter; - new rptEmployee().Generate(ltemplate, employeeID, payrollTypeId, "C:\\Users\\mashfiq.EASE\\Downloads", "Worker.doc"); + string downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads"; + string lFileName = "Worker.doc"; + + string sFilePath = new rptEmployee().Generate(ltemplate, employeeID, payrollTypeId, downloadPath, lFileName); + + + byte[] buffer = new byte[16 * 1024]; + buffer = System.IO.File.ReadAllBytes(sFilePath); + string contentType = GetFileType(sFilePath); + var name = System.IO.Path.ChangeExtension(lFileName, ".pdf"); + if (System.IO.File.Exists(sFilePath)) + { + System.IO.File.Delete(sFilePath); + } + return File(buffer, contentType, name); break; case EnumProfileReportType.Appointment_Letter_Staff: ltemplate.SetObjectID(FixedLetterTemplte.Staff_Appointment_Letter); @@ -1765,5 +1780,103 @@ namespace HRM.UI.Controllers.Report return Ok(bytes); } + + [HttpPost("getGeneratedProfileReport")] + public ActionResult getGeneratedProfileReportData(dynamic data) + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + var items = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); + EnumProfileReportType reportid = (EnumProfileReportType)items["reportID"].ToObject(); + int personID = 0; + int employeeID = 0; + + if (items["personID"] != null) + personID = (int)items["personID"].ToObject(); + if (items["employeeID"] != null) + employeeID = (int)items["employeeID"].ToObject(); + + string reportType = (string)items["reportType"].ToObject(); + + int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault(); + byte[] bytes = null; + + LetterTemplte ltemplate = new LetterTemplte(); + + try + { + + switch (reportid) + { + //case EnumProfileReportType.Print_CV: + // bytes = new rptEmployee().GetEmployeeCV(employeeID, payrollTypeId, reportType); + // break; + //case EnumProfileReportType.Employee_Service_Book: + // break; + //case EnumProfileReportType.Appointment_Letter_Officer: + // bytes = new rptEmployee().GetAsstOfficeAndAbove(employeeID, payrollTypeId, reportType); + // break; + case EnumProfileReportType.Appointment_Letter_Worker: + ltemplate.SetObjectID(FixedLetterTemplte.Worker_Appointment_Letter); + + ltemplate.ID = 3; + ltemplate.Description = "Letter Template Worker"; + ltemplate.Subject = "Letter Template Worker"; + ltemplate.Type = EnumDocType.Desktop_Letter; + ltemplate.TypeID = (int)EnumDocType.Desktop_Letter; + + string downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads"; + string lFileName = "Worker.doc"; + + string sFilePath = new rptEmployee().Generate(ltemplate, employeeID, payrollTypeId, downloadPath, lFileName); + + + byte[] buffer = new byte[16 * 1024]; + buffer = System.IO.File.ReadAllBytes(sFilePath); + string contentType = GetFileType(sFilePath); + var name = System.IO.Path.ChangeExtension(lFileName, ".pdf"); + if (System.IO.File.Exists(sFilePath)) + { + System.IO.File.Delete(sFilePath); + } + return File(buffer, contentType, name); + break; + case EnumProfileReportType.Appointment_Letter_Staff: + ltemplate.SetObjectID(FixedLetterTemplte.Staff_Appointment_Letter); + + ltemplate.ID = 2; + ltemplate.Description = "Letter Template Staff"; + ltemplate.Subject = "Letter Template Staff"; + ltemplate.Type = EnumDocType.Desktop_Letter; + ltemplate.TypeID = (int)EnumDocType.Desktop_Letter; + + new rptEmployee().Generate(ltemplate, employeeID, payrollTypeId, "C:\\Users\\mashfiq.EASE\\Downloads", "Staff.doc"); + break; + default: + break; + + } + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + } + + return Ok(bytes); + } + + [HttpPost] + [Route("get-file-type")] + [AllowAnonymous] + [IgnoreAntiforgeryToken] + [ProducesResponseType(StatusCodes.Status200OK)] + public string GetFileType(string originalFileName) + { + //var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data)); + //string fileName = (string)item["fileName"].ToObject(); + string fileName = originalFileName; + string contentType; + new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); + return contentType ?? "application/octet-stream"; + } } }