using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using _0_Framework.Application; using _0_Framework.InfraStructure; using Company.Domain.LeftWorkAgg; using Company.Domain.PersonnelCodeAgg; using CompanyManagment.App.Contracts.LeftWork; using CompanyManagment.App.Contracts.PersonnleCode; using Microsoft.EntityFrameworkCore; namespace CompanyManagment.EFCore.Repository; public class LeftWorkRepository : RepositoryBase, ILeftWorkRepository { private readonly CompanyContext _context; public LeftWorkRepository(CompanyContext context) : base(context) { _context = context; } public EditLeftWork GetDetails(long id) { return _context.LeftWorkList.Select(x => new EditLeftWork() { Id = x.id, LeftWorkDate = x.LeftWorkDate.ToFarsi(), StartWorkDate = x.StartWorkDate.ToFarsi(), EmployeeFullName = x.EmployeeFullName, WorkshopName = x.WorkshopName, WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId }) .FirstOrDefault(x => x.Id == id); } public string StartWork(long employeeId, long workshopId, string leftWork) { var checkExist = _context.LeftWorkList.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); if (checkExist) { var LeftWorks = _context.LeftWorkList .Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId) .OrderByDescending(x => x.LeftWorkDate).ToList(); var lastLeft = LeftWorks.Select(x => x.LeftWorkDate).FirstOrDefault(); var leftWorkNew = leftWork.ToGeorgianDateTime(); var startWorkList = _context.Contracts .Where(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId) .Where(x => x.ContarctStart < leftWorkNew && x.ContarctStart > lastLeft) .OrderBy(x => x.ContarctStart).ToList(); var startWorkDate = startWorkList.Select(x => x.ContarctStart).FirstOrDefault(); var result = startWorkDate.ToFarsi(); return result; } else { var leftWorkNew = leftWork.ToGeorgianDateTime(); var startWorkList = _context.Contracts .Where(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId) .Where(x => x.ContarctStart < leftWorkNew) .OrderBy(x => x.ContarctStart).ToList(); var startWorkDate = startWorkList.Select(x => x.ContarctStart).FirstOrDefault(); var result = startWorkDate.ToFarsi(); return result; } } public List searchByWorkshopId(long workshopId) { var query = _context.LeftWorkList.Select(x => new LeftWorkViewModel() { Id = x.id, LeftWorkDate = x.LeftWorkDate.ToFarsi(), StartWorkDate = x.StartWorkDate.ToFarsi(), LeftWorkDateGr = x.LeftWorkDate, StartWorkDateGr = x.StartWorkDate, EmployeeFullName = x.EmployeeFullName, WorkshopName = x.WorkshopName, WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId, AddBonusesPay = x.AddBonusesPay, AddYearsPay = x.AddYearsPay, AddLeavePay = x.AddLeavePay, JobId = x.JobId, }).Where(x=>x.WorkshopId == workshopId); return query.ToList(); } public List search(LeftWorkSearchModel searchModel) { var query = _context.LeftWorkList.Select(x => new LeftWorkViewModel() { Id = x.id, LeftWorkDate = x.LeftWorkDate.ToFarsi(), StartWorkDate = x.StartWorkDate.ToFarsi(), LeftWorkDateGr = x.LeftWorkDate, StartWorkDateGr = x.StartWorkDate, EmployeeFullName = x.EmployeeFullName, WorkshopName = x.WorkshopName, WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId, AddBonusesPay = x.AddBonusesPay, AddYearsPay = x.AddYearsPay, AddLeavePay = x.AddLeavePay, JobId = x.JobId, JobName = _context.Jobs.FirstOrDefault(j => j.id == x.JobId).JobName }); if (searchModel.WorkshopId != 0 && searchModel.EmployeeId != 0) query = query.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId); if (searchModel.EmployeeId != 0 && searchModel.WorkshopId == 0) query = query.Where(x => x.EmployeeId == searchModel.EmployeeId); if (searchModel.WorkshopId != 0 && searchModel.EmployeeId == 0) query = query.Where(x => x.WorkshopId == searchModel.WorkshopId); return query.OrderByDescending(x => x.StartWorkDateGr).ToList(); } public LeftWorkViewModel CheckoutleftWorkCheck(DateTime contractStart, long workshopId, long employeeId) { return _context.LeftWorkList.Select(x => new LeftWorkViewModel() { Id = x.id, LeftWorkDate = x.LeftWorkDate.ToFarsi(), StartWorkDate = x.StartWorkDate.ToFarsi(), StartWorkDateGr = x.StartWorkDate, LeftWorkDateGr = x.LeftWorkDate, EmployeeFullName = x.EmployeeFullName, WorkshopName = x.WorkshopName, WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId, AddLeavePay = x.AddLeavePay, AddBonusesPay = x.AddBonusesPay, AddYearsPay = x.AddYearsPay, }) .FirstOrDefault(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId && contractStart >= x.StartWorkDateGr && contractStart <= x.LeftWorkDateGr); } public OperationResult RemoveLeftWork(long id) { var op = new OperationResult(); var item = _context.LeftWorkList.FirstOrDefault(x => x.id == id); if (item != null) { _context.LeftWorkList.Remove(item); _context.SaveChanges(); if (!_context.LeftWorkList.Any(x => x.WorkshopId == item.WorkshopId && x.EmployeeId == item.EmployeeId && item.id != x.id) && HasActiveRollCallStatus(item.WorkshopId, item.EmployeeId)) { RemoveEmployeeRollCallStatus(item.WorkshopId, item.EmployeeId); } } return op.Succcedded(); } public LeftWorkViewModel GetByDateAndWorkshopIdAndEmployeeId(long workshopId, long employeeId, DateTime dateTime) { var entity = _context.LeftWorkList.FirstOrDefault(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId && (x.StartWorkDate <= dateTime && x.LeftWorkDate >= dateTime)); if (entity == null) { return null; } return new() { EmployeeId = entity.EmployeeId, WorkshopId = entity.WorkshopId, LeftWorkDate = entity.LeftWorkDate.ToFarsi(), HasLeft = entity.HasLeft, LeftWorkDateGr = entity.LeftWorkDate, StartWorkDateGr = entity.StartWorkDate, Id = entity.id }; } public List GetAllEmployeeIdsInWorkshop(long workshopId) { var leftWorks = _context.LeftWorkList .Where(x => x.WorkshopId == workshopId) .GroupBy(x => x.EmployeeId).Select(x => x.Key).ToList(); var insuranceLeftWork = _context.LeftWorkInsuranceList .Where(x => x.WorkshopId == workshopId) .GroupBy(x => x.EmployeeId).Select(x => x.Key).ToList(); return leftWorks.Concat(insuranceLeftWork).Distinct().ToList(); } public List GetLeftPersonelByWorkshopId(List workshopIds) { return _context.LeftWorkList.Select(x => new LeftWorkViewModel() { Id = x.id, WorkshopId = x.WorkshopId, EmployeeId = x.EmployeeId, }).Where(x => workshopIds.Contains(x.WorkshopId)).ToList(); } public OperationResult RemoveAllLeftWork(long workshopId, long employeeId) { var op = new OperationResult(); using (var transaction = _context.Database.BeginTransaction()) { try { //bool hasContracts = _context.Contracts.Any(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId && x.IsActiveString == "true"); //if (hasContracts) //{ // return op.Failed("حذف ترک کار این پرسنل به دلیل داشتن قرارداد امکان پذیر نمی باشد."); //} bool hasCheckout = _context.CheckoutSet.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); if (hasCheckout) { return op.Failed("حذف ترک کار این پرسنل به دلیل داشتن فیش حقوقی امکان پذیر نمی باشد."); } var list = _context.LeftWorkList.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).ToList(); if (list != null && list.Count > 0) _context.LeftWorkList.RemoveRange(list); bool hasLeftWorkInsurance = _context.LeftWorkInsuranceList.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); if (!hasLeftWorkInsurance) { var personelCodeList = _context.PersonnelCodeSet.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).ToList(); _context.PersonnelCodeSet.RemoveRange(personelCodeList); } _context.SaveChanges(); transaction.Commit(); if (HasActiveRollCallStatus(workshopId, employeeId)) RemoveEmployeeRollCallStatus(workshopId, employeeId); if (hasLeftWorkInsurance) return op.Succcedded(1, "حذف با موفقیت انجام شد."); //+ "
" + "" + "کد پرسنلی این شخص به دلیل استفاده در ترک کار بیمه قابل حذف نمی باشد. " + ""); else return op.Succcedded(); } catch (Exception ex) { transaction.Rollback(); return op.Failed("حذف اطلاعات با خطا مواجه شد"); } } } public List SearchLeftWork(LeftWorkSearchModel searchModel) { //var query = _context.LeftWorkList.Select(x => new LeftWorkViewModel() //{ // Id = x.id, // LeftWorkDate = x.LeftWorkDate.ToFarsi(), // StartWorkDate = x.StartWorkDate.ToFarsi(), // LeftWorkDateGr = x.LeftWorkDate, // StartWorkDateGr = x.StartWorkDate, // EmployeeFullName = x.EmployeeFullName, // WorkshopName = x.WorkshopName, // WorkshopId = x.WorkshopId, // EmployeeId = x.EmployeeId, //}); var query = new List(); if (searchModel.WorkshopId != 0 && searchModel.EmployeeId != 0) query = _context.LeftWorkList.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId).ToList(); if (searchModel.EmployeeId != 0) query = _context.LeftWorkList.Where(x => x.EmployeeId == searchModel.EmployeeId).ToList(); var list = new List(); foreach (var item in query) { var jobObj = _context.Jobs.Where(x => x.id == item.JobId).FirstOrDefault(); list.Add(new LeftWorkViewModel() { Id = item.id, LeftWorkDate = item.LeftWorkDate.ToFarsi(), StartWorkDate = item.StartWorkDate.ToFarsi(), LeftWorkDateGr = item.LeftWorkDate, StartWorkDateGr = item.StartWorkDate, EmployeeFullName = item.EmployeeFullName, WorkshopName = item.WorkshopName, WorkshopId = item.WorkshopId, EmployeeId = item.EmployeeId, JobId = item.JobId, IncludeStatus = item.IncludeStatus, JobName = jobObj != null ? jobObj.JobName : string.Empty, JobCode = jobObj != null ? jobObj.JobCode : string.Empty, AddLeavePay = item.AddLeavePay, AddBonusesPay = item.AddBonusesPay, AddYearsPay = item.AddYearsPay }); } return list.OrderByDescending(x => x.StartWorkDateGr).ToList(); } public OperationResult CreateLeftWork(InformationLeftwork command) { var op = new OperationResult(); using (var transaction = _context.Database.BeginTransaction()) { try { #region PersonelCode if (command.PersonnelCodeList != null && command.PersonnelCodeList.Count > 0) { var personelcode = command.PersonnelCodeList.Where(x => x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId).FirstOrDefault(); if (personelcode != null) { if (_context.PersonnelCodeSet.Any(x => x.WorkshopId == command.WorkshopId && x.PersonnelCode == personelcode.PersonnelCode)) { op.Failed(" کد پرسنلی در کارگاه " + command.WorkshopName + " تکراری می باشد. "); } if (personelcode.PersonnelCode <= 0) { return op.Failed(" کد پرسنلی در کارگاه " + command.WorkshopName + "نامعتبر است."); } _context.PersonnelCodeSet.Add(new PersonnelCodeDomain(command.WorkshopId, command.EmployeeId, personelcode.PersonnelCode)); } } #endregion if (command.ItemLeftworkList != null && command.ItemLeftworkList.Count > 0) { bool checkRegister = false; var workshop = _context.Workshops .FirstOrDefault(x => x.id == command.WorkshopId); var workshopComputeOptions = workshop.ComputeOptions; var workshopBonusesOptions = workshop.BonusesOptions; foreach (var item2 in command.ItemLeftworkList) { var LeftWork = _context.LeftWorkList.Where(x => x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId).ToList(); _context.LeftWorkList.RemoveRange(LeftWork); DateTime left = item2.LeftWorkDate.ToGeorgianDateTime(); var start = item2.StartWorkDate.ToGeorgianDateTime(); var leftwork = new LeftWork(left, start, item2.WorkshopId, item2.EmployeeId, command.EmployeeFullName, item2.WorkshopName, item2.JobId, item2.IncludeStatus,item2.AddBonusesPay,item2.AddYearsPay,item2.AddLeavePay, workshopComputeOptions, workshopBonusesOptions); Create(leftwork); } } _context.SaveChanges(); transaction.Commit(); op.Succcedded(-1, " ثبت اطلاعات کارگاه " + command.WorkshopName + " با موفقیت انجام شد. "); } catch (Exception ex) { transaction.Rollback(); op.Failed(" ثبت اطلاعات کارگاه " + command.WorkshopName + " با خطا مواجه شد. "); } } return op; } public OperationResult CreateLeftWorkByLeftWorkGroups(string employeeFullName, long commandEmployeeId, List commandPersonnelCode, List leftWorkGroups) { var op = new OperationResult(); using (var transaction = _context.Database.BeginTransaction()) { try { foreach (var item in leftWorkGroups) { #region PersonelCode if (commandPersonnelCode != null && commandPersonnelCode.Count > 0) { var personelcode = commandPersonnelCode.Where(x => x.EmployeeId == commandEmployeeId && x.WorkshopId == item.WorkshopId).FirstOrDefault(); if (personelcode != null && personelcode.HasPersonelCode==false) { if (_context.PersonnelCodeSet.Any(x => x.WorkshopId == item.WorkshopId && x.PersonnelCode == personelcode.PersonnelCode)) { return op.Failed(" کد پرسنلی در کارگاه " + item.WorkshopName + " تکراری می باشد. "); } if (personelcode.PersonnelCode <= 0) { return op.Failed(" کد پرسنلی در کارگاه " + item.WorkshopName + "نامعتبر است."); } _context.PersonnelCodeSet.Add(new PersonnelCodeDomain(item.WorkshopId, commandEmployeeId, personelcode.PersonnelCode)); } } #endregion if (item.LeftWorkViewModels != null && item.LeftWorkViewModels.Count > 0) { bool checkRegister = false; var LeftWorkList = _context.LeftWorkList.Where(x => x.EmployeeId == commandEmployeeId && x.WorkshopId == item.WorkshopId).ToList(); var removeList = new List(); foreach (var removeItem in LeftWorkList) { if (!item.LeftWorkViewModels.Any(x => x.Id == removeItem.id)) removeList.Add(removeItem); } foreach (var item2 in item.LeftWorkViewModels) { DateTime left = new DateTime(); if (!string.IsNullOrWhiteSpace(item2.LeftWorkDate)) left = item2.LeftWorkDate.ToGeorgianDateTime(); var start = item2.StartWorkDate.ToGeorgianDateTime(); if (item2.Id == 0) { var workshop = _context.Workshops .FirstOrDefault(x => x.id == item2.WorkshopId); var workshopComputeOptions = workshop.ComputeOptions; var workshopBonusesOptions = workshop.BonusesOptions; var LeftWorkObj = new LeftWork(left, start, item2.WorkshopId, item2.EmployeeId, employeeFullName, item2.WorkshopName, item2.JobId, item2.IncludeStatus, item2.AddBonusesPay, item2.AddYearsPay, item2.AddLeavePay, workshopComputeOptions, workshopBonusesOptions); Create(LeftWorkObj); } else { var LeftWork = _context.LeftWorkList.Where(x => x.id == item2.Id)?.FirstOrDefault(); LeftWork.Edit(left, start, item2.WorkshopId, item2.EmployeeId, item2.JobId, item2.IncludeStatus, item2.AddBonusesPay, item2.AddYearsPay, item2.AddLeavePay); } } if (removeList != null && removeList.Count > 0) { _context.LeftWorkList.RemoveRange(removeList); } //foreach (var item2 in item.LeftWorkViewModels) //{ // var LeftWork = _context.LeftWorkList.Where(x => x.EmployeeId == commandEmployeeId && x.WorkshopId == item.WorkshopId).ToList(); // _context.LeftWorkList.RemoveRange(LeftWork); // DateTime left = item2.LeftWorkDate.ToGeorgianDateTime(); // var start = item2.StartWorkDate.ToGeorgianDateTime(); // var leftwork = new LeftWork(left, start, item2.WorkshopId, item2.EmployeeId, // employeeFullName, item2.WorkshopName, item2.JobId, item2.IncludeStatus, // item2.AddBonusesPay, item2.AddYearsPay, item2.AddLeavePay); // Create(leftwork); //} } } _context.SaveChanges(); transaction.Commit(); op.Succcedded(-1, " ثبت با موفقیت انجام شد. "); } catch (Exception ex) { transaction.Rollback(); op.Failed(" ثبت اطلاعات با خطا مواجه شد. "); } } return op; } public OperationResult CheckDeleteLeftWork(long workshopId, long employeeId, DateTime date, int type) { var op = new OperationResult(); bool hasContracts = false; var endDateSend = date.Date.AddDays(-1); if (type==1) hasContracts= _context.CheckoutSet.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && (x.ContractStart.Date >= endDateSend) && x.IsActiveString == "true"); else { endDateSend = date.Date; hasContracts = _context.CheckoutSet.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && (x.ContractStart.Date <= endDateSend && x.ContractEnd.Date >= endDateSend) && x.IsActiveString == "true"); } if (type == 1 && hasContracts) { return op.Failed("برای حذف تاریخ شروع به کار، ابتدا تصفیه حساب های مربوطه را حذف کنید"); } else if (type == 2 && hasContracts) { return op.Failed("در این تاریخ تصفیه حساب ثبت شده است.برای حذف تاریخ ترک کار، ابتدا تصفیه حساب های مربوطه را حذف کنید"); } return op.Succcedded(); } public OperationResult CheckEditLeftWork(long workshopId, long employeeId, DateTime date, int type) { var op = new OperationResult(); bool hasContracts = false; if (type == 1) { hasContracts= _context.CheckoutSet.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && (x.ContractStart.Date>= date) && x.IsActiveString == "true"); if(hasContracts) return op.Failed("لطفا ابتدا تصفیه حساب های آتی را حذف نمایید"); } else { var LeftDay = date.Date; var lastDay = date.Date.AddDays(-1); var LastDayChekout = _context.CheckoutSet.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.ContractStart.Date <= lastDay.Date && x.ContractEnd.Date >= lastDay.Date); var LeftDayCheckout = _context.CheckoutSet.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.ContractStart.Date <= LeftDay.Date && x.ContractEnd.Date >= LeftDay.Date); if (LeftDayCheckout != null) { string resultDay = LeftDayCheckout.Month + " " + LeftDayCheckout.Year; return op.Failed("در صورت تمایل به ثبت تاریخ، تصفیه حساب " + resultDay + " را حذف نمایید."); } if (LastDayChekout != null) { string resultDay = LastDayChekout.Month + " " + LastDayChekout.Year; return op.Failed("در صورت تمایل به ثبت تاریخ، تصفیه حساب " + resultDay + " را حذف نمایید."); } var LastDayContract = _context.CheckoutSet.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.IsActiveString == "true" && x.ContractEnd.Date > lastDay.Date); if(LastDayContract) return op.Failed("AfterContracts"); // var contract = _context.Contracts.Where(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId && x.IsActiveString == "true").OrderByDescending(x=>x.ContarctStart).FirstOrDefault(); //if (contract != null) //{ // if ((contract.ContarctStart.Date <= endDateSend.Date && contract.ContractEnd.Date >= endDateSend.Date)) // { // var checkout = _context.CheckoutSet.Where(x => // x.EmployeeId == employeeId && x.WorkshopId == workshopId && // (x.ContractStart.Date <= date.Date && x.ContractEnd.Date >= date.Date)).FirstOrDefault(); // hasContracts = false; // if (checkout != null) // { // string resultDay = checkout.Month + " " + checkout.Year; // return op.Failed("در صورت تمایل به ثبت تاریخ، تصفیه حساب "+resultDay+" را حذف نمایید."); // } // else // { // return op.Succcedded(); // } // } // else if ((contract.ContractEnd.Date == endDateSend)) // { // return op.Succcedded(); // } // else if ((contract.ContractEnd.Date > endDateSend.AddMonths(1))) // { // return op.Failed("AfterContracts"); // } // //else if ((contract.ContractEnd.Date < endDateSend.AddMonths(1))) // //{ // // return op.Failed("NoContracts"); // //} //} return op.Succcedded(); } return op.Succcedded(); } #region Pooya /// /// آیا کارمند در این بازه های زمانی مشغول به کار در کارگاه بوده است؟ /// /// لیستی از بازه ها به این صورت که آیتم اول یا چپ شروع بازه و آیتم دوم یا راست انتهای بازه می باشد public bool IsEmployeeWorkingInDates(long employeeId, long workshopId, List<(DateTime, DateTime)> dates) { var list = _context.LeftWorkList.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).Select(x => new { x.StartWorkDate, x.LeftWorkDate }); return dates.All(x => list.Any(y => y.StartWorkDate <= x.Item1 && y.LeftWorkDate >= x.Item2)); } public List GetByWorkshopIdInDates(long workshopId, DateTime startDateGr, DateTime endDateGr) { return _context.LeftWorkList.Where(x => x.WorkshopId == workshopId && x.LeftWorkDate.AddDays(-1) >= startDateGr && x.StartWorkDate <= endDateGr).Select(x => new LeftWorkViewModel() { StartWorkDateGr = x.StartWorkDate.Date, LeftWorkDateGr = x.LeftWorkDate.Date, EmployeeId = x.EmployeeId }).ToList(); } public LeftWorkViewModel GetByWorkshopIdEmployeeIdInDates(long workshopId, long employeeId, DateTime start, DateTime end) { var entity = _context.LeftWorkList.FirstOrDefault(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId && x.LeftWorkDate.AddDays(-1) >= start.Date && x.StartWorkDate <= end.Date); return new LeftWorkViewModel() { StartWorkDateGr = entity.StartWorkDate.Date, LeftWorkDateGr = entity.LeftWorkDate.Date, EmployeeId = entity.EmployeeId, HasLeft = entity.HasLeft }; } public LeftWork GetLastLeftWorkByEmployeeIdAndWorkshopId(long workshopId, long employeeId) { return _context.LeftWorkList.Where(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId) .OrderByDescending(x => x.StartWorkDate).FirstOrDefault(); } public async Task GetLastLeftWork(long employeeId, long workshopId) { var leftWork =await _context.LeftWorkList.Where(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId) .OrderByDescending(x => x.StartWorkDate).FirstOrDefaultAsync(); return leftWork; } private bool HasActiveRollCallStatus(long workshopId, long employeeId) { var now = DateTime.Today; var isDateUndefined = new DateTime(2121, 03, 21); return _context.RollCallEmployees.Include(x => x.EmployeesStatus).Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.EmployeesStatus.Any(y => (y.EndDate.Date > now && y.StartDate <= now) || (y.EndDate.Date == isDateUndefined))); } private void RemoveEmployeeRollCallStatus(long workshopId, long employeeId) { var entity = _context.RollCallEmployees.Include(x=>x.EmployeesStatus).FirstOrDefault(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId); if (entity == null) return; var hasLeftWork = _context.LeftWorkList.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); //اگر هیچ شرع بکار ترک کاری نداشت if (!hasLeftWork) { var now = DateTime.Today; entity.DeActive(); var rollCallEmployeeStatus = entity.EmployeesStatus.FirstOrDefault(x => x.EndDate.Date.IsDateUndefined() || x.EndDate.Date > now && x.StartDate <= now); if (rollCallEmployeeStatus != null) { var end = rollCallEmployeeStatus.StartDate.Date > DateTime.Now.Date ? rollCallEmployeeStatus.StartDate.Date : DateTime.Now.Date; rollCallEmployeeStatus.Deactivate(end); } _context.SaveChanges(); } //_context.RollCallEmployees.Remove(entity); } #endregion }