Exemplo n.º 1
0
        // Saving reservations in the database
        private void ReservationConfirmButton_Click(object sender, EventArgs e)
        {
            CustomDialog cd;

            if (roomsSelectedListbox.Items.Count > 0 && LabelResOn.Text != "EMPTY")
            {
                Reservation  ResToSave  = null;
                RoomReserved RoomToSave = null;

                // New booking has been added old is edit \/
                if (task == 0)
                {
                    ResToSave = new Reservation();
                    contextDB.Reservation.InsertOnSubmit(ResToSave);
                }
                else if (task == 1)
                {
                    ResToSave = (from reservation in contextDB.Reservation where reservation.Id == reservationId select reservation).SingleOrDefault();
                }

                ResToSave.StartDate   = timePickerStart.Value;
                ResToSave.EndDate     = timePickerEnd.Value;
                ResToSave.Total_price = TakePrice();
                ResToSave.Guest       = contextDB.Guest.Single(c => c.Id == (from guest in contextDB.Guest
                                                                             where guest.Id == PersonId()
                                                                             select guest.Id).Single());
                ResToSave.Worker_ID = workerId;
                contextDB.SubmitChanges();



                // when a new reservation adds room reservations \/
                if (task == 0)
                {
                    for (int i = 0; i < roomsSelectedListbox.Items.Count; i++)
                    {
                        RoomToSave = new RoomReserved();
                        contextDB.RoomReserved.InsertOnSubmit(RoomToSave);

                        RoomToSave.PriceFromDate  = TakePrice();
                        RoomToSave.Reservation_ID = (from res in contextDB.Reservation select res.Id).Max();
                        RoomToSave.Room_ID        = (from room in contextDB.Room
                                                     where room.RoomNameUnique.ToString() == roomsSelectedListbox.Items[i].ToString()
                                                     select room.Id).First();
                        contextDB.SubmitChanges();
                    }
                }
                // New room reservations added

                // when an old booking has been edited deleted room bookings
                else if (task == 1)
                {
                    // *** find all rooms that are reserved for this id ***

                    var query2 = (from roomres in contextDB.RoomReserved where roomres.Reservation_ID == reservationId select roomres).ToList();


                    for (int i = 0; i < roomsSelectedListbox.Items.Count; i++)
                    {
                        if (!contextDB.RoomReserved.Any(x => x.Room.RoomNameUnique == roomsSelectedListbox.Items[i].ToString() && x.Reservation_ID == reservationId))
                        {
                            // if it does not exist in the database add
                            RoomToSave = new RoomReserved();
                            contextDB.RoomReserved.InsertOnSubmit(RoomToSave);

                            RoomToSave.PriceFromDate  = TakePrice();
                            RoomToSave.Reservation_ID = (from res in contextDB.Reservation select res.Id).Max();
                            RoomToSave.Room_ID        = (from room in contextDB.Room
                                                         where room.RoomNameUnique.ToString() == roomsSelectedListbox.Items[i].ToString()
                                                         select room.Id).First();
                        }
                        else if (contextDB.RoomReserved.Any(x => x.Room.RoomNameUnique == roomsSelectedListbox.Items[i].ToString() && x.Reservation_ID == reservationId))
                        {
                            // if there is an extension in the database
                        }
                        contextDB.SubmitChanges();
                    }

                    for (int i = 0; i < query2.Count; i++)
                    {
                        if (!roomsSelectedListbox.Items.Contains(query2[i].Room.RoomNameUnique) && query2[i].Reservation_ID.Equals(reservationId))
                        {
                            // delete from the database there is no such room reservation in the listbox

                            RoomToSave = query2[i];
                            contextDB.RoomReserved.DeleteOnSubmit(RoomToSave);
                            contextDB.SubmitChanges();
                        }
                    }
                }

                // load database in Mainmenu Form and close active form

                task = 0;
                var mainForm = Application.OpenForms.OfType <MainMenu>().Single();
                mainForm.RefreshData();
                this.Close();
            }
            else
            {
                cd = new CustomDialog("Select the room and guest needed to make the booking", 1);
                cd.ShowDialog();
            }
        }
Exemplo n.º 2
0
        private void ReservationConfirmButton_Click(object sender, EventArgs e)
        {
            if (RoomSelectedLB.Items.Count > 0)
            {
                bool AllRoomsGotGuest = true;
                for (int i = 0; i < AddedRooms.Count; i++)
                {
                    if (AddedRooms[i].guests.Count < 1)
                    {
                        AllRoomsGotGuest = false;
                        break;
                    }
                }

                if (AllRoomsGotGuest == true && LabelResOn.Text != "EMPTY")
                {
                    Reservation  ResToSave  = null;
                    RoomReserved RoomToSave = null;

                    if (AddOrEdit == 1) // when adding new reservation
                    {
                        ResToSave = new Reservation();

                        contextDB.Reservation.InsertOnSubmit(ResToSave);
                    }
                    else if (AddOrEdit == 2) // when edit old reservation
                    {
                        ResToSave = (from reservation in contextDB.Reservation where reservation.Id == ReseravtionID select reservation).SingleOrDefault();
                    }

                    ResToSave.StartDate   = TimePickerStart.Value;
                    ResToSave.EndDate     = TimePickerEnd.Value;
                    ResToSave.Total_price = TakePrice();
                    ResToSave.Guest       = contextDB.Guest.Single(c => c.Id == (from guest in contextDB.Guest
                                                                                 where guest.LastName == ReservationOn[0]
                                                                                 select guest.Id).Single());
                    ResToSave.Worker_ID = WorkerId;
                    contextDB.SubmitChanges();

                    // save reservation

                    if (AddOrEdit == 1) // when new reservation add new roomres
                    {
                        for (int i = 0; i < RoomSelectedLB.Items.Count; i++)
                        {
                            RoomToSave = new RoomReserved();
                            contextDB.RoomReserved.InsertOnSubmit(RoomToSave);

                            RoomToSave.PriceFromDate  = TakePrice();
                            RoomToSave.Reservation_ID = (from res in contextDB.Reservation select res.Id).Max();
                            RoomToSave.Room_ID        = (from room in contextDB.Room
                                                         where room.RoomNameUnique.ToString() == RoomSelectedLB.Items[i].ToString()
                                                         select room.Id).First();
                            contextDB.SubmitChanges();
                        }
                    }                        // end adding new roomreservation
                    else if (AddOrEdit == 2) // when old reservation edit/delete/add new roomres
                    {
                        //ad.1 wyszukac wszystkie pokoje o tej rezerwacji i warunek zrobic na pokoje z bazy danych nie listbox
                        var query2 = (from roomres in contextDB.RoomReserved where roomres.Reservation_ID == ReseravtionID select roomres).ToList();


                        for (int i = 0; i < RoomSelectedLB.Items.Count; i++)
                        {
                            if (!contextDB.RoomReserved.Any(x => x.Room.RoomNameUnique == RoomSelectedLB.Items[i].ToString() && x.Reservation_ID == ReseravtionID))
                            {
//MessageBox.Show("nie istnieje w bazie danych");
                                RoomToSave = new RoomReserved();
                                contextDB.RoomReserved.InsertOnSubmit(RoomToSave);

                                RoomToSave.PriceFromDate  = TakePrice();
                                RoomToSave.Reservation_ID = (from res in contextDB.Reservation select res.Id).Max();
                                RoomToSave.Room_ID        = (from room in contextDB.Room
                                                             where room.RoomNameUnique.ToString() == RoomSelectedLB.Items[i].ToString()
                                                             select room.Id).First();
                            }
                            else if (contextDB.RoomReserved.Any(x => x.Room.RoomNameUnique == RoomSelectedLB.Items[i].ToString() && x.Reservation_ID == ReseravtionID))
                            {
//MessageBox.Show("istnieje w bazie danych przedluz moja rezerwacje");
                            }
                            contextDB.SubmitChanges();
                        }

                        for (int i = 0; i < query2.Count; i++)
                        {
                            if (!RoomSelectedLB.Items.Contains(query2[i].Room.RoomNameUnique) && query2[i].Reservation_ID.Equals(ReseravtionID))
                            {
//MessageBox.Show("nie istnieje w listboxie ale istnieje w bazie usun mnie");

                                RoomToSave = query2[i];
                                contextDB.RoomReserved.DeleteOnSubmit(RoomToSave);
                                contextDB.SubmitChanges();
                            }
                        }
                    } // edit roomreservation


                    // load database in Mainmenu Form and close active form
                    AddOrEdit = 1;
                    var mainForm = Application.OpenForms.OfType <MainMenu>().Single();
                    mainForm.RefreshData();
                    this.Close();
                }   // end of adding and editing reservation and roomreservation
                else if (AllRoomsGotGuest == false || LabelResOn.Text == "EMPTY")
                {
                    string       error1         = "Failed to make a reservation, please check that every room has a guest";
                    CustomDialog AddReservation = new CustomDialog(error1, 1);
                    AddReservation.ShowDialog();
                }
            }
            else
            {
                string       error2         = "Failed to make a reservation, please check that you have selected a room";
                CustomDialog AddReservation = new CustomDialog(error2, 1);
                AddReservation.ShowDialog();
            }
        }