diff --git a/Company.Domain/RollCallEmployeeStatusAgg/IRollCallEmployeeStatusRepository.cs b/Company.Domain/RollCallEmployeeStatusAgg/IRollCallEmployeeStatusRepository.cs index ed2db0ec..2009aeec 100644 --- a/Company.Domain/RollCallEmployeeStatusAgg/IRollCallEmployeeStatusRepository.cs +++ b/Company.Domain/RollCallEmployeeStatusAgg/IRollCallEmployeeStatusRepository.cs @@ -26,5 +26,6 @@ namespace Company.Domain.RollCallEmployeeStatusAgg List GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr); List GetByWorkshopIdInDates(long workshopId, DateTime start, DateTime end); bool IsActiveInPeriod(long employeeId, long workshopId, DateTime startDate, DateTime endDate); - } + void RemoveRange(IEnumerable rollCallEmployeeStatusList); + } } diff --git a/CompanyManagment.Application/LeftWorkApplication.cs b/CompanyManagment.Application/LeftWorkApplication.cs index 8f7a30ae..9aca683f 100644 --- a/CompanyManagment.Application/LeftWorkApplication.cs +++ b/CompanyManagment.Application/LeftWorkApplication.cs @@ -30,13 +30,14 @@ public class LeftWorkApplication : ILeftWorkApplication private readonly IEmployeeRepository _employeeRepository; private readonly IWorkshopRepository _workshopRepository; private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; - private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository; + private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository; - public LeftWorkApplication(ILeftWorkRepository leftWorkRepository, IContractRepository contractRepository, + public LeftWorkApplication(ILeftWorkRepository leftWorkRepository, IContractRepository contractRepository, IContractApplication contractApplication, IWorkingHoursApplication workingHoursApplication, IWorkingHoursItemsApplication workingHoursItemsApplication, IEmployeeRepository employeeRepository, - IWorkshopRepository workshopRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository) + IWorkshopRepository workshopRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, + IRollCallEmployeeRepository rollCallEmployeeRepository) { _leftWorkRepository = leftWorkRepository; _contractRepository = contractRepository; @@ -51,7 +52,6 @@ public class LeftWorkApplication : ILeftWorkApplication public OperationResult Create(CreateLeftWork command) { - // var Contracts = _contractRepository.Search(new ContractSearchModel() // { // EmployeeId = command.EmployeeId, @@ -70,7 +70,6 @@ public class LeftWorkApplication : ILeftWorkApplication var start = command.StartWorkDate.ToGeorgianDateTime(); - //if (_leftWorkRepository.Exists(x => // x.StartWorkDate > start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده کوچکتر از سابقه شروع به کار قبلی است"); @@ -112,16 +111,17 @@ public class LeftWorkApplication : ILeftWorkApplication return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName; - var workshop = _workshopRepository.GetDetails(command.WorkshopId); - var workshopName = workshop.WorkshopFullName; + var workshop = _workshopRepository.GetDetails(command.WorkshopId); + var workshopName = workshop.WorkshopFullName; var leftWork = new LeftWork(left, start, command.WorkshopId, command.EmployeeId, employeeFullName, workshopName, command.JobId, command.IncludeStatus, - command.AddBonusesPay, command.AddYearsPay, command.AddLeavePay,workshop.ComputeOptions,workshop.BonusesOptions); + command.AddBonusesPay, command.AddYearsPay, command.AddLeavePay, workshop.ComputeOptions, + workshop.BonusesOptions); _leftWorkRepository.Create(leftWork); _leftWorkRepository.SaveChanges(); - IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId); + IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId, command.WorkshopId); - return operation.Succcedded(); + return operation.Succcedded(); } //public OperationResult Create(CreateLeftWork command) @@ -193,8 +193,8 @@ public class LeftWorkApplication : ILeftWorkApplication command.EmployeeId, command.JobId, command.IncludeStatus, command.AddBonusesPay, command.AddYearsPay, command.AddLeavePay); _leftWorkRepository.SaveChanges(); - IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId); - return operation.Succcedded(); + IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId, command.WorkshopId); + return operation.Succcedded(); } public EditLeftWork GetDetails(long id) @@ -214,11 +214,9 @@ public class LeftWorkApplication : ILeftWorkApplication public List SearchCreateContract(LeftWorkSearchModel searchModel) { - return _leftWorkRepository.SearchCreateContract(searchModel); } - public async Task> searchAsync(LeftWorkSearchModel searchModel) { @@ -228,7 +226,6 @@ public class LeftWorkApplication : ILeftWorkApplication public string GetBeforeDate(string date) { - var persianBefore = ""; var year = Convert.ToInt32(date.Substring(0, 4)); var month = Convert.ToInt32(date.Substring(5, 2)); @@ -255,10 +252,8 @@ public class LeftWorkApplication : ILeftWorkApplication } - public void LeftWorkExtension(long contractId, string CStart, string CEnd) { - var step1 = _contractApplication.GetDetails(contractId); var step2 = _workingHoursApplication.GetByContractId(contractId); var step3 = _workingHoursItemsApplication.GetWorkingHoursItems(); @@ -380,13 +375,11 @@ public class LeftWorkApplication : ILeftWorkApplication ContarctStart = CStart, ContractEnd = CEnd, GetWorkDateHide = step1.GetWorkDate, - }; var step5 = _contractApplication.MandatoryHours(computing); string workingDays = string.Empty; if (step5.NumberOfWorkingDays == "0") { - workingDays = step5.ComplexNumberOfWorkingDays; } else @@ -477,8 +470,6 @@ public class LeftWorkApplication : ILeftWorkApplication OverTimeWorkM = step5.OverTimeWorkM == "0" ? "" : step5.OverTimeWorkM, OverNightWorkH = step5.OverNightWorkH == "0" ? "" : step5.OverNightWorkH, OverNightWorkM = step5.OverNightWorkM == "0" ? "" : step5.OverNightWorkM, - - }; var resss = _contractApplication.Create(createNew); } @@ -529,7 +520,9 @@ public class LeftWorkApplication : ILeftWorkApplication item.LeftWorkDate = string.IsNullOrWhiteSpace(item.LeftWorkDate) ? "1500/01/01" : item.LeftWorkDate; var left = item.LeftWorkDate.ToGeorgianDateTime(); var start = item.StartWorkDate.ToGeorgianDateTime(); - if (_leftWorkRepository.Exists(x => x.StartWorkDate > start && left == null && x.EmployeeId == informationLeftwork.EmployeeId && x.WorkshopId == informationLeftwork.WorkshopId && x.id!=item.Id)) + if (_leftWorkRepository.Exists(x => + x.StartWorkDate > start && left == null && x.EmployeeId == informationLeftwork.EmployeeId && + x.WorkshopId == informationLeftwork.WorkshopId && x.id != item.Id)) return operation.Failed("وارد کردن تاریخ پایان کار اجباری است "); if (_leftWorkRepository.Exists(x => x.StartWorkDate == start && x.EmployeeId == informationLeftwork.EmployeeId && @@ -540,17 +533,23 @@ public class LeftWorkApplication : ILeftWorkApplication x.EmployeeId == informationLeftwork.EmployeeId && x.WorkshopId == informationLeftwork.WorkshopId && x.id != item.Id)) return operation.Failed("شروع به کار قبلی این شخص ترک کار ندارد "); - if (_leftWorkRepository.Exists(x => x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && x.EmployeeId == informationLeftwork.EmployeeId && x.WorkshopId == informationLeftwork.WorkshopId && x.id != item.Id)) + if (_leftWorkRepository.Exists(x => + x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && + x.EmployeeId == informationLeftwork.EmployeeId && + x.WorkshopId == informationLeftwork.WorkshopId && x.id != item.Id)) return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); } } - operation = _leftWorkRepository.CreateLeftWork(informationLeftwork); - if (informationLeftwork != null && operation.IsSuccedded) IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(informationLeftwork.EmployeeId); - return operation; - } + operation = _leftWorkRepository.CreateLeftWork(informationLeftwork); + if (informationLeftwork != null && operation.IsSuccedded) + IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(informationLeftwork.EmployeeId, + informationLeftwork.WorkshopId); + return operation; + } - public OperationResult CreateLeftWorkByLeftWorkGroups(string employeeFullName, long commandEmployeeId, List commandPersonnelCode, List leftWorkGroups) + public OperationResult CreateLeftWorkByLeftWorkGroups(string employeeFullName, long commandEmployeeId, + List commandPersonnelCode, List leftWorkGroups) { var operation = new OperationResult(); @@ -560,7 +559,9 @@ public class LeftWorkApplication : ILeftWorkApplication { foreach (var item2 in item.LeftWorkViewModels) { - item2.LeftWorkDate = string.IsNullOrWhiteSpace(item2.LeftWorkDate) ? "1500/01/01" : item2.LeftWorkDate; + item2.LeftWorkDate = string.IsNullOrWhiteSpace(item2.LeftWorkDate) + ? "1500/01/01" + : item2.LeftWorkDate; var left = item2.LeftWorkDate.ToGeorgianDateTime(); var start = item2.StartWorkDate.ToGeorgianDateTime(); if (_leftWorkRepository.Exists(x => @@ -586,14 +587,16 @@ public class LeftWorkApplication : ILeftWorkApplication } } - operation = _leftWorkRepository.CreateLeftWorkByLeftWorkGroups(employeeFullName, commandEmployeeId, commandPersonnelCode, leftWorkGroups); - if (operation.IsSuccedded) IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(commandEmployeeId); - return operation; - } + operation = _leftWorkRepository.CreateLeftWorkByLeftWorkGroups(employeeFullName, commandEmployeeId, + commandPersonnelCode, leftWorkGroups); + if (operation.IsSuccedded) + IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(commandEmployeeId); + return operation; + } public OperationResult CheckDeleteLeftWork(long workshopId, long employeeId, string date, int type) { - return _leftWorkRepository.CheckDeleteLeftWork( workshopId, employeeId, date.ToGeorgianDateTime(),type); + return _leftWorkRepository.CheckDeleteLeftWork(workshopId, employeeId, date.ToGeorgianDateTime(), type); } public OperationResult CheckEditLeftWork(long workshopId, long employeeId, string date, int type) @@ -607,21 +610,23 @@ public class LeftWorkApplication : ILeftWorkApplication } #region Pooya + //این متد ترک کار های کارمند را با فعالیت حضور غیاب یکپارچه می کند private void IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(long employeeId) { //get last leftworks for employee in all workshops - var leftWorks = _leftWorkRepository.search(new LeftWorkSearchModel() { EmployeeId = employeeId }).GroupBy(x => x.WorkshopId).Select(x => - { - var leftWork = x.MaxBy(y => y.StartWorkDateGr); - return new LeftWorkViewModel() + var leftWorks = _leftWorkRepository.search(new LeftWorkSearchModel() { EmployeeId = employeeId }) + .GroupBy(x => x.WorkshopId).Select(x => { - EmployeeId = employeeId, - WorkshopId = x.Key, - LeftWorkDateGr = leftWork.LeftWorkDateGr.Date.AddDays(-1), - StartWorkDateGr = leftWork.StartWorkDateGr - }; - }).ToList(); + var leftWork = x.MaxBy(y => y.StartWorkDateGr); + return new LeftWorkViewModel() + { + EmployeeId = employeeId, + WorkshopId = x.Key, + LeftWorkDateGr = leftWork.LeftWorkDateGr.Date.AddDays(-1), + StartWorkDateGr = leftWork.StartWorkDateGr + }; + }).ToList(); //get rollCallEmployee associated with those leftworks which have a higher end date than leftworkDate var rollCallsEmployee = _rollCallEmployeeRepository.GetByEmployeeIdWithStatuses(employeeId) @@ -632,17 +637,90 @@ public class LeftWorkApplication : ILeftWorkApplication x.WorkshopId, x.EmployeeId, y.LeftWorkDateGr, - Status = x.Statuses.OrderByDescending(z => z.StartDate).FirstOrDefault(z => z.StartDateGr.Date < y.LeftWorkDateGr && z.EndDateGr.Date > y.LeftWorkDateGr) + Status = x.Statuses.OrderByDescending(z => z.StartDate).FirstOrDefault(z => + z.StartDateGr.Date < y.LeftWorkDateGr && z.EndDateGr.Date > y.LeftWorkDateGr) }); //shaping up the list to send as parameter to repository - var newRollCallRecords = joinedList.Where(x => x.Status != null).Select(x => new AdjustRollCallEmployeesWithEmployeeLeftWork() - { - LeaveDate = x.LeftWorkDateGr, - RollCallStatusId = x.Status.Id - }).ToList(); - if(newRollCallRecords.Count > 0) + var newRollCallRecords = joinedList.Where(x => x.Status != null).Select(x => + new AdjustRollCallEmployeesWithEmployeeLeftWork() + { + LeaveDate = x.LeftWorkDateGr, + RollCallStatusId = x.Status.Id + }).ToList(); + if (newRollCallRecords.Count > 0) _rollCallEmployeeStatusRepository.AdjustRollCallStatusEndDates(newRollCallRecords); + + foreach (var rollCallEmployeeViewModel in rollCallsEmployee) + { + var maxLeftWork = leftWorks.FirstOrDefault(x => x.WorkshopId == rollCallEmployeeViewModel.WorkshopId); + if (maxLeftWork == null) + { + continue; + } + + var employeeStatus = _rollCallEmployeeRepository.GetBy(rollCallEmployeeViewModel.EmployeeId, + rollCallEmployeeViewModel.WorkshopId); + var rollCallEmployeeStatusList = employeeStatus.EmployeesStatus + .Where(x => x.StartDate >= maxLeftWork.LeftWorkDateGr).ToList(); + if (rollCallEmployeeStatusList.Any()) + { + _rollCallEmployeeStatusRepository.RemoveRange(rollCallEmployeeStatusList); + _rollCallEmployeeStatusRepository.SaveChanges(); + } + } + } + + private void IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(long employeeId, long workshopId) + { + //get last leftworks for employee in all workshops + var leftWorks = _leftWorkRepository.search(new LeftWorkSearchModel() + { EmployeeId = employeeId, WorkshopId = workshopId }) + .Select(x => new LeftWorkViewModel() + { + EmployeeId = employeeId, + WorkshopId = x.WorkshopId, + LeftWorkDateGr = x.LeftWorkDateGr.Date.AddDays(-1), + StartWorkDateGr = x.StartWorkDateGr + }).ToList(); + + var maxLeftWork = leftWorks.MaxBy(y => y.StartWorkDateGr); + + + //get rollCallEmployee associated with those leftworks which have a higher end date than leftworkDate + var rollCallsEmployee = _rollCallEmployeeRepository.GetBy(employeeId, workshopId); + + // var joinedList = rollCallsEmployee.Join(leftWorks, x => x.WorkshopId, y => y.WorkshopId, (x, y) => new + // { + // x.WorkshopId, + // x.EmployeeId, + // y.LeftWorkDateGr, + // Status = x.Statuses.OrderByDescending(z => z.StartDate).FirstOrDefault(z => z.StartDateGr.Date < y.LeftWorkDateGr && z.EndDateGr.Date > y.LeftWorkDateGr) + // }); + + + var status = rollCallsEmployee.EmployeesStatus.OrderByDescending(z => z.StartDate) + .FirstOrDefault(rollCallEmployeeStatus => rollCallEmployeeStatus.StartDate.Date < maxLeftWork.LeftWorkDateGr + && rollCallEmployeeStatus.EndDate.Date > + maxLeftWork.LeftWorkDateGr); + + if (status != null) + { + var adjust = new AdjustRollCallEmployeesWithEmployeeLeftWork() + { + LeaveDate = maxLeftWork.LeftWorkDateGr, + RollCallStatusId = status.id + }; + _rollCallEmployeeStatusRepository.AdjustRollCallStatusEndDates([adjust]); + } + + var rollCallEmployeeStatusList = rollCallsEmployee.EmployeesStatus + .Where(x => x.StartDate >= maxLeftWork.LeftWorkDateGr).ToList(); + if (rollCallEmployeeStatusList.Any()) + { + _rollCallEmployeeStatusRepository.RemoveRange(rollCallEmployeeStatusList); + _rollCallEmployeeStatusRepository.SaveChanges(); + } } #endregion diff --git a/CompanyManagment.Application/LeftWorkTempApplication.cs b/CompanyManagment.Application/LeftWorkTempApplication.cs index fbb8daf4..3f2b2dab 100644 --- a/CompanyManagment.Application/LeftWorkTempApplication.cs +++ b/CompanyManagment.Application/LeftWorkTempApplication.cs @@ -27,26 +27,30 @@ namespace CompanyManagment.Application; public class LeftWorkTempApplication : ILeftWorkTempApplication { - private readonly ILeftWorkTempRepository _leftWorkTempRepository; - private readonly ILeftWorkRepository _leftWorkRepository; - private readonly IWorkshopRepository _workshopRepository; - private readonly IEmployeeRepository _employeeRepository; - private readonly IJobRepository _jobRepository; - private readonly ICheckoutRepository _checkoutRepository; - private readonly IContractRepository _contractRepository; + private readonly ILeftWorkTempRepository _leftWorkTempRepository; + private readonly ILeftWorkRepository _leftWorkRepository; + private readonly IWorkshopRepository _workshopRepository; + private readonly IEmployeeRepository _employeeRepository; + private readonly IJobRepository _jobRepository; + private readonly ICheckoutRepository _checkoutRepository; + private readonly IContractRepository _contractRepository; private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository; - public LeftWorkTempApplication(ILeftWorkTempRepository leftWorkTempRepository, ILeftWorkRepository leftWorkRepository, IWorkshopRepository workshopRepository, IEmployeeRepository employeeRepository, IJobRepository jobRepository, ICheckoutRepository checkoutRepository, IContractRepository contractRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository) - { - _leftWorkTempRepository = leftWorkTempRepository; - _leftWorkRepository = leftWorkRepository; - _workshopRepository = workshopRepository; - _employeeRepository = employeeRepository; - _jobRepository = jobRepository; - _checkoutRepository = checkoutRepository; - _contractRepository = contractRepository; + public LeftWorkTempApplication(ILeftWorkTempRepository leftWorkTempRepository, + ILeftWorkRepository leftWorkRepository, IWorkshopRepository workshopRepository, + IEmployeeRepository employeeRepository, IJobRepository jobRepository, ICheckoutRepository checkoutRepository, + IContractRepository contractRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, + IRollCallEmployeeRepository rollCallEmployeeRepository) + { + _leftWorkTempRepository = leftWorkTempRepository; + _leftWorkRepository = leftWorkRepository; + _workshopRepository = workshopRepository; + _employeeRepository = employeeRepository; + _jobRepository = jobRepository; + _checkoutRepository = checkoutRepository; + _contractRepository = contractRepository; _rollCallEmployeeStatusRepository = rollCallEmployeeStatusRepository; _rollCallEmployeeRepository = rollCallEmployeeRepository; } @@ -59,15 +63,16 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication { #region Validation - if (_leftWorkTempRepository.Exists(x=>x.WorkshopId == command.WorkshopId && x.EmployeeId == employeeId)) + if (_leftWorkTempRepository.Exists(x => x.WorkshopId == command.WorkshopId && x.EmployeeId == employeeId)) { - return op.Failed("برای پرسنل وارد شده قبلا درخواست ترک کار ثبت کرده اید"); + return op.Failed("برای پرسنل وارد شده قبلا درخواست ترک کار ثبت کرده اید"); } if (command.LeftWorkTime.TryToGeorgianDateTime(out var leftWorkDateGr) == false) { return op.Failed("تاریخ شروع به کار وارد شده نامعتبر است"); } + if (command.LastDayStanding.TryToGeorgianDateTime(out var lastDayStandingDateGr) == false) { return op.Failed("تاریخ شروع به کار وارد شده نامعتبر است"); @@ -101,7 +106,6 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication } - //if (leftWork.StartWorkDate >= leftWorkDateGr) //{ // return op.Failed("ترک کار نمیتواند کوچک تر یا مساوی شروع به کار باشد"); @@ -121,10 +125,10 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication // return op.Failed("این پرسنل در تاریخ ترک کار وارد شده دارای قرارداد میباشد"); //} - #endregion - var leftWorkTemp = LeftWorkTemp.CreateLeftWork(leftWork.id, leftWork.StartWorkDate, leftWorkDateGr, lastDayStandingDateGr, + var leftWorkTemp = LeftWorkTemp.CreateLeftWork(leftWork.id, leftWork.StartWorkDate, leftWorkDateGr, + lastDayStandingDateGr, command.WorkshopId, employeeId, leftWork.JobId); await _leftWorkTempRepository.CreateAsync(leftWorkTemp); @@ -135,61 +139,63 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication } public Task GetStartAndLeftWorkDetails(long employeeId, long workshopId) - { - return _leftWorkTempRepository.GetStartAndLeftWorkDetails(employeeId, workshopId); - } + { + return _leftWorkTempRepository.GetStartAndLeftWorkDetails(employeeId, workshopId); + } - public async Task AcceptStartWork(AcceptStartWorkTemp command) - { - var op = new OperationResult(); - if (command.StartDateTime.TryToGeorgianDateTime(out var startDateGr) == false) - { - return op.Failed("تاریخ شروع به کار وارد شده نامعتبر است"); - } + public async Task AcceptStartWork(AcceptStartWorkTemp command) + { + var op = new OperationResult(); + if (command.StartDateTime.TryToGeorgianDateTime(out var startDateGr) == false) + { + return op.Failed("تاریخ شروع به کار وارد شده نامعتبر است"); + } - var leftWorkTemp = _leftWorkTempRepository.Get(command.LeftWorkTempId); + var leftWorkTemp = _leftWorkTempRepository.Get(command.LeftWorkTempId); - if (leftWorkTemp.LeftWorkType != LeftWorkTempType.StartWork) - { - return op.Failed("اطلاعات وارد شده نامعتبر است"); - } + if (leftWorkTemp.LeftWorkType != LeftWorkTempType.StartWork) + { + return op.Failed("اطلاعات وارد شده نامعتبر است"); + } - if (_leftWorkRepository.Exists(x => x.WorkshopId == leftWorkTemp.WorkshopId && x.EmployeeId == leftWorkTemp.EmployeeId && x.LeftWorkDate >= startDateGr)) - { - return op.Failed("شروع به کار وارد شده با ترک کار های قبلی تداخل دارد"); - } + if (_leftWorkRepository.Exists(x => + x.WorkshopId == leftWorkTemp.WorkshopId && x.EmployeeId == leftWorkTemp.EmployeeId && + x.LeftWorkDate >= startDateGr)) + { + return op.Failed("شروع به کار وارد شده با ترک کار های قبلی تداخل دارد"); + } - if (_jobRepository.Exists(x => x.id == command.JobId) == false) - { - return op.Failed("سمت وارد شده نامعتبر است"); - } + if (_jobRepository.Exists(x => x.id == command.JobId) == false) + { + return op.Failed("سمت وارد شده نامعتبر است"); + } - var defaultTime = new DateTime(2121, 03, 21); + var defaultTime = new DateTime(2121, 03, 21); - var workshop = _workshopRepository.Get(leftWorkTemp.WorkshopId); - var employee = _employeeRepository.Get(leftWorkTemp.EmployeeId); + var workshop = _workshopRepository.Get(leftWorkTemp.WorkshopId); + var employee = _employeeRepository.Get(leftWorkTemp.EmployeeId); - var newLeftWork = new LeftWork(defaultTime, startDateGr, leftWorkTemp.WorkshopId, leftWorkTemp.EmployeeId, - employee.FullName, workshop.WorkshopFullName, command.JobId, false, false, false, false, "", ""); + var newLeftWork = new LeftWork(defaultTime, startDateGr, leftWorkTemp.WorkshopId, leftWorkTemp.EmployeeId, + employee.FullName, workshop.WorkshopFullName, command.JobId, false, false, false, false, "", ""); - await _leftWorkRepository.CreateAsync(newLeftWork); - _leftWorkTempRepository.Remove(leftWorkTemp); - var rollCallEmployee = _rollCallEmployeeRepository.GetBy(leftWorkTemp.EmployeeId, leftWorkTemp.WorkshopId); + await _leftWorkRepository.CreateAsync(newLeftWork); + _leftWorkTempRepository.Remove(leftWorkTemp); + var rollCallEmployee = _rollCallEmployeeRepository.GetBy(leftWorkTemp.EmployeeId, leftWorkTemp.WorkshopId); - var rollCallStatus = rollCallEmployee?.EmployeesStatus.MaxBy(x => x.StartDate); + var rollCallStatus = rollCallEmployee?.EmployeesStatus.MaxBy(x => x.StartDate); - if (rollCallStatus != null) - { - var startWork = newLeftWork.StartWorkDate; - rollCallStatus.Edit(startWork, rollCallStatus.EndDate); + if (rollCallStatus != null) + { + var startWork = newLeftWork.StartWorkDate; + rollCallStatus.Edit(startWork, rollCallStatus.EndDate); + } - } - await _leftWorkRepository.SaveChangesAsync(); - await _leftWorkTempRepository.SaveChangesAsync(); + await _leftWorkRepository.SaveChangesAsync(); + await _leftWorkTempRepository.SaveChangesAsync(); - return op.Succcedded(); - } + return op.Succcedded(); + } public async Task AcceptLeftWork(AcceptLeftWorkTemp command) { @@ -237,9 +243,12 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication x.WorkshopId == leftWorkTemp.WorkshopId && x.ContractStart <= lastDayStandingGr && x.ContractEnd >= lastDayStandingGr)) { - return op.Failed("این پرسنل در تاریخ ترک کار وارد شده دارای فیش حقوقی میباشد. ابتدا فیش حقوقی پرسنل را حذف کنید "); + return op.Failed( + "این پرسنل در تاریخ ترک کار وارد شده دارای فیش حقوقی میباشد. ابتدا فیش حقوقی پرسنل را حذف کنید "); } + var transaction = await _rollCallEmployeeStatusRepository.BeginTransactionAsync(); + leftWork.Edit(leftWorkDateGr, leftWork.StartWorkDate, leftWork.WorkshopId, leftWork.EmployeeId, leftWork.JobId, leftWork.IncludeStatus, leftWork.AddBonusesPay, leftWork.AddYearsPay, leftWork.AddLeavePay); @@ -247,8 +256,9 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication await _leftWorkRepository.SaveChangesAsync(); await _leftWorkTempRepository.SaveChangesAsync(); - IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(leftWork.EmployeeId); + IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(leftWork.EmployeeId, leftWork.WorkshopId); + await transaction.CommitAsync(); return op.Succcedded(); } @@ -259,45 +269,59 @@ public class LeftWorkTempApplication : ILeftWorkTempApplication public List GetLeftWorksByWorkshopId(long workshopId) { - return _leftWorkTempRepository.GetLeftWorksByWorkshopId(workshopId); - + return _leftWorkTempRepository.GetLeftWorksByWorkshopId(workshopId); } //این متد ترک کار های کارمند را با فعالیت حضور غیاب یکپارچه می کند - private void IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(long employeeId) + private void IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(long employeeId, long workshopId) { //get last leftworks for employee in all workshops - var leftWorks = _leftWorkRepository.search(new LeftWorkSearchModel() { EmployeeId = employeeId }).GroupBy(x => x.WorkshopId).Select(x => - { - var leftWork = x.MaxBy(y => y.StartWorkDateGr); - return new LeftWorkViewModel() + var leftWorks = _leftWorkRepository.search(new LeftWorkSearchModel() + { EmployeeId = employeeId, WorkshopId = workshopId }) + .Select(x => new LeftWorkViewModel() { EmployeeId = employeeId, - WorkshopId = x.Key, - LeftWorkDateGr = leftWork.LeftWorkDateGr.Date.AddDays(-1), - StartWorkDateGr = leftWork.StartWorkDateGr - }; - }).ToList(); + WorkshopId = x.WorkshopId, + LeftWorkDateGr = x.LeftWorkDateGr.Date.AddDays(-1), + StartWorkDateGr = x.StartWorkDateGr + }).ToList(); + + var maxLeftWork = leftWorks.MaxBy(y => y.StartWorkDateGr); + //get rollCallEmployee associated with those leftworks which have a higher end date than leftworkDate - var rollCallsEmployee = _rollCallEmployeeRepository.GetByEmployeeIdWithStatuses(employeeId) - .Where(x => leftWorks.Any(y => y.WorkshopId == x.WorkshopId)).ToList(); + var rollCallsEmployee = _rollCallEmployeeRepository.GetBy(employeeId, workshopId); - var joinedList = rollCallsEmployee.Join(leftWorks, x => x.WorkshopId, y => y.WorkshopId, (x, y) => new - { - x.WorkshopId, - x.EmployeeId, - y.LeftWorkDateGr, - Status = x.Statuses.OrderByDescending(z => z.StartDate).FirstOrDefault(z => z.StartDateGr.Date < y.LeftWorkDateGr && z.EndDateGr.Date > y.LeftWorkDateGr) - }); + // var joinedList = rollCallsEmployee.Join(leftWorks, x => x.WorkshopId, y => y.WorkshopId, (x, y) => new + // { + // x.WorkshopId, + // x.EmployeeId, + // y.LeftWorkDateGr, + // Status = x.Statuses.OrderByDescending(z => z.StartDate).FirstOrDefault(z => z.StartDateGr.Date < y.LeftWorkDateGr && z.EndDateGr.Date > y.LeftWorkDateGr) + // }); - //shaping up the list to send as parameter to repository - var newRollCallRecords = joinedList.Where(x => x.Status != null).Select(x => new AdjustRollCallEmployeesWithEmployeeLeftWork() + + var status = rollCallsEmployee.EmployeesStatus.OrderByDescending(z => z.StartDate) + .FirstOrDefault(rollCallEmployeeStatus => rollCallEmployeeStatus.StartDate.Date < maxLeftWork.LeftWorkDateGr + && rollCallEmployeeStatus.EndDate.Date > + maxLeftWork.LeftWorkDateGr); + + if (status != null) { - LeaveDate = x.LeftWorkDateGr, - RollCallStatusId = x.Status.Id - }).ToList(); - if (newRollCallRecords.Count > 0) - _rollCallEmployeeStatusRepository.AdjustRollCallStatusEndDates(newRollCallRecords); + var adjust = new AdjustRollCallEmployeesWithEmployeeLeftWork() + { + LeaveDate = maxLeftWork.LeftWorkDateGr, + RollCallStatusId = status.id + }; + _rollCallEmployeeStatusRepository.AdjustRollCallStatusEndDates([adjust]); + } + + var rollCallEmployeeStatusList = rollCallsEmployee.EmployeesStatus + .Where(x => x.StartDate >= maxLeftWork.LeftWorkDateGr).ToList(); + if (rollCallEmployeeStatusList.Any()) + { + _rollCallEmployeeStatusRepository.RemoveRange(rollCallEmployeeStatusList); + _rollCallEmployeeStatusRepository.SaveChanges(); + } } } \ No newline at end of file diff --git a/CompanyManagment.EFCore/Repository/RollCallEmployeeRepository.cs b/CompanyManagment.EFCore/Repository/RollCallEmployeeRepository.cs index 7e027a38..8955c814 100644 --- a/CompanyManagment.EFCore/Repository/RollCallEmployeeRepository.cs +++ b/CompanyManagment.EFCore/Repository/RollCallEmployeeRepository.cs @@ -478,7 +478,8 @@ public class RollCallEmployeeRepository : RepositoryBase public RollCallEmployee GetBy(long employeeId, long workshopId) { - return _context.RollCallEmployees.Include(x => x.EmployeesStatus).FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); + return _context.RollCallEmployees.Include(x => x.EmployeesStatus) + .FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId); } #endregion