//When Save button is pushed this Method will Calculate a calculated measurement if it has all of the Measurements are contained in the Calculated Measurement
        private void AddDatapointsToCalculatedMeasurements()
        {
            var calculatedMeasurements = repository.GetCalculatedMeasurements();

            var applicableDate = DateTime.Today;
            if (IsForLastMonth(applicableDate))
                applicableDate = applicableDate.AddMonths(-1);
            applicableDate = applicableDate.AddDays(-(DateTime.Today.Day - 1));

            foreach (var calculatedMeasurement in calculatedMeasurements)
            {
                var formulaString = calculatedMeasurement.Formula;
                var value = repository.CalculateMeasurement(formulaString, applicableDate);

                if (value.HasValue)
                {
                    var datapoint = new Datapoint
                    {
                        Value_AMT = (decimal)value,
                        Applicable_DT = applicableDate,
                        Created_DT = DateTime.Now,
                        Created_TM = DateTime.Now.TimeOfDay,
                        Measurement_ID = calculatedMeasurement.Measurement_ID,
                        Sbmt_By = User.Identity.Name
                    };

                    var datapointId = repository.GetDatapointId(calculatedMeasurement.Measurement_ID, applicableDate);
                    if (datapointId.HasValue)
                        datapoint.Datapoint_ID = (int)datapointId;

                    repository.UpdateAddDataPoint(datapoint);
                }
            }
            repository.SaveChanges();
        }
        public ActionResult SaveToDb()
        {
            var idReturnList = new List<int>();
            bool isChanged = false;
            var formString = Request.Params;
            var idsMapToValues = GetIdsListFromFormString(formString);
            string hasSubmitted = Request["hasSubmitted"];
            string error = "Nothing Was Changed";

            foreach (var key in idsMapToValues.Keys)
            {
                idReturnList.Add(key);
                decimal? val;
                bool valExists = idsMapToValues.TryGetValue(key, out val);
                if (valExists && val != null)
                {
                    isChanged = true;
                    var applicableDate = DateTime.Today.Date;
                    if (IsForLastMonth(applicableDate))
                        applicableDate = applicableDate.AddMonths(-1);
                    applicableDate = applicableDate.AddDays(-(DateTime.Today.Day - 1));

                    var datapoint = new Datapoint
                    {
                        Applicable_DT = applicableDate,
                        Measurement_ID = key,
                        HasSubmitted_IN = Convert.ToInt16(hasSubmitted),
                        Value_AMT = Convert.ToDecimal(val),
                        Created_DT = DateTime.Now.Date,
                        Created_TM = DateTime.Now.TimeOfDay,
                        Sbmt_By = User.Identity.Name.Substring(0,3)
                    };

                    var dId = repository.GetDatapointId(key, applicableDate);

                    //Get reason for why the datapoint did not meet a goal
                    var dataPointReason = repository.GetReasonByDataPoint(dId);
                    string goalType = repository.GetGoalInfo(datapoint.Measurement_ID, datapoint.Value_AMT);
                    if ((dataPointReason.Count == 0 && (goalType.Equals("Does Not Meet") || goalType.Equals("Sev 1"))) && hasSubmitted == "1")
                    {
                        TempData["Error"] = "All data points that don't meet goals must have a reason.";
                        return RedirectToAction("Index");
                    }

                    if (dId.HasValue)
                    {
                        datapoint.Datapoint_ID = (int)dId;
                        if (datapoint.HasSubmitted_IN == 0)
                            datapoint.HasSubmitted_IN = repository.GetSubmittedInFromId((int)dId);
                    }

                    repository.UpdateAddDataPoint(datapoint);
                }
            }

            repository.PutUserMeasurementSessionData(idReturnList);
            Session["SubmissionIdList"] = idReturnList;
            AddDatapointsToCalculatedMeasurements();

            if (isChanged)
                error = repository.SaveChanges();

            if (error.ToLower().Contains("success"))
                TempData["Success"] = "You have successfully saved data points and your selected measurements";
            else
                TempData["Error"] = error;
            if (Request["submitted"] == "true")
            {
                TempData["submitted"] = true;
            }

            return RedirectToAction("Index");
        }