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