Exemple #1
0
        // GET: Reservation/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Reservation reservation = await _context.Reservations.FindAsync(id);

            if (reservation == null)
            {
                return(NotFound());
            }

            ReservationEditViewModel model = new ReservationEditViewModel
            {
                Id             = reservation.Id,
                User           = reservation.User,
                CheckInDate    = reservation.CheckInDate,
                CheckOutDate   = reservation.CheckOutDate,
                HasBreakfast   = reservation.HasBreakfast,
                IsAllInclusive = reservation.IsAllInclusive
            };

            return(View(model));
        }
Exemple #2
0
        public IActionResult Edit(string id)
        {
            Reservation reservation = context.Reservations.FindAsync(id).Result;

            List <string> choosenClients = reservation.ClientReservations.Select(cr => cr.Client.Id).ToList();

            List <ClientViewModel> clients = context.Clients.Select(c => new ClientViewModel(c.Id, c.FirstName,
                                                                                             c.LastName, c.IsAdult))
                                             .ToList()
                                             .OrderBy(c => c.FirstName)
                                             .ThenBy(c => c.LastName)
                                             .ToList();

            List <RoomViewModel> rooms = context.Rooms.Select(r => new RoomViewModel(r.Id, r.Capacity, r.Type,
                                                                                     r.IsFree, r.Number))
                                         .ToList()
                                         .OrderBy(r => r.Number)
                                         .ToList();

            ReservationEditViewModel model = new ReservationEditViewModel(id, reservation.CheckInDate,
                                                                          reservation.CheckOutDate, reservation.IsBreakfastIncluded, reservation.IsAllInclusive,
                                                                          choosenClients, clients, rooms, reservation.Room.Id);

            return(View(model));
        }
Exemple #3
0
        public HttpResponseMessage Put(int id, ReservationEditViewModel model)
        {
            var itemVm = Mapper.Map <ReservationEditViewModel, Reservation>(model);

            if (itemVm != null)
            {
                _reservationService.Update(id, itemVm);
                _db.SaveChanges();
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }


            return(new HttpResponseMessage(HttpStatusCode.BadRequest));
        }
Exemple #4
0
        public ViewResult EditReservation(int id)
        {
            Reservation reservation = _reservationRepository.GetReservation(id);
            ReservationEditViewModel reservationEditViewModel = new ReservationEditViewModel
            {
                ReservationID = reservation.ReservationID,
                RoomID        = reservation.RoomID,
                TotalPrice    = reservation.TotalPrice,
                UserName      = reservation.UserName,
                CheckInDate   = reservation.CheckInDate,
                CheckOutDate  = reservation.CheckOutDate,
                CountDays     = reservation.CountDays,
            };

            return(View(reservationEditViewModel));
        }
Exemple #5
0
        public async Task <IActionResult> UpdateReservation(int id)
        {
            var r = await GetReservation(id);

            var model = new ReservationEditViewModel
            {
                SittingId         = r.SittingId,
                CustomerId        = r.CustomerId,
                ReservationTypeId = r.ReservationTypeId,
                Customer          = r.Customer,
                DateTime          = r.DateTime,
                Guests            = r.Guests,
                Notes             = r.Notes,
                FirstName         = r.Customer.FirstName,
                LastName          = r.Customer.LastName,
                Email             = r.Customer.Email,
                PhoneNumber       = r.Customer.PhoneNumber
            };

            return(View(model));
        }
Exemple #6
0
        public async Task <IActionResult> UpdateReservation(ReservationEditViewModel vm)
        {
            if (ModelState.IsValid)
            {
                if (await IsDateValid(vm.DateTime))
                {
                    var sitting = await SittingByDate(vm.DateTime);

                    var c = new Customer
                    {
                        Id          = vm.CustomerId,
                        FirstName   = vm.FirstName,
                        LastName    = vm.LastName,
                        Email       = vm.Email,
                        PhoneNumber = vm.PhoneNumber
                    };
                    await EditCustomer(c);

                    var r = new Reservation
                    {
                        SittingId         = sitting.SittingId,
                        CustomerId        = c.Id,
                        ReservationTypeId = vm.ReservationTypeId,
                        DateTime          = vm.DateTime,
                        Guests            = vm.Guests,
                        Notes             = vm.Notes,
                    };
                    await EditReservation(r);

                    return(RedirectToAction("ReservationIndex"));
                }
                ViewBag.ErrorMessage = "This time or day is not avaiable try other time";
                return(View(vm));
            }

            return(View(vm));
        }
Exemple #7
0
        public async Task <IActionResult> Edit(ReservationEditViewModel editModel)
        {
            if (ModelState.IsValid)
            {
                Reservation reservation = new Reservation()
                {
                    Id             = editModel.Id,
                    User           = editModel.User,
                    CheckInDate    = editModel.CheckInDate,
                    CheckOutDate   = editModel.CheckOutDate,
                    HasBreakfast   = editModel.HasBreakfast,
                    IsAllInclusive = editModel.IsAllInclusive
                };

                try
                {
                    _context.Update(reservation);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ReservationExists(reservation.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        reservation.Id = editModel.Id;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(editModel));
        }
Exemple #8
0
        public IActionResult EditReservation(ReservationEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Reservation reservation = _reservationRepository.GetReservation(model.ReservationID);
                reservation.RoomID       = model.RoomID;
                reservation.UserName     = model.UserName;
                reservation.CheckInDate  = model.CheckInDate;
                reservation.CheckOutDate = model.CheckOutDate;
                reservation.TotalPrice   = model.TotalPrice;
                reservation.CountDays    = model.CountDays;


                if (reservation.CheckInDate < DateTime.Today)
                {
                    return(RedirectToAction("ReservationLateEdit"));
                }

                if (reservation.CheckInDate == reservation.CheckOutDate)
                {
                    return(RedirectToAction("ReservationTooShortEdit"));
                }

                if (reservation.CheckInDate > reservation.CheckOutDate)
                {
                    return(RedirectToAction("ReservationWronglyScheduledEdit"));
                }

                _reservationRepository.Update(reservation);

                //Aktualizacja ilości dni i nowej ceny cłkowitej
                if (reservation.CheckInDate.HasValue && reservation.CheckOutDate.HasValue)
                {
                    DateTime dt1 = reservation.CheckInDate.Value;
                    DateTime dt2 = reservation.CheckOutDate.Value;

                    reservation.CountDays = (dt2 - dt1).Days;

                    var roomId = reservation.RoomID;


                    // tutaj wyciągam z bazy danych cenę pokoju na 2 sposoby
                    var price1 = _roomRepository.GetPrice(reservation.RoomID);

                    //var price2 = _roomRepository.GetPrice(roomId);

                    var price = reservation.Room.Price;


                    //reservation.TotalPrice = reservation.CountDays * reservation.Room.Price;
                    reservation.TotalPrice = reservation.CountDays * price;

                    _reservationRepository.Update(reservation);
                }

                if (signInManager.IsSignedIn(User) && userManager.GetUserAsync(User).Result.UserName == "*****@*****.**")
                {
                    return(RedirectToAction("GetAllReservations"));
                }
                else
                {
                    return(RedirectToAction("MyReservation"));
                }
            }
            return(View());
        }
Exemple #9
0
        public async Task <IActionResult> Edit(ReservationEditViewModel model)
        {
            //If the model state is invalid OR Arrival is earlier than now OR later than or the same date as departure, return the view for resubmission
            if (!ModelState.IsValid ||
                DateTime.Compare(model.Arrival, DateTime.Now) < 0 ||
                DateTime.Compare(model.Arrival, model.Departure) >= 0)
            {
                //Repopulate the customer and room pairs for the dropdowns/lists
                model.Customers = _customerRepository.Items.Select(item => new CustomerPair()
                {
                    Id        = item.Id,
                    FirstName = item.FirstName,
                    LastName  = item.LastName
                }).ToList();

                model.Rooms = _roomRepository.Items.Where(item => item.IsAvailable == true || item.Id == model.RoomId).Select(item => new RoomPair()
                {
                    Id         = item.Id,
                    RoomNumber = item.RoomNumber
                }).ToList();

                return(View(model));
            }

            //Caluclate TotalSum
            #region Sum Calculation

            IQueryable <Customer> selectedCustomers = _customerRepository.Items.Where(item => model.SelectedCustomerIds.Contains(item.Id));

            int    adults        = selectedCustomers.Where(item => item.IsAdult).Count();
            int    children      = selectedCustomers.Where(item => !item.IsAdult).Count();
            double adultBedPrice = _roomRepository.Items.FirstOrDefault(room => room.Id == model.RoomId).AdultBedPrice;
            double childBedPrice = _roomRepository.Items.FirstOrDefault(room => room.Id == model.RoomId).ChildBedPrice;

            int days = (model.Departure - model.Arrival).Days;

            double totalSum = CalculateTotalSum(adults, adultBedPrice, children, childBedPrice, model.IsAllInclusive, model.BreakfastIncluded, days);
            #endregion

            var user = await _userManager.GetUserAsync(User);

            //Make a Reservation object
            Reservation reservation = new Reservation()
            {
                Id                   = model.Id,
                RoomId               = model.RoomId,
                CreatorId            = user.Id,
                Arrival              = model.Arrival,
                Departure            = model.Departure,
                BreakfastIncluded    = model.BreakfastIncluded,
                IsAllInclusive       = model.IsAllInclusive,
                TotalSum             = totalSum,
                CustomerReservations = model.SelectedCustomerIds.Select(customerId => new CustomerReservation()
                {
                    CustomerId    = customerId,
                    ReservationId = model.Id
                }).ToList()
            };

            //Check if the reservation's room has been changed
            if (oldRoomId != 0 && oldRoomId != reservation.RoomId)
            {
                //Vacate the room that was previously assigned to this reservation
                await VacateRoom(oldRoomId);

                oldRoomId = 0;
            }

            await _reservationRepository.AddOrUpdate(reservation);

            return(RedirectToAction("Index"));
        }
Exemple #10
0
        public IActionResult Edit(int?id)
        {
            Reservation reservation = _reservationRepository.Items.FirstOrDefault(item => item.Id == id);

            ReservationEditViewModel model;

            if (reservation == null)
            {
                //If there are no free rooms or customers, do not allow a reservation to be made
                if (_roomRepository.Items.Where(item => item.IsAvailable == true).Count() == 0 || _customerRepository.Items.Count() == 0)
                {
                    return(RedirectToAction("Index"));
                }

                //No Reservation found, so we create a new one
                model = new ReservationEditViewModel()
                {
                    Id        = 0,
                    Customers = _customerRepository.Items.Select(item => new CustomerPair()
                    {
                        Id        = item.Id,
                        FirstName = item.FirstName,
                        LastName  = item.LastName
                    }).ToList(),
                    Rooms = _roomRepository.Items.Where(item => item.IsAvailable == true).Select(item => new RoomPair()
                    {
                        Id         = item.Id,
                        RoomNumber = item.RoomNumber
                    }).ToList(),
                    SelectedCustomerIds = new List <int>()
                };
            }
            else
            {
                //If there are no free rooms or customers, do not allow a reservation to be made
                if (_roomRepository.Items.Where(item => item.IsAvailable == true || item.Id == reservation.Room.Id).Count() == 0 ||
                    _customerRepository.Items.Count() == 0)
                {
                    return(RedirectToAction("Index"));
                }

                //Keep the old room id
                oldRoomId = reservation.RoomId;

                //A Reservation was found, so we edit it
                model = new ReservationEditViewModel()
                {
                    Id                = reservation.Id,
                    RoomId            = reservation.RoomId,
                    CreatorId         = reservation.CreatorId,
                    Arrival           = reservation.Arrival,
                    Departure         = reservation.Departure,
                    BreakfastIncluded = reservation.BreakfastIncluded,
                    IsAllInclusive    = reservation.IsAllInclusive,
                    Customers         = _customerRepository.Items.Select(item => new CustomerPair()
                    {
                        Id        = item.Id,
                        FirstName = item.FirstName,
                        LastName  = item.LastName
                    }).ToList(),
                    SelectedCustomerIds = reservation.CustomerReservations.Select(cr => cr.CustomerId).ToList(),
                    Rooms = _roomRepository.Items.Where(item => item.IsAvailable == true || item.Id == reservation.RoomId).Select(item => new RoomPair()
                    {
                        Id         = item.Id,
                        RoomNumber = item.RoomNumber
                    }).ToList()
                };
            }

            return(View(model));
        }
Exemple #11
0
        public IActionResult Edit(ReservationEditViewModel model)
        {
            if (model.CheckInDate < DateTime.Now)
            {
                ModelState.AddModelError("CheckInDate", "Check-in date cannot be in the past.");
            }

            if (model.CheckOutDate < DateTime.Now)
            {
                ModelState.AddModelError("CheckOutDate", "Check-out date cannot be in the past.");
            }

            if (ModelState.IsValid)
            {
                Reservation reservation = context.Reservations.FindAsync(model.Id).Result;

                foreach (var previousReservation in context.Reservations)
                {
                    if (previousReservation.Id != reservation.Id &&
                        previousReservation.Room.Id == model.ChoosenRoom &&
                        ((previousReservation.CheckInDate <= model.CheckInDate &&
                          model.CheckInDate < previousReservation.CheckOutDate) ||
                         (previousReservation.CheckInDate < model.CheckOutDate &&
                          model.CheckInDate <= previousReservation.CheckOutDate)))
                    {
                        return(Redirect("~/Reservation/RoomNotFree"));
                        //ModelState.AddModelError("ChoosenRoom", "There is already created room with this number.");
                    }
                }

                Room room = context.Rooms.Find(model.ChoosenRoom);

                if (model.ChoosenClients.Count > room.Capacity)
                {
                    return(Redirect("~/Reservation/TooManyPeople"));
                }

                User creator = context.Users.Find(userManager.GetUserId(User));

                //Reservation reservation = new Reservation(room, creator, new List<ClientReservation>(), model.CheckInDate, model.CheckOutDate, model.IsBreakfastIncluded, model.IsAllInclusive, 0);

                reservation.Room                = room;
                reservation.CheckInDate         = model.CheckInDate;
                reservation.CheckOutDate        = model.CheckOutDate;
                reservation.IsBreakfastIncluded = model.IsBreakfastIncluded;
                reservation.IsAllInclusive      = model.IsAllInclusive;

                ClientReservation[] clientReservations = new ClientReservation[reservation.ClientReservations.Count];
                reservation.ClientReservations.CopyTo(clientReservations);

                foreach (var clientReservation in clientReservations)
                {
                    reservation.ClientReservations.Remove(clientReservation);
                    clientReservation.Client.ClientReservations.Remove(clientReservation);
                    context.ClientReservation.Remove(clientReservation);
                }

                reservation.ClientReservations = model.ChoosenClients.Select(c => context.Clients.Find(c))
                                                 .Select(c => new ClientReservation(c.Id, c, reservation.Id, reservation)).ToList();

                reservation.Price = 0;
                foreach (var client in reservation.ClientReservations.Select(cr => cr.Client))
                {
                    if (client.IsAdult)
                    {
                        reservation.Price += reservation.Room.AdultPrice;
                    }
                    else
                    {
                        reservation.Price += reservation.Room.ChildPrice;
                    }
                }

                context.Update(reservation);
                context.SaveChanges();

                StringBuilder body = new StringBuilder();
                body.AppendLine("<h1>Reservation details:</h1><br/>");
                body.AppendLine($"<h3>Check-in date:</h3> {reservation.CheckInDate.Date}<br/>");
                body.AppendLine($"<h3>Check-out date:</h3> {reservation.CheckOutDate.Date}<br/>");
                if (reservation.IsBreakfastIncluded)
                {
                    body.AppendLine("<h3>Extra:</h3> With included breakfast.<br/>");
                    body.AppendLine();
                }
                if (reservation.IsAllInclusive)
                {
                    body.AppendLine("<h3>Extra:</h3> With all-inclusive.<br/>");
                    body.AppendLine();
                }
                body.AppendLine("<h3>Clients:</h3><ul>");
                foreach (var client in reservation.ClientReservations.Select(cl => cl.Client))
                {
                    body.AppendLine($"<li> {client.FirstName} {client.LastName}</li>");
                }
                body.AppendLine("</ul>");
                body.AppendLine("<h3>Room:</h3>");
                if (room.Type == RoomType.TwoBeds)
                {
                    body.AppendLine($"Room No. {reservation.Room.Number} - Room with separate beds<br/>");
                }
                else if (room.Type == RoomType.DoubleBed)
                {
                    body.AppendLine($"Room No. {reservation.Room.Number} - Room with a double bed<br/>");
                }
                else if (room.Type == RoomType.PentHouse)
                {
                    body.AppendLine($"Room No. {reservation.Room.Number} - Penthouse<br/>");
                }
                else
                {
                    body.AppendLine($"Room No. {reservation.Room.Number} - {reservation.Room.Type}<br/>");
                }
                body.AppendLine($"<h3>Price:</h3> {reservation.Price}");


                foreach (var client in reservation.ClientReservations.Select(cl => cl.Client.Email))
                {
                    SendEmail(client, "Hotel reservation edited", body.ToString());
                }

                return(Redirect("~/Reservation/Details/" + reservation.Id));
            }

            model.Clients = context.Clients.Select(c => new ClientViewModel()
            {
                FirstName = c.FirstName,
                LastName  = c.LastName,
                Id        = c.Id,
                IsAdult   = c.IsAdult
            }).ToList();

            model.Rooms = context.Rooms.Select(r => new RoomViewModel()
            {
                Id       = r.Id,
                Capacity = r.Capacity,
                Type     = r.Type,
                Number   = r.Number
            }).ToList();

            return(View(model));
        }