/// <summary> /// Method to update status and goal for monthly entry /// </summary> /// <param name="targetId">The target identifier.</param> /// <param name="selectedDate">The selected date.</param> /// <param name="userName">logged in user name</param> public virtual void UpdateMonthlyActualStatusAndGoalForMonth(int targetId, DateTime selectedDate, string userName) { //get logged in user id int loggedInUserId = userRepository.GetAll().FirstOrDefault( x => x.AccountName == userName)?.Id ?? 0; var target = targetRepository.Get(targetId); //Update corresponding monthly actual var existingMonthlyActual = monthlyActualRepository.GetAll().FirstOrDefault(x => x.TargetId == targetId && x.Month == selectedDate.Month); if (existingMonthlyActual != null) { existingMonthlyActual.LastModifiedBy = loggedInUserId; existingMonthlyActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); decimal?monthlyTarget = goalCalculator.GetMonthlyGoal(targetId, selectedDate.Month); existingMonthlyActual.Status = TargetActualComparer.GetActualStatus(monthlyTarget, existingMonthlyActual.ActualValue, target.Metric.GoalTypeId); // Add the history as well existingMonthlyActual.MonthlyActualHistory.Add( ActualConverters.ConvertMonthlyActualToMonthlyActualHistory(existingMonthlyActual)); } monthlyActualRepository.Save(); }
/// <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); }
/// <summary> /// Method to update daily actual and monthly entry /// </summary> /// <param name="actualRequest">daily actual request</param> /// <param name="goalTypeId">metric goal type id</param> /// <param name="dataTypeId">metric data type id</param> /// <param name="userName">logged in user name</param> public virtual void UpdateDailyActual(ActualItem actualRequest, int goalTypeId, int dataTypeId, string userName) { //get logged in user id int loggedInUserId = userRepository.GetAll().FirstOrDefault( x => x.AccountName == userName).Id; //get existing daily actual var existingDailyActual = dailyActualRepository.Get(actualRequest.Id.Value); 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); } //update daily actual with new changes existingDailyActual.ActualValue = actualRequest.ActualValue; existingDailyActual.GoalValue = goalValueToBeSaved; existingDailyActual.Status = (actualValueToBeCompared.HasValue) ? TargetActualComparer.GetActualStatus(goalValueToBeCompared, actualValueToBeCompared, goalTypeId) : ActualStatus.NotEntered; existingDailyActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); existingDailyActual.LastModifiedBy = loggedInUserId; //add history of daily actual existingDailyActual.DailyActualHistory.Add( ActualConverters.ConvertDailyActualToDailyActualHistory(existingDailyActual)); // 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.Save(); }
/// <summary> /// Method to update monthly actual entry /// </summary> /// <param name="actualRequest">daily actual request</param> /// <param name="goalTypeId">metric goal type id</param> /// <param name="userName">logged in user name</param> public virtual void UpdateMonthlyActual(ActualItem actualRequest, int goalTypeId, string userName) { //get logged in user id int loggedInUserId = userRepository.GetAll().FirstOrDefault( x => x.AccountName == userName)?.Id ?? 0; var monthlyActual = monthlyActualRepository.Get(actualRequest.Id.Value); monthlyActual.ActualValue = actualRequest.ActualValue; monthlyActual.Status = TargetActualComparer.GetActualStatus(actualRequest.GoalValue, actualRequest.ActualValue, goalTypeId); monthlyActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); monthlyActual.LastModifiedBy = loggedInUserId; monthlyActual.MonthlyActualHistory.Add( ActualConverters.ConvertMonthlyActualToMonthlyActualHistory(monthlyActual)); monthlyActualRepository.Save(); }
/// <summary> /// Create MonthlyActual entity from ActualEntry request /// </summary> /// <param name="actualRequest">Actual entry</param> /// <param name="goalTypeId">Goal type id</param> /// <param name="loggedInUserId">Logged in user id</param> /// <returns></returns> private MonthlyActual CreateMonthlyActual(ActualItem actualRequest, int goalTypeId, int loggedInUserId) { DateTime curTimestamp = TimeZoneUtility.GetCurrentTimestamp(); var monthlyActual = new MonthlyActual() { Month = actualRequest.Date.Month, ActualValue = actualRequest.ActualValue, Status = TargetActualComparer.GetActualStatus(actualRequest.GoalValue, actualRequest.ActualValue, goalTypeId), TargetId = actualRequest.TargetId, CreatedOn = curTimestamp, LastModifiedOn = curTimestamp, CreatedBy = loggedInUserId, LastModifiedBy = loggedInUserId }; monthlyActual.MonthlyActualHistory = new List <MonthlyActualHistory>() { ActualConverters.ConvertMonthlyActualToMonthlyActualHistory(monthlyActual) }; return(monthlyActual); }
/// <summary> /// Update existing monthly actual while adding or updating daily actuals /// </summary> /// <param name="existingActual">existing monthly actual</param> /// <param name="actualUpdRequest">Actual update request</param> /// <param name="goalTypeId">goal type id</param> /// <param name="dataTypeId">data type id</param> /// <param name="loggedInUserId">logged in user id</param> /// <returns>Updated monthly actual</returns> public virtual MonthlyActual UpdateExistingMonthlyActualOfDailyActual( MonthlyActual existingActual, ActualItem actualUpdRequest, int goalTypeId, int dataTypeId, int loggedInUserId) { //set monthly actual value as sum of daily actuals in case //metric data type is amount/whole number if (dataTypeId == Constants.DataTypeAmount || dataTypeId == Constants.DataTypeWholeNumber || dataTypeId == Constants.DataTypeDecimalNumber) { if (actualUpdRequest.ActualValue != null) { existingActual.ActualValue = FindSumOfDailyActualsExcludingCurrentEntryDate (actualUpdRequest.TargetId, actualUpdRequest.Date) + actualUpdRequest.ActualValue.Value; } } else { existingActual.ActualValue = actualUpdRequest.ActualValue; } existingActual.LastModifiedBy = loggedInUserId; existingActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); decimal?monthlyTarget = goalCalculator.GetMonthlyGoal(actualUpdRequest.TargetId, actualUpdRequest.Date.Month); existingActual.Status = TargetActualComparer.GetActualStatus(monthlyTarget, existingActual.ActualValue, goalTypeId); // Add the history as well existingActual.MonthlyActualHistory.Add( ActualConverters.ConvertMonthlyActualToMonthlyActualHistory(existingActual)); return(existingActual); }
/// <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); }
/// <summary> /// Method to update status and goal for daily actual and monthly entry /// </summary> /// <param name="targetId">The target identifier.</param> /// <param name="selectedDate">The selected date.</param> /// <param name="userName">logged in user name</param> public virtual void UpdateDailyActualStatusAndGoalForMonth(int targetId, DateTime selectedDate, string userName) { //get logged in user id. Default NdmsAdmin int loggedInUserId = userRepository.GetAll().FirstOrDefault( x => x.AccountName == userName)?.Id ?? 0; //get existing daily actual var existingDailyActuals = dailyActualRepository.GetAll().Where(x => x.TargetId == targetId && x.Date.Month == selectedDate.Month && x.Date.Year == selectedDate.Year)?.ToList(); if (existingDailyActuals == null || !existingDailyActuals.Any()) { return; } var target = targetRepository.Get(targetId); foreach (var dailyActual in existingDailyActuals) { decimal?actualValueToBeCompared = dailyActual.ActualValue; decimal?goalValueToBeCompared; decimal?goalValueToBeSaved; dailyActual.GoalValue = goalCalculator.GetDailyGoal(dailyActual.TargetId, dailyActual.Date.Month, dailyActual.Date.Day); goalValueToBeCompared = goalValueToBeSaved = dailyActual.GoalValue; //get cumulative actual & goal value that needs to be compared in case of cumulative plotting if (target.GraphPlottingMethodId == Constants.GraphPlottingMethodCumulative) { actualValueToBeCompared = actualCalculator.CalculateCumulativeActual( dailyActual.TargetId, dailyActual.Date, dailyActual.ActualValue); goalValueToBeCompared = goalCalculator.CalculateCumulativeGoal( target, dailyActual.Date); } //update daily actual with new changes dailyActual.GoalValue = goalValueToBeSaved; dailyActual.Status = (actualValueToBeCompared.HasValue) ? TargetActualComparer.GetActualStatus(goalValueToBeCompared, actualValueToBeCompared, target.Metric.GoalTypeId) : ActualStatus.NotEntered; dailyActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); dailyActual.LastModifiedBy = loggedInUserId; //add history of daily actual dailyActual.DailyActualHistory.Add( ActualConverters.ConvertDailyActualToDailyActualHistory(dailyActual)); } //Update corresponding monthly actual var existingMonthlyActual = monthlyActualRepository.GetAll().FirstOrDefault(x => x.TargetId == targetId && x.Month == selectedDate.Month); if (existingMonthlyActual != null) { existingMonthlyActual.LastModifiedBy = loggedInUserId; existingMonthlyActual.LastModifiedOn = TimeZoneUtility.GetCurrentTimestamp(); decimal?monthlyTarget = goalCalculator.GetMonthlyGoal(targetId, selectedDate.Month); existingMonthlyActual.Status = TargetActualComparer.GetActualStatus(monthlyTarget, existingMonthlyActual.ActualValue, target.Metric.GoalTypeId); // Add the history as well existingMonthlyActual.MonthlyActualHistory.Add( ActualConverters.ConvertMonthlyActualToMonthlyActualHistory(existingMonthlyActual)); } dailyActualRepository.Save(); }