Beispiel #1
0
        public ActionResult NewReservation(NewReservationViewModel model)
        {
            try
            {
                var room     = _roomsManagement.Get(r => r.ID == model.Book.RoomID);
                var roomType = room.RoomTypeID;

                var books = _booksManagement.GetAll(b => b.RoomID == room.ID).OrderByDescending(b => b.ArrivalDate).AsNoTracking().ToList();

                if (books.Count > 0)
                {
                    foreach (var book in books)
                    {
                        if (book.CustomerID != model.Book.CustomerID && !(DateHelper.AvailableDate(model.Book.ArrivalDate, model.Book.DepartureDate, book.ArrivalDate, book.DepartureDate)))
                        {
                            var roomNo = _roomsManagement.Get(r => r.ID == model.Book.RoomID).RoomNo;
                            throw new UnavailableRoomException(roomNo);
                        }
                    }
                }

                if (model.Book.ID == 0)
                {
                    model.Book.BookingDate = DateTime.Now;
                    model.Book.EmployeeID  = (User as CustomPrincipal).ID;
                }

                model.Book.Night = (int)(model.Book.DepartureDate - model.Book.ArrivalDate).TotalDays;
                if (model.Book.Night <= 0)
                {
                    throw new BookingDateException(model.Book.ArrivalDate, model.Book.DepartureDate);
                }

                model.Book.Discount = model.Discount / 100;
                model.Book.Price    = _roomTypesManagement.Get(r => r.ID == roomType).Price;

                _booksManagement.AddOrUpdate(model.Book);
                this.SuccessMessage("Reservation has been saved!");
            }
            catch (UnavailableRoomException e)
            {
                this.ErrorMessage(e.Message);
                return(RedirectToAction("NewReservation", "ReservationManagement"));
            }
            catch (BookingDateException e)
            {
                this.ErrorMessage(e.Message);
                return(RedirectToAction("NewReservation", "ReservationManagement"));
            }
            catch (Exception e)
            {
                this.ErrorMessage($"Reservation could not be saved! ({e.Message})");
            }

            return(RedirectToAction("Index"));
        }
        public string NewBooking(HomeViewModel model)
        {
            if (model.Book.ArrivalDate < DateTime.Today)
            {
                return("Arrival Date must be greater than today.");
            }
            else if (model.Book.ArrivalDate > model.Book.DepartureDate)
            {
                return("Departure Date must be greater than Arrival Date.");
            }

            bool available = false;

            model.Book.BookingDate = DateTime.Now;
            var customer = _customersManagement.Get(c => c.Nation == model.Customer.Nation && c.NationalID == model.Customer.NationalID);
            var rooms    = _roomsManagement.GetAll(r => r.RoomTypeID == model.RoomTypeID).ToList();

            if (customer == null)
            {
                Customers newCustomer = model.Customer;
                if (String.IsNullOrEmpty(newCustomer.Email) || String.IsNullOrEmpty(newCustomer.Name))
                {
                    return("Your E-Mail and/or Name is empty. Try again");
                }
                _customersManagement.Add(newCustomer);
                customer = _customersManagement.Get(c => c.Nation == model.Customer.Nation && c.NationalID == model.Customer.NationalID);
            }

            model.Book.CustomerID = customer.ID;
            model.Book.Night      = (int)(model.Book.DepartureDate - model.Book.ArrivalDate).TotalDays;

            if (String.IsNullOrEmpty(customer.Name))
            {
                if (!String.IsNullOrEmpty(model.Customer.Name))
                {
                    customer.Name = model.Customer.Name;
                    _customersManagement.Update(customer);
                }
                else
                {
                    return("Your Name is empty.");
                }
            }
            if (String.IsNullOrEmpty(customer.Email))
            {
                if (!String.IsNullOrEmpty(model.Customer.Email))
                {
                    customer.Email = model.Customer.Email;
                    _customersManagement.Update(customer);
                }
                else
                {
                    return("Your E-Mail is empty.");
                }
            }

            foreach (var room in rooms)
            {
                var books = _booksManagement.GetAll(b => b.RoomID == room.ID).ToList();
                if (books.Count == 0)
                {
                    available = true;
                }
                foreach (var book in books)
                {
                    available = DateHelper.AvailableDate(model.Book.ArrivalDate, model.Book.DepartureDate, book.ArrivalDate, book.DepartureDate);

                    if (!available)
                    {
                        break;
                    }
                }

                if (available)
                {
                    model.Book.RoomID = room.ID;
                    _booksManagement.Add(model.Book);
                    break;
                }
            }

            if (!available)
            {
                return("All rooms are reserved. Please, change room type or date and try again.");
            }

            return(String.Format($"Your booking has been saved!\nRoom No: {_roomsManagement.Get(r=>r.ID == model.Book.RoomID).RoomNo}\nAll details have been sent to {customer.Email}"));
        }