public IActionResult LinkClientReservation(ReservationsViewModel linkModel) { if (GlobalVar.LoggedOnUserId == -1) { return(RedirectToAction("LogInRequired", "Users")); } var clientId = linkModel.ClientId; var reservationId = linkModel.Id; if (reservationId <= 0) { return(RedirectToAction("Index")); } if (clientId <= 0) { return(RedirectToAction("Detail", new { id = reservationId })); } var clientReservation = new ClientReservation() { ClientId = clientId, ReservationId = reservationId }; var currentRoomOccupyCount = (_context.ClientReservation.Where(x => x.ReservationId == reservationId).ToList()).Count; Room room = _context.Rooms.Find(_context.Reservations.Find(reservationId).RoomId); if (currentRoomOccupyCount >= room.Capacity) { return(RedirectToAction("Detail", new { id = reservationId })); } var elem = _context.ClientReservation.Find(clientId, reservationId); if (elem != null) { throw new InvalidOperationException($"CUSTOM EXCEPTION: This client {clientId} is already added to this reservation {reservationId}"); } else { _context.ClientReservation.Add(clientReservation); _context.SaveChanges(); bool isClientAdult = (_context.Clients.Find(clientId)).IsAdult; decimal pricePerDay = 0; if (isClientAdult) { pricePerDay += (_context.Rooms.Find(room.Id)).PriceAdult; } else { pricePerDay += (_context.Rooms.Find(room.Id)).PriceChild; } Reservation reservation = _context.Reservations.Find(reservationId); decimal clientOverall = pricePerDay * CalculateDaysPassed(reservation.DateOfAccommodation, reservation.DateOfExemption); clientOverall = AddExtras(clientOverall, reservation.IsAllInclusive, reservation.IsBreakfastIncluded); reservation.OverallBill += clientOverall; _context.Reservations.Update(reservation); _context.SaveChanges(); } return(RedirectToAction("Detail", new { id = reservationId })); }
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)); }