/// <summary> /// Calculates total price for the booking when the car is returned, an returns the updated /// booking with the updated total price as well as updated "enddate" according to which date /// the car was actually returned. /// </summary> /// <param name="bookingNumber">Booking id connected to the reservation that is now ending (car is returned).</param> /// <param name="returnDate">Date of the actual return of the car.</param> /// <param name="currentCarMilage">The current car milage when the car is returned.</param> /// <returns>The booking object with upddated totalprice for the rental after the car is returned.</returns> public Booking ReturnRentedCar(int bookingNumber, DateTime returnDate, int currentCarMilage) { var theBooking = repos.DataSet <Booking>().Include(c => c.Car).SingleOrDefault(b => b.Id == bookingNumber); if (theBooking == null) { throw new DBItemNotFoundException("There is no booking with the given bookingnumber, please check entered number."); } if (returnDate < theBooking.RentalDate) { throw new DateOutOfRangeException("ReturnDate for the car can not be before startdate of the booking/reservation."); } if (theBooking.TotalCostOfRent != null) { throw new CarAlreadyReturnedException("Car has already been registered as returned in the system."); } if (currentCarMilage < theBooking.Car.TotalCarMilage) { throw new ArgumentException("Can not set total car milage to a lower value than it was in the start of the rental period."); } var numberOfDrivenKilometers = currentCarMilage - theBooking.Car.TotalCarMilage; theBooking.DateOfRentalEnd = returnDate; theBooking.KmDrivenWithinReservation = numberOfDrivenKilometers; var totalRentalPrice = CalculatePriceOfCarRental(theBooking); theBooking.TotalCostOfRent = totalRentalPrice; repos.SaveChanges(); return(theBooking); }