public JsonResult addNValues(Int16 indicatorID, Int16 fiscalYear)
        {
            var indicatorNValue = db.Indicators.FirstOrDefault(x => x.Indicator_N_Value == true && x.Indicator_N_Value_ID == indicatorID);
            var title = "N-Values: " + db.Indicators.FirstOrDefault(x => x.Indicator_ID == indicatorID).Indicator;
            if (indicatorNValue == null)
            {
                indicatorNValue = new Indicators()
                {
                    Indicator_N_Value = true,
                    Indicator_N_Value_ID = indicatorID,
                    Indicator = title
                };
                db.Indicators.Add(indicatorNValue);
                db.SaveChanges();
                ModelState.Clear();
            }
            var indicatorNValueMap = new Indicator_CoE_Maps(){
                Fiscal_Year = fiscalYear,
                CoE_ID = 0,
            };
            if (!db.Indicator_CoE_Maps.Any(x => x.Indicator.Indicator_N_Value_ID == indicatorID && x.Fiscal_Year == fiscalYear))
            {
                indicatorNValueMap.Indicator_ID = indicatorNValue.Indicator_ID;
                db.Indicator_CoE_Maps.Add(indicatorNValueMap);
                db.SaveChanges();
            }

            return Json(new { indicatorID = indicatorNValue.Indicator_ID, mapID = indicatorNValueMap.Map_ID, coeID = indicatorNValueMap.CoE_ID, areaID = indicatorNValueMap.Indicator.Area_ID }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult newIndicatorAtPR(Int16 fiscalYear, Int16 areaID, Int16 coeID, Int16 indicatorID, Int16? newIndicatorID)
        {
            Indicators indicator = new Indicators();

            if (newIndicatorID.HasValue)
            {
                indicator = db.Indicators.FirstOrDefault(x => x.Indicator_ID == newIndicatorID);
            }
            else
            {
                indicator = new Indicators();
                indicator.Area_ID = areaID;
                indicator.Indicator = "";
                db.Indicators.Add(indicator);
                db.SaveChanges();
            }

            var newMap = new Indicator_CoE_Maps();
            newMap.Indicator_ID = indicator.Indicator_ID;

            int number = 0;
            if (indicatorID != 0)
            {
                number = db.Indicator_CoE_Maps.Where(x => x.CoE_ID == coeID && x.Fiscal_Year == x.Fiscal_Year).FirstOrDefault(x => x.Indicator_ID == indicatorID).Number + 1;
            }
            newMap.Number = (Int16)number;

            var allMaps = db.Indicator_CoE_Maps.ToList();
            foreach (var map in allMaps.OrderBy(x => x.Number).Where(x => x.CoE_ID == coeID && x.Fiscal_Year == x.Fiscal_Year && x.Number >= number))
            {
                map.Number++;
                db.Entry(map).State = EntityState.Modified;
                db.SaveChanges();
            }
            newMap.CoE_ID = coeID;
            newMap.Fiscal_Year = fiscalYear;
            db.Indicator_CoE_Maps.Add(newMap);
            db.SaveChanges();

            return Json(new { indicatorID = indicator.Indicator_ID, mapID = newMap.Map_ID, newAreaID = indicator.Area_ID }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult newIndicatorAtPRdbl(Int16 fiscalYear, Int16 areaID, Int16 coeID, Int16 coeID2)
        {
            Indicators indicator = new Indicators();
            Indicators indicator2 = new Indicators();

            indicator = db.Indicators.Create();
            indicator2 = db.Indicators.Create();
            db.Indicators.Add(indicator);
            db.Indicators.Add(indicator2);
            db.SaveChanges();
            indicator.Area_ID = areaID;
            indicator2.Area_ID = areaID;

            var type = indicator.GetType();
            var property = type.GetProperty(Helpers.FiscalYear.FYStrFull("FY_Color_ID", fiscalYear));
            property.SetValue(indicator, Convert.ChangeType(1, property.PropertyType), null);
            var type2 = indicator2.GetType();
            var property2 = type.GetProperty(Helpers.FiscalYear.FYStrFull("FY_Color_ID", fiscalYear));
            property2.SetValue(indicator2, Convert.ChangeType(1, property2.PropertyType), null);

            indicator.Indicator = "";
            indicator.Indicator_Link = indicator2.Indicator_ID;
            db.Entry(indicator).State = EntityState.Modified;
            db.SaveChanges();
            indicator2.Indicator = "";
            indicator2.Indicator_Link = indicator.Indicator_ID;
            db.Entry(indicator2).State = EntityState.Modified;
            db.SaveChanges();

            var newMap = new Indicator_CoE_Maps();
            newMap.Indicator_ID = indicator.Indicator_ID;
            newMap.CoE_ID = coeID;
            newMap.Fiscal_Year = fiscalYear;
            db.Indicator_CoE_Maps.Add(newMap);
            db.SaveChanges();

            var newMap2 = new Indicator_CoE_Maps();
            newMap2.Indicator_ID = indicator2.Indicator_ID;
            newMap2.CoE_ID = coeID2;
            newMap2.Fiscal_Year = fiscalYear;
            db.Indicator_CoE_Maps.Add(newMap2);
            db.SaveChanges();

            var colorID = (Int16)newMap.Indicator.GetType().GetProperty(FiscalYear.FYStrFull("FY_", fiscalYear) + "Color_ID").GetValue(newMap.Indicator, null);

            return Json(new { indicatorID = indicator.Indicator_ID, mapID = newMap.Map_ID, newAreaID = indicator.Area_ID, colorID = colorID,
                              indicatorID2 = indicator2.Indicator_ID, mapID2 = newMap2.Map_ID, newAreaID2 = indicator2.Area_ID, colorID2 = colorID
                            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult editCoEs(String CoE_ID_Filter)
        {
            var viewModelItems = db.CoEs.ToArray();
            var viewModel = viewModelItems.OrderBy(x => x.CoE_ID).Select(x => new CoEsViewModel
            {
                CoE_ID = x.CoE_ID,
                CoE = x.CoE,
                CoE_Abbr = x.CoE_Abbr,
                CoE_Notes = x.CoE_Notes,
                CoE_Subtitle = x.CoE_Subtitle,
                CoE_Type = x.CoE_Type,
                CoE_Title = x.CoE_Title
            }).ToList();
            if (Request.IsAjaxRequest())
            {
                if (CoE_ID_Filter == "")
                {
                    var newCoE = db.CoEs.Create();
                    newCoE.CoE_Abbr = "";
                    db.CoEs.Add(newCoE);
                    db.SaveChanges();

                    var newIndicator = new Indicators();
                    var newAreaCoEMap = new Area_CoE_Maps();
                    var newIndicatorCoEMap = new Indicator_CoE_Maps();

                    var allFiscalYear = db.Fiscal_Years.ToList();
                    var allAreas = db.Areas.ToList();
                    foreach (var fiscalYear in allFiscalYear)
                    {
                        foreach (var area in allAreas)
                        {
                            for (var i = 1; i <= 5; i++)
                            {
                                newIndicator = db.Indicators.Create();
                                db.Indicators.Add(newIndicator);
                                db.SaveChanges();
                                newIndicator.Area_ID = area.Area_ID;
                                db.Entry(newIndicator).State = EntityState.Modified;
                                db.SaveChanges();

                                newIndicatorCoEMap = new Indicator_CoE_Maps
                                {
                                    CoE_ID = newCoE.CoE_ID,
                                    Fiscal_Year = fiscalYear.Fiscal_Year_Code,
                                    Indicator_ID = newIndicator.Indicator_ID
                                };
                                db.Indicator_CoE_Maps.Add(newIndicatorCoEMap);
                                db.SaveChanges();
                            }
                            newAreaCoEMap = new Area_CoE_Maps{
                                Area_ID = area.Area_ID,
                                CoE_ID = newCoE.CoE_ID,
                                Fiscal_Year = fiscalYear.Fiscal_Year_Code,
                                Objective = ""
                            };
                            db.Area_CoE_Maps.Add(newAreaCoEMap);
                            db.SaveChanges();
                        }
                    }

                    viewModel = new List<CoEsViewModel>();
                    var newViewModelItem = new CoEsViewModel
                    {
                        CoE_ID = newCoE.CoE_ID,
                        CoE = newCoE.CoE,
                        CoE_Abbr = newCoE.CoE_Abbr,
                        CoE_Notes = newCoE.CoE_Notes,
                        CoE_Subtitle = newCoE.CoE_Subtitle,
                        CoE_Type = newCoE.CoE_Type,
                        CoE_Title = newCoE.CoE_Title
                    };
                    viewModel.Add(newViewModelItem);

                    return Json(viewModel, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(viewModel.Where(x => x.CoE_ID.ToString().Contains(CoE_ID_Filter == null ? "" : CoE_ID_Filter)), JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return View(viewModel);
            }
        }
        public JsonResult newIndicatorAtPR(Int16 fiscalYear, Int16 areaID, Int16 coeID, Int16 indicatorID, Int16? newIndicatorID)
        {
            Indicators indicator = new Indicators();

            if (newIndicatorID.HasValue)
            {
                indicator = db.Indicators.FirstOrDefault(x => x.Indicator_ID == newIndicatorID);
            }
            else
            {
                indicator = db.Indicators.Create();//new Indicators();
                db.Indicators.Add(indicator);
                db.SaveChanges();
                indicator.Area_ID = areaID;

                var type = indicator.GetType();
                var property = type.GetProperty(Helpers.FiscalYear.FYStrFull("FY_Color_ID", fiscalYear));
                property.SetValue(indicator, Convert.ChangeType(1, property.PropertyType), null);

                indicator.Indicator = "";
                db.Entry(indicator).State = EntityState.Modified;
                //db.Indicators.Add(indicator);
                db.SaveChanges();
            }

            var newMap = new Indicator_CoE_Maps();
            newMap.Indicator_ID = indicator.Indicator_ID;

            int number = 0;
            if (indicatorID != 0)
            {
                if (db.Indicator_CoE_Maps.Where(x => x.CoE_ID == coeID && x.Fiscal_Year == fiscalYear).FirstOrDefault(x => x.Indicator_ID == indicatorID) != null)
                {
                    number = db.Indicator_CoE_Maps.Where(x => x.CoE_ID == coeID && x.Fiscal_Year == fiscalYear).FirstOrDefault(x => x.Indicator_ID == indicatorID).Number + 1;
                }
                else
                {
                    number = 0;
                }
            }
            newMap.Number = (Int16)number;

            var allMaps = db.Indicator_CoE_Maps.ToList();
            foreach (var map in allMaps.OrderBy(x => x.Number).Where(x => x.CoE_ID == coeID && x.Fiscal_Year == x.Fiscal_Year && x.Number >= number))
            {
                map.Number++;
                db.Entry(map).State = EntityState.Modified;
                db.SaveChanges();
            }
            newMap.CoE_ID = coeID;
            newMap.Fiscal_Year = fiscalYear;
            db.Indicator_CoE_Maps.Add(newMap);
            db.SaveChanges();

            var colorID = (Int16)newMap.Indicator.GetType().GetProperty(FiscalYear.FYStrFull("FY_", fiscalYear) + "Color_ID").GetValue(newMap.Indicator, null);

            return Json(new { indicatorID = indicator.Indicator_ID, mapID = newMap.Map_ID, newAreaID = indicator.Area_ID, colorID = colorID}, JsonRequestBehavior.AllowGet);
        }