From 4ffdf14de2a08c030a59cadd0650331cbbf696d9 Mon Sep 17 00:00:00 2001 From: MahanCh Date: Mon, 14 Apr 2025 16:02:51 +0330 Subject: [PATCH] add excel export for file page --- Company.Domain/File1/IFileRepository.cs | 3 + .../CaseManagementExcelGenerator.cs | 125 ++++++++++++++++++ ...panyManagement.Infrastructure.Excel.csproj | 3 + .../File1/FileExcelViewModel.cs | 49 +++++++ .../File1/IFileApplication.cs | 2 + .../FileApplication.cs | 6 + .../Repository/FileRepository.cs | 37 +++++- DadmehrGostar.sln.DotSettings | 2 + .../Admin/Pages/Company/FilePage/Index.cshtml | 12 +- .../Pages/Company/FilePage/Index.cshtml.cs | 14 ++ 10 files changed, 247 insertions(+), 6 deletions(-) create mode 100644 CompanyManagement.Infrastructure.Excel/CaseManagement/CaseManagementExcelGenerator.cs create mode 100644 CompanyManagment.App.Contracts/File1/FileExcelViewModel.cs create mode 100644 DadmehrGostar.sln.DotSettings diff --git a/Company.Domain/File1/IFileRepository.cs b/Company.Domain/File1/IFileRepository.cs index 8ac1cf0f..da88974a 100644 --- a/Company.Domain/File1/IFileRepository.cs +++ b/Company.Domain/File1/IFileRepository.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using _0_Framework_b.Domain; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.Employer; @@ -22,4 +23,6 @@ public interface IFileRepository : IRepository List GetFileList(FileSearchModel searchModel); #endregion + + Task> GetExcelDetails(); } \ No newline at end of file diff --git a/CompanyManagement.Infrastructure.Excel/CaseManagement/CaseManagementExcelGenerator.cs b/CompanyManagement.Infrastructure.Excel/CaseManagement/CaseManagementExcelGenerator.cs new file mode 100644 index 00000000..77e239c0 --- /dev/null +++ b/CompanyManagement.Infrastructure.Excel/CaseManagement/CaseManagementExcelGenerator.cs @@ -0,0 +1,125 @@ +using System.Drawing; +using OfficeOpenXml.Style; +using OfficeOpenXml; +using CompanyManagement.Infrastructure.Excel.Checkout; +using CompanyManagment.App.Contracts.File1; +using Microsoft.AspNetCore.Server.HttpSys; +using _0_Framework.Application; + +namespace CompanyManagement.Infrastructure.Excel.CaseManagement; + +public class CaseManagementExcelGenerator +{ + public static Dictionary Header { get; set; } = new() + { + {"ArchiveNumber","شماره بایگانی"}, + {"FileClass","کلاسه پرونده"}, + {"Client","موکل"}, + {"LitigationParty","طرف دعوی"}, + {"DiagnosisPetitionTotalPenalty","مبلغ دادنامه تشخیص"}, + {"DisputeResolutionTotalPenalty","مبلغ دادنامه حل اختلاف"}, + {"MclsUsername","نام کاربری"}, + {"MclsPassword","رمز عبور"}, + + }; + public static byte[] GenerateCheckoutTempExcelInfo(List data) + { + OfficeOpenXml.ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using var package = new ExcelPackage(); + var worksheet = package.Workbook.Worksheets.Add("Sheet1"); + + var indexCell = worksheet.Cells[1, 1]; + indexCell.Value = "ردیف"; + ApplyHeaderStyle(indexCell); + + for (int i = 0; i < Header.Count; i++) + { + worksheet.Cells[1, i + 2].Value = Header.ElementAt(i).Value; + ApplyHeaderStyle(worksheet.Cells[1, i + 2]); + } + var dataRow = 2; + foreach (var item in data) + { + var column = 2; + foreach (var header in Header) + { + var property = item.GetType().GetProperty(header.Key); + var value = property.GetValue(item, null)?.ToString(); + + var cell = worksheet.Cells[dataRow, column]; + + // Check if the property requires MoneyToDouble() + + if (RequiresMoneyToDouble(property.Name)) + { + cell.Value = MoneyToDouble(value); + cell.Style.Numberformat.Format = "#,##0"; + } + else + { + cell.Value = value; + } + ApplyGeneralDataStyle(worksheet.Cells[dataRow, column]); + switch (item.Status) + { + case 1: + cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Gray); + break; + case 2: + cell.Style.Fill.BackgroundColor.SetColor(Color.White); + break; + case 3: + cell.Style.Fill.BackgroundColor.SetColor(Color.Orange); + break; + } + column++; + } + + + + var rowCounter = worksheet.Cells[dataRow, 1]; + rowCounter.Value = dataRow - 1; + + dataRow++; + } + + worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); + worksheet.View.RightToLeft = true; + return package.GetAsByteArray(); + } + + private static bool RequiresMoneyToDouble(string propertyName) + { + var propertiesRequiringConversion = new HashSet + { + "DiagnosisPetitionTotalPenalty", "DisputeResolutionTotalPenalty" + }; + return propertiesRequiringConversion.Contains(propertyName); + } + private static double MoneyToDouble(string value) + { + Console.WriteLine(value); + var min = value.Length > 1 ? value.Substring(0, 2) : ""; + var test = min == "\u200e\u2212" ? value.MoneyToDouble() * -1 : value.MoneyToDouble(); + + Console.WriteLine(test); + return test; + } + + + private static void ApplyHeaderStyle(ExcelRange cell) + { + cell.Style.Font.Bold = true; + cell.Style.Fill.PatternType = ExcelFillStyle.Solid; + cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray); + cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + cell.Style.Border.BorderAround(ExcelBorderStyle.Thin); + } + private static void ApplyGeneralDataStyle(ExcelRange cell) + { + cell.Style.Border.BorderAround(ExcelBorderStyle.Thin); + cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + cell.Style.Fill.PatternType = ExcelFillStyle.Solid; + } +} \ No newline at end of file diff --git a/CompanyManagement.Infrastructure.Excel/CompanyManagement.Infrastructure.Excel.csproj b/CompanyManagement.Infrastructure.Excel/CompanyManagement.Infrastructure.Excel.csproj index 831f89cc..a62b92f9 100644 --- a/CompanyManagement.Infrastructure.Excel/CompanyManagement.Infrastructure.Excel.csproj +++ b/CompanyManagement.Infrastructure.Excel/CompanyManagement.Infrastructure.Excel.csproj @@ -13,5 +13,8 @@ + + + diff --git a/CompanyManagment.App.Contracts/File1/FileExcelViewModel.cs b/CompanyManagment.App.Contracts/File1/FileExcelViewModel.cs new file mode 100644 index 00000000..28090ca2 --- /dev/null +++ b/CompanyManagment.App.Contracts/File1/FileExcelViewModel.cs @@ -0,0 +1,49 @@ +namespace CompanyManagment.App.Contracts.File1; + +public class FileExcelViewModel +{ + /// + /// شماره بایگانی + /// + public long ArchiveNumber { get; set; } + + /// + /// کلاسه پرونده + /// + public string FileClass { get; set; } = string.Empty; + + /// + /// موکل + /// + public string Client { get; set; } = string.Empty; + + /// + /// طرف دعوی + /// + public string LitigationParty { get; set; } = string.Empty; + + /// + /// مبلغ دادنامه تشخیص + /// + public string DiagnosisPetitionTotalPenalty { get; set; } = string.Empty; + + /// + /// مبلغ دادنامه حل اختلاف + /// + public string DisputeResolutionTotalPenalty { get; set; } = string.Empty; + + + /// + /// نام کاربری اداره کار + /// + public string MclsUsername { get; set; } = string.Empty; + /// + /// رمز عبور اداره کار + /// + public string MclsPassword { get; set; } = string.Empty; + + /// + /// وضعیت پرونده. یک به معنی غیرفعال. دو به معنی فعال. سه به معنی خروج موقت + /// + public int Status { get; set; } +} \ No newline at end of file diff --git a/CompanyManagment.App.Contracts/File1/IFileApplication.cs b/CompanyManagment.App.Contracts/File1/IFileApplication.cs index 531ee3c5..972b4f50 100644 --- a/CompanyManagment.App.Contracts/File1/IFileApplication.cs +++ b/CompanyManagment.App.Contracts/File1/IFileApplication.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using _0_Framework_b.Application; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.Employer; @@ -23,4 +24,5 @@ public interface IFileApplication List GetAllEmploees(bool filter = true); List GetAllEmployers(bool filter = true); //bool CheckValue(string viewModel, string searchModel); + Task> GetExcelDetails(); } \ No newline at end of file diff --git a/CompanyManagment.Application/FileApplication.cs b/CompanyManagment.Application/FileApplication.cs index 64ce92bd..9f110cbe 100644 --- a/CompanyManagment.Application/FileApplication.cs +++ b/CompanyManagment.Application/FileApplication.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using _0_Framework_b.Application; using Company.Domain.File1; using Company.Domain.ProceedingSession; @@ -199,6 +200,11 @@ public class FileApplication : IFileApplication return employers; } + public async Task> GetExcelDetails() + { + return await _fileRepository.GetExcelDetails(); + } + public FileViewModel GetFileDetails(FileViewModel file) { var viewModel = new FileViewModel(); diff --git a/CompanyManagment.EFCore/Repository/FileRepository.cs b/CompanyManagment.EFCore/Repository/FileRepository.cs index fcbf8ea3..e2b641e6 100644 --- a/CompanyManagment.EFCore/Repository/FileRepository.cs +++ b/CompanyManagment.EFCore/Repository/FileRepository.cs @@ -1,11 +1,13 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using _0_Framework_b.InfraStructure; using _0_Framework_b.Application; using Company.Domain.File1; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.Employer; using CompanyManagment.App.Contracts.File1; +using Microsoft.EntityFrameworkCore; namespace CompanyManagment.EFCore.Repository; @@ -261,7 +263,7 @@ public class FileRepository : RepositoryBase, EmployeeFullName = x.FName + " " + x.LName }).FirstOrDefault(); - return result?.EmployeeFullName??""; + return result?.EmployeeFullName ?? ""; } public string GetEmployerFullNameById(long id) @@ -271,7 +273,7 @@ public class FileRepository : RepositoryBase, FullName = x.FullName }).FirstOrDefault(); - return result?.FullName??""; + return result?.FullName ?? ""; } public List GetAllEmploees() @@ -361,4 +363,35 @@ public class FileRepository : RepositoryBase, return query.OrderByDescending(x => x.ArchiveNo).Skip(searchModel.PageIndex).Take(30).ToList(); } + + public async Task> GetExcelDetails() + { + var fileQuery = _context.Files.Include(x => x.PetitionsList).AsQueryable(); + var requesterEmployeeQuery = await _context.Employees.Where(x => fileQuery.Any(f => f.Reqester == x.id)).ToListAsync(); + var summonedEmployerQuery = await _context.Employers.Where(x => fileQuery.Any(f => f.Summoned == x.id)).ToListAsync(); + + var files = await fileQuery.ToListAsync(); + + var res = files.Select(x => + { + var requester = requesterEmployeeQuery.FirstOrDefault(e => e.id == x.Reqester); + var summoned = summonedEmployerQuery.FirstOrDefault(e => e.id == x.Summoned); + + return new FileExcelViewModel + { + ArchiveNumber = x.ArchiveNo, + FileClass = x.FileClass, + Client = x.Client == 1 ? requester?.FullName : summoned?.FullName ?? "-", + LitigationParty = x.Client == 1 ? summoned?.FullName : requester?.FullName ?? "-", + DiagnosisPetitionTotalPenalty = + x.PetitionsList.FirstOrDefault(p => p.BoardType_Id == 1)?.TotalPenalty ?? "-", + DisputeResolutionTotalPenalty = x.PetitionsList.FirstOrDefault(p => p.BoardType_Id == 2)?.TotalPenalty ?? "-", + MclsUsername = x.Client == 1 ? requester?.MclsUserName : summoned?.MclsUserName ?? "-", + MclsPassword = x.Client == 1 ? requester?.MclsPassword : summoned?.MclsPassword ?? "-", + Status = x.Status + }; + }).OrderBy(x=>x.Status ==1).ThenBy(x=>x.Status == 3).ThenBy(x=>x.Status==2); + + return res.ToList(); + } } \ No newline at end of file diff --git a/DadmehrGostar.sln.DotSettings b/DadmehrGostar.sln.DotSettings new file mode 100644 index 00000000..71841217 --- /dev/null +++ b/DadmehrGostar.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml b/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml index 16b78113..4b2cbe69 100644 --- a/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml +++ b/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml @@ -155,10 +155,14 @@

ایجاد پرونده جدید

-

- زمانبندی -

- +

+ زمانبندی +

+ +

+ دانلود اکسل +

+ diff --git a/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml.cs b/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml.cs index 9ca7a73c..88fde182 100644 --- a/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml.cs +++ b/ServiceHost/Areas/Admin/Pages/Company/FilePage/Index.cshtml.cs @@ -1,4 +1,5 @@ using _0_Framework_b.Application; +using CompanyManagement.Infrastructure.Excel.CaseManagement; using CompanyManagment.App.Contracts.Board; using CompanyManagment.App.Contracts.Contact2; using CompanyManagment.App.Contracts.Evidence; @@ -650,4 +651,17 @@ public class IndexModel : PageModel return new JsonResult(result); } + + #region Mahan + + public async Task OnGetExcelFileDetails() + { + var data = await _fileApplication.GetExcelDetails(); + var excelBytes = CaseManagementExcelGenerator.GenerateCheckoutTempExcelInfo(data); + return File(excelBytes, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + $"پرونده.xlsx"); + } + + #endregion } \ No newline at end of file