using System.Drawing; using _0_Framework.Application; using OfficeOpenXml; using OfficeOpenXml.Style; namespace CompanyManagement.Infrastructure.Excel.Checkout; public class CustomizeCheckoutExcelGenerator { public static Dictionary Header { get; set; } = new() { { "Month", "ماه" }, { "Year", "سال" }, { "EmployeeFullName", "نام و نام خانوادگی" }, { "PersonnelCodeString", "شماره پرسنلی" }, { "NationalCode", "کدملی" }, { "SumOfWorkingDays", "روز کارکرد" }, { "MonthlySalary", "حقوق ماهانه" }, { "BaseYearsPay", "سنوات" }, { "MarriedAllowance", "حق تاهل" }, { "OvertimePay", "اضافه کاری" }, { "NightworkPay", "شب کاری" }, { "FridayPay", "جمعه کاری" }, { "MissionPay", "مأموریت" }, { "ShiftPay", "نوبت کاری" }, { "FamilyAllowance", "حق فرزند" }, { "BonusesPay", "پاداش" }, { "LeavePay", "مزد مرخصی" }, { "RewardPay", "پاداش" }, { "FineDeduction", "جریمه" }, { "InsuranceDeduction", "حق بیمه" }, { "TaxDeducation", "مالیات" }, { "InstallmentDeduction", "قسط وام" }, { "SalaryAidDeduction", "مساعده" }, { "AbsenceDeduction", "غیبت" }, { "EarlyExitDeduction", "تعجیل در خروج" }, { "LateToWorkDeduction", "تاخیر در ورود" }, { "TotalClaims", "جمع مطالبات" }, { "TotalDeductions", "جمع کسورات" }, { "TotalPayment", "مبلغ قابل پرداخت" }, { "CardNumber", "شماره کارت" }, { "ShebaNumber", "شماره شبا" }, { "BankAccountNumber", "شماره حساب" }, { "BankName", "نام بانک" }, }; public static byte[] GenerateCheckoutTempExcelInfo(List data, List selectedParameters) { ExcelPackage.License.SetNonCommercialOrganization("Gozareshgir Noncommercial organization"); using var package = new ExcelPackage(); var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Define headers Dictionary filteredHeaders; if (!selectedParameters.Any()) { filteredHeaders = Header; } else { // Filter headers based on selected parameters filteredHeaders = Header.Where(h => selectedParameters.Contains(h.Key)).ToDictionary(); } var indexCell = worksheet.Cells[1, 1]; indexCell.Value = "ردیف"; ApplyHeaderStyle(indexCell); // Add headers to worksheet for (int i = 0; i < filteredHeaders.Count; i++) { worksheet.Cells[1, i + 2].Value = filteredHeaders.ElementAt(i).Value; ApplyHeaderStyle(worksheet.Cells[1, i + 2]); } var dataRow = 2; int totalPaymentColumnIndex = -1; foreach (var item in data) { var column = 2; foreach (var header in filteredHeaders) { var property = item.GetType().GetProperty(header.Key); var value = property.GetValue(item, null)?.ToString(); // Check if the property requires MoneyToDouble() if (RequiresMoneyToDouble(property.Name)) { worksheet.Cells[dataRow, column].Value = MoneyToDouble(value); } else { worksheet.Cells[dataRow, column].Value = value; } ApplyGeneralDataStyle(worksheet.Cells[dataRow, column]); ApplySpecificStyle(worksheet.Cells[dataRow, column], column, worksheet); // Apply specific styles if (header.Key == "TotalPayment") { totalPaymentColumnIndex = column; } column++; } var rowCounter = worksheet.Cells[dataRow, 1]; rowCounter.Value = dataRow - 1; ApplyGeneralDataStyle(rowCounter); ApplySpecificStyle(rowCounter, 1, worksheet); dataRow++; } worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); worksheet.PrinterSettings.PaperSize = ePaperSize.A4; worksheet.PrinterSettings.Orientation = eOrientation.Landscape; worksheet.PrinterSettings.FitToPage = true; worksheet.PrinterSettings.FitToWidth = 1; worksheet.PrinterSettings.FitToHeight = 0; worksheet.PrinterSettings.Scale = 85; worksheet.View.RightToLeft = true; if (totalPaymentColumnIndex != -1) { ApplyConditionalFormatting(worksheet, dataRow, totalPaymentColumnIndex); } return package.GetAsByteArray(); } // Method to check if a property requires MoneyToDouble() private static bool RequiresMoneyToDouble(string propertyName) { var propertiesRequiringConversion = new HashSet { "MonthlySalary", "RewardPay", "FridayPay", "OvertimePay", "ShiftPay", "NightWorkPay", "MarriedAllowance", "FamilyAllowance", "BonusesPay", "BaseYearsPay", "LeavePay", "AbsenceDeduction", "LateToWorkDeduction", "EarlyExitDeduction", "SalaryAidDeduction", "InstallmentDeduction", "FineDeduction", "InsuranceDeduction", "TaxDeduction", "TotalClaims", "TotalDeductions", "TotalPayment" }; return propertiesRequiringConversion.Contains(propertyName); } // Placeholder for the MoneyToDouble() method 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; } private static void ApplySpecificStyle(ExcelRange cell, int columnIndex, ExcelWorksheet worksheet) { var headerCell = worksheet.Cells[1, columnIndex].Value.ToString(); var index = Header.Values.ToList().IndexOf(headerCell); index += 2; switch (index) { case int n when (n >= 1 && n <= 8): cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow); break; case int n when (n >= 9 && n <= 19): cell.Style.Fill.BackgroundColor.SetColor(1, 208, 248, 208); cell.Style.Numberformat.Format = "#,##0"; break; case int n when (n >= 20 && n <= 27): cell.Style.Fill.BackgroundColor.SetColor(1, 246, 176, 176); cell.Style.Numberformat.Format = "#,##0"; break; case 28: cell.Style.Fill.BackgroundColor.SetColor(1, 169, 208, 142); cell.Style.Numberformat.Format = "#,##0"; break; case 29: cell.Style.Fill.BackgroundColor.SetColor(1, 241, 143, 143); cell.Style.Numberformat.Format = "#,##0"; break; case 30: cell.Style.Fill.BackgroundColor.SetColor(1, 168, 186, 254); cell.Style.Numberformat.Format = "#,##0"; break; case >= 31 and <= 34: cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow); break; } } private static void ApplyConditionalFormatting(ExcelWorksheet worksheet, int dataRow, int totalPaymentColumnIndex) { for (int rowIndex = 2; rowIndex < dataRow; rowIndex++) { var totalPaymentValue = worksheet.Cells[rowIndex, totalPaymentColumnIndex].Value; if (totalPaymentValue != null && double.TryParse(totalPaymentValue.ToString(), out double payment)) { if (payment < 0) { var rowRange = worksheet.Cells[rowIndex, 1, rowIndex, worksheet.Dimension.End.Column]; rowRange.Style.Fill.PatternType = ExcelFillStyle.Solid; rowRange.Style.Fill.BackgroundColor.SetColor(Color.Red); foreach (var cell in rowRange) { cell.Style.Font.Color.SetColor(Color.White); } } } } } }