using _0_Framework.Application; using Company.Domain.LeftWorkAgg; using Company.Domain.RollCallEmployeeAgg; using Company.Domain.RollCallEmployeeStatusAgg; using CompanyManagment.App.Contracts.RollCallEmployeeStatus; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Company.Domain.RollCallServiceAgg; using Company.Domain.LeftWorkTempAgg; using Company.Domain.LeftWorkInsuranceAgg; using CompanyManagment.App.Contracts.LeftWorkTemp; namespace CompanyManagment.Application { public class RollCallEmployeeStatusApplication : IRollCallEmployeeStatusApplication { private readonly IRollCallEmployeeStatusRepository _employeeRollCallStatusRepository; private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; private readonly ILeftWorkRepository _leftWorkRepository; private readonly IRollCallServiceRepository _rollCallServiceRepository; private readonly ILeftWorkTempRepository _leftWorkTempRepository; private readonly ILeftWorkInsuranceRepository _leftWorkInsuranceRepository; public RollCallEmployeeStatusApplication(IRollCallEmployeeStatusRepository employeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, ILeftWorkRepository leftWorkRepository, IRollCallServiceRepository rollCallServiceRepository, ILeftWorkTempRepository leftWorkTempRepository, ILeftWorkInsuranceRepository leftWorkInsuranceRepository) { _employeeRollCallStatusRepository = employeeStatusRepository; _rollCallEmployeeRepository = rollCallEmployeeRepository; _leftWorkRepository = leftWorkRepository; _rollCallServiceRepository = rollCallServiceRepository; _leftWorkTempRepository = leftWorkTempRepository; _leftWorkInsuranceRepository = leftWorkInsuranceRepository; } public OperationResult Create(CreateRollCallEmployeeStatus cmd) { OperationResult op = new(); RollCallEmployee rollCallEmployee = _rollCallEmployeeRepository.Get(cmd.RollCallEmployeeId); if (rollCallEmployee == null) return op.Failed("کارمند مجاز نیست"); if (!_leftWorkRepository.Exists(x => x.EmployeeId == rollCallEmployee.EmployeeId && x.WorkshopId == rollCallEmployee.WorkshopId && x.StartWorkDate <= DateTime.Now && x.LeftWorkDate > DateTime.Now) && !_leftWorkTempRepository.Exists(x => x.EmployeeId == rollCallEmployee.EmployeeId && x.WorkshopId == rollCallEmployee.WorkshopId && x.LeftWorkType == LeftWorkTempType.StartWork)) { return op.Failed("کارمند شروع به کار ندارد"); } if (_employeeRollCallStatusRepository.Exists(y => rollCallEmployee.id == y.RollCallEmployeeId && y.EndDate.Date > DateTime.Now.Date)) return op.Failed("کارمند فعال می باشد"); if (_employeeRollCallStatusRepository.Exists(y => rollCallEmployee.id == y.RollCallEmployeeId && y.EndDate.Date == DateTime.Now.Date)) { RollCallEmployeeStatus previousStatusInDate = _employeeRollCallStatusRepository.GetByRollCallEmployeeIdAndDate(cmd.RollCallEmployeeId, DateTime.Now.Date); previousStatusInDate.Edit(previousStatusInDate.StartDate, Tools.GetUndefinedDateTime()); } else { var pc = new PersianCalendar(); var startStatus = DateTime.Today; LeftWork leftWork = _leftWorkRepository.GetLastLeftWork(rollCallEmployee.EmployeeId, rollCallEmployee.WorkshopId).GetAwaiter().GetResult(); if (leftWork != null && leftWork.StartWorkDate > DateTime.Today) startStatus = leftWork.StartWorkDate; //else if(pc.GetMonth(DateTime.Today) == pc.GetMonth(leftWork.StartWorkDate)) //{ // startStatus = new DateTime(pc.GetYear(leftWork.StartWorkDate), pc.GetMonth(leftWork.StartWorkDate), // 1, pc); //} RollCallEmployeeStatus newRecord = new(rollCallEmployee.id, startStatus); _employeeRollCallStatusRepository.Create(newRecord); } _employeeRollCallStatusRepository.SaveChanges(); return op.Succcedded(); } public bool HasRollCallRecord(long employeeId, long workshopId, DateTime contractStart, DateTime contractEnd) { return _rollCallEmployeeRepository.HasRollCallRecord(employeeId, workshopId, contractStart, contractEnd).GetAwaiter().GetResult(); } public List GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr) { return _employeeRollCallStatusRepository.GetActiveByWorkshopIdInDate(workshopId, startDateGr, endDateGr); } public bool IsActiveInPeriod(long employeeId, long workshopId, DateTime startDate, DateTime endDate) { return _employeeRollCallStatusRepository.IsActiveInPeriod(employeeId, workshopId, startDate, endDate); } public OperationResult Deactivate(long id) { OperationResult op = new(); RollCallEmployeeStatus entity = _employeeRollCallStatusRepository.Get(id); if (entity == null) return op.Failed(ApplicationMessages.RecordNotFound); if (!entity.EndDate.IsDateUndefined()) return op.Failed("کارمند قبلا غیر فعال شده است"); entity.Deactivate(DateTime.Now.Date); _employeeRollCallStatusRepository.SaveChanges(); return op.Succcedded(); } public OperationResult Edit(EditRollCallEmployeeStatus cmd) { OperationResult op = new(); RollCallEmployeeStatus entity = _employeeRollCallStatusRepository.Get(cmd.Id); if (entity == null) return op.Failed(ApplicationMessages.RecordNotFound); entity.Edit(cmd.StartDate, cmd.EndDate); _employeeRollCallStatusRepository.SaveChanges(); return op.Succcedded(); } public List GetAll() { return _employeeRollCallStatusRepository.GetAll(); } public void SyncRollCallEmployeeWithLeftWork(long rollCallEmployeeId) { var rollCallEmployee = _rollCallEmployeeRepository.GetWithRollCallStatus(rollCallEmployeeId); if (rollCallEmployee == null) return; var rollCallStatus = rollCallEmployee.EmployeesStatus.MaxBy(x => x.StartDate); if (rollCallStatus == null) return; var today = DateTime.Today; var firstDayOfMonth = today.FindFirstDayOfMonthGr(); var employeeId = rollCallEmployee.EmployeeId; var workshopId = rollCallEmployee.WorkshopId; var leftWork = _leftWorkRepository.GetLastLeftWorkByEmployeeIdAndWorkshopId(workshopId, employeeId); if (leftWork == null) return; var startWork = leftWork.StartWorkDate; rollCallStatus.Edit(startWork < firstDayOfMonth ? firstDayOfMonth : startWork, rollCallStatus.EndDate); _employeeRollCallStatusRepository.SaveChanges(); } } }