Beispiel #1
0
        public override Task <DayOffsResponse> GetByPersonIdAndDateRange(ByPersonIdAndDateRangeRequest request, ServerCallContext context)
        {
            DayOffsResponse response = new()
            {
                Status = new BaseResponse {
                    Code = Code.Success, ErrorMessage = string.Empty
                }
            };

            try
            {
                IQueryable <DayOff> dayOffs = _dayOffRepository.GetByDateRangeAndPersonId(request.Range.From.ToDateTime(), request.Range.To.ToDateTime(), request.Person.PersonId);

                foreach (DayOff dayOff in dayOffs)
                {
                    response.Data.Add(ToRpcModel(dayOff));
                }

                LogData logData = new()
                {
                    CallSide         = nameof(DayOffsService),
                    CallerMethodName = nameof(GetByPersonIdAndDateRange),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = response
                };
                _logger.AddLog(logData);
            }
            catch (Exception ex)
            {
                LogData logData = new()
                {
                    CallSide         = nameof(DayOffsService),
                    CallerMethodName = nameof(GetByPersonIdAndDateRange),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = ex
                };
                _logger.AddErrorLog(logData);
                response.Status.Code         = Code.UnknownError;
                response.Status.ErrorMessage = "An error occured while loading day offs data";
            }
            return(Task.FromResult(response));
        }
Beispiel #2
0
        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);
        }