Ejemplo n.º 1
0
        /// <summary>
        /// Method to Add or Update Daily Target
        /// </summary>
        /// <param name="monthlyTarget">The monthly target.</param>
        /// <param name="date">The date.</param>
        /// <param name="loggedInUserId">The logged in user identifier.</param>
        public virtual void AddOrUpdateDailyTarget(MonthlyTarget monthlyTarget, DateTime date, int loggedInUserId)
        {
            var currentDate = TimeZoneUtility.GetCurrentTimestamp();
            var dailyTarget = dailyTargetRepository.GetAll().FirstOrDefault(x => x.MonthlyTargetId == monthlyTarget.Id && x.Day == date.Day);

            //  add update delete

            if (dailyTarget == null)
            {
                var dailyTargetDate = new DailyTarget()
                {
                    MonthlyTargetId = monthlyTarget.Id,
                    Day             = date.Day,
                    MaxGoalValue    = monthlyTarget.DailyRate,
                    CreatedOn       = currentDate,
                    CreatedBy       = loggedInUserId,
                    LastModifiedOn  = currentDate,
                    LastModifiedBy  = loggedInUserId
                };
                dailyTargetRepository.AddOrUpdate(dailyTargetDate);
            }
            else
            {
                dailyTarget.MaxGoalValue   = monthlyTarget.DailyRate;
                dailyTarget.LastModifiedBy = loggedInUserId;
                dailyTarget.LastModifiedOn = currentDate;
                dailyTargetRepository.AddOrUpdate(dailyTarget);
            }

            dailyTargetRepository.Save();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the last day with target for current month.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="monthlyTarget">The monthly target.</param>
        private int GetLastDayWithTargetForCurrentMonth(Target target, MonthlyTarget monthlyTarget)
        {
            bool        isRolledUpTarget = false;
            var         currentDate      = TimeZoneUtility.GetCurrentTimestamp();
            var         day         = currentDate.Day;
            DailyTarget dailyTarget = new DailyTarget();

            if (target.CascadedMetricsTrackingMethodId.HasValue)
            {
                // check whether cascaded metrics tracking method is rolled up targets
                isRolledUpTarget = (target.CascadedMetricsTrackingMethodId.Value == (int)CascadedMetricsTrackingMethod.RolledUpTargets && target.IsCascaded);
            }
            if (isRolledUpTarget)
            {
                dailyTarget = monthlyTarget.DailyTargets
                              .Where(x => x.Day <= currentDate.Day && (x.RolledUpGoalValue != null))
                              .OrderByDescending(x => x.Day)
                              .FirstOrDefault();
            }
            else
            {
                dailyTarget = monthlyTarget.DailyTargets
                              .Where(x => x.Day <= currentDate.Day && (x.MaxGoalValue != null))
                              .OrderByDescending(x => x.Day)
                              .FirstOrDefault();
            }
            if (dailyTarget != null)
            {
                day = dailyTarget.Day;
            }
            return(day);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks if target exists for holidays.
        /// </summary>
        /// <param name="monthlyTarget">The monthly target.</param>
        /// <param name="scorecardId">The scorecard identifier.</param>
        /// <param name="targetYear">The target year.</param>
        /// <returns>true if target exists, otherwise false</returns>
        public bool CheckIfTargetExistsForHolidays(MonthlyTarget monthlyTarget, int scorecardId, int targetYear)
        {
            bool isHolidayTargetExists = false;

            if (monthlyTarget.DailyTargets.Any(x => x.MaxGoalValue.HasValue && holidayCalculator.CheckIfDateIsaHoliday(scorecardId, new DateTime(targetYear, monthlyTarget.Month, x.Day))))
            {
                isHolidayTargetExists = true;
            }

            return(isHolidayTargetExists);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Updates the existing monthly target.
        /// </summary>
        /// <param name="existingMonthlyTarget">The existing monthly target.</param>
        /// <param name="targetValue">The target value.</param>
        /// <param name="targetEntryDate">The target entry date.</param>
        /// <param name="loggedInUserId">The logged in user identifier.</param>
        /// <returns></returns>
        private MonthlyTarget UpdateExistingMonthlyRollupTarget(MonthlyTarget existingMonthlyTarget, int loggedInUserId)
        {
            DateTime curTimestamp = TimeZoneUtility.GetCurrentTimestamp();

            existingMonthlyTarget.LastModifiedBy = loggedInUserId;
            existingMonthlyTarget.LastModifiedOn = curTimestamp;
            var monthlyTargetHistory = TargetConverters.ConvertMonthlyTargetToMonthlyTargetHistory(existingMonthlyTarget);

            monthlyTargetHistory.TargetId = existingMonthlyTarget.TargetId;
            existingMonthlyTarget.MonthlyTargetHistory.Add(monthlyTargetHistory);
            return(existingMonthlyTarget);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Convert MonthlyTargetItem data structure to MonthlyTarget
        /// </summary>
        /// <param name="monthlyTarget">Input MonthlyTargetItem</param>
        /// <param name="loggedInUserId">Logged In UserId</param>
        /// <returns>MonthlyTarget DS</returns>
        public static MonthlyTarget ConvertMonthlyTargetItemToMonthlyTargetEntity(
            MonthlyTargetItem monthlyTarget, int loggedInUserId)
        {
            DateTime      curTimestamp = TimeZoneUtility.GetCurrentTimestamp();
            MonthlyTarget mthlyTarget  = new MonthlyTarget();

            mthlyTarget.Month            = monthlyTarget.Month.Id;
            mthlyTarget.MaxGoalValue     = monthlyTarget.GoalValue;
            mthlyTarget.DailyRate        = monthlyTarget.DailyRateValue;
            mthlyTarget.StretchGoalValue = monthlyTarget.StretchGoalValue;
            mthlyTarget.CreatedOn        = curTimestamp;
            mthlyTarget.LastModifiedOn   = curTimestamp;
            mthlyTarget.CreatedBy        = loggedInUserId; //Need to update with logged in user in future
            mthlyTarget.LastModifiedBy   = loggedInUserId; //Need to update with logged in user in future
            return(mthlyTarget);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Convert MonthlyTarget data structure to MonthlyTargetHistory
 /// </summary>
 /// <param name="monthlyTarget">Input MonthlyTarget DS</param>
 /// <returns>MonthlyTarget DS</returns>
 public static MonthlyTargetHistory ConvertMonthlyTargetToMonthlyTargetHistory(
     MonthlyTarget monthlyTarget)
 {
     return(new MonthlyTargetHistory()
     {
         MonthlyTarget = monthlyTarget,
         Target = monthlyTarget.Target,
         Month = monthlyTarget.Month,
         DailyRate = monthlyTarget.DailyRate,
         MaxGoalValue = monthlyTarget.MaxGoalValue,
         StretchGoalValue = monthlyTarget.StretchGoalValue,
         RolledUpGoalValue = monthlyTarget.RolledUpGoalValue,
         CreatedOn = monthlyTarget.CreatedOn,
         LastModifiedOn = monthlyTarget.LastModifiedOn,
         CreatedBy = monthlyTarget.CreatedBy,
         LastModifiedBy = monthlyTarget.LastModifiedBy
     });
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Method to convert monthly target entity to monthly target item DTO
        /// </summary>
        /// <param name="monthlyTarget">MonthlyTarget Entity</param>
        /// <param name="monthItem">MonthItem DTO</param>
        /// <returns></returns>
        public static MonthlyTargetItem ConvertMonthlyTargetToMonthlyTargetItemDTO
            (MonthlyTarget monthlyTarget, MonthItem monthItem)
        {
            var currentDate = TimeZoneUtility.GetCurrentTimestamp();

            var monthlyTargetItem = new MonthlyTargetItem()
            {
                Id                     = monthlyTarget.Id,
                Month                  = monthItem,
                GoalValue              = monthlyTarget.MaxGoalValue,
                DailyRateValue         = monthlyTarget.DailyRate,
                StretchGoalValue       = monthlyTarget.StretchGoalValue,
                HasManualTarget        = monthlyTarget.DailyTargets.Any(x => x.IsManual),
                HasRolledUpDailyTarget = monthlyTarget.DailyTargets.Any(x => x.RolledUpGoalValue != null),
                HasDailyTarget         = monthlyTarget.DailyTargets.Any(x => x.MaxGoalValue != null),
                IsPastMonth            = (monthItem.Id < (currentDate.Month - 1) && monthItem.Year == currentDate.Year) || monthItem.Year < currentDate.Year,
                RolledupGoalValue      = monthlyTarget.RolledUpGoalValue
            };

            return(monthlyTargetItem);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Convert DailyTargetItem DS to DailyTarget
        /// </summary>
        /// <param name="dailyTarget">Input DailyTargetItem</param>
        /// <param name="monthlyTarget">Corresponding monthly target</param>
        /// <param name="loggedInUserId">Logged In UserId</param>
        /// <returns>DailyTarget DS</returns>
        public static DailyTarget ConvertDailyTargetItemToDailyTargetEntity(
            DailyTargetItem dailyTarget,
            MonthlyTarget monthlyTarget,
            int loggedInUserId)
        {
            DateTime    curTimestamp = TimeZoneUtility.GetCurrentTimestamp();
            DailyTarget daily        = new DailyTarget();

            daily.MonthlyTarget      = monthlyTarget;
            daily.Day                = dailyTarget.Day;
            daily.MaxGoalValue       = dailyTarget.GoalValue;
            daily.IsManual           = dailyTarget.IsManual;
            daily.CreatedOn          = curTimestamp;
            daily.LastModifiedOn     = curTimestamp;
            daily.CreatedBy          = loggedInUserId; //Need to update with logged in user in future
            daily.LastModifiedBy     = loggedInUserId; //Need to update with logged in user in future
            daily.DailyTargetHistory = new List <DailyTargetHistory>()
            {
                ConvertDailyTargetToDailyTargetHistory(daily)
            };
            return(daily);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates the monthly target.
        /// </summary>
        /// <param name="targetId">The target identifier.</param>
        /// <param name="targetValue">The target value.</param>
        /// <param name="targetEntryDate">The target entry date.</param>
        /// <param name="loggedInUserId">The logged in user identifier.</param>
        /// <returns></returns>
        private MonthlyTarget CreateMonthlyRollupTarget(int targetId, int month, int loggedInUserId)
        {
            DateTime curTimestamp  = TimeZoneUtility.GetCurrentTimestamp();
            var      monthlyTarget = new MonthlyTarget
            {
                TargetId       = targetId,
                Month          = month,
                CreatedBy      = loggedInUserId,
                CreatedOn      = curTimestamp,
                LastModifiedBy = loggedInUserId,
                LastModifiedOn = curTimestamp
            };

            var monthlyTargetHistory = TargetConverters.ConvertMonthlyTargetToMonthlyTargetHistory(monthlyTarget);

            monthlyTargetHistory.TargetId      = targetId;
            monthlyTarget.MonthlyTargetHistory = new List <MonthlyTargetHistory>
            {
                monthlyTargetHistory
            };

            return(monthlyTarget);
        }
Ejemplo n.º 10
0
        private IList <DailyTargetItem> DistributeDailyTarget(Target currentRollupTarget, MonthlyTarget monthlyTarget, int year)
        {
            bool isDailyTargetExists = (monthlyTarget?.DailyTargets.Count(x => x.MaxGoalValue.HasValue) ?? 0) > 0;
            bool isTargetOnHoliday   = targetModifier.CheckIfTargetExistsForHolidays(monthlyTarget, currentRollupTarget.ScorecardId, year);

            if (!isDailyTargetExists || isTargetOnHoliday)
            {
                List <DailyTargetItem> dailyTargetItemList = targetModifier.DistributeExistingDailyTarget(
                    new GenerateDailyTargetsRequest
                {
                    ScorecardId         = currentRollupTarget.ScorecardId,
                    YearId              = currentRollupTarget.CalendarYearId,
                    MonthId             = monthlyTarget.Month,
                    MetricId            = currentRollupTarget.MetricId,
                    EffectiveStartDate  = currentRollupTarget.EffectiveStartDate,
                    TargetEntryMethodId = currentRollupTarget.TargetEntryMethodId.Value,
                    EffectiveEndDate    = currentRollupTarget.EffectiveEndDate,
                    MonthlyGoalValue    = monthlyTarget.MaxGoalValue,
                    DailyRateValue      = monthlyTarget.DailyRate,
                }, year).ToList();
                return(dailyTargetItemList);
            }

            return(null);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Calculates daily goal considering metric datatype, total days in month and
        /// total number of holidays
        /// </summary>
        /// <param name="target">Corresponding Target entity</param>
        /// <param name="monthlyTarget">Corresponding Monthly target entity</param>
        /// <param name="selectedDate">Day for which goal is required</param>
        /// <returns>Calculated goal</returns>
        private decimal?CalculateDailyGoal(Target target, MonthlyTarget monthlyTarget,
                                           DateTime selectedDate)
        {
            decimal?dailyTargetValue = null;

            // If metric data type is whole number/amount/decimal number get the goal value by
            // dividing monthly target by no of remaining working days
            if (target.Metric.DataTypeId != Constants.DataTypePercentage)
            {
                int     year  = selectedDate.Year;
                int     month = selectedDate.Month;
                decimal sumOfTargetsInDailyActual  = 0;
                int     countActualEnteredTillDate = 0;
                //set start date and end date for the month as the start and end date can be
                //any date in the month
                DateTime monthStartDate = new DateTime(year, month, 1);
                DateTime monthEndDate   = new DateTime(year, month, DateTime.DaysInMonth
                                                           (year, month));
                if (target.EffectiveStartDate > monthStartDate)
                {
                    monthStartDate = target.EffectiveStartDate;
                }
                if (target.EffectiveEndDate < monthEndDate)
                {
                    monthEndDate = target.EffectiveEndDate;
                }

                if (target.GraphPlottingMethodId == Constants.GraphPlottingMethodCumulative)
                {
                    var dailyTargetDays = monthlyTarget.DailyTargets.Where(x => x.MaxGoalValue.HasValue).Select(x => x.Day);
                    if (dailyTargetDays?.Count() > 0)
                    {
                        // Count of actual entries till date
                        countActualEnteredTillDate = dailyActualRepository.GetAll()
                                                     .Where(x => x.TargetId == target.Id && x.Date <= monthEndDate && x.Date >= monthStartDate && x.ActualValue != null && !dailyTargetDays.Any(y => y == selectedDate.Day))?.Count() ?? 0;

                        if (countActualEnteredTillDate > 0)
                        {
                            // Get the sum of goal value entered for previous dates, excluding the daily targets
                            sumOfTargetsInDailyActual = dailyActualRepository.GetAll()
                                                        .Where(x => x.TargetId == target.Id && x.Date <= monthEndDate && x.Date >= monthStartDate && !dailyTargetDays.Any(y => y == selectedDate.Day))
                                                        .Sum(x => x.GoalValue) ?? 0;
                        }
                    }
                    else
                    {
                        // Count of actual entries till date
                        countActualEnteredTillDate = dailyActualRepository.GetAll()
                                                     .Where(x => x.TargetId == target.Id && x.Date <= monthEndDate && x.Date >= monthStartDate && x.ActualValue != null)?.Count() ?? 0;

                        if (countActualEnteredTillDate > 0)
                        {
                            // Get the sum of goal value entered for previous dates, excluding the daily targets
                            sumOfTargetsInDailyActual = dailyActualRepository.GetAll()
                                                        .Where(x => x.TargetId == target.Id && x.Date <= monthEndDate && x.Date >= monthStartDate)
                                                        .Sum(x => x.GoalValue) ?? 0;
                        }
                    }
                }

                //Get total holidays between the dates
                int totalNumberofHolidays = holidayCalculator.CountHolidaysBetweenDaysOfMonth(target.Id,
                                                                                              target.ScorecardId, monthStartDate, monthEndDate);

                //get count total effective days in a month
                int totalDaysInMonth = (int)(monthEndDate - monthStartDate).TotalDays + 1;

                // Calculate the remaining days by subtracting the sum of holidays and days
                // for which daily targets are entered
                // and the actual entered days from the total days in a month
                int?remainingDays = (totalDaysInMonth) - (totalNumberofHolidays
                                                          + monthlyTarget.DailyTargets?.Count(x => x.MaxGoalValue.HasValue) + countActualEnteredTillDate);

                if (remainingDays > 0)
                {
                    // Get the sum of target of already entered days
                    var sumOfDailyTargets = monthlyTarget.DailyTargets.Sum(x => x.MaxGoalValue);
                    dailyTargetValue = (monthlyTarget.MaxGoalValue - sumOfDailyTargets - sumOfTargetsInDailyActual)
                                       / remainingDays;
                }
            }
            // Daily goal is same as monthly goal in case metric data type is percentage
            else
            {
                dailyTargetValue = monthlyTarget.MaxGoalValue;
            }
            return(dailyTargetValue);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Method to convert Target item DTO to Target entity
        /// </summary>
        /// <param name="targetItem">Target item DTO to be converted</param>
        /// <param name="Parent target Id">Identifier of the parent target</param>
        /// <param name="loggedInUserId">Logged In UserId</param>
        /// <returns>Target entity</returns>
        public static Target ConvertTargetItemDTOToTarget(TargetItem targetItem,
                                                          int?parentTargetId, int loggedInUserId)
        {
            DateTime curTimestamp = TimeZoneUtility.GetCurrentTimestamp();
            var      target       = new Target()
            {
                ScorecardId                     = targetItem.ScorecardId,
                KPIId                           = targetItem.KPIId.Value,
                MetricId                        = targetItem.MetricId.Value,
                MetricType                      = targetItem.MetricType,
                EffectiveStartDate              = targetItem.EffectiveStartDate,
                EffectiveEndDate                = targetItem.EffectiveEndDate,
                CascadeFromParent               = targetItem.CascadeFromParent,
                ParentTargetId                  = parentTargetId,
                IsStretchGoalEnabled            = targetItem.IsStretchGoalEnabled ?? false,
                TrackingMethodId                = targetItem.TrackingMethodId ?? Constants.TrackingMethodDaily,
                TargetEntryMethodId             = targetItem.TargetEntryMethodId,
                GraphPlottingMethodId           = targetItem.GraphPlottingMethodId,
                MTDPerformanceTrackingMethodId  = targetItem.MTDPerformanceTrackingMethodId,
                CascadedMetricsTrackingMethodId = targetItem.CascadedMetricsTrackingMethodId,
                AnnualTarget                    = targetItem.AnnualTarget,
                IsCascaded                      = false,
                IsActive                        = true,
                IsCopiedMetric                  = false,
                CalendarYearId                  = targetItem.CalendarYearId.Value,
                CreatedOn                       = curTimestamp,
                LastModifiedOn                  = curTimestamp,
                CreatedBy                       = loggedInUserId, //Need to update with logged in user in future
                LastModifiedBy                  = loggedInUserId, //Need to update with logged in user in future
            };

            target.TargetHistory = new List <TargetHistory>()
            {
                ConvertTargetToTargetHistory(target)
            };

            // Iterate monthly targets and insert
            List <MonthlyTarget> monthlyTargets = new List <MonthlyTarget>();

            foreach (var monthlyTarget in targetItem.MonthlyTargets)
            {
                if (monthlyTarget.GoalValue != null || monthlyTarget.DailyRateValue != null)
                {
                    MonthlyTarget mthlyTarget = ConvertMonthlyTargetItemToMonthlyTargetEntity
                                                    (monthlyTarget, loggedInUserId);
                    mthlyTarget.MonthlyTargetHistory = new List <MonthlyTargetHistory>()
                    {
                        ConvertMonthlyTargetToMonthlyTargetHistory(mthlyTarget)
                    };

                    mthlyTarget.DailyTargets = new List <DailyTarget>();
                    if (monthlyTarget.DailyTargets != null)
                    {
                        foreach (var dailyTargetItem in monthlyTarget.DailyTargets)
                        {
                            if (!dailyTargetItem.IsHoliday && !dailyTargetItem.IsOutofRange)
                            {
                                DailyTarget dailyTarget =
                                    ConvertDailyTargetItemToDailyTargetEntity(dailyTargetItem,
                                                                              mthlyTarget, loggedInUserId);
                                mthlyTarget.DailyTargets.Add(dailyTarget);
                            }
                        }
                    }

                    monthlyTargets.Add(mthlyTarget);
                }
            }
            target.MonthlyTargets = monthlyTargets;
            return(target);
        }