Пример #1
0
        /// <summary>
        /// Method to add new actual entry
        /// </summary>
        /// <param name="actualRequest">Daily actual request</param>
        /// <param name="goalTypeId">Metric goal type id</param>
        /// <param name="dataTypeId">Data type id</param>
        /// <param name="userName">Logged in user name</param>
        /// <returns>Newly created Entity Id</returns>
        public virtual int AddDailyActual(ActualItem actualRequest, int goalTypeId,
                                          int dataTypeId, string userName)
        {
            // Get logged in user id
            int loggedInUserId = userRepository.GetAll().FirstOrDefault(
                x => x.AccountName == userName).Id;

            var     target = targetRepository.Get(actualRequest.TargetId);
            decimal?actualValueToBeCompared = actualRequest.ActualValue;
            decimal?goalValueToBeCompared   = actualRequest.GoalValue;
            decimal?goalValueToBeSaved      = actualRequest.GoalValue;

            //get cumulative actual & goal value that needs to be compared in case of cumulative plotting
            if (target.GraphPlottingMethodId == Constants.GraphPlottingMethodCumulative)
            {
                actualValueToBeCompared = actualCalculator.CalculateCumulativeActual(
                    actualRequest.TargetId, actualRequest.Date, actualRequest.ActualValue);
                goalValueToBeCompared = goalCalculator.CalculateCumulativeGoal(
                    target, actualRequest.Date);
                goalValueToBeSaved = goalCalculator.GetDailyGoal(actualRequest.TargetId,
                                                                 actualRequest.Date.Month, actualRequest.Date.Day);
            }

            DateTime curTimestamp = TimeZoneUtility.GetCurrentTimestamp();
            // Creating daily actual entity
            var dailyActual = new DailyActual()
            {
                Date        = actualRequest.Date,
                GoalValue   = goalValueToBeSaved,
                ActualValue = actualRequest.ActualValue,
                Status      = (actualValueToBeCompared.HasValue) ? TargetActualComparer.GetActualStatus(
                    goalValueToBeCompared, actualValueToBeCompared, goalTypeId)
                    : ActualStatus.NotEntered,
                TargetId       = actualRequest.TargetId,
                CreatedOn      = curTimestamp,
                LastModifiedOn = curTimestamp,
                CreatedBy      = loggedInUserId,
                LastModifiedBy = loggedInUserId
            };

            dailyActual.DailyActualHistory = new List <DailyActualHistory>()
            {
                ActualConverters.ConvertDailyActualToDailyActualHistory(dailyActual)
            };

            // Add/Update corresponding monthly actual
            var monthlyActual = AdjustMonthlyActual(actualRequest, goalTypeId,
                                                    dataTypeId, loggedInUserId);

            // If it is a new monthly actual entry, add explicitly
            if (monthlyActual.Id == 0)
            {
                monthlyActualRepository.AddOrUpdate(monthlyActual);
            }

            dailyActualRepository.AddOrUpdate(dailyActual);
            dailyActualRepository.Save();
            return(dailyActual.Id);
        }
Пример #2
0
        /// <summary>
        /// Get Month To date performance to determine month Color
        /// </summary>
        /// <param name="target"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public MonthToDatePerformanceItem GetMonthToDatePerformance(Target target, int year, int month)
        {
            if (target.MTDPerformanceTrackingMethodId == null)
            {
            }
            var mTDPerformance = new MonthToDatePerformanceItem
            {
                Year   = year,
                Month  = month,
                Status = ActualStatus.NotEntered
            };

            var currentDate = TimeZoneUtility.GetCurrentTimestamp();
            var day         = currentDate.Day;

            if ((month > currentDate.Month && currentDate.Year == year) || year > currentDate.Year)
            {
                return(mTDPerformance);
            }
            else if ((currentDate.Month > month && currentDate.Year == year) || (currentDate.Year > year))
            {
                day = holidayCalculator.GetLastWorkingDayOfMonthForTarget(target, month, year).Day;
            }

            var selectedDate = new DateTime(year, month, day).Date;

            var dailyActualsList = dailyActualRepository.GetAll()
                                   .Where(x =>
                                          x.TargetId == target.Id &&
                                          x.Date.Month == month && x.ActualValue != null &&
                                          x.Status != ActualStatus.Holiday)
                                   ?.ToList();

            if (dailyActualsList.Any())
            {
                var latestActualDate = dailyActualsList.OrderBy(x => x.Date).LastOrDefault().Date;

                switch ((MTDPerformanceTrackingMethod)target.MTDPerformanceTrackingMethodId)
                {
                case MTDPerformanceTrackingMethod.Cumulative:
                {
                    mTDPerformance.GoalValue   = goalCalculator.CalculateCumulativeGoal(target, latestActualDate);
                    mTDPerformance.ActualValue = dailyActualsList.Sum(x => x.ActualValue);
                }
                break;

                case MTDPerformanceTrackingMethod.Average:
                {
                    mTDPerformance.GoalValue   = goalCalculator.CalculateAverageMTDGoal(target, selectedDate.Month);
                    mTDPerformance.ActualValue = Math.Round(dailyActualsList.Average(x => x.ActualValue).Value, 2, MidpointRounding.AwayFromZero);

                    if (target.Metric.DataTypeId == Constants.DataTypeWholeNumber)
                    {
                        mTDPerformance.ActualValue = Math.Round(mTDPerformance.ActualValue.Value, MidpointRounding.AwayFromZero);
                    }
                }
                break;

                case MTDPerformanceTrackingMethod.Latest:
                {
                    mTDPerformance.GoalValue = dailyActualsList?.OrderBy(x => x.Date).LastOrDefault(x => x.GoalValue != null)?.GoalValue;
                    if (!mTDPerformance.GoalValue.HasValue)
                    {
                        mTDPerformance.GoalValue = target.IsCascaded && target.CascadedMetricsTrackingMethodId.Value == (int)CascadedMetricsTrackingMethod.RolledUpTargets ?
                                                   target.MonthlyTargets?.FirstOrDefault(x => x.Month == month)?.DailyTargets?.OrderBy(d => d.Day).LastOrDefault(y => y.Day <= latestActualDate.Day)?.RolledUpGoalValue :
                                                   target.MonthlyTargets?.FirstOrDefault(x => x.Month == month)?.DailyTargets?.OrderBy(d => d.Day).LastOrDefault(y => y.Day <= latestActualDate.Day)?.MaxGoalValue;
                    }

                    mTDPerformance.ActualValue = dailyActualsList?.FirstOrDefault(x => x.Date == latestActualDate.Date)?.ActualValue;
                }
                break;

                default: break;
                }
                //set status if actual and goal has values
                if (mTDPerformance.ActualValue.HasValue && mTDPerformance.GoalValue.HasValue)
                {
                    mTDPerformance.Status = TargetActualComparer.GetActualStatus(mTDPerformance.GoalValue, mTDPerformance.ActualValue, target.Metric.GoalTypeId);
                }
            }
            return(mTDPerformance);
        }