rollcal history fixed

This commit is contained in:
SamSys
2024-11-13 16:37:39 +03:30
parent 333a9fc576
commit 3f89c031c5
5 changed files with 180 additions and 192 deletions

View File

@@ -5,7 +5,7 @@ namespace Company.Domain.RollCallAgg
{
public class RollCall : EntityBase
{
public RollCall(long workshopId, long employeeId, string employeeFullName, DateTime? startDate, DateTime? endDate, int year, int month)
public RollCall(long workshopId, long employeeId, string employeeFullName, DateTime? startDate, DateTime? endDate, int year, int month, RollCallModifyType rollCallModifyType= RollCallModifyType.None)
{
WorkshopId = workshopId;
EmployeeId = employeeId;
@@ -14,7 +14,7 @@ namespace Company.Domain.RollCallAgg
EndDate = endDate;
Year = year;
Month = month;
RollCallModifyType = RollCallModifyType.None;
RollCallModifyType = rollCallModifyType;
}
public long WorkshopId { get; private set; }
@@ -46,9 +46,9 @@ namespace Company.Domain.RollCallAgg
public enum RollCallModifyType
{
None,
CutByBgService,
EditByEmployer,
AddedManually
}
None,
CutByBgService,
EditByEmployer,
Undefined
}
}

View File

@@ -263,7 +263,7 @@ public class RollCallApplication : IRollCallApplication
var name = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(command.EmployeeId, command.WorkshopId).EmployeeFullName;
var rollCallsAsEntityModels = result.Select(x => new RollCall(command.WorkshopId, command.EmployeeId, name, x.Start, x.End,
Convert.ToInt32(x.Start.ToFarsi().Substring(0, 4)), Convert.ToInt32(x.Start.ToFarsi().Substring(5, 2)))).ToList();
Convert.ToInt32(x.Start.ToFarsi().Substring(0, 4)), Convert.ToInt32(x.Start.ToFarsi().Substring(5, 2)), RollCallModifyType.EditByEmployer)).ToList();
_rollCallRepository.RemoveEmployeeRollCallsInDate(command.WorkshopId, command.EmployeeId, date);

View File

@@ -607,6 +607,7 @@ public class EmployeeRepository : RepositoryBase<long, Employee>, IEmployeeRepos
DateOfBirth = (DateTime)x.DateOfBirth == initial ? "" : x.DateOfBirth.ToFarsi(),
InsuranceCode = x.InsuranceCode,
IsActiveString = x.IsActiveString,
IsActive = x.IsActive,
EmployeeFullName = x.FName.Trim() + " " + x.LName.Trim(),
}).ToList();
@@ -622,7 +623,9 @@ public class EmployeeRepository : RepositoryBase<long, Employee>, IEmployeeRepos
if (searchModel.Id > 0)
{
hasSearch = true;
query = query.Where(x => x.Id == searchModel.Id).ToList();
var serchedById = query.FirstOrDefault(x => x.Id == searchModel.Id);
query = query.Where(x => x.EmployeeFullName.Contains(serchedById.EmployeeFullName)).ToList();
}
else if (!string.IsNullOrEmpty(searchModel.EmployeeName))
{
@@ -694,52 +697,25 @@ public class EmployeeRepository : RepositoryBase<long, Employee>, IEmployeeRepos
hasSearch = true;
query = query.Where(x => x.City == searchModel.City).ToList();
}
if (searchModel.IsActiveString == null)
switch (searchModel.IsActiveString)
{
query = query.Where(x => x.IsActiveString == "true").ToList();
}
if (searchModel.IsActiveString == "false")
{
hasSearch = true;
query = query.Where(x => x.IsActiveString == "false").ToList();
}
else if (searchModel.IsActiveString == "both")
{
hasSearch = true;
query = query.Where(x => x.IsActiveString == "false" || x.IsActiveString == "true").ToList();
}
//foreach (var item in query)
//{
// var workshopList = new List<WorkshopViewModel>();
// if (item.LeftWorkInsuranceList != null && item.LeftWorkInsuranceList.Count > 0)
// {
// foreach (var leftWorkInsurancet in item.LeftWorkInsuranceList)
// {
// if (!workshopList.Any(x => x.Id== leftWorkInsurancet.WorkshopId))
// {
// var workshop = new WorkshopViewModel();
// workshop.Id = leftWorkInsurancet.Id;
// workshop.WorkshopName = leftWorkInsurancet.WorkshopName;
// workshopList.Add(workshop);
// }
// }
// }
// if (item.LeftWorkList != null && item.LeftWorkList.Count > 0)
// {
// foreach (var leftWork in item.LeftWorkList)
// {
// if (!workshopList.Any(x => x.Id == leftWork.WorkshopId))
// {
// var workshop = new WorkshopViewModel();
// workshop.Id = leftWork.Id;
// workshop.WorkshopName = leftWork.WorkshopName;
// workshopList.Add(workshop);
// }
// }
// }
// item.WorkshopList=workshopList;
//}
case null:
case "":
query = query.Where(x => x.IsActive == true).ToList();
break;
case "false":
query = query.Where(x => x.IsActive == false).ToList();
hasSearch = true;
break;
case "both":
query = query.Where(x => x.IsActive == true || x.IsActive == false).ToList();
hasSearch = true;
break;
}
if (hasSearch)
{
return query.OrderByDescending(x => x.Id).ToList();

View File

@@ -30,25 +30,36 @@ namespace CompanyManagment.EFCore.Repository
}).ToList();
}
/// <summary>
/// برای تغییر بازه فعالبت هنگام ترک کار
/// </summary>
public void AdjustRollCallStatusEndDates(List<AdjustRollCallEmployeesWithEmployeeLeftWork> command)
{
//status ids
var statusIds = command.Select(x => x.RollCallStatusId);
var statusIds = command.Select(x => x.RollCallStatusId);
var finalList = _context.RollCallEmployeesStatus.Where(x => statusIds.Contains(x.id)).AsEnumerable();
finalList.Where(x => command.Any(y => !y.LeaveDate.IsDateUndefined() && y.LeaveDate < x.EndDate)).ToList().ForEach(
z =>
{
var cmd = command.FirstOrDefault(y => y.RollCallStatusId == z.id);
//fetch by ids
var finalList = _context.RollCallEmployeesStatus.Where(x => statusIds.Contains(x.id)).AsEnumerable();
if (cmd!.LeaveDate >= z.StartDate)
z.Edit(z.StartDate, cmd!.LeaveDate);
}
);
_context.SaveChanges();
}
//get the statuses which have higher end date than employee's leftwork
finalList.Where(x => command.Any(y => !y.LeaveDate.IsDateUndefined() && y.LeaveDate.AddDays(-1).Date < x.EndDate.Date)).ToList().ForEach(
z =>
{
var cmd = command.FirstOrDefault(y => y.RollCallStatusId == z.id);
public RollCallEmployeeStatus GetByRollCallEmployeeIdAndDate(long rollCallEmployeeId, DateTime date)
if (cmd!.LeaveDate.Date.AddDays(-1) > z.StartDate.Date)
{
z.Edit(z.StartDate, cmd!.LeaveDate.Date.AddDays(-1));
}
}
);
_context.SaveChanges();
}
public RollCallEmployeeStatus GetByRollCallEmployeeIdAndDate(long rollCallEmployeeId, DateTime date)
{
return _context.RollCallEmployeesStatus.FirstOrDefault(x => x.RollCallEmployeeId == rollCallEmployeeId && x.StartDate.Date <= date.Date && x.EndDate.Date >= date.Date);

View File

@@ -112,157 +112,158 @@ public class RollCallRepository : RepositoryBase<long, RollCall>, IRollCallRepos
}
//جستجوی سوابق حضور غیاب بر اساس کارمند
public EmployeeRollCallsByMonthViewModel GetEmployeeRollCallsHistory(long employeeId, long workshopId,
DateTime? startDateTime, DateTime? endDateTime, DateTime? exactDateTime, DateTime? dateIndex)
{
//جستجوی سوابق حضور غیاب بر اساس کارمند
public EmployeeRollCallsByMonthViewModel GetEmployeeRollCallsHistory(long employeeId, long workshopId,
DateTime? startDateTime, DateTime? endDateTime, DateTime? exactDateTime, DateTime? dateIndex)
{
//get RollCallEmployee and RollCallEmployeeStatus for that employee in that workshop
var employeeRollCallStatuses = _context.RollCallEmployees.Include(x => x.EmployeesStatus)
.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId);
//get RollCallEmployee and RollCallEmployeeStatus for that employee in that workshop
var employeeRollCallStatuses = _context.RollCallEmployees.Include(x => x.EmployeesStatus)
.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId);
//if none was found return empty
if (employeeRollCallStatuses == null)
return new();
//if none was found return empty
if (employeeRollCallStatuses == null)
return new();
//this list will have all the months which employee was active in
var activeMonths = new List<PersianDateTime>();
//this list will have all the months which employee was active in
var activeMonths = new List<PersianDateTime>();
//filling the list
foreach (var status in employeeRollCallStatuses.EmployeesStatus)
{
var persianEndDate = new PersianDateTime(status.EndDate.Date);
var persianStartDate = new PersianDateTime(status.StartDate.Date);
//filling the list
foreach (var status in employeeRollCallStatuses.EmployeesStatus)
{
var persianEndDate = new PersianDateTime(status.EndDate.Date);
var persianStartDate = new PersianDateTime(status.StartDate.Date);
var persianStartFirstDayOfMonth = new PersianDateTime(persianStartDate.Year, persianStartDate.Month, 1);
var persianStartFirstDayOfMonth = new PersianDateTime(persianStartDate.Year, persianStartDate.Month, 1);
for (PersianDateTime start = persianStartFirstDayOfMonth; start <= persianEndDate && start < PersianDateTime.Today.Date; start = start.AddMonths(1))
{
activeMonths.Add(start);
}
}
//might have duplicated records, this is the reason for distinct
var activeMonthsList = activeMonths.OrderByDescending(x => x.Date).Distinct().ToList();
for (PersianDateTime start = persianStartFirstDayOfMonth; start <= persianEndDate && start < PersianDateTime.Today.Date; start = start.AddMonths(1))
{
activeMonths.Add(start);
}
}
//might have duplicated records, this is the reason for distinct
var activeMonthsList = activeMonths.OrderByDescending(x => x.Date).Distinct().ToList();
PersianDateTime startSearch = new();
PersianDateTime endSearch = new();
PersianDateTime startSearch = new();
PersianDateTime endSearch = new();
//if search has these parameters below
if (startDateTime.HasValue && endDateTime.HasValue)
{
//change them to persian date time and save them
startSearch = new PersianDateTime(startDateTime.Value);
endSearch = new PersianDateTime(endDateTime.Value);
//if search has these parameters below
if (startDateTime.HasValue && endDateTime.HasValue)
{
//change them to persian date time and save them
startSearch = new PersianDateTime(startDateTime.Value);
endSearch = new PersianDateTime(endDateTime.Value);
//get the months that include these dates
activeMonthsList = activeMonthsList.Where(x => x.Year >= startSearch.Year && x.Month >= startSearch.Month && x.Year <= endSearch.Year && x.Month <= endSearch.Month).ToList();
}
//if exact datetime is given
if (exactDateTime.HasValue)
{
//start and end will be the same date
startSearch = new PersianDateTime(exactDateTime.Value);
endSearch = startSearch;
//get the
activeMonthsList = activeMonthsList.Where(x => x.Year >= startSearch.Year && x.Month >= startSearch.Month && x.Year <= endSearch.Year && x.Month <= endSearch.Month).ToList();
}
//get the months that include these dates
activeMonthsList = activeMonthsList.Where(x => x.Year >= startSearch.Year && x.Month >= startSearch.Month && x.Year <= endSearch.Year && x.Month <= endSearch.Month).ToList();
}
//if exact datetime is given
if (exactDateTime.HasValue)
{
//start and end will be the same date
startSearch = new PersianDateTime(exactDateTime.Value);
endSearch = startSearch;
//get the
activeMonthsList = activeMonthsList.Where(x => x.Year >= startSearch.Year && x.Month >= startSearch.Month && x.Year <= endSearch.Year && x.Month <= endSearch.Month).ToList();
}
if (dateIndex != null)
{
var persianDateIndex = new PersianDateTime(dateIndex.Value);
var dateIndexFirstOfMonth = persianDateIndex.AddDays(-(persianDateIndex.Day - 1));
activeMonthsList = activeMonthsList.Where(x => dateIndexFirstOfMonth >= x).ToList();
}
if (dateIndex != null)
{
var persianDateIndex = new PersianDateTime(dateIndex.Value);
var dateIndexFirstOfMonth = persianDateIndex.AddDays(-(persianDateIndex.Day - 1));
activeMonthsList = activeMonthsList.Where(x => dateIndexFirstOfMonth >= x).ToList();
}
if (!activeMonthsList.Any()) return new();
if (!activeMonthsList.Any()) return new();
//get the last month which user was active in
PersianDateTime selectedMonthPersian = activeMonthsList.First();
//get the last month which user was active in
PersianDateTime selectedMonthPersian = activeMonthsList.First();
DateTime selectedMonthFirstDay = selectedMonthPersian;
DateTime nextMonthFirstDay = selectedMonthPersian.AddMonths(1);
DateTime selectedMonthFirstDay = selectedMonthPersian;
DateTime nextMonthFirstDay = selectedMonthPersian.AddMonths(1);
var statusesOfMonth = employeeRollCallStatuses.EmployeesStatus.Where(x => x.EndDate >= selectedMonthFirstDay &&
x.StartDate < nextMonthFirstDay);
var statusesOfMonth = employeeRollCallStatuses.EmployeesStatus.Where(x => x.EndDate >= selectedMonthFirstDay &&
x.StartDate < nextMonthFirstDay);
var leavesQuery =
_context.LeaveList.Where(x => (x.LeaveType == "استعلاجی" || (x.LeaveType == "استحقاقی" && x.PaidLeaveType == "روزانه")) &&
x.IsAccepted &&
x.EndLeave >= selectedMonthFirstDay && x.StartLeave < nextMonthFirstDay && x.WorkshopId == workshopId);
var leavesQuery =
_context.LeaveList.Where(x => (x.LeaveType == "استعلاجی" || (x.LeaveType == "استحقاقی" && x.PaidLeaveType == "روزانه")) &&
x.IsAccepted &&
x.EndLeave >= selectedMonthFirstDay && x.StartLeave < nextMonthFirstDay && x.WorkshopId == workshopId);
var rollCalls = _context.RollCalls.Where(x =>
!leavesQuery.Any(y =>
y.StartLeave.Date <= x.StartDate.Value.Date && y.EndLeave.Date >= x.StartDate.Value.Date) &&
x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.StartDate != null &&
x.StartDate >= selectedMonthFirstDay && x.StartDate < nextMonthFirstDay);
var rollCalls = _context.RollCalls.Where(x =>
!leavesQuery.Any(y =>
y.StartLeave.Date <= x.StartDate.Value.Date && y.EndLeave.Date >= x.StartDate.Value.Date) &&
x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.StartDate != null &&
x.StartDate >= selectedMonthFirstDay && x.StartDate < nextMonthFirstDay && x.RollCallModifyType != RollCallModifyType.Undefined);
var personnelCode =
_context.PersonnelCodeSet.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId)?.PersonnelCode;
var personnelCode =
_context.PersonnelCodeSet.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId)?.PersonnelCode;
var employeeName = _context.RollCallEmployees.Where(x => x.WorkshopId == workshopId).Select(x => new { x.EmployeeId, x.EmployeeFullName })
.FirstOrDefault(x => x.EmployeeId == employeeId);
var employeeName = _context.RollCallEmployees.Where(x => x.WorkshopId == workshopId).Select(x => new { x.EmployeeId, x.EmployeeFullName })
.FirstOrDefault(x => x.EmployeeId == employeeId);
var rollCallsList = rollCalls.ToList();
var leavesList = leavesQuery.ToList();
var rollCallsList = rollCalls.ToList();
var leavesList = leavesQuery.ToList();
int dateRange = (int)(nextMonthFirstDay - selectedMonthFirstDay).TotalDays;
int dateRange = (int)(nextMonthFirstDay - selectedMonthFirstDay).TotalDays;
var todayDate = DateTime.Now.Date;
//all the dates from start to end, to be compared with present days to get absent dates
var completeDaysList = Enumerable.Range(0, dateRange).Select(offset => selectedMonthFirstDay.AddDays(offset).Date).Where(x => x.Date < todayDate);
var todayDate = DateTime.Now.Date;
//all the dates from start to end, to be compared with present days to get absent dates
var completeDaysList = Enumerable.Range(0, dateRange).Select(offset => selectedMonthFirstDay.AddDays(offset).Date).Where(x => x.Date < todayDate);
//if user search range is within a month for example, we dont want 30/31/29 days for month, we want it to be the size of the search range
//user input = 2024/04/15~2024/04/21, output days count is 21-15+1=6 days from 15th to 21st
if (exactDateTime.HasValue || (startDateTime.HasValue && endDateTime.HasValue))
completeDaysList = completeDaysList.Where(x => x.Date >= startSearch.Date && x.Date <= endSearch.Date);
//if user search range is within a month for example, we dont want 30/31/29 days for month, we want it to be the size of the search range
//user input = 2024/04/15~2024/04/21, output days count is 21-15+1=6 days from 15th to 21st
if (exactDateTime.HasValue || (startDateTime.HasValue && endDateTime.HasValue))
completeDaysList = completeDaysList.Where(x => x.Date >= startSearch.Date && x.Date <= endSearch.Date);
var result = completeDaysList.Where(x => !rollCallsList.Any(y => y.StartDate.Value.Date == x.Date && y.EndDate == null) &&
statusesOfMonth.Any(y => x >= y.StartDate.Date && x <= y.EndDate.Date))
.Select(x =>
{
var leave = leavesList.FirstOrDefault(y => y.EndLeave >= x.Date.Date && y.StartLeave <= x.Date.Date);
return new RollCallViewModel()
{
DateGr = x.Date.Date,
DateFa = x.Date.Date.ToFarsi(),
RollCallTimesList = rollCallsList.Where(y => x.Date.Date == y.StartDate!.Value.Date).Select(y =>
new RollCallTimeViewModel()
{
StartDate = y.StartDate.Value.ToString("HH:mm"),
EndDate = y.EndDate!.Value.ToString("HH:mm")
}),
TotalWorkingHoursSpan = new TimeSpan(rollCallsList.Where(y => x.Date.Date == y.StartDate!.Value.Date)
.Sum(y => (y.EndDate!.Value - y.StartDate.Value).Ticks)),
Reason = leave?.LeaveType ?? "",
HasLeave = (leave?.PaidLeaveType == "روزانه" || leave?.LeaveType == "استعلاجی") ? true : false
};
});
result = result.Select(x => new RollCallViewModel()
{
EmployeeFullName = employeeName.EmployeeFullName,
EmployeeId = employeeId,
PersonnelCode = personnelCode.ToString(),
DateGr = x.DateGr,
DateFa = x.DateFa,
DayOfWeekFa = x.DateGr.DayOfWeek.DayOfWeeKToPersian(),
IsHoliday = _holidayItemApplication.IsHoliday(x.DateGr),
IsAbsent = !x.RollCallTimesList.Any(),
RollCallTimesList = x.RollCallTimesList,
TotalWorkingHours = $"{(int)x.TotalWorkingHoursSpan.TotalHours}:{x.TotalWorkingHoursSpan.Minutes:00}",
}).ToList();
return new EmployeeRollCallsByMonthViewModel()
{
PersianMonthName = selectedMonthPersian.ToString("MMMM"),
PersianYear = selectedMonthPersian.ToString("yyyy"),
DateGr = selectedMonthFirstDay,
RollCalls = result,
DateIndex = activeMonthsList.Count > 1 ? activeMonthsList.Skip(1).First().ToString("yyyy/MM/dd") : null
var result = completeDaysList.Where(x => !rollCallsList.Any(y => y.StartDate.Value.Date == x.Date && y.EndDate == null) &&
statusesOfMonth.Any(y => x >= y.StartDate.Date && x <= y.EndDate.Date))
.Select(x =>
{
var leave = leavesList.FirstOrDefault(y => y.EndLeave >= x.Date.Date && y.StartLeave <= x.Date.Date);
return new RollCallViewModel()
{
DateGr = x.Date.Date,
DateFa = x.Date.Date.ToFarsi(),
RollCallTimesList = rollCallsList.Where(y => x.Date.Date == y.StartDate!.Value.Date).Select(y =>
new RollCallTimeViewModel()
{
StartDate = y.StartDate.Value.ToString("HH:mm"),
EndDate = y.EndDate!.Value.ToString("HH:mm")
}),
TotalWorkingHoursSpan = new TimeSpan(rollCallsList.Where(y => x.Date.Date == y.StartDate!.Value.Date)
.Sum(y => (y.EndDate!.Value - y.StartDate.Value).Ticks)),
Reason = leave?.LeaveType ?? "",
HasLeave = (leave?.PaidLeaveType == "روزانه" || leave?.LeaveType == "استعلاجی") ? true : false
};
});
result = result.Select(x => new RollCallViewModel()
{
EmployeeFullName = employeeName.EmployeeFullName,
EmployeeId = employeeId,
PersonnelCode = personnelCode.ToString(),
DateGr = x.DateGr,
DateFa = x.DateFa,
DayOfWeekFa = x.DateGr.DayOfWeek.DayOfWeeKToPersian(),
IsHoliday = _holidayItemApplication.IsHoliday(x.DateGr),
IsAbsent = !x.RollCallTimesList.Any(),
RollCallTimesList = x.RollCallTimesList,
TotalWorkingHours = $"{(int)x.TotalWorkingHoursSpan.TotalHours}:{x.TotalWorkingHoursSpan.Minutes.ToString("00")}",
}).ToList();
return new EmployeeRollCallsByMonthViewModel()
{
PersianMonthName = selectedMonthPersian.ToString("MMMM"),
PersianYear = selectedMonthPersian.ToString("yyyy"),
DateGr = selectedMonthFirstDay,
RollCalls = result.OrderByDescending(x => x.DateGr),
DateIndex = activeMonthsList.Count > 1 ? activeMonthsList.Skip(1).First().ToString("yyyy/MM/dd") : null
};
}
public void AddRange(List<RollCall> rollCalls)
};
}
public void AddRange(List<RollCall> rollCalls)
{
_context.RollCalls.AddRange(rollCalls);
}