//
 // GET: /Booking/Confirm
 public ActionResult Confirm(int booking_id)
 {
     var email = "";
      if (User.Identity.IsAuthenticated)
           email = Membership.GetUser().Email;
      UserObj u = CustomUserService.loadUser(email);
      if (!u.Customer)
           return RedirectToAction("Login", "Account", new {
                returnUrl = "/Booking/Confirm?booking_id=" + booking_id  } );
      BookingConfirmUI bookingConfirm = new BookingConfirmUI();
      bookingConfirm = logic.ShowBookingConfirm(booking_id, u.Id());
      return View(bookingConfirm);
 }
        public Models.ui.BookingConfirmUI CreateBooking(Models.ui.BookingCreateUI car, int userId)
        {
            var result = new BookingConfirmUI();

             var supplierObj = new SupplierBObj();
             var carObj = new CarBObj(supplierObj);
             var booking = new BookingBObj(carObj, supplierObj);

             var startDate = car.fromDate;
             var endDate = car.toDate;
             var car_reg = car.car_reg;

             var carId = db.getCarIdByReg(car_reg);

             Car carDb = db.getCar(carId);
             carObj.setId(carId);
             carObj.setDailyRate(carDb.cost_per_day);

             booking.setBookingRange(startDate, endDate);

             var dbUnavailabilities = db.listUnavailabilitiesForCar(carId);
             var unavailabilities = new List<UnavailableDateBObj>();

             foreach (var u in dbUnavailabilities)
             {
                  var unavailable = new UnavailableDateBObj { fromDate = u.start_date, toDate = u.end_date };
                  unavailabilities.Add(unavailable);
             }

             car.valid = booking.valid(unavailabilities);
             if (car.valid)
             {
                  car.amount = booking.getTotalCost();
                  UnavailableDateBObj date = booking.create();

                  var customer_id = userId;
                  result.BookingId = db.createNewBooking(customer_id, carId, startDate, endDate);

             }
             result.CarReg = car_reg;
             result.Cost = booking.getTotalCost();
             return result;
        }
        public Models.ui.BookingConfirmUI ShowBookingConfirm(int bookingId, int userId)
        {
            var result = new BookingConfirmUI();
             result.BookingId = bookingId;
             BookingConfirmDTO b = db.getBooking(bookingId, userId);
             if (b != null)
             {
                  result.CarReg = b.CarReg;
                  TimeSpan span = b.toDate - b.fromDate;
                  int numberOfDays = span.Days;
                  result.Cost = b.Cost * numberOfDays;
                  result.CustomerEmail = b.CustomerEmail;
                  result.SupplierEmail = b.SupplierEmail;
                  result.fromDate = b.fromDate;
                  result.toDate = b.toDate;
             }

             return result;
        }
        public ActionResult Create(BookingCreateUI booking)
        {
            var email = "";
             if (User.Identity.IsAuthenticated)
                  email = Membership.GetUser().Email;
             UserObj u = CustomUserService.loadUser(email);
             if (!u.Customer)
                  return RedirectToAction("Login", "Account");

             if (ModelState.IsValid)
             {
                  BookingConfirmUI bookingConfirm = new BookingConfirmUI();
                  bookingConfirm = logic.CreateBooking(booking, u.Id());
                  return RedirectToAction("Confirm", new { booking_id = bookingConfirm.BookingId });
             }

             return View(booking);
        }