/// <summary> /// Gets the last day with target for previous month. /// </summary> /// <param name="target">The target.</param> /// <param name="monthlyTarget">The monthly target.</param> /// <param name="yearId">The year.</param> private int GetLastDayWithTargetForPreviousMonth(Target target, MonthlyTarget monthlyTarget, int yearId) { 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.RolledUpGoalValue != null) .OrderByDescending(x => x.Day) .FirstOrDefault(); } else { dailyTarget = monthlyTarget.DailyTargets .Where(x => x.MaxGoalValue != null) .OrderByDescending(x => x.Day) .FirstOrDefault(); } if (dailyTarget != null) { day = dailyTarget.Day; } else { day = holidayCalculator.GetLastWorkingDayOfMonthForTarget(target, monthlyTarget.Month, yearId).Day; } return(day); }
/// <summary> /// Calculates the cumulative goal for a day for old targets /// </summary> /// <param name="target">Target entity</param> /// <param name="selectedDate">Day for which goal is required</param> /// <returns></returns> private decimal?CalculateCumulativeGoalForLegendTargets(Target target, DateTime selectedDate) { int year = selectedDate.Year; int month = selectedDate.Month; DateTime effectiveStartDate = new DateTime(year, month, 1); if (target.EffectiveStartDate > effectiveStartDate) { effectiveStartDate = target.EffectiveStartDate; } // Get monthly target var monthlyTarget = target.MonthlyTargets.FirstOrDefault( x => x.Month == selectedDate.Month); // If selected date is last working day of month it will just return the max goal value for the month. if (selectedDate == holidayCalculator.GetLastWorkingDayOfMonthForTarget(target, month, year)) { return(monthlyTarget?.MaxGoalValue); } //get daily goal decimal? perDayGoal = GetDailyGoal(target, selectedDate); decimal? dailyTargetsTillSelDate = null; decimal? dailyActualGoalValueTillSelectedDate = null; int? countActualEnteredTillDate = null; List <int> dailyTargetDaysTillDate = null; //get number of holidays till the selected date int holidaysTillSelDate = holidayCalculator.CountHolidaysBetweenDaysOfMonth(target.Id, target.ScorecardId, effectiveStartDate, selectedDate); //get number of days for which daily target is entered till selected day int?dailyTargetDaysTillSelDate = monthlyTarget.DailyTargets?.Where (x => x.Day <= selectedDate.Day && x.MaxGoalValue.HasValue).Count(); //get total number of effective days between selectedDate and //target effective start date excluding holidays and daily target entered days if (dailyTargetDaysTillSelDate != null) { // get days having daily target entry dailyTargetDaysTillDate = monthlyTarget.DailyTargets?.Where (x => x.Day <= selectedDate.Day && x.MaxGoalValue.HasValue).Select(x => x.Day).ToList(); // get number of actual entries till date, excluding the days having daily targets countActualEnteredTillDate = dailyActualRepository.GetAll() .Where(x => x.TargetId == target.Id && x.Date < selectedDate && x.Date >= effectiveStartDate && x.ActualValue != null && !dailyTargetDaysTillDate.Any(y => y == x.Date.Day)).Count(); } else { countActualEnteredTillDate = dailyActualRepository.GetAll() .Where(x => x.TargetId == target.Id && x.Date < selectedDate && x.Date >= effectiveStartDate && x.ActualValue != null).Count(); } int effectiveDays = (selectedDate.Day - effectiveStartDate.Day + 1) - (holidaysTillSelDate + (dailyTargetDaysTillSelDate ?? 0) + (countActualEnteredTillDate ?? 0)); if (dailyTargetDaysTillSelDate != null) { dailyTargetsTillSelDate = monthlyTarget.DailyTargets?.Where (x => x.Day <= selectedDate.Day && x.MaxGoalValue.HasValue).Sum(x => x.MaxGoalValue); } if (countActualEnteredTillDate != null) { // Get the sum of goal value entered till date, excluding days having daily targets if (dailyTargetDaysTillSelDate != null) { dailyActualGoalValueTillSelectedDate = dailyActualRepository.GetAll() .Where(x => x.TargetId == target.Id && x.Date < selectedDate && x.Date >= effectiveStartDate && !dailyTargetDaysTillDate.Any(y => y == x.Date.Day)) .Sum(x => x.GoalValue); } else { dailyActualGoalValueTillSelectedDate = dailyActualRepository.GetAll() .Where(x => x.TargetId == target.Id && x.Date < selectedDate && x.Date >= effectiveStartDate) .Sum(x => x.GoalValue); } } //sum up daily targets till selectedDate, calculated goal value in daily actuals and effective days goal to get cumulative goal var cumulativeGoal = (dailyTargetsTillSelDate ?? 0) + (dailyActualGoalValueTillSelectedDate ?? 0) + (perDayGoal * effectiveDays); return((cumulativeGoal.HasValue && target.Metric.DataTypeId == Constants.DataTypeWholeNumber) ? Math.Floor(cumulativeGoal.Value) : cumulativeGoal); }