Ejemplo n.º 1
0
        private bool IsWithinTarget(ProjectIndicator projectIndicator, IntermediateIndicator intermediateIndicator)
        {
            var projectIndicatorTarget = projectIndicator.Target;

            var projectIntermediates = projectIndicator.IntermediateIndicators;

            decimal totalItermediateTargets = 0;

            //loop through intermediate

            foreach (var intermediate in projectIntermediates)
            {
                totalItermediateTargets += intermediate.Target;
            }

            totalItermediateTargets += intermediateIndicator.Target;

            if (totalItermediateTargets > projectIndicatorTarget)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public ActionResult Save(ProjectIndicator projectIndicator)
        {
            if (ModelState.IsValid)
            {
                //check if the indicator is already available

                if (this._context.ProjectIndicators.Any(i => i.Name == projectIndicator.Name))
                {
                    ModelState.AddModelError("Name", "The indicator by this name is already present in the system");

                    //return View("Create", projectIndicator);
                }



                //check if the request to save is for a new entry or edit

                if (projectIndicator.Id == 0)
                {
                    //add a new indicator

                    this._context.ProjectIndicators.Add(projectIndicator);
                    this._context.SaveChanges();

                    TempData["SuccessMessage"] = string.Format("New indicator {0} created successfully", projectIndicator.Name);

                    //return RedirectToAction("Index");
                }

                //get the indicator having the id sent

                var indicatorDb = this._context.ProjectIndicators.Single(i => i.Id == projectIndicator.Id);

                if (indicatorDb == null)
                {
                    return(HttpNotFound());
                }

                indicatorDb.Name     = projectIndicator.Name;
                indicatorDb.BaseLine = projectIndicator.BaseLine;
                indicatorDb.Target   = projectIndicator.Target;
                indicatorDb.Unit     = projectIndicator.Unit;


                //save the changes

                this._context.SaveChanges();

                TempData["SuccessMessage"] = string.Format("Indicator {0} has been updated", projectIndicator.Name);

                return(RedirectToAction("Details", "Project", new { id = projectIndicator.ProjectId }));
            }

            //create a list of unit types

            List <SelectListItem> data = new List <SelectListItem>();

            List <string> units = new List <string>()
            {
                "Number", "Percentage", "Metric ton", "Hectare(Ha)", "Yes/No"
            };

            foreach (var unit in units)
            {
                data.Add(new SelectListItem()
                {
                    Value = unit,
                    Text  = unit,
                });
            }

            ViewBag.Unit = data;
            return(View("Create", projectIndicator));
        }