//Adding Issue
        //GET
        public ActionResult Add()
        {
            var obj = new Issues();
            Sites s = new Sites();
            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = s;

            return View(obj);
        }
        //Adding Requirement
        //GET
        public ActionResult Add()
        {
            var req = new Requirements();
            //sites list
            Sites s = new Sites();
            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
            req.Sites = s;

            return View(req);
        }
        public ActionResult Add()
        {
            //object
            var obj = new Utilities_Datos_Generales();
            //sites list
            Sites s = new Sites();
            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = s;

            return View(obj);
        }
        //Adding Batch
        //GET
        public ActionResult Add()
        {
            //object Batch
            var obj = new Batch();
            //sites list
            Sites s = new Sites();
            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = s;

            return View(obj);
        }
 //List All Sites
 public ActionResult AllSites()
 {
     Sites obj = new Sites();
     var sites = obj.GetAllSites();
     if (sites.Count() > 0)
     {
         return View(sites.ToList());
     }
     else
     {
         ViewBag.Message = "There are not registered sites.";
         return View();
     }
 }
        public ActionResult Add(Sites site)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;

            if (ModelState.IsValid)
            {
                //Verify if it doesn't exist with that name
                var existingSite = from s in db.Sites where s.SiteName.Equals(site.SiteName) select s;

                if (existingSite.Count() > 0)
                {
                    //the site name is already registered
                    TempData["previous"] = thisPageUrl;
                    TempData["message"] = "There is already a site registered with this name.";
                    return View("~/Views/Shared/Error.cshtml");
                }
                else
                {
                    try
                    {
                        //adding Site properties
                        site.RegisteredBy = LoggedUser();
                        site.RegisteredDate = DateTime.Now;
                        db.Sites.Add(site);
                        db.SaveChanges();
                        TempData["message"] = "The site has been registered successfully.";

                        TempData["previous"] = thisPageUrl;
                        return View("~/Views/Shared/Success.cshtml");
                    }
                    catch (Exception e)
                    {
                        TempData["previous"] = thisPageUrl;
                        TempData["message"] = "There was an error registering site.  Try again or contact administrator.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
            }
            return View();
        }
 //Search Site
 public ActionResult Search()
 {
     var obj = new Sites();
     obj.SitesList = new SelectList(obj.GetAllSites(),"SiteID","SiteName");
     return View(obj);
 }
 public ActionResult Search(Sites site)
 {
     String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
     if (!String.IsNullOrEmpty(site.SelectedSite))
     {
         int SiteID = int.Parse(site.SelectedSite);
         site = db.Sites.Find(SiteID);  //searching site
         if (site != null)
         {
             return View("Details", site);
         }
         else
         {
             //site doesn't exist
             TempData["previous"] = thisPageUrl;
             TempData["message"] = "The site you are looking for doesn't exist. Try again with another name.";
             return View("~/Views/Shared/Error.cshtml");
         }
     }
     else
     {
         ModelState.AddModelError("Site", "You must enter a valid name.");
         var obj = new Sites();
         obj.SitesList = new SelectList(obj.GetAllSites(), "SiteID", "SiteName");
         return View(obj);
     }
 }
        public ActionResult Search(Sprints sprint)
        {
            var errors = false;

            //getting site
            if (!string.IsNullOrEmpty(sprint.Batch.Sites.SelectedSite))
            {
                int SiteID = int.Parse(sprint.Batch.Sites.SelectedSite);
                Sites site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //getting batch
                    if (!string.IsNullOrEmpty(sprint.Batch.SelectedBatch))
                    {
                        int BatchID = int.Parse(sprint.Batch.SelectedBatch);
                        Batch batch = db.Batch.Find(BatchID);
                        //batch-site relation
                        if (batch != null && batch.SiteID == site.SiteID)
                        {
                            var sprints = from s in db.Sprints where s.BatchID == BatchID select s;
                            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                            batch.Sites = site;
                            sprint.Batch = batch;
                            sprint.BatchSprints = sprints.ToList();
                            ViewBag.Error = false;
                            return View(sprint);
                        }
                        else
                        {
                            TempData["previous"] = "Search";
                            TempData["message"] = "The batch you selected doesn't exist or doesn't belong to selected site. Try again with another site.";
                            return View("~/Views/Shared/Error.cshtml");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Batch", "You must select a batch.");
                        errors = true;
                    }
                }
                else
                {
                    TempData["previous"] = "Search";
                    TempData["message"] = "The site you selected doesn't exist. Try again with another site.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                ModelState.AddModelError("Site", "You must select a site.");
                ModelState.AddModelError("Batch", "You must select a batch.");
                errors = true;
            }

            if (errors)
            {
                var obj = new Sprints();
                var s = new Sites();
                var b = new Batch();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                b.Sites = s;
                obj.Batch = b;
                return View(obj);
            }
            return View();
        }
 //Update Sprint
 public ActionResult Update(string SprintID)
 {
     if (!string.IsNullOrEmpty(SprintID))
     {
         try
         {
             int id = int.Parse(SprintID);
             Sprints sprint = db.Sprints.Find(id);
             Batch batch = new Batch();
             Sites site = new Sites();
             if (sprint != null)
             {
                 var batchs = from b in db.Batch where b.BatchID == sprint.BatchID select b;
                 if (batchs.Count() > 0) { batch = batchs.First(); }
                 var sites = from s in db.Sites where s.SiteID == sprint.Batch.SiteID select s;
                 if (sites.Count() > 0) { site = sites.First(); }
                 site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                 batch.Sites = site;
                 batch.BatchList = new SelectList(batch.GetAllBatches(), "BatchID", "BatchDescription");
                 sprint.Batch = batch;
                 return View(sprint);
             }
             else
             {
                 TempData["previous"] = "Search";
                 TempData["message"] = "There was an error.  Please try again or contact administrator.";
                 return View("~/Views/Shared/Error.cshtml");
             }
         }
         catch (Exception e)
         {
             TempData["previous"] = "Search";
             TempData["message"] = "There was an error.  Please try again or contact administrator.";
             return View("~/Views/Shared/Error.cshtml");
         }
     }
     else
     {
         TempData["previous"] = "Search";
         TempData["message"] = "The sprint doesn't exist.  Please try again or contact administrator.";
         return View("~/Views/Shared/Error.cshtml");
     }
 }
        public ActionResult Add(Utilities_Datos_Generales u)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            //getting site ID
            int SiteID = 0;
            if (!string.IsNullOrEmpty(u.SelectedSite))
            {
                try
                {
                    SiteID = int.Parse(u.SelectedSite);
                    var site = db.Sites.Find(SiteID);

                    if (site != null)
                    {
                        if (!string.IsNullOrEmpty(u.AceptaAnulacion))
                        {
                            u.SiteID = SiteID;
                            u.RegisteredBy = LoggedUser();
                            u.RegisteredDate = DateTime.Now;
                            //saving database
                            db.Utilities_Datos_Generales.Add(u);
                            db.SaveChanges();

                            TempData["message"] = "The utilitie was added.";
                            return View("~/Views/Shared/Success.cshtml");
                        }
                        else
                        {
                            ModelState.AddModelError("AceptaAnulacion", "You must select an option.");
                            //object
                            var obj = new Utilities_Datos_Generales();
                            //sites list
                            Sites s = new Sites();
                            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                            obj.Sites = s;

                            return View(obj);
                        }
                    }
                    else
                    {
                        TempData["message"] = "The site you selected doesn't exist. Try again with another site.";
                        return View("~/Views/Shared/Error.cshtml");
                    }

                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error registering utility.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                ModelState.AddModelError("Site", "You must select a site.");
                //object
                var obj = new Utilities_Datos_Generales();
                //sites list
                Sites s = new Sites();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                obj.Sites = s;

                return View(obj);
            }
        }
 //Search by Site and Batches
 public ActionResult Search()
 {
     var obj = new Sprints();
     var s = new Sites();
     var b = new Batch();
     s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
     b.Sites = s;
     obj.Batch = b;
     return View(obj);
 }
        public ActionResult Search(string SelectedSite)
        {
            Sites site = new Sites();
            if (!string.IsNullOrEmpty(SelectedSite))
            {
                int SiteID = int.Parse(SelectedSite);
                site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //issues
                    var issues = from i in db.Issues where i.SiteID == SiteID select i;
                    Issues issue = new Issues();
                    site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                    issue.Sites = site;
                    issue.SiteIssues = issues.ToList();
                    ViewBag.Error = false;
                    return View(issue);
                }
                else
                {
                    ViewBag.Message = "There are not issues for the selected site.";
                    return View();
                }
            }

            ViewBag.Error = true;
            ModelState.AddModelError("Site", "You must select a site.");
            var obj = new Issues();
            site = new Sites();
            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = site;
            return View(obj);
        }
        //Update Issue
        public ActionResult Update(string IssueID)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            if (!string.IsNullOrEmpty(IssueID))
            {
                try
                {
                    int id = int.Parse(IssueID);
                    Issues issue = db.Issues.Find(id);
                    Sites site = new Sites();
                    if (issue != null)
                    {
                        var sites = from s in db.Sites where s.SiteID == issue.SiteID select s;
                        if (sites.Count() > 0) { site = sites.First(); }
                        site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                        issue.Sites = site;
                        return View(issue);
                    }
                    else
                    {
                        TempData["message"] = "There was an error.  Please try again or contact administrator.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error.  Please try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                TempData["message"] = "The issue doesn't exist.  Please try again or contact administrator.";
                return View("~/Views/Shared/Error.cshtml");
            }
        }
        public ActionResult Search(string SelectedSite)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            Sites site = new Sites();
            if (!string.IsNullOrEmpty(SelectedSite))
            {
                int SiteID = int.Parse(SelectedSite);
                site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //utilities
                    var utilities = from b in db.Utilities_Datos_Generales where b.SiteID == SiteID select b;
                    Utilities_Datos_Generales utility = new Utilities_Datos_Generales();
                    site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                    utility.Sites = site;
                    utility.SiteUDG = utilities.ToList();
                    ViewBag.Error = false;
                    return View(utility);
                }
                else
                {
                    ViewBag.Message = "There are none utilities for the selected site.";
                    return View();
                }
            }

            ViewBag.Error = true;
            ModelState.AddModelError("Site", "You must select a site.");
            var obj = new Batch();
            site = new Sites();
            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = site;
            return View(obj);
        }
 //Search by Site
 public ActionResult Search()
 {
     var obj = new Utilities_Datos_Generales();
     var s = new Sites();
     s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
     obj.Sites = s;
     return View(obj);
 }
        public ActionResult Add(Requirements requirement)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            //getting SiteID
            int SiteID = 0;
            if (!string.IsNullOrEmpty(requirement.SelectedSite))
            {
                try
                {
                    SiteID = int.Parse(requirement.SelectedSite);
                    var site = db.Sites.Find(SiteID);
                    if (site != null)
                    {
                        if (ModelState.IsValid)
                        {
                            requirement.SiteID = SiteID;
                            requirement.RegisteredBy = LoggedUser();
                            requirement.RegisteredDate = DateTime.Now;
                            //saving database
                            db.Requirements.Add(requirement);
                            db.SaveChanges();

                            TempData["message"] = "The requirement was added.";
                            return View("~/Views/Shared/Success.cshtml");
                        }
                        else
                        {
                            var req = new Requirements();
                            //sites list
                            Sites s = new Sites();
                            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                            req.Sites = s;

                            return View(req);
                        }
                    }
                    else
                    {

                        TempData["message"] = "There site you selected doesn't exist.  Try again or contact administrator.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                catch (Exception e)
                {

                    TempData["message"] = "There was an error registering requirement.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                var req = new Requirements();
                //sites list
                Sites s = new Sites();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                req.Sites = s;

                return View(req);
            }
        }
        public ActionResult Search(string SelectedSite)
        {
            Sites site = new Sites();
            if (!string.IsNullOrEmpty(SelectedSite))
            {
                int SiteID = int.Parse(SelectedSite);
                site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //requirements
                    var requirements = from r in db.Requirements where r.SiteID == SiteID select r;
                    Requirements req = new Requirements();
                    site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                    req.Sites = site;
                    req.SiteRequirements = requirements.ToList();
                    ViewBag.Error = false;
                    return View(req);
                }
                else
                {
                    ViewBag.Message = "There are not batches for the selected site.";
                    return View();
                }
            }

            ViewBag.Error = true;
            ModelState.AddModelError("Site", "You must select a site.");
            var obj = new Requirements();
            site = new Sites();
            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = site;
            return View(obj);
        }
 //Search by Site
 public ActionResult Search()
 {
     var obj = new Requirements();
     var s = new Sites();
     s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
     obj.Sites = s;
     return View(obj);
 }
        public ActionResult Update(Sites site)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;

            try
            {

                //getting sites with same name as new site name
                var sameName = from s in db.Sites where s.SiteName.Equals(site.SiteName) select s;

                if (site != null)
                {
                    if (sameName.Count() > 1)
                    {
                        TempData["previous"] = thisPageUrl;
                        TempData["message"] = "The name you are trying to use is already taken.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                    else
                    {
                        db.Entry(site).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        TempData["previous"] = thisPageUrl;
                        TempData["message"] = "The site was updated.";
                        return View("~/Views/Shared/Success.cshtml");
                    }
                }
                else
                {
                    TempData["previous"] = thisPageUrl;
                    TempData["message"] = "There was an error updating site.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }

            }
            catch (Exception e)
            {
                TempData["previous"] = thisPageUrl;
                TempData["message"] = "There was an error updating site.  Try again or contact administrator.";
                return View("~/Views/Shared/Error.cshtml");
            }
        }
        public ActionResult Update(Issues issue)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            if (ModelState.IsValid)
            {
                try
                {
                    if (issue != null)
                    {
                        try
                        {
                            issue.LastUpdatedBy = LoggedUser();
                            issue.LastUpdatedDate = DateTime.Now;
                            db.Entry(issue).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                            TempData["previous"] = "Search";
                            TempData["message"] = "The issue was updated.";
                            return View("~/Views/Shared/Success.cshtml");
                        }
                        catch (Exception e)
                        {
                            TempData["message"] = "There was an error. Try again or contact administrator.";
                            return View("~/Views/Shared/Error.cshtml");
                        }
                    }
                    else
                    {
                        TempData["message"] = "There was an error. Try again or contact administrator.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error. Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }

            Sites site = new Sites();
            var sites = from s in db.Sites where s.SiteID == issue.SiteID select s;
            if (sites.Count() > 0) { site = sites.First(); }
            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
            issue.Sites = site;
            return View(issue);
        }
        public ActionResult Add(Issues issue)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            if (!string.IsNullOrEmpty(issue.SelectedSite))
            {

                try
                {
                    //getting site
                    int id = int.Parse(issue.SelectedSite);
                    Sites site = db.Sites.Find(id);

                    if (site != null)
                    {
                        //registration data
                        issue.RegisteredBy = LoggedUser();
                        issue.RegisteredDate = DateTime.Now;
                        issue.SiteID = id;

                        if (ModelState.IsValid)
                        {
                            if (issue.EndDate != null)
                            {
                                if (issue.EndDate < issue.StartDate)
                                {
                                    ModelState.AddModelError("EndDate", "End date should be greater than start date");
                                    var obj = new Issues();
                                    Sites s = new Sites();
                                    s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                                    obj.Sites = s;

                                    return View(obj);
                                }
                                else
                                {
                                    try
                                    {
                                        //adding to database
                                        db.Issues.Add(issue);
                                        db.SaveChanges();

                                        //redirecting
                                        TempData["message"] = "The issue was saved.";
                                        return View("~/Views/Shared/Success.cshtml");
                                    }
                                    catch (Exception e)
                                    {
                                        TempData["message"] = "There was an error saving issue.  Try again or contact administrator.";
                                        return View("~/Views/Shared/Error.cshtml");
                                    }
                                }
                            }
                            else
                            {
                                try
                                {
                                    //adding to database
                                    db.Issues.Add(issue);
                                    db.SaveChanges();

                                    //redirecting
                                    TempData["message"] = "The issue was saved.";
                                    return View("~/Views/Shared/Success.cshtml");
                                }
                                catch (Exception e)
                                {
                                    TempData["message"] = "There was an error saving issue.  Try again or contact administrator.";
                                    return View("~/Views/Shared/Error.cshtml");
                                }
                            }

                        }
                        else
                        {
                            var obj = new Issues();
                            Sites s = new Sites();
                            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                            obj.Sites = s;

                            return View(obj);
                        }
                    }
                    else
                    {
                        TempData["message"] = "The site doesn't exist.  Try again or contact administrator.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error saving issue.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }

            }
            else
            {
                ModelState.AddModelError("Site", "You must select a site.");
                var obj = new Issues();
                Sites s = new Sites();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                obj.Sites = s;

                return View(obj);
            }
        }
        public ActionResult Update(string UtilityID)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;
            if (!string.IsNullOrEmpty(UtilityID))
            {
                try
                {
                    int id = int.Parse(UtilityID);
                    Utilities_Datos_Generales u = db.Utilities_Datos_Generales.Find(id);
                    //sites list
                    Sites s = new Sites();
                    s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                    if (u != null)
                    {
                        u.Sites = s;
                        //the utility is found and returned for update
                        return View(u);
                    }
                    else
                    {
                        //the site doesn't exist,return error
                        TempData["message"] = "The utility you are trying to update doesn't exist.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error updating utility.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }

            }

            return RedirectToAction("Search");
        }