public void AddGuest() { //add a new guest using (var context = new Sunshine_HotelEntities()) { //get the last booking ID from the new booking var bookingID = context.Bookings.Select(b => b.BookingID).ToList().LastOrDefault(); //CREATE new entry var newGuest = new Guest { Name = this.Name, Address = this.Address, NumberOfGuests = this.GuestNumbers, RoomBooked = Convert.ToInt32(this.RoomSelected), BookingIDFK = bookingID, //get the booking ID from the booking table, this BookingDate = DateTime.Today.Date }; context.Guests.Add(newGuest); context.SaveChanges(); } }
/// <summary> /// This returns back a list of the rooms and the prices for those rooms that are available between the dates chosen from the calender /// </summary> public List<string> OutputAllRoomCosts() { var OutputAllRoomCostsList = new List<string>(); //get the free rooms var AllRoomsFree = GetFreeRooms(this.BookingFrom, this.BookingTo); //get the free rooms for the DGV to show //FreeRoomsForDGV(GetFreeRooms(BookingFrom, BookingTo)); //calculate the cost of each room based on the number of guests // a room goes at the cost of 2 people, tariff2People //a single person is charged as 2 people unless they are in the room with only single beds - bunk room //any extra beds in a room are charged with TariffExtraPerson to 2 extra bed //get the tariffs for the rooms using (var context = new Sunshine_HotelEntities()) { var RoomTariffDetails = context.Rooms.Where(r => AllRoomsFree.Contains(r.RoomID)) .Select( r => new { Room = r.RoomID, Singlebeds = r.NumSingleBeds, Doublebeds = r.NumDoubleBeds, r.TariffSinglePerson, r.Tariff2People, r.TariffExtraPerson, r.NumSingleBeds, r.NumDoubleBeds }); //clear old entries before the foreach loop runs // OutputAllRoomCosts.Clear(); RoomCostDict.Clear(); //} //using (var context = new Sunshine_HotelEntities()) //{ //import the guestnumbers foreach (var Room in RoomTariffDetails) { RoomCost = 0; string RoomFullMessage = ""; //if there are more than 2 extra beds then the room is full // decimal roomcost = 0; //holds the final cost int doublebeds = Convert.ToInt32(Room.Doublebeds); //how many double beds int singlebeds = Convert.ToInt32(Room.Singlebeds); //how many single beds int extrabeds = 2; // only 2 extra beds in a room cound down from 2 allowed ////pass the guest number to a local variable that is decremented and refreshed for each new room int GuestnumbersForForeach = GuestNumbers; if (GuestnumbersForForeach == 1 && Room.Doublebeds > 0) { //if there is one person in an ordinay room they pay the double rate ie: the price of the room RoomCost = Room.Tariff2People.Value; } if (GuestnumbersForForeach == 1 && Room.Doublebeds == 0) { //if one person is in a room with no double beds they pay a single rate - its a dorm RoomCost = Room.TariffSinglePerson.Value; } if (GuestnumbersForForeach > 1) //so its 2 or more { do //loop through the options until the guest number = 0 { if (doublebeds > 0) { //if the room has double beds RoomCost += Room.Tariff2People.Value; // $200 GuestnumbersForForeach -= 2; doublebeds -= 1; } else if (singlebeds > 0) { //if no doubles then count through single beds RoomCost += Room.TariffSinglePerson.Value; // $150 GuestnumbersForForeach -= 1; singlebeds -= 1; } else if (extrabeds > 0) { //if no double or single beds left then count through extra beds until there are 2 RoomCost += Room.TariffExtraPerson.Value; // $20 extrabeds -= 1; GuestnumbersForForeach -= 1; } else { //overflow message if (GuestnumbersForForeach > 1) { RoomFullMessage = GuestnumbersForForeach + " Guests must be in another room."; } else { RoomFullMessage = GuestnumbersForForeach + " Guest must be in another room."; } //break the do loop by setting to 0 GuestnumbersForForeach = 0; } } while (GuestnumbersForForeach > 0); } RoomCost += Convert.ToDecimal(this.DaysBooked()) * RoomCost; //add the rooms to a dictionary with the room number and the room cost. This is all becuase I can't add the roomcost to the dgv (grr) so i have to pass it to a list and a dict to hold the data. The dict is used when we add the room cost to the database once the room is selected. RoomCostDict.Add(Room.Room, RoomCost); //output the data to a listview to show on the form OutputAllRoomCostsList.Add(Room.Room + ": " + string.Format("{0:C}", RoomCost) + " " + RoomFullMessage); } } return OutputAllRoomCostsList; }
/// <summary> /// Gets the Free rooms to show on the dgv with beds NOT USED? /// </summary> /// <param name="AllRoomsFree">Free room numbers</param> //private void FreeRoomsForDGV(List<int> AllRoomsFree) { // using (var context = new Sunshine_HotelEntities()) { // //get all the bookings, get all the booked rooms, subtract one from the other and you are left with free rooms. // //then match the free rooms by the dates booked and the guest numbers to get the best rooms // //leftover rooms are by logic free, get the rest of their details such as beds THIS IS FOR THE DGV ONLY // var FreeRoomsAndBedsForDGV = // context.Rooms.Where(r => AllRoomsFree.Contains(r.RoomID)) // .Select(r => new { Room = r.RoomID, Singlebeds = r.NumSingleBeds, Doublebeds = r.NumDoubleBeds }); // //output the other details to the dgv // // dgv.DataSource = FreeRoomsAndBedsForDGV.ToList(); // } // } /// <summary> /// Get the rooms that are free /// </summary> /// <returns>List of the free rooms</returns> private List<int> GetFreeRooms(DateTime BookingFrom, DateTime BookingTo) { List<int> AllRoomsFreeID; using (var context = new Sunshine_HotelEntities()) { //get all the bookings after today var AllBookedRooms = context.Bookings.Where(b => b.BookingFrom >= DateTime.Today) .Select(b => new { b.RoomIDFK, b.BookingFrom, b.BookingTo }) .OrderBy(b => b.RoomIDFK) .ThenBy(b => b.BookingFrom); //get all the rooms var AllRooms = context.Rooms.Select(r => r.RoomID); //make a list of all the room numbers (maybe i could leave it as allrooms but whatever) AllRoomsFreeID = new List<int>(AllRooms.ToList()); //loop through all the booked rooms foreach (var BookedRoom in AllBookedRooms) { //find if the booking dates are inside the room dates if (DatesOverlap(BookingFrom, BookingTo, BookedRoom.BookingFrom, BookedRoom.BookingTo)) { if (AllRoomsFreeID.Contains(Convert.ToInt32(BookedRoom.RoomIDFK))) { //remove rooms from the list of all rooms that are in conflict AllRoomsFreeID.Remove(Convert.ToInt32(BookedRoom.RoomIDFK)); } } } } return AllRoomsFreeID; }
public void AddNewBooking() { using (var context = new Sunshine_HotelEntities()) { // CREATE a new booking var newbooking = new Booking(); newbooking.RoomIDFK = Convert.ToInt32(this.RoomSelected); newbooking.BookingFrom = this.BookingFrom.Date; newbooking.BookingTo = this.BookingTo.Date; //add in the cost of the room extracted from the dictionary newbooking.RoomCost = RoomBookedPrice(); context.Bookings.Add(newbooking); context.SaveChanges(); //string BookedConfirmationMessage = Environment.NewLine + "You have booked Room " + this.RoomSelected + Environment.NewLine + "From " + this.BookingFrom + "To " + Environment.NewLine + this.BookingTo + Environment.NewLine + "For " + (string.Format("{0:C}", newbooking.RoomCost)); //show a confirmation message //MessageBox.Show(BookedConfirmationMessage); } }