/// <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 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(); }