diff --git a/0_Framework/Application/PagedResult.cs b/0_Framework/Application/PagedResult.cs index ecfa9ea1..c076e18d 100644 --- a/0_Framework/Application/PagedResult.cs +++ b/0_Framework/Application/PagedResult.cs @@ -7,7 +7,7 @@ namespace _0_Framework.Application; public class PagedResult where T : class { public int TotalCount { get; set; } - public List List { get; set; } + public List List { get; set; } = []; } public class PagedResult:PagedResult where T : class { diff --git a/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs b/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs index 78132004..0a717f0d 100644 --- a/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs +++ b/CompanyManagment.App.Contracts/RollCall/IRollCallApplication.cs @@ -139,7 +139,7 @@ namespace CompanyManagment.App.Contracts.RollCall public TimeSpan TotalWorkingTime { get; set; } public List Records { get; set; } public RollCallRecordStatus Status { get; set; } - + public long EmployeeId { get; set; } } public enum RollCallRecordStatus diff --git a/CompanyManagment.EFCore/Repository/RollCallRepository.cs b/CompanyManagment.EFCore/Repository/RollCallRepository.cs index 5923d1de..f055dc1c 100644 --- a/CompanyManagment.EFCore/Repository/RollCallRepository.cs +++ b/CompanyManagment.EFCore/Repository/RollCallRepository.cs @@ -17,7 +17,9 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Globalization; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using _0_Framework.Exceptions; namespace CompanyManagment.EFCore.Repository; @@ -777,6 +779,46 @@ public class RollCallRepository : RepositoryBase, IRollCallRepos } } + private TimeSpan CalculateExitTimeDifferencesTimeSpan(TimeSpan early, TimeSpan late) + { + if (early == TimeSpan.Zero && late == TimeSpan.Zero) + { + return TimeSpan.Zero; + } + + if (late != TimeSpan.Zero) + { + return late * 1; + } + + if (early != TimeSpan.Zero) + { + return early * -1; + } + + return TimeSpan.Zero; + } + + private TimeSpan CalculateEntryTimeDifferencesTimeSpan(TimeSpan early, TimeSpan late) + { + if (early == TimeSpan.Zero && late == TimeSpan.Zero) + { + return TimeSpan.Zero; + } + + if (late != TimeSpan.Zero) + { + return late * -1; + } + + if (early != TimeSpan.Zero) + { + return early*1; + } + + return TimeSpan.Zero; + } + //Without Paginate, With EndDate==null, without undefined, Ordered Descending By StartDate public List GetEmployeeRollCallsHistoryAllInDates(long workshopId, long employeeId, DateTime start, DateTime end) @@ -2202,7 +2244,7 @@ public class RollCallRepository : RepositoryBase, IRollCallRepos dates = dates .Where(x => x.Date == date.Date).ToList(); } - else if (!string.IsNullOrWhiteSpace(searchModel.StartDate) + else if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var start = searchModel.StartDate.ToGeorgianDateTime(); @@ -2243,8 +2285,95 @@ public class RollCallRepository : RepositoryBase, IRollCallRepos return res; } - public Task> GetCaseHistoryDetails(long workshopId, string titleId, RollCallCaseHistorySearchModel searchModel) + public async Task> GetCaseHistoryDetails(long workshopId, string titleId, + RollCallCaseHistorySearchModel searchModel) { - throw new NotImplementedException(); + var query = _context.RollCallEmployeesStatus + .Include(x => x.RollCallEmployee) + .Where(x => x.RollCallEmployee.WorkshopId == workshopId); + + //این برای این هست که اگر بر اساس پرسنل جستجو شده بود و در titleId اگر به فرمت YYYY_MM اومد به این معنی هست که باید بر طبق ماه فیلتر بشه نه به صورت روزانه + if (searchModel.EmployeeId is > 0) + { + //TODO: set correct regex. + // if (!Regex.IsMatch(titleId, "")) + // throw new BadRequestException("شناسه سر تیتر وارد شده نامعتبر است"); + var splitDate = titleId.Split("_"); + var year = Convert.ToInt32(splitDate.First()); + var month = Convert.ToInt32(splitDate.Last()); + + var startDateFa = $"{year:D4}/{month:D2}/01"; + var endDateFa = startDateFa.FindeEndOfMonth(); + + var startDate = startDateFa.ToGeorgianDateTime(); + var endDate = endDateFa.ToGeorgianDateTime(); + + query = query.Where(x => x.StartDate <= endDate && x.EndDate >= startDate + && x.RollCallEmployee.EmployeeId == searchModel.EmployeeId); + + var employeeIds =await query.Select(x => x.RollCallEmployee.EmployeeId).ToListAsync(); + + var rollCalls =await _context.RollCalls + .Where(x => x.WorkshopId == workshopId && employeeIds.Contains(x.EmployeeId) + && startDate <= x.ShiftDate && endDate >= x.ShiftDate).ToListAsync(); + + var leaves =await _context.LeaveList.Where(x => x.WorkshopId == workshopId + && employeeIds.Contains(x.EmployeeId) && + x.StartLeave >= endDate && x.EndLeave <= startDate).ToListAsync(); + var dateRange = (int)(endDate - startDate).TotalDays + 1; + + var dates = Enumerable.Range(0, dateRange).Select(x => startDate.AddDays(x)).ToList(); + + var employees = await _context.Employees.Where(x => employeeIds + .Contains(x.id)).ToListAsync(); + + var personnelCodes =await _context.PersonnelCodeSet + .Where(x => x.WorkshopId == workshopId + && employeeIds.Contains(x.EmployeeId)).ToListAsync(); + + var res = new PagedResult(); + foreach (var date in dates) + { + var rollCallInDate = rollCalls + .Where(x => x.ShiftDate.Date == date.Date).ToList(); + foreach (var employeeId in employeeIds) + { + var leave = leaves.FirstOrDefault(y => y.EmployeeId == employeeId); + var employeeRollCallsForDate = rollCallInDate + .Where(y => y.EmployeeId == employeeId).ToList(); + var employee = employees.FirstOrDefault(x => x.id == employeeId); + + var item = new RollCallCaseHistoryDetail() + { + EmployeeFullName = employee?.FullName ?? "", + EmployeeId = employee?.id ?? 0, + Records = employeeRollCallsForDate.OrderBy(r => r.StartDate).Select(y => + new RollCallCaseHistoryDetailRecord() + { + EndTime = y.EndDate!.Value.ToString("HH:mm"), + StartTime = y.StartDate!.Value.ToString("HH:mm"), + EntryTimeDifference = + CalculateEntryTimeDifferencesTimeSpan(y.EarlyEntryDuration, y.LateEntryDuration), + ExitTimeDifference = + CalculateExitTimeDifferencesTimeSpan(y.EarlyExitDuration, y.LateExitDuration) + }).ToList(), + Status = employeeRollCallsForDate.Any() ? RollCallRecordStatus.Worked + : leave != null ? RollCallRecordStatus.Leaved + : RollCallRecordStatus.Absent, + TotalWorkingTime = + new TimeSpan( + employeeRollCallsForDate.Sum(y => (y.EndDate!.Value - y.StartDate!.Value).Ticks)), + PersonnelCode = personnelCodes.FirstOrDefault(y => y.EmployeeId == employeeId)?.PersonnelCode + .ToString(), + }; + res.List.Add(item); + } + } + + return res; + + + } + return new PagedResult(); } } \ No newline at end of file diff --git a/ServiceHost/Areas/Client/Controllers/RollCall/CaseHistoryController.cs b/ServiceHost/Areas/Client/Controllers/RollCall/CaseHistoryController.cs index b6cbeccc..b5d18351 100644 --- a/ServiceHost/Areas/Client/Controllers/RollCall/CaseHistoryController.cs +++ b/ServiceHost/Areas/Client/Controllers/RollCall/CaseHistoryController.cs @@ -24,7 +24,7 @@ public class CaseHistoryController:ClientBaseController } [HttpGet("{titleId}")] - public async Task GetDetails(string titleId, RollCallCaseHistorySearchModel searchModel) + public async Task>> GetDetails(string titleId, RollCallCaseHistorySearchModel searchModel) { return await _rollCallApplication.GetCaseHistoryDetails(_workshopId, titleId, searchModel); } diff --git a/ServiceHost/Properties/launchSettings.json b/ServiceHost/Properties/launchSettings.json index d7381591..788962e4 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;https://192.168.0.117:5006", + "applicationUrl": "https://localhost:5004;http://localhost:5003;", "jsWebView2Debugging": false, "hotReloadEnabled": true },