From bf2a102a55592079b2096f5f48b333a5bf11d036 Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 2 Feb 2026 19:07:11 +0330 Subject: [PATCH] add Excel export functionality for roll call case history --- .../RollCall/RollCallExcelGenerator.cs | 111 ++++++++++++++++++ .../CompanyManagment.Application.csproj | 1 + .../RollCallApplication.cs | 47 +++++++- ServiceHost/Properties/launchSettings.json | 2 +- 4 files changed, 155 insertions(+), 6 deletions(-) diff --git a/CompanyManagement.Infrastructure.Excel/RollCall/RollCallExcelGenerator.cs b/CompanyManagement.Infrastructure.Excel/RollCall/RollCallExcelGenerator.cs index 8c749dd4..8f0247a3 100644 --- a/CompanyManagement.Infrastructure.Excel/RollCall/RollCallExcelGenerator.cs +++ b/CompanyManagement.Infrastructure.Excel/RollCall/RollCallExcelGenerator.cs @@ -1,6 +1,12 @@ using _0_Framework.Excel; +using _0_Framework.Application; +using CompanyManagment.App.Contracts.RollCall; using OfficeOpenXml; using OfficeOpenXml.Drawing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; namespace CompanyManagement.Infrastructure.Excel.RollCall; @@ -308,6 +314,111 @@ public class RollCallExcelGenerator : ExcelGenerator return package.GetAsByteArray(); } + public static byte[] CaseHistoryExcelForEmployee(List data, string titleId) + { + if (!Regex.IsMatch(titleId, @"^\d{4}_\d{2}$")) + throw new ArgumentException("Invalid titleId format.", nameof(titleId)); + + var splitDate = titleId.Split("_"); + var year = Convert.ToInt32(splitDate.First()); + var month = Convert.ToInt32(splitDate.Last()); + + var startDateFa = $"{year:D4}/{month:D2}/01"; + var startDate = startDateFa.ToGeorgianDateTime(); + var endDateFa = startDateFa.FindeEndOfMonth(); + var endDate = endDateFa.ToGeorgianDateTime(); + + var dateRange = (int)(endDate.Date - startDate.Date).TotalDays + 1; + var dates = Enumerable.Range(0, dateRange).Select(x => startDate.AddDays(x)).ToList(); + + var safeData = data ?? new List(); + var first = safeData.FirstOrDefault(); + var totalWorkingTime = new TimeSpan(safeData.Sum(x => x.TotalWorkingTime.Ticks)); + + var viewModel = new CaseHistoryRollCallExcelForEmployeeViewModel + { + EmployeeId = first?.EmployeeId ?? 0, + DateGr = startDate, + PersonnelCode = first?.PersonnelCode, + EmployeeFullName = first?.EmployeeFullName, + PersianMonthName = month.ToFarsiMonthByIntNumber(), + PersianYear = year.ToString(), + TotalWorkingHoursFa = totalWorkingTime.ToFarsiHoursAndMinutes("-"), + TotalWorkingTimeSpan = $"{(int)totalWorkingTime.TotalHours}:{totalWorkingTime.Minutes:00}", + RollCalls = dates.Select((date, index) => + { + var item = index < safeData.Count ? safeData[index] : null; + var records = item?.Records ?? new List(); + + return new RollCallItemForEmployeeExcelViewModel + { + DateGr = date, + DateFa = date.ToFarsi(), + DayOfWeekFa = date.DayOfWeek.DayOfWeeKToPersian(), + PersonnelCode = item?.PersonnelCode, + EmployeeFullName = item?.EmployeeFullName, + IsAbsent = item?.Status == RollCallRecordStatus.Absent, + HasLeave = item?.Status == RollCallRecordStatus.Leaved, + IsHoliday = false, + TotalWorkingHours = (item?.TotalWorkingTime ?? TimeSpan.Zero).ToFarsiHoursAndMinutes("-"), + StartsItems = JoinRecords(records, r => r.StartTime), + EndsItems = JoinRecords(records, r => r.EndTime), + EnterTimeDifferences = JoinRecords(records, r => FormatSignedTimeSpan(r.EntryTimeDifference)), + ExitTimeDifferences = JoinRecords(records, r => FormatSignedTimeSpan(r.ExitTimeDifference)) + }; + }).ToList() + }; + + return CaseHistoryExcelForEmployee(viewModel); + } + + public static byte[] CaseHistoryExcelForOneDay(List data, string titleId) + { + if (!Regex.IsMatch(titleId, @"^\d{4}/\d{2}/\d{2}$")) + throw new ArgumentException("Invalid titleId format.", nameof(titleId)); + + var dateGr = titleId.ToGeorgianDateTime(); + var safeData = data ?? new List(); + + var viewModel = new CaseHistoryRollCallForOneDayViewModel + { + DateFa = titleId, + DateGr = dateGr, + DayOfWeekFa = dateGr.DayOfWeek.DayOfWeeKToPersian(), + RollCalls = safeData.Select(item => + { + var records = item.Records ?? new List(); + return new RollCallItemForOneDayExcelViewModel + { + EmployeeFullName = item.EmployeeFullName, + PersonnelCode = item.PersonnelCode, + StartsItems = JoinRecords(records, r => r.StartTime), + EndsItems = JoinRecords(records, r => r.EndTime), + TotalWorkingHours = item.TotalWorkingTime.ToFarsiHoursAndMinutes("-") + }; + }).ToList() + }; + + return CaseHistoryExcelForOneDay(viewModel); + } + + private static string JoinRecords(IEnumerable records, Func selector) + { + var safeRecords = records ?? Enumerable.Empty(); + var values = safeRecords.Select(selector).Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); + return values.Count == 0 ? string.Empty : string.Join(Environment.NewLine, values); + } + + private static string FormatSignedTimeSpan(TimeSpan value) + { + if (value == TimeSpan.Zero) + return "-"; + + var abs = value.Duration(); + var sign = value.Ticks < 0 ? "-" : "+"; + return $"{(int)abs.TotalHours}:{abs.Minutes:00}{sign}"; + } + private string CalculateExitMinuteDifference(TimeSpan early, TimeSpan late) { if (early == TimeSpan.Zero && late == TimeSpan.Zero) diff --git a/CompanyManagment.Application/CompanyManagment.Application.csproj b/CompanyManagment.Application/CompanyManagment.Application.csproj index f6e0f038..d33e4227 100644 --- a/CompanyManagment.Application/CompanyManagment.Application.csproj +++ b/CompanyManagment.Application/CompanyManagment.Application.csproj @@ -13,6 +13,7 @@ + diff --git a/CompanyManagment.Application/RollCallApplication.cs b/CompanyManagment.Application/RollCallApplication.cs index 1397b15b..e987d47e 100644 --- a/CompanyManagment.Application/RollCallApplication.cs +++ b/CompanyManagment.Application/RollCallApplication.cs @@ -3,9 +3,11 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using _0_Framework.Application; using _0_Framework.Domain.CustomizeCheckoutShared.Enums; +using _0_Framework.Exceptions; using Company.Domain.CheckoutAgg; using Company.Domain.CustomizeCheckoutAgg; using Company.Domain.CustomizeCheckoutTempAgg; @@ -17,6 +19,7 @@ using Company.Domain.RollCallAgg; using Company.Domain.RollCallAgg.DomainService; using Company.Domain.RollCallEmployeeAgg; using Company.Domain.WorkshopAgg; +using CompanyManagement.Infrastructure.Excel.RollCall; using CompanyManagment.App.Contracts.Checkout; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.RollCall; @@ -874,11 +877,45 @@ public class RollCallApplication : IRollCallApplication } public async Task DownloadCaseHistoryExcel(long workshopId, string titleId, - RollCallCaseHistorySearchModel searchModel) + RollCallCaseHistorySearchModel searchModel) { - var data = await _rollCallRepository - .GetCaseHistoryDetails(workshopId, titleId, searchModel); - var workshopFullName = _workshopRepository.(workshopId); - byte[] excelBytes = RollCallExcelGenerator.CaseHistoryExcelForOneDay(data); + var data = await _rollCallRepository + .GetCaseHistoryDetails(workshopId, titleId, searchModel); + string nameSecondPart = ""; + byte[] excelBytes; + + if (Regex.IsMatch(titleId, @"^\d{4}_\d{2}$")) + { + var splitDate = titleId.Split("_"); + var year = Convert.ToInt32(splitDate.First()); + var month = Convert.ToInt32(splitDate.Last()); + + var monthName = Convert.ToInt32(month).ToFarsiMonthByIntNumber(); + nameSecondPart = $"{year}/{monthName}"; + excelBytes = RollCallExcelGenerator.CaseHistoryExcelForEmployee(data, titleId); + } + else if (Regex.IsMatch(titleId, @"^\d{4}/\d{2}/\d{2}$")) + { + var oneDayDate = titleId.ToGeorgianDateTime(); + nameSecondPart = $" {oneDayDate.DayOfWeek.DayOfWeeKToPersian()}،{titleId}"; + excelBytes = RollCallExcelGenerator.CaseHistoryExcelForOneDay(data, titleId); + } + else + { + throw new BadRequestException("شناسه سر تیتر وارد شده نامعتبر است"); + } + + var workshopFullName = _workshopRepository.Get(workshopId)?.WorkshopFullName ?? "بدون کارگاه"; + + var fileName = $"{workshopFullName} - {nameSecondPart}.xlsx"; + + var res = new RollCallCaseHistoryExcelDto() + { + Bytes = excelBytes, + MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + FileName = fileName + }; + + return res; } } \ No newline at end of file diff --git a/ServiceHost/Properties/launchSettings.json b/ServiceHost/Properties/launchSettings.json index 788962e4..3c6abef3 100644 --- a/ServiceHost/Properties/launchSettings.json +++ b/ServiceHost/Properties/launchSettings.json @@ -19,7 +19,7 @@ "sqlDebugging": true, "dotnetRunMessages": "true", "nativeDebugging": true, - "applicationUrl": "https://localhost:5004;http://localhost:5003;", + "applicationUrl": "https://localhost:5004;http://localhost:5003;https://192.168.0.117:5005", "jsWebView2Debugging": false, "hotReloadEnabled": true },