ContractService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 != null && 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. foreach (var item in list)
  87. {
  88. if (item.UserId != null)
  89. {
  90. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  91. if (user != null)
  92. {
  93. item.EmployeeName = user.FirstName + " " + user.LastName;
  94. item.EmployeeEmail = user.Email;
  95. }
  96. }
  97. }
  98. var response = new PagingResultDto<ContractDto>
  99. {
  100. Result = list,
  101. Total = total
  102. };
  103. return response;
  104. }
  105. public async Task<PagingResultDto<ContractDto>> GetAllForHr(ContractPagingInputDto PagingInputDto)
  106. {
  107. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  108. var query = res.Item1;
  109. if (_globalInfo.UserType != UserTypeEnum.Business)
  110. {
  111. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  112. }
  113. if (PagingInputDto.Filter != null)
  114. {
  115. var filter = PagingInputDto.Filter;
  116. query = query.Where(u => u.JobDescription.Contains(filter));
  117. }
  118. if (PagingInputDto.ContractStatusId != null)
  119. {
  120. query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
  121. }
  122. if (PagingInputDto.ContractTypeId != null)
  123. {
  124. query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
  125. }
  126. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  127. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  128. var total = await query.CountAsync();
  129. var list = MapperObject.Mapper
  130. .Map<IList<ContractDto>>(await page.ToListAsync());
  131. var teamUsersList = await _unitOfWork.TeamUser.GetAllWithChildrenAsync();
  132. var vacationAllocations = await _unitOfWork.OrderAllocation.GetAllAsync();
  133. foreach (var item in list)
  134. {
  135. if (item.UserId != null)
  136. {
  137. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  138. if (user != null)
  139. {
  140. item.EmployeeName = user.FirstName + " " + user.LastName;
  141. item.EmployeeEmail = user.Email;
  142. //___________Get Teams
  143. var TeamsList = teamUsersList.Item1.Where(t => t.AssignedUserId == item.UserId).Select(t => GlobalInfo.lang == "en" ? t.Team.NameEn : t.Team.NameAr);
  144. item.Teams = TeamsList == null ? "" : string.Join(",", TeamsList);
  145. //_____________Get vacation balance
  146. var remainVacations = vacationAllocations.Item1.FirstOrDefault(t => t.EmployeeId == item.UserId && t.ContractId == item.Id);
  147. item.Vacations = remainVacations == null ? "" : remainVacations.NumberOfDays + " / " + item.VacationDays;
  148. }
  149. }
  150. }
  151. var response = new PagingResultDto<ContractDto>
  152. {
  153. Result = list,
  154. Total = total
  155. };
  156. return response;
  157. }
  158. public async Task<bool> ChangeStatus(long contractId, int statusId)
  159. {
  160. var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
  161. if (entity == null)
  162. throw new AppException(ExceptionEnum.RecordNotExist);
  163. entity.ContractStatusId = statusId;
  164. bool result = true;
  165. if (statusId == (int)ContractStatusEnum.Approved)
  166. {
  167. var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
  168. addVacationAllocation(entity.VacationDays.HasValue ? entity.VacationDays.Value : 0, entity.UserId, contractId);
  169. if (!updatedSuccess) {
  170. result = false;
  171. }
  172. }
  173. return result;
  174. }
  175. private async Task addVacationAllocation(int noVacationDays, string employeeId, long contractId)
  176. {
  177. try
  178. {
  179. await _orderAllocationService.Create(new OrderAllocationDto { ContractId = contractId, EmployeeId = employeeId, OrderTypeId = 1, LeaveTypeId = 1, NumberOfDays = noVacationDays, Period = DateTime.Now.Year });
  180. // var orderTypes = await _unitOfWork.OrderType.GetAllAsync();
  181. // var leaveTypes = await _unitOfWork.LeaveType.GetAllAsync();
  182. //foreach (var orderType in orderTypes.Item1)
  183. //{
  184. // foreach (var leaveType in leaveTypes.Item1)
  185. // {
  186. // if (orderType.Id != 1 && leaveType.Id != 1 && (leaveType.DefaultDays > 0 || orderType.DefaultDays > 0))
  187. // {
  188. // await _orderAllocationService.Create(new OrderAllocationDto
  189. // {
  190. // ContractId = contractId,
  191. // EmployeeId = employeeId,
  192. // OrderTypeId = (int)orderType.Id,
  193. // LeaveTypeId = (int)leaveType.Id,
  194. // NumberOfDays = leaveType.DefaultDays > 0 ? leaveType.DefaultDays : orderType.DefaultDays,
  195. // Period = DateTime.Now.Year
  196. // }
  197. // );
  198. // }
  199. // }
  200. //}
  201. }
  202. catch(Exception ex)
  203. {
  204. throw ;
  205. }
  206. }
  207. }
  208. }