diff --git a/CompanyManagment.EFCore/Repository/RollCallMandatoryRepository.cs b/CompanyManagment.EFCore/Repository/RollCallMandatoryRepository.cs index 196b4245..ae9a3c0f 100644 --- a/CompanyManagment.EFCore/Repository/RollCallMandatoryRepository.cs +++ b/CompanyManagment.EFCore/Repository/RollCallMandatoryRepository.cs @@ -39,6 +39,7 @@ using PersianTools.Core; + namespace CompanyManagment.EFCore.Repository; public class RollCallMandatoryRepository : RepositoryBase, IRollCallMandatoryRepository @@ -64,12 +65,12 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll } - #region OfficialChckout - public ComputingViewModel MandatoryCompute(long employeeId, long workshopId, DateTime contractStart, - DateTime contractEnd, - CreateWorkingHoursTemp command, bool holidayWorking, bool isStaticCheckout, bool rotatingShiftCompute, bool totalLeaveCompute) - { - #region Entities + #region OfficialChckout + public ComputingViewModel MandatoryCompute(long employeeId, long workshopId, DateTime contractStart, + DateTime contractEnd, + CreateWorkingHoursTemp command, bool holidayWorking, bool isStaticCheckout, bool rotatingShiftCompute, bool totalLeaveCompute) + { + #region Entities string SumWorkeTime = string.Empty; var weeklyTime = new TimeSpan(); @@ -144,7 +145,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll groupedRollCall = rollCallResult.GroupBy(x => x.ShiftDate.Date).Select(x => { - + DateTime friday = new DateTime(); @@ -197,19 +198,19 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll BreakTimeSpan = x.BreakTimeSpan }).ToList(); - groupedRollCall = rollCallResult.GroupBy(x => x.CreationDate.Date).Select(x => - { - TimeSpan breakTime; - if (contractStart > endOfFarvardin) - { - breakTime = CalculateBreakTime( - x.First().BreakTimeSpan, - new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks))); - } - else - { - breakTime = CalculateBreakTime(breakTimeEntity, new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks))); - } + groupedRollCall = rollCallResult.GroupBy(x => x.CreationDate.Date).Select(x => + { + TimeSpan breakTime; + if (contractStart > endOfFarvardin) + { + breakTime = CalculateBreakTime( + x.First().BreakTimeSpan, + new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks))); + } + else + { + breakTime = CalculateBreakTime(breakTimeEntity, new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks))); + } DateTime friday = new DateTime(); @@ -220,7 +221,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll s.StartDate != null && s.EndDate != null && (s.StartDate.Value.DayOfWeek == DayOfWeek.Friday)) .StartDate.Value; - + } if (x.Any(s => @@ -233,15 +234,15 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll } return new GroupedRollCalls() - { - CreationDate = x.Key, - ShiftList = x.Select(s => new ShiftList() { Start = s.StartDate!.Value, End = s.EndDate!.Value, EndWithOutResTime = s.EndDate!.Value }) - .ToList(), - //HasFriday = x.Any(s => - // s.StartDate != null && s.EndDate != null && (s.StartDate.Value.DayOfWeek == DayOfWeek.Friday || - // s.EndDate.Value!.DayOfWeek == DayOfWeek.Friday)), + { + CreationDate = x.Key, + ShiftList = x.Select(s => new ShiftList() { Start = s.StartDate!.Value, End = s.EndDate!.Value, EndWithOutResTime = s.EndDate!.Value }) + .ToList(), + //HasFriday = x.Any(s => + // s.StartDate != null && s.EndDate != null && (s.StartDate.Value.DayOfWeek == DayOfWeek.Friday || + // s.EndDate.Value!.DayOfWeek == DayOfWeek.Friday)), - Fridays = friday, + Fridays = friday, SumOneDaySpan = new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks)) - breakTime, @@ -265,7 +266,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll TimeSpan sumSpans = new TimeSpan(groupedRollCall.Sum(x => x.SumOneDaySpan.Ticks)); - TimeSpan sumSpansWhitOutleaves = new TimeSpan(groupedRollCall.Sum(x => x.SumOneDaySpan.Ticks)); + TimeSpan sumSpansWhitOutleaves = new TimeSpan(groupedRollCall.Sum(x => x.SumOneDaySpan.Ticks)); //بدست آوردن مرخصی ساعتی @@ -347,7 +348,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll totalLeave = totalLeave.Add(hoursesleaveTimeSpans); //totalLeaveOnThisCheckout = totalLeave; var totalLeaveDouble = (totalLeave.TotalMinutes) / 60; - + //اگر مدت مرخصی از مجاز بیشتر بود و مدل محاسبه کامل نبود if (totalLeaveDouble > starndardHoursesPerTotalDays && !totalLeaveCompute) { @@ -902,7 +903,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll //مرخصی استعلاجی TotalSickLeave = new TimeSpan(sickLeaveTimeSpans.Sum(x => x.Ticks)), - + //ساعت موظفی پرسنل در این ماه EmployeeMandatoryHours = mandatoryHoursTimeSpan, #endregion @@ -1802,6 +1803,68 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll var startContract = new PersianDateTime(syear, smonth, sday, start.Hour, start.Minute); var endContract = new PersianDateTime(eyear, emonth, eday, 23, 59); var countLeves = leaveSearchResult.Where(x => x.PaidLeaveType == "روزانه").ToList(); + //int totalLeaveDays = countLeves.Sum(x => int.Parse(x.LeaveHourses)); + List affectedLeaves = new List(); + + DateTime contractStart = ($"{startContract}").ToGeorgianDateTime(); + DateTime contractEnd = ($"{endContract}").ToGeorgianDateTime(); + int leavingDayCout = 0; + List thisCheckoutLeaves = new List(); + + + if (leaveSearchResult.Count > 0) + { + foreach (var leave in leaveSearchResult) + { + //مرخصی های مابین + if (leave.StartLeaveGr >= contractStart && leave.EndLeaveGr <= contractEnd) + { + var modifyleave = new LeaveViewModel() + { + Id = leave.Id, + StartLeaveGr = leave.StartLeaveGr, + EndLeaveGr = leave.EndLeaveGr + }; + thisCheckoutLeaves.Add(leave); + } + + // مرخصی که شروعش قبل از شروع تصفیه حساب است + if (leave.StartLeaveGr < contractStart && leave.EndLeaveGr <= contractEnd) + { + var modifyleave = new LeaveViewModel() + { + Id = leave.Id, + StartLeaveGr = contractStart, + EndLeaveGr = leave.EndLeaveGr + }; + thisCheckoutLeaves.Add(modifyleave); + } + // مرخصی که پایانش بعد از پایان تصفیه حساب است + if (leave.EndLeaveGr > contractEnd && leave.StartLeaveGr >= contractStart) + { + var modifyleave = new LeaveViewModel() + { + Id = leave.Id, + StartLeaveGr = leave.StartLeaveGr, + EndLeaveGr = contractEnd, + }; + thisCheckoutLeaves.Add(modifyleave); + } + + //مرخصی استثنا که تمام بازه فیش را پوشش میدهد + if (leave.StartLeaveGr < contractStart && leave.EndLeaveGr > contractEnd) + { + var modifyleave = new LeaveViewModel() + { + Id = leave.Id, + StartLeaveGr = leave.StartLeaveGr, + EndLeaveGr = contractEnd, + }; + thisCheckoutLeaves.Add(modifyleave); + } + } + } + for (var da = startDateAndTime; da <= endContract; da = da.AddHours(addHours)) { @@ -1826,7 +1889,7 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll Console.WriteLine($"{currentDateFa} - {currentDateGr.Date} - start : {startComplex} end : {endComplex}"); - var hasLeave = countLeves.Any(x => x.StartLeaveGr <= startComplex && x.EndLeaveGr >= endComplex); + var hasLeave = thisCheckoutLeaves.Any(x => startComplex.Date <= x.EndLeaveGr.Date && endComplex.Date >= x.StartLeaveGr.Date); if (!hasLeave) { rollCallList.Add(new RollCallViewModel() @@ -1838,7 +1901,18 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll ShiftEndWithoutRest = endComplex }); } - + else + { + var affected = thisCheckoutLeaves.FirstOrDefault(x => startComplex.Date <= x.EndLeaveGr.Date && endComplex.Date >= x.StartLeaveGr.Date); + var modifyleave = new LeaveViewModel() + { + Id = affected.Id, + StartLeaveGr = affected.StartLeaveGr, + EndLeaveGr = affected.EndLeaveGr + }; + affectedLeaves.Add(modifyleave); + } + var endCal = end - start; @@ -1847,16 +1921,22 @@ public class RollCallMandatoryRepository : RepositoryBase, IRoll } - - //if (countLeves.Count > 0) - //{ - // int totalDays = countLeves.Sum(x => int.Parse(x.LeaveHourses)); - // int countRollCall = rollCallList.Count(); - // int takRollCall = totalDays < countRollCall ? (countRollCall - totalDays) : 0; - // rollCallList = rollCallList.Take(takRollCall).ToList(); - //} + affectedLeaves = affectedLeaves.DistinctBy(x => x.Id).ToList(); + + //مرخصی هایی که تاریخ آن ها بین رکورد های حضور پرسنل بوده و اعمال نشده + var exceptionLeaves = thisCheckoutLeaves + .Where(t => !affectedLeaves.Select(a => a.Id).Contains(t.Id)) + .ToList(); + + //حذف رکورد های حضور به تعداد مرخصی های اعمال نشده + if (exceptionLeaves.Count > 0) + { + int countRollCall = rollCallList.Count(); + int takRollCall = exceptionLeaves.Count < countRollCall ? (countRollCall - exceptionLeaves.Count) : 0; + rollCallList = rollCallList.Take(takRollCall).ToList(); + } }