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) { //موقت // دادمهرگستر 11 * //585 کاشی گالری سرامیس (بابک ابراهیمی ) * //آموزشگاه ملل 604 * //کاریابی ملل 605 * //368 پیتزا امیر آماده سازی //367 پیتزا امیر رستوران //286 مرکز توان بخشی رسالت * DateTime start1404 = new DateTime(2025, 03, 21); bool skipRollCallByWorkshopId = false; if (contractStart < start1404) { skipRollCallByWorkshopId = workshopId is 11 or 585 or 604 or 605 or 368 or 367 or 286; } else { skipRollCallByWorkshopId = workshopId is 368 or 367; } //#if DEBUG // skipRollCallByWorkshopId = workshopId is 11 or 585 or 604 or 605 or 368 or 367; //#endif if (skipRollCallByWorkshopId) return false; // 42550 مصطفی مقدس نژاد فومنی bool skipRollCallByEmployeeId = employeeId is 42550; if (skipRollCallByEmployeeId) return false; //9211 محسا تازه if (employeeId == 9211 && contractStart >= start1404) return false; var service = _rollCallServiceRepository.GetAllServiceByWorkshopId(workshopId); if (!service.Any(x => x.StartService.Date <= contractStart.Date && x.EndService.Date >= contractEnd.Date)) return false; var rollCallEmployee = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId); if (rollCallEmployee == null) return false; return _employeeRollCallStatusRepository.Exists(x => x.RollCallEmployeeId == rollCallEmployee.Id && x.StartDate.Date <= contractStart.Date && x.EndDate.Date >= contractEnd.Date); } 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(); } } }