ContractService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Microsoft.EntityFrameworkCore;
  2. using MTWorkHR.Application.Identity;
  3. using MTWorkHR.Application.Mapper;
  4. using MTWorkHR.Application.Models;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Core.UnitOfWork;
  7. using MTWorkHR.Application.Services.Interfaces;
  8. using MTWorkHR.Core.Entities;
  9. using System.Linq.Dynamic.Core;
  10. using Microsoft.AspNetCore.Identity;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using System.Linq;
  13. namespace MTWorkHR.Application.Services
  14. {
  15. public class ContractService : BaseService<Contract, ContractDto, ContractDto>, IContractService
  16. {
  17. private readonly IUnitOfWork _unitOfWork;
  18. private readonly IUserService _userService;
  19. private readonly GlobalInfo _globalInfo;
  20. private readonly IOrderAllocationService _orderAllocationService;
  21. public ContractService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo, IOrderAllocationService orderAllocationService) : base(unitOfWork)
  22. {
  23. _unitOfWork = unitOfWork;
  24. _userService = userService;
  25. _globalInfo = globalInfo;
  26. _orderAllocationService = orderAllocationService;
  27. }
  28. public override async Task<ContractDto> GetById(long id)
  29. {
  30. var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(id);
  31. var response = MapperObject.Mapper.Map<ContractDto>(entity);
  32. response.WorkingDays = entity.WorkingDays != null ? entity.WorkingDays.Split(",").ToList() : null;
  33. return response;
  34. }
  35. public override async Task<ContractDto> Create(ContractDto input)
  36. {
  37. var entity = MapperObject.Mapper.Map<Contract>(input);
  38. if (entity is null)
  39. {
  40. throw new AppException(ExceptionEnum.MapperIssue);
  41. }
  42. entity.WorkingDays = input.WorkingDays!=null? string.Join(",", input.WorkingDays): null;
  43. var team = await _unitOfWork.Contract.AddAsync(entity);
  44. await _unitOfWork.CompleteAsync();
  45. var response = MapperObject.Mapper.Map<ContractDto>(team);
  46. return response;
  47. }
  48. //public override async Task<ContractDto> Update(ContractDto input)
  49. //{
  50. // var entity = MapperObject.Mapper.Map<Contract>(input);
  51. // if (entity is null)
  52. // {
  53. // throw new AppException(ExceptionEnum.MapperIssue);
  54. // }
  55. // var contract = await _unitOfWork.Contract.upd(entity);
  56. // await _unitOfWork.CompleteAsync();
  57. // var response = Mapper.MapperObject.Mapper.Map<ContractDto>(entity);
  58. // return response;
  59. //}
  60. public async Task<PagingResultDto<ContractDto>> GetAll(ContractPagingInputDto PagingInputDto)
  61. {
  62. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  63. var query = res.Item1;
  64. if (_globalInfo.UserType != UserTypeEnum.Business)
  65. {
  66. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  67. }
  68. if (PagingInputDto.Filter != null)
  69. {
  70. var filter = PagingInputDto.Filter;
  71. query = query.Where(u => u.JobDescription.Contains(filter));
  72. }
  73. if (PagingInputDto.ContractStatusId != null)
  74. {
  75. query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
  76. }
  77. if (PagingInputDto.ContractTypeId != null)
  78. {
  79. query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
  80. }
  81. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  82. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  83. var total = await query.CountAsync();
  84. var list = MapperObject.Mapper
  85. .Map<IList<ContractDto>>(await page.ToListAsync());
  86. var teamUsersList = await _unitOfWork.TeamUser.GetAllWithChildrenAsync();
  87. var vacationAllocations = await _unitOfWork.OrderAllocation.GetAllAsync();
  88. foreach (var item in list)
  89. {
  90. if (item.UserId != null)
  91. {
  92. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  93. if (user != null)
  94. {
  95. item.EmployeeName = user.FirstName + " " + user.LastName;
  96. item.EmployeeEmail = user.Email;
  97. //___________Get Teams
  98. var TeamsList = teamUsersList.Item1.Where(t => t.AssignedUserId == item.UserId).Select(t => GlobalInfo.lang == "en" ? t.Team.NameEn : t.Team.NameAr);
  99. item.Teams = TeamsList == null ? "" : string.Join(",", TeamsList);
  100. //_____________Get vacation balance
  101. var remainVacations = vacationAllocations.Item1.FirstOrDefault(t => t.EmployeeId == item.UserId && t.ContractId == item.Id);
  102. item.Vacations = remainVacations == null ? "" : remainVacations.NumberOfDays + " / " + item.VacationDays;
  103. }
  104. }
  105. }
  106. var response = new PagingResultDto<ContractDto>
  107. {
  108. Result = list,
  109. Total = total
  110. };
  111. return response;
  112. }
  113. public async Task<bool> ChangeStatus(long contractId, int statusId)
  114. {
  115. var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
  116. if (entity == null)
  117. throw new AppException(ExceptionEnum.RecordNotExist);
  118. entity.ContractStatusId = statusId;
  119. bool result = true;
  120. if (statusId == (int)ContractStatusEnum.Approved)
  121. {
  122. var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
  123. addVacationAllocation(entity.VacationDays.HasValue ? entity.VacationDays.Value : 0, entity.UserId, contractId);
  124. if (!updatedSuccess) {
  125. result = false;
  126. }
  127. }
  128. return result;
  129. }
  130. private async void addVacationAllocation(int noVacationDays, string employeeId, long contractId)
  131. {
  132. await _orderAllocationService.Create(new OrderAllocationDto { ContractId = contractId, EmployeeId = employeeId, OrderTypeId = 1, LeaveTypeId = 1, NumberOfDays = noVacationDays, Period = DateTime.Now.Year });
  133. var orderTypes = await _unitOfWork.OrderType.GetAllAsync();
  134. var leaveTypes = await _unitOfWork.LeaveType.GetAllAsync();
  135. foreach(var orderType in orderTypes.Item1)
  136. {
  137. foreach (var leaveType in leaveTypes.Item1)
  138. {
  139. if(orderType.Id !=1 && leaveType.Id != 1 && (leaveType.DefaultDays> 0 || orderType.DefaultDays > 0))
  140. {
  141. await _orderAllocationService.Create(new OrderAllocationDto
  142. {
  143. ContractId = contractId,
  144. EmployeeId = employeeId,
  145. OrderTypeId =(int) orderType.Id,
  146. LeaveTypeId = (int)leaveType.Id,
  147. NumberOfDays = leaveType.DefaultDays > 0 ? leaveType.DefaultDays : orderType.DefaultDays,
  148. Period = DateTime.Now.Year }
  149. );
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }