142 lines
5.2 KiB
C#
142 lines
5.2 KiB
C#
using _0_Framework.Application;
|
|
using OfficeOpenXml;
|
|
using OfficeOpenXml.Style;
|
|
|
|
namespace CompanyManagement.Infrastructure.Excel.Checkout;
|
|
|
|
public class YektaTejaratCheckout
|
|
{
|
|
private static readonly Dictionary<string, string> Header = new()
|
|
{
|
|
{ "FullName", "نام و نام خانوادگی" },
|
|
{ "NationalCode", "کد ملی" },
|
|
{ "StaticPayableAmount", "مبلغ قابل پرداخت استاتیک" },
|
|
{ "AttendancePayableAmount", "مبلغ قابل پرداخت حضور غیاب" }
|
|
};
|
|
|
|
public static byte[] GenerateYektaTejaratCheckoutExcel(List<YektaTejaratCheckoutViewModel> data)
|
|
{
|
|
ExcelPackage.License.SetNonCommercialOrganization("Gozareshgir Noncommercial organization");
|
|
using var package = new ExcelPackage();
|
|
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
|
|
|
|
// Add row index header
|
|
var indexCell = worksheet.Cells[1, 1];
|
|
indexCell.Value = "ردیف";
|
|
ApplyHeaderStyle(indexCell);
|
|
|
|
// Add headers to worksheet
|
|
for (int i = 0; i < Header.Count; i++)
|
|
{
|
|
worksheet.Cells[1, i + 2].Value = Header.ElementAt(i).Value;
|
|
ApplyHeaderStyle(worksheet.Cells[1, i + 2]);
|
|
}
|
|
|
|
// Add data rows
|
|
var dataRow = 2;
|
|
foreach (var item in data)
|
|
{
|
|
// Row number
|
|
var rowCounter = worksheet.Cells[dataRow, 1];
|
|
rowCounter.Value = dataRow - 1;
|
|
ApplyGeneralDataStyle(rowCounter);
|
|
ApplySpecificStyle(rowCounter, 1);
|
|
|
|
var column = 2;
|
|
foreach (var header in Header)
|
|
{
|
|
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);
|
|
|
|
column++;
|
|
}
|
|
|
|
dataRow++;
|
|
}
|
|
|
|
// Auto-fit columns and set print settings
|
|
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
|
|
worksheet.PrinterSettings.PaperSize = ePaperSize.A4;
|
|
worksheet.PrinterSettings.Orientation = eOrientation.Portrait;
|
|
worksheet.PrinterSettings.FitToPage = true;
|
|
worksheet.PrinterSettings.FitToWidth = 1;
|
|
worksheet.PrinterSettings.FitToHeight = 0;
|
|
worksheet.View.RightToLeft = true;
|
|
|
|
return package.GetAsByteArray();
|
|
}
|
|
|
|
// Method to check if a property requires MoneyToDouble()
|
|
private static bool RequiresMoneyToDouble(string propertyName)
|
|
{
|
|
var propertiesRequiringConversion = new HashSet<string>
|
|
{
|
|
"StaticPayableAmount", "AttendancePayableAmount"
|
|
};
|
|
return propertiesRequiringConversion.Contains(propertyName);
|
|
}
|
|
|
|
// Convert money string to double
|
|
private static double MoneyToDouble(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
return 0;
|
|
|
|
var min = value.Length > 1 ? value.Substring(0, 2) : "";
|
|
var result = min == "\u200e\u2212" ? value.MoneyToDouble() * -1 : value.MoneyToDouble();
|
|
|
|
return result;
|
|
}
|
|
|
|
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)
|
|
{
|
|
switch (columnIndex)
|
|
{
|
|
case 1: // ردیف
|
|
cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
|
|
break;
|
|
case 2: // نام و نام خانوادگی
|
|
case 3: // کد ملی
|
|
cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow);
|
|
break;
|
|
case 4: // مبلغ قابل پرداخت استاتیک
|
|
cell.Style.Fill.BackgroundColor.SetColor(1, 208, 248, 208); // Light green
|
|
cell.Style.Numberformat.Format = "#,##0";
|
|
break;
|
|
case 5: // مبلغ قابل پرداخت حضور غیاب
|
|
cell.Style.Fill.BackgroundColor.SetColor(1, 168, 186, 254); // Light blue
|
|
cell.Style.Numberformat.Format = "#,##0";
|
|
break;
|
|
}
|
|
}
|
|
} |