public ActionResult Create(int? eventId)
 {
     TempData["ReturnPath"] = Request.UrlReferrer;
     if (eventId == null)
     {
         return Redirect(Request.UrlReferrer.ToString());
     }
     var ev = db.Events.Find(eventId);
     if (ev == null)
     {
         return Redirect(Request.UrlReferrer.ToString());
     }
     
     var reservation = new Reservation { 
         Event = ev,
         startTime = ev.startTime,
         endTime = ev.endTime,
         attendeeCount = ev.attendeeCount,
         attendeeCountDGS = ev.attendeeCountDGS,
         attendeeCountNonDGS = ev.attendeeCountNonDGS,
         Locations = ev.Locations,
         Options = ev.Options,
         created = DateTime.Now,
         createdBy = Helpers.Helpers.GetUser(User.Identity).loginName,
         modified = DateTime.Now,
         modifiedBy = Helpers.Helpers.GetUser(User.Identity).loginName
     };
     ViewBag.Options = Helpers.Helpers.GetReservationOptions(ev.Options.Select(ro => ro.optionId).ToList());
     ViewBag.LocationList = Helpers.Helpers.GetLocationList(ev.Locations.Select(rl => rl.locationId).ToList());
     ViewBag.eventId = eventId;
     return View(reservation);
 }
 private ReservationViewModel CreateSingleReservation(EventManagementSystemEntities dbEMS, DateTime startTime, DateTime endTime)
 {
     try
     {
         
             Reservation reservation = new Reservation
             { 
                 startTime = startTime,
                 endTime = endTime,
                 attendeeCount = attendeeCount,
                 attendeeCountDGS = attendeeCountDGS,
                 attendeeCountNonDGS = attendeeCountNonDGS
             };
             if (locations != null)
             {
                 foreach (var l in locations.Where(x => x.Length > 0))
                 {
                     Int32 locationId = Convert.ToInt32(l);
                     Location location = dbEMS.Locations.Where(ll => ll.locationId == locationId).FirstOrDefault();
                     reservation.Locations.Add(location);
                 }
             }
             if (options != null)
             {
                 foreach (var o in options.Where(x => x.Length > 0))
                 {
                     Int32 optionId = Convert.ToInt32(o);
                     Option option = dbEMS.Options.Where(oo => oo.optionId == optionId).FirstOrDefault();
                     reservation.Options.Add(option);
                 }
             }
             ReservationViewModel rvm = new ReservationViewModel();
             rvm.Reservation = reservation;
             rvm.GetConflicts();
             //reservation.GetConflicts();
             return rvm;
         
     }
     catch (Exception ex)
     {
         return null;
     }
 }
 public String AsyncDelete(Reservation reservation)
 {
     var returnPath = TempData["ReturnPath"] ?? Request.UrlReferrer;
     try
     {
         db.Entry(reservation).State = EntityState.Deleted;
         db.Logs.Add(Helpers.Helpers.WriteLog(User, "ReservationsController:delete", reservation));
         db.SaveChanges();
         if (returnPath != null)
         {
             return s.Serialize(new { @class = "success", message = "Reservation Deleted", returnpath = returnPath });
         }
         return s.Serialize(new { @class = "success", message = "Reservation Deleted" });
     }
     catch (Exception ex)
     {
         return s.Serialize(new { @class = "warning", message = ex.Message.ToString() });
     }
 }
        public String AsyncCreate(Reservation reservation, List<int> ReservationLocation, List<int> ReservationOption, string NewNote)
        {
            try 
            {
                var returnPath = TempData["ReturnPath"] ?? Request.UrlReferrer;

                if (ReservationLocation == null || ReservationLocation.Count() == 0)
                {
                    return s.Serialize(new { @class = "warning", message = "At least one location is required" });
                }
                if (reservation.eventId == 0)
                {
                    return s.Serialize(new { @class = "warning", message = "Event must already exist" });
                }
                var ev = db.Events.Find(reservation.eventId);
                if (ev == null)
                {
                    return s.Serialize(new { @class = "warning", message = "Event must already exist" });
                }

                foreach (var location in ReservationLocation)
                {
                    var loc = db.Locations.Find(location);
                    reservation.Locations.Add(loc);
                }
                if (ReservationOption != null)
                {
                    foreach (var option in ReservationOption)
                    {
                        var opt = db.Options.Find(option);
                        reservation.Options.Add(opt);
                    }
                }
                if (NewNote != null && NewNote != string.Empty)
                {
                    var note = new ReservationNote { note = NewNote, createdBy = reservation.createdBy, created = reservation.created.Value };
                    reservation.ReservationNotes.Add(note);
                }

                db.Reservations.Add(reservation);
                //db.Logs.Add(Helpers.Helpers.WriteLog(User, "CreateReservationFromExistingEvent", reservation));
                db.SaveChanges();
                if (returnPath != null)
                {
                    return s.Serialize(new { @class = "success", message = "Reservation Created", returnpath = returnPath });
                }
                return s.Serialize(new { @class = "success", message = "Reservation Created" });
            }
            catch (Exception ex)
            {
                return s.Serialize(new { @class = "warning", message = ex.Message.ToString() });
            }
        }
        public String AsyncHospitalityEdit(Reservation reservation, List<int> ReservationOption)
        {
            var returnPath = TempData["ReturnPath"] ?? Request.UrlReferrer;
            var existingReservation = db.Reservations.Find(reservation.reservationId);
            
            if (existingReservation != null)
            {
                try
                {
                    if (reservation.attendeeCount != null && reservation.attendeeCount != existingReservation.attendeeCount)
                    {
                        existingReservation.attendeeCount = reservation.attendeeCount;
                    }
                    if (reservation.attendeeCountDGS != null && reservation.attendeeCountDGS != existingReservation.attendeeCountDGS)
                    {
                        existingReservation.attendeeCountDGS = reservation.attendeeCountDGS;
                    }
                    if (reservation.attendeeCountNonDGS != null && reservation.attendeeCountNonDGS != existingReservation.attendeeCountNonDGS)
                    {
                        existingReservation.attendeeCountNonDGS = reservation.attendeeCountNonDGS;
                    }
                    //db.Entry(existingReservation).State = EntityState.Modified;

                    //var existingOptions = db.Reservations.Include("Options").Where(r => r.reservationId == existingReservation.reservationId).FirstOrDefault().Options.ToList();
                    //existingOptions.ForEach(ero => reservation.Options.Add(ero));
                    foreach (var option in db.Options)
                    {
                        if (ReservationOption == null || (existingReservation.Options.Contains(option) && !ReservationOption.Contains(option.optionId)))
                        {
                            existingReservation.Options.Remove(option);
                        }
                        else if (!existingReservation.Options.Contains(option) && ReservationOption.Contains(option.optionId))
                        {
                            existingReservation.Options.Add(option);
                        }
                    }
                    db.Logs.Add(Helpers.Helpers.WriteLog(User, "ReservationsController:edit", existingReservation));
                    db.SaveChanges();
                    if (returnPath != null)
                    {
                        return s.Serialize(new { @class = "success", message = "Reservation Updated", returnpath = returnPath });
                    }
                    return s.Serialize(new { @class = "success", message = "Reservation Updated" });
                }
                catch (Exception ex)
                {
                    return s.Serialize(new { @class = "warning", message = ex.Message.ToString() });
                }
            }
            return "Model not valid";
        }
        public String AsyncEdit(Reservation reservation, List<int> ReservationLocation, List<int> ReservationOption)
        {
            var returnPath = TempData["ReturnPath"] ?? Request.UrlReferrer;
            if (ReservationLocation == null || ReservationLocation.Count() == 0)
            {
                return s.Serialize(new { @class = "warning", message = "At least one location is required" });
            }
            if (reservation.Event == null)
            {
                reservation.Event = db.Events.Find(reservation.eventId);
            }
            if (ModelState.IsValid)
            {
                try
                {
                    db.Entry(reservation).State = EntityState.Modified;
                    var existingLocations = db.Reservations.Include("Locations").Where(r => r.reservationId == reservation.reservationId).FirstOrDefault().Locations.ToList();
                    existingLocations.ForEach(erl =>
                        reservation.Locations.Add(erl)
                        );
                    foreach (var location in db.Locations)
                    {
                        if (ReservationLocation == null || (reservation.Locations.Contains(location) && !ReservationLocation.Contains(location.locationId)))
                        {
                            reservation.Locations.Remove(location);
                        }
                        if (!reservation.Locations.Contains(location) && ReservationLocation.Contains(location.locationId))
                        {
                            reservation.Locations.Add(location);
                        }
                    }

                    var existingOptions = db.Reservations.Include("Options").Where(r => r.reservationId == reservation.reservationId).FirstOrDefault().Options.ToList();
                    existingOptions.ForEach(ero => reservation.Options.Add(ero));
                    foreach (var option in db.Options)
                    {
                        if (ReservationOption == null || (reservation.Options.Contains(option) && !ReservationOption.Contains(option.optionId)))
                        {
                            reservation.Options.Remove(option);
                        }
                        else if (!reservation.Options.Contains(option) && ReservationOption.Contains(option.optionId))
                        {
                            reservation.Options.Add(option);
                        }
                    }
                    reservation.modified = DateTime.Now;
                    reservation.modifiedBy = Helpers.Helpers.GetUser(User.Identity).loginName;
                    db.Logs.Add(Helpers.Helpers.WriteLog(User, "ReservationsController:edit", reservation));
                    db.SaveChanges();


                    // This is where we send the reservation modified email
                    Helpers.Helpers.SendReservationModifiedEmail(reservation);


                    if (returnPath != null)
                    {
                        return s.Serialize(new { @class = "success", message = "Reservation Updated", returnpath = returnPath });
                    }
                    return s.Serialize(new { @class = "success", message = "Reservation Updated" });
                }
                catch (Exception ex)
                {
                    return s.Serialize(new { @class = "warning", message = ex.Message.ToString() });
                }
            }
            return "Model not valid";
        }