From 88744bd4cf2e7d5bd65507b99092d3182de2dfb9 Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 2 Feb 2026 17:54:33 +0330 Subject: [PATCH 1/3] add endpoint to download case history as Excel file --- .../RollCall/IRollCallApplication.cs | 10 +++++++ .../RollCallApplication.cs | 16 +++++++++-- .../RollCall/RollCallCaseHistoryController.cs | 27 ++++++++++--------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs b/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs index bdff1e85..31960578 100644 --- a/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs +++ b/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs @@ -131,6 +131,16 @@ namespace CompanyManagment.App.Contracts.RollCall Task> GetCaseHistoryTitles(long workshopId,RollCallCaseHistorySearchModel searchModel); Task> GetCaseHistoryDetails(long workshopId, string titleId, RollCallCaseHistorySearchModel searchModel); + + Task DownloadCaseHistoryExcel(long workshopId, string titleId, + RollCallCaseHistorySearchModel searchModel); + } + + public class RollCallCaseHistoryExcelDto + { + public byte[] Bytes { get; set; } + public string FileName { get; set; } + public string MimeType { get; set; } } public class RollCallCaseHistoryDetail diff --git a/CompanyManagment.Application/RollCallApplication.cs b/CompanyManagment.Application/RollCallApplication.cs index 7f145407..1397b15b 100644 --- a/CompanyManagment.Application/RollCallApplication.cs +++ b/CompanyManagment.Application/RollCallApplication.cs @@ -16,6 +16,7 @@ using Company.Domain.LeaveAgg; using Company.Domain.RollCallAgg; using Company.Domain.RollCallAgg.DomainService; using Company.Domain.RollCallEmployeeAgg; +using Company.Domain.WorkshopAgg; using CompanyManagment.App.Contracts.Checkout; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.RollCall; @@ -34,8 +35,9 @@ public class RollCallApplication : IRollCallApplication private readonly ICustomizeWorkshopSettingsRepository _customizeWorkshopSettingsRepository; private readonly ICustomizeWorkshopEmployeeSettingsRepository _customizeWorkshopEmployeeSettingsRepository; private readonly ICustomizeCheckoutTempRepository _customizeCheckoutTempRepository; + private readonly IWorkshopRepository _workshopRepository; - public RollCallApplication(IRollCallRepository rollCallRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, IEmployeeRepository employeeRepository, ILeaveRepository leaveRepository, ICustomizeCheckoutRepository customizeCheckoutRepository, ICheckoutRepository checkoutRepository, IRollCallDomainService rollCallDomainService, ICustomizeWorkshopSettingsRepository customizeWorkshopSettingsRepository, ICustomizeWorkshopEmployeeSettingsRepository customizeWorkshopEmployeeSettingsRepository, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository) + public RollCallApplication(IRollCallRepository rollCallRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, IEmployeeRepository employeeRepository, ILeaveRepository leaveRepository, ICustomizeCheckoutRepository customizeCheckoutRepository, ICheckoutRepository checkoutRepository, IRollCallDomainService rollCallDomainService, ICustomizeWorkshopSettingsRepository customizeWorkshopSettingsRepository, ICustomizeWorkshopEmployeeSettingsRepository customizeWorkshopEmployeeSettingsRepository, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository, IWorkshopRepository workshopRepository) { _rollCallRepository = rollCallRepository; _rollCallEmployeeRepository = rollCallEmployeeRepository; @@ -47,7 +49,8 @@ public class RollCallApplication : IRollCallApplication _customizeWorkshopSettingsRepository = customizeWorkshopSettingsRepository; _customizeWorkshopEmployeeSettingsRepository = customizeWorkshopEmployeeSettingsRepository; _customizeCheckoutTempRepository = customizeCheckoutTempRepository; - } + _workshopRepository = workshopRepository; + } public OperationResult Create(CreateRollCall command) { @@ -869,4 +872,13 @@ public class RollCallApplication : IRollCallApplication { return await _rollCallRepository.GetCaseHistoryDetails(workshopId, titleId, searchModel); } + + public async Task DownloadCaseHistoryExcel(long workshopId, string titleId, + RollCallCaseHistorySearchModel searchModel) + { + var data = await _rollCallRepository + .GetCaseHistoryDetails(workshopId, titleId, searchModel); + var workshopFullName = _workshopRepository.(workshopId); + byte[] excelBytes = RollCallExcelGenerator.CaseHistoryExcelForOneDay(data); + } } \ No newline at end of file diff --git a/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs b/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs index 5bbf6c70..fc22ec04 100644 --- a/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs +++ b/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs @@ -1,6 +1,7 @@ using _0_Framework.Application; using CompanyManagement.Infrastructure.Excel.RollCall; using CompanyManagment.App.Contracts.RollCall; +using CompanyManagment.App.Contracts.Workshop; using Microsoft.AspNetCore.Mvc; using ServiceHost.BaseControllers; @@ -10,11 +11,12 @@ public class RollCallCaseHistoryController : ClientBaseController { private readonly IRollCallApplication _rollCallApplication; private readonly long _workshopId; - + private readonly IWorkshopApplication _workshopApplication; public RollCallCaseHistoryController(IRollCallApplication rollCallApplication, - IAuthHelper authHelper) + IAuthHelper authHelper, IWorkshopApplication workshopApplication) { _rollCallApplication = rollCallApplication; + _workshopApplication = workshopApplication; _workshopId = authHelper.GetWorkshopId(); } @@ -93,16 +95,17 @@ public class RollCallCaseHistoryController : ClientBaseController } } - // [HttpGet("excel")] - // public async Task GetDownload(string titleId,RollCallCaseHistorySearchModel searchModel) - // { - // var data = await _rollCallApplication.GetCaseHistoryDetails(_workshopId, titleId, searchModel); - // byte[] excelBytes = RollCallExcelGenerator.CaseHistoryExcelForOneDay(data); - // return File(excelBytes, - // "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - // $"{workshopFullName} - {caseHistoryRollCallExcelForOneDay.DayOfWeekFa}،{caseHistoryRollCallExcelForOneDay.DateFa}.xlsx"); - // - // } + [HttpGet("excel")] + public async Task GetDownload(string titleId, RollCallCaseHistorySearchModel searchModel) + { + var res =await _rollCallApplication.DownloadCaseHistoryExcel(_workshopId, titleId, searchModel); + + + return File(res.Bytes, + res.MimeType, + res.FileName); + + } // [HttpGet("edit")] // public ActionResult<> GetEditDetails(string date,long employeeId) From bf2a102a55592079b2096f5f48b333a5bf11d036 Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 2 Feb 2026 19:07:11 +0330 Subject: [PATCH 2/3] 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 }, From 3dace574ffaca65caa555b995bc6d1fde0d6597e Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 2 Feb 2026 19:08:25 +0330 Subject: [PATCH 3/3] refactor date parsing method in RollCallCaseHistoryController --- .../RollCall/RollCallCaseHistoryController.cs | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs b/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs index fc22ec04..ec8d347d 100644 --- a/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs +++ b/ServiceHost/Areas/Client/Controllers/RollCall/RollCallCaseHistoryController.cs @@ -78,28 +78,12 @@ public class RollCallCaseHistoryController : ClientBaseController return op.Succcedded(duration); } - private static bool TryParseDateTime(string date, string time, out DateTime result) - { - result = default; - - try - { - var dateTime = date.ToGeorgianDateTime(); - var timeOnly = TimeOnly.Parse(time); - result = dateTime.AddTicks(timeOnly.Ticks); - return true; - } - catch - { - return false; - } - } + [HttpGet("excel")] public async Task GetDownload(string titleId, RollCallCaseHistorySearchModel searchModel) { var res =await _rollCallApplication.DownloadCaseHistoryExcel(_workshopId, titleId, searchModel); - return File(res.Bytes, res.MimeType, @@ -127,4 +111,22 @@ public class RollCallCaseHistoryController : ClientBaseController // TotalRollCallsDuration = total.ToFarsiHoursAndMinutes("-") // }; // } + + + private static bool TryParseDateTime(string date, string time, out DateTime result) + { + result = default; + + try + { + var dateTime = date.ToGeorgianDateTime(); + var timeOnly = TimeOnly.Parse(time); + result = dateTime.AddTicks(timeOnly.Ticks); + return true; + } + catch + { + return false; + } + } } \ No newline at end of file