ContractService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #region HR data____________________________
  106. public async Task<PagingResultDto<ContractDto>> GetAllForHr(ContractPagingInputDto PagingInputDto)
  107. {
  108. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  109. var query = res.Item1;
  110. if (_globalInfo.UserType != UserTypeEnum.Business)
  111. {
  112. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  113. }
  114. if (PagingInputDto.Filter != null)
  115. {
  116. var filter = PagingInputDto.Filter;
  117. query = query.Where(u => u.JobDescription.Contains(filter));
  118. }
  119. if (PagingInputDto.ContractStatusId != null)
  120. {
  121. query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
  122. }
  123. if (PagingInputDto.ContractTypeId != null)
  124. {
  125. query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
  126. }
  127. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  128. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  129. var total = await query.CountAsync();
  130. var list = MapperObject.Mapper
  131. .Map<IList<ContractDto>>(await page.ToListAsync());
  132. var teamUsersList = await _unitOfWork.TeamUser.GetAllWithChildrenAsync();
  133. var vacationAllocations = await _unitOfWork.OrderAllocation.GetAllAsync();
  134. foreach (var item in list)
  135. {
  136. if (item.UserId != null)
  137. {
  138. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  139. if (user != null)
  140. {
  141. item.EmployeeName = user.FirstName + " " + user.LastName;
  142. item.EmployeeEmail = user.Email;
  143. //___________Get Teams
  144. var TeamsList = teamUsersList.Item1.Where(t => t.AssignedUserId == item.UserId).Select(t => GlobalInfo.lang == "en" ? t.Team.NameEn : t.Team.NameAr);
  145. item.Teams = TeamsList == null ? "" : string.Join(",", TeamsList);
  146. //_____________Get vacation balance
  147. var remainVacations = vacationAllocations.Item1.FirstOrDefault(t => t.EmployeeId == item.UserId && t.ContractId == item.Id);
  148. item.Vacations = remainVacations == null ? "" : remainVacations.NumberOfDays + " / " + item.VacationDays;
  149. }
  150. }
  151. }
  152. var response = new PagingResultDto<ContractDto>
  153. {
  154. Result = list,
  155. Total = total
  156. };
  157. return response;
  158. }
  159. public async Task<ContractHRDto> GetByIdHRDetails(long contractId)
  160. {
  161. var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(contractId);
  162. if (entity != null)
  163. {
  164. var response = MapperObject.Mapper.Map<ContractHRDto>(entity);
  165. response.WorkingDays = entity.WorkingDays != null ? entity.WorkingDays.Split(",").ToList() : null;
  166. //--------------------
  167. var user = await _userService.GetUserWithAttachmentById(entity.UserId);
  168. response.EmployeeName = user.FirstName + " " + user.LastName;
  169. response.EmployeeEmail = user.Email;
  170. //___________Get Teams
  171. var teamUsersList = await _unitOfWork.TeamUser.GetUserTeams(entity.UserId);
  172. var teamsList = MapperObject.Mapper.Map<List<TeamDto>>(teamUsersList.Item1);
  173. response.TeamList = teamsList;
  174. //_____________Get vacation balance
  175. var vacationAllocations = await _unitOfWork.OrderAllocation.GetUserAllocations(entity.UserId, 1, 1, entity.Id, DateTime.Now.Year);
  176. var remainVacations = vacationAllocations;
  177. response.Vacations = remainVacations == null ? "" : remainVacations.NumberOfDays + " / " + entity.VacationDays;
  178. //__-----------Order Requests----
  179. var orderRequestsList = await _unitOfWork.OrderRequest.GetAllUserOrdersAsync(entity.UserId, contractId);
  180. var orderList = MapperObject.Mapper.Map<List<OrderRequestDto>>(orderRequestsList.Item1);
  181. response.OrderList = orderList;
  182. /////------------------------
  183. var invoiceList = await _unitOfWork.Invoice.GetAllUserInvoices(contractId);
  184. var Invoices = MapperObject.Mapper.Map<List<InvoiceDto>>(invoiceList.Item1);
  185. response.InvoiceList = Invoices;
  186. //------------------
  187. if(entity.ContractTypeId == (int)ContractTypeEnum.EmployeeContract)
  188. {
  189. var lastInvoice = Invoices.LastOrDefault();
  190. var diffDate = DateTime.Now.Date - entity.StartDate.Value.Date;
  191. if(lastInvoice != null)
  192. {
  193. if (entity.BillingCycle == "Monthly")
  194. response.NextSalaryDate = lastInvoice.InvoiceDate.Value.AddMonths(1);
  195. else if (entity.BillingCycle == "Fortnightly")
  196. response.NextSalaryDate = lastInvoice.InvoiceDate.Value.AddDays(15);
  197. }
  198. else
  199. {
  200. if (entity.BillingCycle == "Monthly")
  201. response.NextSalaryDate = entity.StartDate.Value.AddMonths(1);
  202. else if(entity.BillingCycle == "Fortnightly")
  203. response.NextSalaryDate = entity.StartDate.Value.AddDays(15);
  204. }
  205. }
  206. return response;
  207. }
  208. return new ContractHRDto();
  209. }
  210. #endregion
  211. public async Task<bool> ChangeStatus(long contractId, int statusId)
  212. {
  213. var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
  214. if (entity == null)
  215. throw new AppException(ExceptionEnum.RecordNotExist);
  216. entity.ContractStatusId = statusId;
  217. bool result = true;
  218. if (statusId == (int)ContractStatusEnum.Approved)
  219. {
  220. var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
  221. addVacationAllocation(entity.VacationDays.HasValue ? entity.VacationDays.Value : 0, entity.UserId, contractId);
  222. if (!updatedSuccess) {
  223. result = false;
  224. }
  225. }
  226. return result;
  227. }
  228. private async Task addVacationAllocation(int noVacationDays, string employeeId, long contractId)
  229. {
  230. try
  231. {
  232. await _orderAllocationService.Create(new OrderAllocationDto { ContractId = contractId, EmployeeId = employeeId, OrderTypeId = 1, LeaveTypeId = 1, NumberOfDays = noVacationDays, Period = DateTime.Now.Year });
  233. // var orderTypes = await _unitOfWork.OrderType.GetAllAsync();
  234. // var leaveTypes = await _unitOfWork.LeaveType.GetAllAsync();
  235. //foreach (var orderType in orderTypes.Item1)
  236. //{
  237. // foreach (var leaveType in leaveTypes.Item1)
  238. // {
  239. // if (orderType.Id != 1 && leaveType.Id != 1 && (leaveType.DefaultDays > 0 || orderType.DefaultDays > 0))
  240. // {
  241. // await _orderAllocationService.Create(new OrderAllocationDto
  242. // {
  243. // ContractId = contractId,
  244. // EmployeeId = employeeId,
  245. // OrderTypeId = (int)orderType.Id,
  246. // LeaveTypeId = (int)leaveType.Id,
  247. // NumberOfDays = leaveType.DefaultDays > 0 ? leaveType.DefaultDays : orderType.DefaultDays,
  248. // Period = DateTime.Now.Year
  249. // }
  250. // );
  251. // }
  252. // }
  253. //}
  254. }
  255. catch(Exception ex)
  256. {
  257. throw ;
  258. }
  259. }
  260. }
  261. }