123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using Microsoft.EntityFrameworkCore;
- using MTWorkHR.Application.Identity;
- using MTWorkHR.Application.Mapper;
- using MTWorkHR.Application.Models;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Entities;
- using System.Linq.Dynamic.Core;
- using Microsoft.AspNetCore.Identity;
- using MTWorkHR.Infrastructure.Entities;
- using System.Linq;
- namespace MTWorkHR.Application.Services
- {
- public class ContractService : BaseService<Contract, ContractDto, ContractDto>, IContractService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly IUserService _userService;
- private readonly GlobalInfo _globalInfo;
- private readonly IOrderAllocationService _orderAllocationService;
- public ContractService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo, IOrderAllocationService orderAllocationService) : base(unitOfWork)
- {
- _unitOfWork = unitOfWork;
- _userService = userService;
- _globalInfo = globalInfo;
- _orderAllocationService = orderAllocationService;
- }
- public override async Task<ContractDto> GetById(long id)
- {
- var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(id);
- var response = MapperObject.Mapper.Map<ContractDto>(entity);
- response.WorkingDays = entity.WorkingDays != null ? entity.WorkingDays.Split(",").ToList() : null;
- return response;
- }
-
- public override async Task<ContractDto> Create(ContractDto input)
- {
- var entity = MapperObject.Mapper.Map<Contract>(input);
- if (entity is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- entity.WorkingDays = input.WorkingDays!=null? string.Join(",", input.WorkingDays): null;
- var team = await _unitOfWork.Contract.AddAsync(entity);
- await _unitOfWork.CompleteAsync();
- var response = MapperObject.Mapper.Map<ContractDto>(team);
- return response;
- }
- //public override async Task<ContractDto> Update(ContractDto input)
- //{
- // var entity = MapperObject.Mapper.Map<Contract>(input);
- // if (entity is null)
- // {
- // throw new AppException(ExceptionEnum.MapperIssue);
- // }
- // var contract = await _unitOfWork.Contract.upd(entity);
- // await _unitOfWork.CompleteAsync();
- // var response = Mapper.MapperObject.Mapper.Map<ContractDto>(entity);
- // return response;
- //}
-
- public async Task<PagingResultDto<ContractDto>> GetAll(ContractPagingInputDto PagingInputDto)
- {
- var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
- var query = res.Item1;
- if (_globalInfo.UserType != UserTypeEnum.Business)
- {
- // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
- }
- if (PagingInputDto.Filter != null)
- {
- var filter = PagingInputDto.Filter;
- query = query.Where(u => u.JobDescription != null && u.JobDescription.Contains(filter));
- }
- if (PagingInputDto.ContractStatusId != null)
- {
- query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
- }
- if (PagingInputDto.ContractTypeId != null)
- {
- query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
- }
- var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
- var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
- var total = await query.CountAsync();
- var list = MapperObject.Mapper
- .Map<IList<ContractDto>>(await page.ToListAsync());
- foreach (var item in list)
- {
- if (item.UserId != null)
- {
- var user = await _userService.GetUserWithAttachmentById(item.UserId);
- if (user != null)
- {
- item.EmployeeName = user.FirstName + " " + user.LastName;
- item.EmployeeEmail = user.Email;
- }
- }
- }
- var response = new PagingResultDto<ContractDto>
- {
- Result = list,
- Total = total
- };
- return response;
- }
- public async Task<PagingResultDto<ContractDto>> GetAllForHr(ContractPagingInputDto PagingInputDto)
- {
- var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
- var query = res.Item1;
- if (_globalInfo.UserType != UserTypeEnum.Business)
- {
- // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
- }
- if (PagingInputDto.Filter != null)
- {
- var filter = PagingInputDto.Filter;
- query = query.Where(u => u.JobDescription.Contains(filter));
- }
- if (PagingInputDto.ContractStatusId != null)
- {
- query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
- }
- if (PagingInputDto.ContractTypeId != null)
- {
- query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
- }
- var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
- var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
- var total = await query.CountAsync();
- var list = MapperObject.Mapper
- .Map<IList<ContractDto>>(await page.ToListAsync());
- var teamUsersList = await _unitOfWork.TeamUser.GetAllWithChildrenAsync();
- var vacationAllocations = await _unitOfWork.OrderAllocation.GetAllAsync();
- foreach (var item in list)
- {
- if (item.UserId != null)
- {
- var user = await _userService.GetUserWithAttachmentById(item.UserId);
- if (user != null)
- {
- item.EmployeeName = user.FirstName + " " + user.LastName;
- item.EmployeeEmail = user.Email;
- //___________Get Teams
- var TeamsList = teamUsersList.Item1.Where(t => t.AssignedUserId == item.UserId).Select(t => GlobalInfo.lang == "en" ? t.Team.NameEn : t.Team.NameAr);
- item.Teams = TeamsList == null ? "" : string.Join(",", TeamsList);
- //_____________Get vacation balance
- var remainVacations = vacationAllocations.Item1.FirstOrDefault(t => t.EmployeeId == item.UserId && t.ContractId == item.Id);
- item.Vacations = remainVacations == null ? "" : remainVacations.NumberOfDays + " / " + item.VacationDays;
- }
- }
- }
- var response = new PagingResultDto<ContractDto>
- {
- Result = list,
- Total = total
- };
- return response;
- }
- public async Task<bool> ChangeStatus(long contractId, int statusId)
- {
- var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
- if (entity == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- entity.ContractStatusId = statusId;
- bool result = true;
- if (statusId == (int)ContractStatusEnum.Approved)
- {
- var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
- addVacationAllocation(entity.VacationDays.HasValue ? entity.VacationDays.Value : 0, entity.UserId, contractId);
- if (!updatedSuccess) {
- result = false;
- }
- }
- return result;
- }
- private async Task addVacationAllocation(int noVacationDays, string employeeId, long contractId)
- {
- try
- {
- await _orderAllocationService.Create(new OrderAllocationDto { ContractId = contractId, EmployeeId = employeeId, OrderTypeId = 1, LeaveTypeId = 1, NumberOfDays = noVacationDays, Period = DateTime.Now.Year });
- // var orderTypes = await _unitOfWork.OrderType.GetAllAsync();
- // var leaveTypes = await _unitOfWork.LeaveType.GetAllAsync();
- //foreach (var orderType in orderTypes.Item1)
- //{
- // foreach (var leaveType in leaveTypes.Item1)
- // {
- // if (orderType.Id != 1 && leaveType.Id != 1 && (leaveType.DefaultDays > 0 || orderType.DefaultDays > 0))
- // {
- // await _orderAllocationService.Create(new OrderAllocationDto
- // {
- // ContractId = contractId,
- // EmployeeId = employeeId,
- // OrderTypeId = (int)orderType.Id,
- // LeaveTypeId = (int)leaveType.Id,
- // NumberOfDays = leaveType.DefaultDays > 0 ? leaveType.DefaultDays : orderType.DefaultDays,
- // Period = DateTime.Now.Year
- // }
- // );
- // }
- // }
- //}
- }
- catch(Exception ex)
- {
- throw ;
- }
-
- }
- }
- }
|