From 22c1847cbed1225717819b12eadb2926640fa255 Mon Sep 17 00:00:00 2001 From: MahanCh Date: Wed, 12 Mar 2025 20:55:27 +0330 Subject: [PATCH] add condition for creating employee status --- .../LeftWorkAgg/ILeftWorkRepository.cs | 3 + .../RollCallEmployeeStatusApplication.cs | 93 +++++++++++-------- .../Repository/LeftWorkRepository.cs | 8 ++ 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/Company.Domain/LeftWorkAgg/ILeftWorkRepository.cs b/Company.Domain/LeftWorkAgg/ILeftWorkRepository.cs index 4a1f37eb..7c8951bb 100644 --- a/Company.Domain/LeftWorkAgg/ILeftWorkRepository.cs +++ b/Company.Domain/LeftWorkAgg/ILeftWorkRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using _0_Framework.Application; using _0_Framework.Domain; using CompanyManagment.App.Contracts.LeftWork; @@ -40,4 +41,6 @@ public interface ILeftWorkRepository : IRepository List GetByWorkshopIdInDates(long workshopId, DateTime startDateGr, DateTime endDateGr); LeftWorkViewModel GetByWorkshopIdEmployeeIdInDates(long workshopId, long employeeId, DateTime start, DateTime end); #endregion + + Task GetLastLeftWork(long employeeId, long workshopId); } \ No newline at end of file diff --git a/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs b/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs index 236ded26..33621db8 100644 --- a/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs +++ b/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs @@ -5,25 +5,26 @@ 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; namespace CompanyManagment.Application { - public class RollCallEmployeeStatusApplication : IRollCallEmployeeStatusApplication - { - private readonly IRollCallEmployeeStatusRepository _employeeRollCallStatusRepository; - private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; - private readonly ILeftWorkRepository _leftWorkRepository; + public class RollCallEmployeeStatusApplication : IRollCallEmployeeStatusApplication + { + private readonly IRollCallEmployeeStatusRepository _employeeRollCallStatusRepository; + private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; + private readonly ILeftWorkRepository _leftWorkRepository; private readonly IRollCallServiceRepository _rollCallServiceRepository; - public RollCallEmployeeStatusApplication(IRollCallEmployeeStatusRepository employeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, ILeftWorkRepository leftWorkRepository, IRollCallServiceRepository rollCallServiceRepository) - { - _employeeRollCallStatusRepository = employeeStatusRepository; - _rollCallEmployeeRepository = rollCallEmployeeRepository; - _leftWorkRepository = leftWorkRepository; + public RollCallEmployeeStatusApplication(IRollCallEmployeeStatusRepository employeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, ILeftWorkRepository leftWorkRepository, IRollCallServiceRepository rollCallServiceRepository) + { + _employeeRollCallStatusRepository = employeeStatusRepository; + _rollCallEmployeeRepository = rollCallEmployeeRepository; + _leftWorkRepository = leftWorkRepository; _rollCallServiceRepository = rollCallServiceRepository; } @@ -35,7 +36,7 @@ namespace CompanyManagment.Application return op.Failed("کارمند مجاز نیست"); if (!_leftWorkRepository.Exists(x => x.EmployeeId == rollCallEmployee.EmployeeId && x.WorkshopId == rollCallEmployee.WorkshopId && - x.LeftWorkDate.Date > DateTime.Now.Date && x.StartWorkDate.Date <= DateTime.Now.Date)) + (x.LeftWorkDate.Date > DateTime.Now.Date && x.StartWorkDate.Date <= DateTime.Now.Date) || x.StartWorkDate >= DateTime.Today)) return op.Failed("کارمند در کارگاه شروع به کار نکرده است"); if (_employeeRollCallStatusRepository.Exists(y => @@ -50,7 +51,21 @@ namespace CompanyManagment.Application } else { - RollCallEmployeeStatus newRecord = new(rollCallEmployee.id, DateTime.Now.Date); + var pc = new PersianCalendar(); + var startStatus = DateTime.Today; + LeftWork leftWork = + _leftWorkRepository.GetLastLeftWork(rollCallEmployee.EmployeeId, rollCallEmployee.WorkshopId).GetAwaiter().GetResult(); + + if(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); } @@ -83,7 +98,7 @@ namespace CompanyManagment.Application if (!service.Any(x => x.StartService.Date <= contractStart.Date && x.EndService.Date >= contractEnd.Date)) return false; var rollCallEmployee = - _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId,workshopId); + _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId); if (rollCallEmployee == null) return false; @@ -99,33 +114,33 @@ namespace CompanyManagment.Application } 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(); - } + { + 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); + 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(); - } + entity.Edit(cmd.StartDate, cmd.EndDate); + _employeeRollCallStatusRepository.SaveChanges(); + return op.Succcedded(); + } - public List GetAll() - { - return _employeeRollCallStatusRepository.GetAll(); - } - } + public List GetAll() + { + return _employeeRollCallStatusRepository.GetAll(); + } + } } diff --git a/CompanyManagment.EFCore/Repository/LeftWorkRepository.cs b/CompanyManagment.EFCore/Repository/LeftWorkRepository.cs index 86d00b13..ccf0449f 100644 --- a/CompanyManagment.EFCore/Repository/LeftWorkRepository.cs +++ b/CompanyManagment.EFCore/Repository/LeftWorkRepository.cs @@ -1,6 +1,7 @@ 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; @@ -627,6 +628,13 @@ public class LeftWorkRepository : RepositoryBase, ILeftWorkRepos }; } + 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;