Files
Backend-Api/CompanyManagement.Infrastructure.Excel/Temp/GetAllContractingPartyExcelGenerator.cs

170 lines
7.3 KiB
C#

using CompanyManagment.App.Contracts.InstitutionContract;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
namespace CompanyManagement.Infrastructure.Excel.Temp;
public class GetAllContractingPartyExcelGenerator
{
public static byte[] GenerateExcel(List<DataExcelResult> dataList)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using var package = new ExcelPackage();
var archive99Sheet = package.Workbook.Worksheets.Add("کارگاه های 99");
var rollCallSheet = package.Workbook.Worksheets.Add("حضورغیابی");
var archive99Data = dataList.Where(x => x.Workshops.Any(a => a.ArchiveCode == "99")).ToList();
CreateSheet(archive99Data, archive99Sheet);
CreateSheet(dataList.Where(x=>x.Workshops.Any(w=>w.HasRollCall)).ToList(), rollCallSheet);
return package.GetAsByteArray();
}
private static void CreateSheet(List<DataExcelResult> dataList, ExcelWorksheet ws)
{
string[] headers = new[]
{
"ContractingPartyId", "نام طرف حساب", "EmployerId", "نام کارفرما", "WorkshopId", "کد کارگاه", "نام کارگاه",
"خدمات قرارداد", "خدمات قرارداد حضوری", "خدمات بیمه", "خدمات بیمه حضوری",
"خدمات حضورغیاب", "فیش حقوقی غیر رسمی", "تعداد استند", "تعداد ماه های بدهی قبلی برای حضورغیاب"
};
for (int i = 0; i < headers.Length; i++)
{
var cell = ws.Cells[1, i + 1];
cell.Value = headers[i];
cell.Style.Font.Bold = true;
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
cell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
}
int row = 2;
foreach (var data in dataList)
{
int partyStartRow = row;
foreach (var workshop in data.Workshops)
{
int workshopStartRow = row;
var employers = workshop.Employers.Any()
? workshop.Employers
: new List<EmployerExcelResultData> { new() };
foreach (var employer in employers)
{
ws.Cells[row, 1].Value = data.ContractingPartyId;
ws.Cells[row, 2].Value = data.ContractingPartyName;
ws.Cells[row, 3].Value = employer?.Id;
ws.Cells[row, 4].Value = employer?.EmployerName;
ws.Cells[row, 5].Value = workshop.Id;
ws.Cells[row, 6].Value = workshop.ArchiveCode;
ws.Cells[row, 7].Value = workshop.WorkshopName;
ws.Cells[row, 8].Value = Convert.ToInt32(workshop.HasContract);
ws.Cells[row, 9].Value = 0; // فرضی
ws.Cells[row, 10].Value = Convert.ToInt32(workshop.HasInsurance);
ws.Cells[row, 11].Value = 0; // فرضی
ws.Cells[row, 12].Value = Convert.ToInt32(workshop.HasRollCall);
ws.Cells[row, 13].Value = Convert.ToInt32(workshop.HasCustomizeCheckout);
ws.Cells[row, 14].Value = 0;
ws.Cells[row, 15].Value = workshop.DebtRollCallMonth;
// 🌿 رنگ سبز برای سلول‌هایی که مقدارشان 1 است (True)
int[] boolCols = new[] { 8, 10, 12, 13, 14, 15 };
foreach (var col in boolCols)
{
var cell = ws.Cells[row, col];
if (Convert.ToInt32(cell.Value) > 0)
{
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(198, 239, 206)); // سبز اکسل
}
}
// Style: وسط‌چین + بوردر نازک برای همه سلول‌ها
for (int col = 1; col <= 15; col++)
{
var cell = ws.Cells[row, col];
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
cell.Style.Border.Top.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Left.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Right.Style = ExcelBorderStyle.Thin;
}
row++;
}
// Merge Workshop Columns
if (employers.Count > 1)
{
for (int col = 5; col <= 15; col++)
{
ws.Cells[workshopStartRow, col, row - 1, col].Merge = true;
var merged = ws.Cells[workshopStartRow, col, row - 1, col];
merged.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
merged.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
}
}
// Merge Contracting Party Columns
if (row - partyStartRow > 1)
{
for (int col = 1; col <= 2; col++)
{
ws.Cells[partyStartRow, col, row - 1, col].Merge = true;
var merged = ws.Cells[partyStartRow, col, row - 1, col];
merged.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
merged.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
}
// می‌تونی در صورت نیاز خط بالا و پایین ضخیم برای گروه‌ها رو هم فعال کنی اینجا
// for (int col = 1; col <= 15; col++)
// {
// ws.Cells[partyStartRow, col].Style.Border.Top.Style = ExcelBorderStyle.Medium;
// ws.Cells[row - 1, col].Style.Border.Bottom.Style = ExcelBorderStyle.Medium;
// }
}
ws.Cells.AutoFitColumns();
ws.View.RightToLeft = true;
}
}
public class DataExcelResult
{
public long ContractingPartyId { get; set; }
public string ContractingPartyName { get; set; }
public List<WorkshopExcelResultData> Workshops { get; set; }
}
public class EmployerExcelResultData
{
public long Id { get; set; }
public string EmployerName { get; set; }
}
public class WorkshopExcelResultData
{
public long Id { get; set; }
public string WorkshopName { get; set; }
public bool HasContract { get; set; }
public bool HasInsurance { get; set; }
public bool HasRollCall { get; set; }
public bool HasCustomizeCheckout { get; set; }
public List<EmployerExcelResultData> Employers { get; set; }
public int DebtRollCallMonth { get; set; }
public string ArchiveCode { get; set; }
}