using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using _0_Framework.Application; using Company.Domain.ContractAgg; using Company.Domain.EmployeeAgg; using Company.Domain.LeftWorkAgg; using Company.Domain.RollCallEmployeeAgg; using Company.Domain.RollCallEmployeeStatusAgg; using Company.Domain.WorkshopAgg; using CompanyManagment.App.Contracts.Contract; using CompanyManagment.App.Contracts.LeftWork; using CompanyManagment.App.Contracts.PersonnleCode; using CompanyManagment.App.Contracts.RollCallEmployee; using CompanyManagment.App.Contracts.WorkingHours; using CompanyManagment.App.Contracts.WorkingHoursItems; using CompanyManagment.App.Contracts.Workshop.DTOs; using PersianTools.Core; namespace CompanyManagment.Application; public class LeftWorkApplication : ILeftWorkApplication { private readonly ILeftWorkRepository _leftWorkRepository; private readonly IContractRepository _contractRepository; private readonly IContractApplication _contractApplication; private readonly IWorkingHoursApplication _workingHoursApplication; private readonly IWorkingHoursItemsApplication _workingHoursItemsApplication; private readonly IEmployeeRepository _employeeRepository; private readonly IWorkshopRepository _workshopRepository; private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository; public LeftWorkApplication(ILeftWorkRepository leftWorkRepository, IContractRepository contractRepository, IContractApplication contractApplication, IWorkingHoursApplication workingHoursApplication, IWorkingHoursItemsApplication workingHoursItemsApplication, IEmployeeRepository employeeRepository, IWorkshopRepository workshopRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository) { _leftWorkRepository = leftWorkRepository; _contractRepository = contractRepository; _contractApplication = contractApplication; _workingHoursApplication = workingHoursApplication; _workingHoursItemsApplication = workingHoursItemsApplication; _employeeRepository = employeeRepository; _workshopRepository = workshopRepository; _rollCallEmployeeStatusRepository = rollCallEmployeeStatusRepository; _rollCallEmployeeRepository = rollCallEmployeeRepository; } public OperationResult Create(CreateLeftWork command) { // var Contracts = _contractRepository.Search(new ContractSearchModel() // { // EmployeeId = command.EmployeeId, // WorkshopIds = command.WorkshopId // }); // var lastContract = Contracts.FirstOrDefault(); var operation = new OperationResult(); //if (Contracts == null) // { // return operation.Failed("این پرسنل در این کارگاه قرارداد ندارد!"); // } var left = command.LeftWorkDate.ToGeorgianDateTime(); var start = command.StartWorkDate.ToGeorgianDateTime(); //if (_leftWorkRepository.Exists(x => // x.StartWorkDate > start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده کوچکتر از سابقه شروع به کار قبلی است"); //if (_leftWorkRepository.Exists(x => // x.StartWorkDate > start && left == null && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("وارد کردن تاریخ پایان کار اجباری است "); //if (_leftWorkRepository.Exists(x => // x.StartWorkDate == start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده برابر سابقه شروع به کار قبلی است"); //if (_leftWorkRepository.Exists(x => // x.StartWorkDate <= start && x.LeftWorkDate == left && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("شروع به کار قبلی این شخص ترک کار ندارد "); //if (_leftWorkRepository.Exists(x => // x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); if (_leftWorkRepository.Exists(x => x.StartWorkDate > start && left == null && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) return operation.Failed("وارد کردن تاریخ پایان کار اجباری است "); if (_leftWorkRepository.Exists(x => x.StartWorkDate == start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) return operation.Failed("تاریخ وارد شده برابر سابقه شروع به کار قبلی است"); if (_leftWorkRepository.Exists(x => x.StartWorkDate <= start && x.LeftWorkDate == left && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) return operation.Failed("شروع به کار قبلی این شخص ترک کار ندارد "); if (_leftWorkRepository.Exists(x => x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName; 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); _leftWorkRepository.Create(leftWork); _leftWorkRepository.SaveChanges(); IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId, command.WorkshopId); return operation.Succcedded(); } //public OperationResult Create(CreateLeftWork command) //{ // //var Contracts = _contractRepository.Search(new ContractSearchModel() // //{ // // EmployeeId = command.EmployeeId, // // WorkshopIds = command.WorkshopId // //}); // //var lastContract = Contracts.FirstOrDefault(); // var operation = new OperationResult(); // var left = command.LeftWorkDate.ToGeorgianDateTime(); // var start = command.StartWorkDate.ToGeorgianDateTime(); // //var nextMonthStartGr = lastContract.ContractEndGr.AddDays(1); // //var nextMonthStartFarsi = nextMonthStartGr.ToFarsi(); // //var nextMonthEnd = nextMonthStartFarsi.FindeEndOfMonth(); // //var nextMonthEndGr = nextMonthEnd.ToGeorgianDateTime(); // if (_leftWorkRepository.Exists(x => // x.StartWorkDate > start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده کوچکتر از سابقه شروع به کار قبلی است"); // if (_leftWorkRepository.Exists(x => // x.StartWorkDate == start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده برابر سابقه شروع به کار قبلی است"); // if (_leftWorkRepository.Exists(x => // x.StartWorkDate <= start && x.LeftWorkDate == left && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("شروع به کار قبلی این شخص ترک کار ندارد "); // if (_leftWorkRepository.Exists(x => // x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); // //if (left <= lastContract.ContractStartGr) // // return operation.Failed("تاریخ وارد شده کوچکتر از تاریخ شروع آخرین قرارداد است"); // //if (_leftWorkRepository.Exists(x => // // lastContract.ContractEndGr >= left && x.LeftWorkDate >= lastContract.ContractStartGr && x.LeftWorkDate <= left && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // // return operation.Failed("برای آخرین قرارداد سابقه ترک کار وجود دارد"); // //if (_leftWorkRepository.Exists(x => // // lastContract.ContractEndGr < left && x.LeftWorkDate >= lastContract.ContractStartGr && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId)) // // return operation.Failed("برای تاریخ وارد شده قراردادی وجود ندارد"); // //if (left > nextMonthEndGr) // // return operation.Failed("برای تاریخ وارد شده قراردادی وجود ندارد"); // //if (left > nextMonthStartGr) // //{ // // LeftWorkExtension(lastContract.Id, nextMonthStartFarsi, nextMonthEnd); // //} // if (command.WorkshopId < 1) // return operation.Failed("انتخاب کارگاه ضروری است"); // var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName; // var workshopName = _workshopRepository.GetDetails(command.WorkshopId).WorkshopFullName; // var leftWork = new LeftWork(left,start,command.WorkshopId, // command.EmployeeId, employeeFullName, workshopName); // _leftWorkRepository.Create(leftWork); // _leftWorkRepository.SaveChanges(); // return operation.Succcedded(); //} public OperationResult Edit(EditLeftWork command) { var operation = new OperationResult(); var left = command.LeftWorkDate.ToGeorgianDateTime(); var start = command.StartWorkDate.ToGeorgianDateTime(); var leftWorkEdit = _leftWorkRepository.Get(command.Id); if (leftWorkEdit == null) operation.Failed("رکورد مورد نظر وجود ندارد"); if (start >= left) return operation.Failed("تاریخ وارد شده کوچکتر یا مساوی تاریخ شروع به کار است"); leftWorkEdit.Edit(left, start, command.WorkshopId, command.EmployeeId, command.JobId, command.IncludeStatus, command.AddBonusesPay, command.AddYearsPay, command.AddLeavePay); _leftWorkRepository.SaveChanges(); IfEmployeeHasNewLeftWorkDateAddEndDateToRollCallStatus(command.EmployeeId, command.WorkshopId); return operation.Succcedded(); } public EditLeftWork GetDetails(long id) { return _leftWorkRepository.GetDetails(id); } public LeftWorkViewModel CheckoutleftWorkCheck(DateTime contractStart, long workshopId, long employeeId) { return _leftWorkRepository.CheckoutleftWorkCheck(contractStart, workshopId, employeeId); } public List search(LeftWorkSearchModel searchModel) { return _leftWorkRepository.search(searchModel); } public List SearchCreateContract(LeftWorkSearchModel searchModel) { return _leftWorkRepository.SearchCreateContract(searchModel); } public async Task> searchAsync(LeftWorkSearchModel searchModel) { var res = search(searchModel); return res; } public string GetBeforeDate(string date) { var persianBefore = ""; var year = Convert.ToInt32(date.Substring(0, 4)); var month = Convert.ToInt32(date.Substring(5, 2)); var day = Convert.ToInt32(date.Substring(8, 2)); var persianDate = new PersianDateTime(year, month, day); var persianBeforeDate = persianDate.AddDays(-1); persianBefore = persianBeforeDate.ToString("yyyy/MM/dd"); return persianBefore; } public string StartWork(long employeeId, long workshopId, string leftWork) { return _leftWorkRepository.StartWork(employeeId, workshopId, leftWork); } public OperationResult RemoveLeftWork(long id) { return _leftWorkRepository.RemoveLeftWork(id); } public List GetLeftPersonelByWorkshopId(List workshopIds) { return _leftWorkRepository.GetLeftPersonelByWorkshopId(workshopIds); } public void LeftWorkExtension(long contractId, string CStart, string CEnd) { var step1 = _contractApplication.GetDetails(contractId); var step2 = _workingHoursApplication.GetByContractId(contractId); var step3 = _workingHoursItemsApplication.GetWorkingHoursItems(); var step4 = step3.Where(x => x.WorkingHoursId == step2.Id).ToList(); var RestTime = step4.Where(x => x.DayOfWork == "0").Select(x => x.RestTime).SingleOrDefault(); var RestTimeYekshanbeh = step4.Where(x => x.DayOfWork == "1").Select(x => x.RestTime).SingleOrDefault(); var RestTimeDoshanbeh = step4.Where(x => x.DayOfWork == "2").Select(x => x.RestTime).SingleOrDefault(); var RestTimeSeshanbeh = step4.Where(x => x.DayOfWork == "3").Select(x => x.RestTime).SingleOrDefault(); var RestTimeCheharshanbeh = step4.Where(x => x.DayOfWork == "4").Select(x => x.RestTime).SingleOrDefault(); var RestTimePanjshanbeh = step4.Where(x => x.DayOfWork == "5").Select(x => x.RestTime).SingleOrDefault(); var RestTimeJomeh = step4.Where(x => x.DayOfWork == "6").Select(x => x.RestTime).SingleOrDefault(); var SingleShift1 = step4.Where(x => x.DayOfWork == "0").Select(x => x.Start1).SingleOrDefault(); var SingleShift2 = step4.Where(x => x.DayOfWork == "0").Select(x => x.End1).SingleOrDefault(); var SingleShift1Yekshanbeh = step4.Where(x => x.DayOfWork == "1").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Yekshanbeh = step4.Where(x => x.DayOfWork == "1").Select(x => x.End1).SingleOrDefault(); var SingleShift1Doshanbeh = step4.Where(x => x.DayOfWork == "2").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Doshanbeh = step4.Where(x => x.DayOfWork == "2").Select(x => x.End1).SingleOrDefault(); var SingleShift1Seshanbeh = step4.Where(x => x.DayOfWork == "3").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Seshanbeh = step4.Where(x => x.DayOfWork == "3").Select(x => x.End1).SingleOrDefault(); var SingleShift1Cheharshanbeh = step4.Where(x => x.DayOfWork == "4").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Cheharshanbeh = step4.Where(x => x.DayOfWork == "4").Select(x => x.End1).SingleOrDefault(); var SingleShift1Panjshanbeh = step4.Where(x => x.DayOfWork == "5").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Panjshanbeh = step4.Where(x => x.DayOfWork == "5").Select(x => x.End1).SingleOrDefault(); var SingleShift1Jomeh = step4.Where(x => x.DayOfWork == "6").Select(x => x.Start1).SingleOrDefault(); var SingleShift2Jomeh = step4.Where(x => x.DayOfWork == "6").Select(x => x.End1).SingleOrDefault(); var TowShifts1 = step4.Where(x => x.DayOfWork == "0").Select(x => x.Start2).SingleOrDefault(); var TowShifts2 = step4.Where(x => x.DayOfWork == "0").Select(x => x.End2).SingleOrDefault(); var TowShifts1Yekshanbeh = step4.Where(x => x.DayOfWork == "1").Select(x => x.Start2).SingleOrDefault(); var TowShifts2Yekshanbeh = step4.Where(x => x.DayOfWork == "1").Select(x => x.End2).SingleOrDefault(); var TowShifts1Doshanbeh = step4.Where(x => x.DayOfWork == "2").Select(x => x.Start2).SingleOrDefault(); var TowShifts2Doshanbeh = step4.Where(x => x.DayOfWork == "2").Select(x => x.End2).SingleOrDefault(); var TowShifts1Seshanbeh = step4.Where(x => x.DayOfWork == "3").Select(x => x.Start2).SingleOrDefault(); var TowShifts2Seshanbeh = step4.Where(x => x.DayOfWork == "3").Select(x => x.End2).SingleOrDefault(); var TowShifts1Cheharshanbeh = step4.Where(x => x.DayOfWork == "4").Select(x => x.Start1).SingleOrDefault(); var TowShifts2Cheharshanbeh = step4.Where(x => x.DayOfWork == "4").Select(x => x.End2).SingleOrDefault(); var TowShifts1Panjshanbeh = step4.Where(x => x.DayOfWork == "5").Select(x => x.Start2).SingleOrDefault(); var TowShifts2Panjshanbeh = step4.Where(x => x.DayOfWork == "5").Select(x => x.End2).SingleOrDefault(); var TowShifts1Jomeh = step4.Where(x => x.DayOfWork == "6").Select(x => x.Start2).SingleOrDefault(); var TowShifts2Jomeh = step4.Where(x => x.DayOfWork == "6").Select(x => x.End2).SingleOrDefault(); var Start1224 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexStart).SingleOrDefault(); var End1224 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexEnd).SingleOrDefault(); var Start1236 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexStart).SingleOrDefault(); var End1236 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexEnd).SingleOrDefault(); var Start2424 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexStart).SingleOrDefault(); var End2424 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexEnd).SingleOrDefault(); var Start2448 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexStart).SingleOrDefault(); var End2448 = step4.Where(x => x.DayOfWork == "7").Select(x => x.ComplexEnd).SingleOrDefault(); var computing = new CreateContract { ShiftWork = step2.ShiftWork, RestTime = RestTime, RestTimeYekshanbeh = RestTimeYekshanbeh, RestTimeDoshanbeh = RestTimeDoshanbeh, RestTimeSeshanbeh = RestTimeSeshanbeh, RestTimeCheharshanbeh = RestTimeCheharshanbeh, RestTimePanjshanbeh = RestTimePanjshanbeh, RestTimeJomeh = RestTimeJomeh, SingleShift1 = SingleShift1, SingleShift2 = SingleShift2, SingleShift1Yekshanbeh = SingleShift1Yekshanbeh, SingleShift2Yekshanbeh = SingleShift2Yekshanbeh, SingleShift1Doshanbeh = SingleShift1Doshanbeh, SingleShift2Doshanbeh = SingleShift2Doshanbeh, SingleShift1Seshanbeh = SingleShift1Seshanbeh, SingleShift2Seshanbeh = SingleShift2Seshanbeh, SingleShift1Cheharshanbeh = SingleShift1Cheharshanbeh, SingleShift2Cheharshanbeh = SingleShift2Cheharshanbeh, SingleShift1Panjshanbeh = SingleShift1Panjshanbeh, SingleShift2Panjshanbeh = SingleShift2Panjshanbeh, SingleShift1Jomeh = SingleShift1Jomeh, SingleShift2Jomeh = SingleShift2Jomeh, TowShifts1 = TowShifts1, TowShifts2 = TowShifts2, TowShifts1Yekshanbeh = TowShifts1Yekshanbeh, TowShifts2Yekshanbeh = TowShifts2Yekshanbeh, TowShifts1Doshanbeh = TowShifts1Doshanbeh, TowShifts2Doshanbeh = TowShifts2Doshanbeh, TowShifts1Seshanbeh = TowShifts1Seshanbeh, TowShifts2Seshanbeh = TowShifts2Seshanbeh, TowShifts1Cheharshanbeh = TowShifts1Cheharshanbeh, TowShifts2Cheharshanbeh = TowShifts2Cheharshanbeh, TowShifts1Panjshanbeh = TowShifts1Panjshanbeh, TowShifts2Panjshanbeh = TowShifts2Panjshanbeh, TowShifts1Jomeh = TowShifts1Jomeh, TowShifts2Jomeh = TowShifts2Jomeh, Start1224 = Start1224, End1224 = End1224, Start1236 = Start1236, End1236 = End1236, Start2424 = Start2424, End2424 = End2424, Start2448 = Start2448, End2448 = End2448, EmployeeId = step1.EmployeeId, ContarctStart = CStart, ContractEnd = CEnd, GetWorkDateHide = step1.GetWorkDate, }; var step5 = _contractApplication.MandatoryHours(computing); string workingDays = string.Empty; if (step5.NumberOfWorkingDays == "0") { workingDays = step5.ComplexNumberOfWorkingDays; } else { workingDays = step5.NumberOfWorkingDays; } var createNew = new CreateContract { EmployeeId = step1.EmployeeId, EmployerId = step1.EmployerId, WorkshopIds = step1.WorkshopIds, GetWorkDate = step1.GetWorkDate, SetContractDate = CStart, ArchiveCode = step1.ArchiveCode, ContarctStart = CStart, ContractEnd = CEnd, YearlySalaryId = step1.YearlySalaryId, ConsumableItems = step5.ConsumableItems, HousingAllowance = step5.HousingAllowance, DayliWage = step5.SalaryCompute, FamilyAllowance = step5.FamilyAllowance, WeeklyWorkingTime = step5.SumTime44, WorkingHoursWeekly = step5.SumTime44, JobType = step1.JobType, JobTypeId = step1.JobTypeId, ContractType = step1.ContractType, WorkshopAddress1 = step1.WorkshopAddress1, WorkshopAddress2 = step1.WorkshopAddress2, AgreementSalary = step1.AgreementSalary, ContractPeriod = step1.ContractPeriod, ShiftWork = step2.ShiftWork, RestTime = RestTime, RestTimeYekshanbeh = RestTimeYekshanbeh, RestTimeDoshanbeh = RestTimeDoshanbeh, RestTimeSeshanbeh = RestTimeSeshanbeh, RestTimeCheharshanbeh = RestTimeCheharshanbeh, RestTimePanjshanbeh = RestTimePanjshanbeh, RestTimeJomeh = RestTimeJomeh, SingleShift1 = SingleShift1, SingleShift2 = SingleShift2, SingleShift1Yekshanbeh = SingleShift1Yekshanbeh, SingleShift2Yekshanbeh = SingleShift2Yekshanbeh, SingleShift1Doshanbeh = SingleShift1Doshanbeh, SingleShift2Doshanbeh = SingleShift2Doshanbeh, SingleShift1Seshanbeh = SingleShift1Seshanbeh, SingleShift2Seshanbeh = SingleShift2Seshanbeh, SingleShift1Cheharshanbeh = SingleShift1Cheharshanbeh, SingleShift2Cheharshanbeh = SingleShift2Cheharshanbeh, SingleShift1Panjshanbeh = SingleShift1Panjshanbeh, SingleShift2Panjshanbeh = SingleShift2Panjshanbeh, SingleShift1Jomeh = SingleShift1Jomeh, SingleShift2Jomeh = SingleShift2Jomeh, TowShifts1 = TowShifts1, TowShifts2 = TowShifts2, TowShifts1Yekshanbeh = TowShifts1Yekshanbeh, TowShifts2Yekshanbeh = TowShifts2Yekshanbeh, TowShifts1Doshanbeh = TowShifts1Doshanbeh, TowShifts2Doshanbeh = TowShifts2Doshanbeh, TowShifts1Seshanbeh = TowShifts1Seshanbeh, TowShifts2Seshanbeh = TowShifts2Seshanbeh, TowShifts1Cheharshanbeh = TowShifts1Cheharshanbeh, TowShifts2Cheharshanbeh = TowShifts2Cheharshanbeh, TowShifts1Panjshanbeh = TowShifts1Panjshanbeh, TowShifts2Panjshanbeh = TowShifts2Panjshanbeh, TowShifts1Jomeh = TowShifts1Jomeh, TowShifts2Jomeh = TowShifts2Jomeh, Start1224 = Start1224, End1224 = End1224, Start1236 = Start1236, End1236 = End1236, Start2424 = Start2424, End2424 = End2424, Start2448 = Start2448, End2448 = End2448, NumberOfWorkingDays = workingDays, NumberOfFriday = step5.NumberOfFriday, TotalHoursesH = step5.TotalHoursesH == "0" ? "" : step5.TotalHoursesH, TotalHoursesM = step5.TotalHoursesM == "0" ? "" : step5.TotalHoursesM, OverTimeWorkH = step5.OverTimeWorkH == "0" ? "" : step5.OverTimeWorkH, OverTimeWorkM = step5.OverTimeWorkM == "0" ? "" : step5.OverTimeWorkM, OverNightWorkH = step5.OverNightWorkH == "0" ? "" : step5.OverNightWorkH, OverNightWorkM = step5.OverNightWorkM == "0" ? "" : step5.OverNightWorkM, }; var resss = _contractApplication.Create(createNew); } public OperationResult RemoveAllLeftWork(long workshopId, long employeeId) { return _leftWorkRepository.RemoveAllLeftWork(workshopId, employeeId); } public List SearchLeftWork(LeftWorkSearchModel searchModel) { var searchList = _leftWorkRepository.SearchLeftWork(searchModel); foreach (var item in searchList) { item.StartWorkDate = item.StartWorkDateGr.ToFarsi(); if (item.LeftWorkDate != "1500/01/01") { //item.LeftWorkDate = item.LeftWorkDateGr.ToFarsi(); item.EndWorkDate = GetBeforeDate(item.LeftWorkDate); item.IsActiveWorkshop = false; } else { item.LeftWorkDate = ""; item.IsActiveWorkshop = true; } } //foreach (var item in searchList) //{ // if (item.LeftWorkDate == "1500/01/01") // item.LeftWorkDate = ""; //} return searchList; } public OperationResult CreateLeftWork(InformationLeftwork informationLeftwork) { var operation = new OperationResult(); if (informationLeftwork != null && informationLeftwork.ItemLeftworkList != null && informationLeftwork.ItemLeftworkList.Count > 0) { foreach (var item in informationLeftwork.ItemLeftworkList) { 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)) return operation.Failed("وارد کردن تاریخ پایان کار اجباری است "); if (_leftWorkRepository.Exists(x => x.StartWorkDate == start && 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.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)) return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); } } 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) { var operation = new OperationResult(); foreach (var item in leftWorkGroups) { if (item.LeftWorkViewModels != null && item.LeftWorkViewModels.Count() > 0) { foreach (var item2 in item.LeftWorkViewModels) { 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 => x.StartWorkDate > start && left == null && x.EmployeeId == commandEmployeeId && x.WorkshopId == item2.WorkshopId && x.id != item2.Id)) return operation.Failed("وارد کردن تاریخ پایان کار اجباری است "); if (_leftWorkRepository.Exists(x => x.StartWorkDate == start && x.EmployeeId == item2.EmployeeId && x.WorkshopId == item2.WorkshopId && x.id != item2.Id)) return operation.Failed("تاریخ وارد شده برابر سابقه شروع به کار قبلی است"); if (_leftWorkRepository.Exists(x => x.StartWorkDate <= start && x.LeftWorkDate == left && x.EmployeeId == item2.EmployeeId && x.WorkshopId == item2.WorkshopId && x.id != item2.Id)) return operation.Failed("شروع به کار قبلی این شخص ترک کار ندارد "); if (_leftWorkRepository.Exists(x => x.StartWorkDate <= start && x.LeftWorkDate < left && x.LeftWorkDate >= start && x.EmployeeId == item2.EmployeeId && x.WorkshopId == item2.WorkshopId && x.id != item2.Id)) return operation.Failed("تاریخ وارد شده در بازه زمانی سابقه ترک کار قبلی است"); } } } 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); } public OperationResult CheckEditLeftWork(long workshopId, long employeeId, string date, int type) { return _leftWorkRepository.CheckEditLeftWork(workshopId, employeeId, date.ToGeorgianDateTime(), type); } public AutoExtensionDto AutoExtentionEmployees(long workshopId) { return _leftWorkRepository.AutoExtentionEmployees(workshopId); } #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() { 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) .Where(x => leftWorks.Any(y => y.WorkshopId == x.WorkshopId)).ToList(); 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() { 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 }