public override Task <HolidaysResponse> GetByDateRange(ByDateRangeRequest request, ServerCallContext context) { HolidaysResponse response = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; try { IQueryable <Holiday> holidays = _holidaysRepository.GetByDateRange(request.From.ToDateTime().Date, request.To.ToDateTime().Date); foreach (Holiday holiday in holidays) { response.Data.Add(ToRpcModel(holiday)); } LogData logData = new() { CallSide = nameof(HolidaysService), CallerMethodName = nameof(GetByDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(logData); } catch (Exception ex) { LogData logData = new() { CallSide = nameof(HolidaysService), CallerMethodName = nameof(GetByDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); response.Status.Code = Code.UnknownError; response.Status.ErrorMessage = "An error occured while loading holidays data"; } return(Task.FromResult(response)); }
public override Task <ISalaryResponse> GetSalary(SalaryRequest request, ServerCallContext context) { ISalaryResponse response = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; try { IQueryable <Staff> staffs = request.ManagerId == 0 ? _staffRepository.GetAll() : _staffRepository.GetByManagerId(request.ManagerId); IQueryable <IGrouping <long?, Staff> > groupedStaff = staffs.Where(e => e.PersonId.HasValue) .GroupBy(e => e.PersonId); foreach (IGrouping <long?, Staff> data in groupedStaff) { SalaryResponse salaryResponse = CalculateCurrentSalary(data, request.StartDate.ToDateTime(), request.EndDate.ToDateTime()); salaryResponse = CalculateOtherPayments(salaryResponse, request.StartDate.ToDateTime(), request.EndDate.ToDateTime()); response.SalaryResponse.Add(salaryResponse); } } catch (NullReferenceException nrex) { LogData logData = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = nrex }; _logger.AddErrorLog(logData); response.Status.ErrorMessage = $"Some data has not found (type: {nrex.GetType().Name})"; response.Status.Code = Code.DataError; } catch (Exception ex) { LogData logData = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); response.Status.ErrorMessage = ex.Message; response.Status.Code = Code.UnknownError; } LogData log = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(log); return(Task.FromResult(response)); } private SalaryResponse CalculateCurrentSalary(IGrouping <long?, Staff> staff, DateTime startDate, DateTime endDate) { IQueryable <DayOff> dayOffs = _dayOffRepository.GetByDateRangeAndPersonId(startDate, endDate, staff.First().PersonId.Value); IQueryable <Holiday> holidays = _holidaysRepository.GetByDateRange(startDate, endDate); SalaryResponse response = new() { StartedOn = Timestamp.FromDateTime(staff.First().CreatedOn.ToUniversalTime()) }; double workHours = GetWorkHours(); for (DateTime current = startDate.Date; current.Date <= endDate.Date; current = current.AddDays(1)) { bool todoDay = (current.DayOfWeek == DayOfWeek.Saturday || current.DayOfWeek == DayOfWeek.Sunday) && holidays.Any(e => e.ToDoDate.HasValue && e.ToDoDate.Value.Date == current.Date); bool workDay = current.DayOfWeek != DayOfWeek.Saturday && current.DayOfWeek != DayOfWeek.Sunday && !holidays.Any(e => e.HolidayDate.Date == current.Date); Staff currentStaff = staff.OrderByDescending(e => e.CreatedOn).FirstOrDefault(e => e.CreatedOn.Date <= current.Date); if (currentStaff is not null) { Position position = _positionsRepository.Get(currentStaff.PositionId); MotivationModificator modificator = null; if (currentStaff.MotivationModificatorId.HasValue) { modificator = _motivationModificatorRepository.GetByStaffId(currentStaff.MotivationModificatorId.Value); } double rate = modificator != null ? position.HourRate * modificator.ModValue : position.HourRate; response.CurrentPosition = position.Id; response.PersonId = currentStaff.PersonId.GetValueOrDefault(); if (workDay || todoDay) { if (!dayOffs.Any(e => e.CreatedOn.Date == current.Date)) { response.Salary += workHours * rate; } else { DayOff dayOff = dayOffs.First(e => e.CreatedOn.Date == current.Date); response.DayOffs.Add(new DayOffInfo { DayOffType = (int)dayOff.DayOffType, Hours = dayOff.Hours }); if (dayOff.IsPaid) { if (dayOff.Hours < workHours) { response.Salary += dayOff.Hours * rate + (workHours - dayOff.Hours) * rate; } else { response.Salary += dayOff.Hours * rate; } } else { response.Salary += (workHours - dayOff.Hours) * rate; } } } else if (holidays.Any(e => e.HolidayDate.Date == current.Date && !e.ToDoDate.HasValue)) { response.Salary += workHours * rate; } } } return(response); } private SalaryResponse CalculateOtherPayments(SalaryResponse calculatedSalary, DateTime start, DateTime end) { IQueryable <OtherPayment> otherPayments = _otherPaymentsRepository.GetByPersonIdAndDateRange(calculatedSalary.PersonId, start, end); foreach (OtherPayment otherPayment in otherPayments) { calculatedSalary.Salary += otherPayment.Value; } return(calculatedSalary); }