private decimal? MakeCurrencyExchangeRequest(CurrencyExchange ce)
        {
            //request to webservice 
            try
            {
                string requestURL = string.Format("http://www.freecurrencyconverterapi.com/api/convert?q={0}", ce.CurrencyExchangeName);


                WebClient client = new WebClient();
                var response = client.DownloadString(requestURL);

                JObject root = JObject.Parse(response);
                //JArray items = new JArray(root);


                var newAmountForCurrency = (decimal)root["results"][ce.CurrencyExchangeName]["val"];

                return newAmountForCurrency;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
 public ActionResult Edit(CurrencyExchange currencyexchange)
 {
     if (ModelState.IsValid)
     {
         db.Entry(currencyexchange).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(currencyexchange);
 }
        public ActionResult Create(CurrencyExchange currencyexchange)
        {
            if (ModelState.IsValid)
            {
                db.CurrencyExchanges.Add(currencyexchange);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(currencyexchange);
        }
        public int UpdateCurrency(CurrencyExchange theCurrency)
        {
            try
            {
                theCurrency.CurrencyExchangeRate = MakeCurrencyExchangeRequest(theCurrency);
                theCurrency.LastUpdated = DateTime.Now;
                //update the db
                using (var db = new PortVillasContext())
                {
                    db.Entry(theCurrency).State = EntityState.Modified;
                    db.SaveChanges();

                    return 0;
                }

            }
            catch (Exception ex)
            {
                //log
               /* this.mailService.Mail("[email protected]!", "Problem in PRC with currency converter");*/
                throw ex;
            }
        }
        public Booking CreateBooking(Booking booking, Property property, Customer theCustomer, PortugalVillasContext db)
        {

            //set default currency

            booking.BookingCurrencyConversionSymbol = theCustomer.PreferredCurrencySymbol;
            booking.BookingPreferredCurrency = theCustomer.PreferredCurrency;

            var cc = new CurrencyConverterController();



            //if the booking is not in GBP convert to EUROS



            //NEED TO CONVERT THE CURRENCY BASED ON WHETHER IT NEEDS TO BE EU OR UK





            int adults, kids, infants;

            adults = booking.NumberOfAdults ?? 0;
            kids = booking.NumberOfChildren ?? 0;
            infants = booking.NumberOfInfants ?? 0;


            booking.NumberOfAdults = adults;
            booking.NumberOfChildren = kids;
            booking.NumberOfInfants = infants;



            booking.CustomerID = theCustomer.CustomerID;
            booking.BookingTypeID = 1; //always a property booking

            booking.NumberOfGuests = adults + kids + infants;
            booking.TotalNumberOfMinors = kids + infants;
            booking.NumberOfNights = GeneralStaticHelperMethods.CalculateNoofNights(booking.StartDate, booking.EndDate);


            try
            {
                var exchangeRateFromDB = new CurrencyExchange();
                var baseCurrency = "";
                var newCurrency = "";

                booking.NumberOfNights = GeneralStaticHelperMethods.CalculateNoofNights(booking.StartDate,
                    booking.EndDate);
                booking.CalculateBookingPricingForAPropertyBooking(db);
                booking.CalculateExtrasPriceForAPropertyBooking(property, booking, db);
                //set this now because need to convert it
                booking.SetBreakageDepositDueDate(); //1 month before
                booking.SetBreakageDepositAmount(); //depends on property

                booking.BookingCurrencyLongName = "G.B. Pounds Sterling";
                //NOW CONVERT CURRENCY IF NECESSARY SO OTHER CALCS ARE CORRECT
                //CHANGE THIS!!! IT'S USING HIDDEN EXTERNAL DEPENDENY
                booking.BookingCurrencyConversionSymbol = "£";
                booking.BookingCurrencyExchangeRate = 1.00M;
                booking.BookingPreferredCurrency = "GBP";


                if (theCustomer.Country.ToLower() != "united kingdom" && ConfigurationManager.AppSettings["defaultCurrency"] == "GBP")
                {

                    //euro strategy
                    baseCurrency = "GBP";
                    newCurrency = "EUR";


                    //set exchange rate
                    booking.BookingCurrencyExchangeRate = exchangeRateFromDB.CurrencyExchangeRate;
                    booking.BookingCurrencyLongName = "Euros";
                    booking.BookingCurrencyConversionSymbol = exchangeRateFromDB.CurrencyExchangeSymbol;
                    booking.BookingCurrencyExchangeRate = exchangeRateFromDB.CurrencyExchangeRate;
                    booking.BookingPreferredCurrency = "EUR";

                    try
                    {
                        booking.BookingPrice = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)booking.BookingPrice);
                        booking.BookingCurrencyConversionPrice = booking.BookingPrice;

                        booking.TowelsPrice = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)booking.TowelsPrice);
                        booking.MidVactionCleaningPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.MidVactionCleaningPrice);
                        booking.SwimmingPoolHeatingPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.SwimmingPoolHeatingPrice);
                        booking.ExtraLininSetPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.ExtraLininSetPrice);
                        booking.BreakageDeposit = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.BreakageDeposit);
                        booking.CleaningPostVisitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.CleaningPostVisitPrice);
                        booking.HeatingPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.HeatingPrice);





                    }
                    catch (Exception)
                    {

                        throw;
                    }

                }

                //ALL BOOKINGS FOR US SYSTEM IN USD
                else if (ConfigurationManager.AppSettings["defaultCurrency"] == "USD")
                {
                    baseCurrency = "GBP";
                    newCurrency = "USD";

                    exchangeRateFromDB =
                        db.CurrencyExchanges.First(x => x.CurrencyExchangeName == "GBP-USD");

                    //set exchange rate and currencies
                    booking.BookingCurrencyExchangeRate = exchangeRateFromDB.CurrencyExchangeRate;
                    booking.BookingCurrencyLongName = "U.S. Dollars";
                    booking.BookingCurrencyConversionSymbol = exchangeRateFromDB.CurrencyExchangeSymbol;
                    booking.BookingCurrencyExchangeRate = exchangeRateFromDB.CurrencyExchangeRate;
                    booking.BookingPreferredCurrency = "USD";




                }

                //do all cuurency conversions if not a UK booking
                if (theCustomer.Country.ToLower() != "united kingdom" || !ConfigurationManager.AppSettings["defaultCurrency"].Equals("GBP"))
                {
                    try
                    {
                        booking.BookingPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.BookingPrice);
                        booking.BookingCurrencyConversionPrice = booking.BookingPrice;

                        booking.TowelsPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.TowelsPrice);
                        booking.MidVactionCleaningPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.MidVactionCleaningPrice);
                        booking.SwimmingPoolHeatingPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.SwimmingPoolHeatingPrice);
                        booking.ExtraLininSetPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.ExtraLininSetPrice);
                        booking.BreakageDeposit = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.BreakageDeposit);
                        booking.CleaningPostVisitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.CleaningPostVisitPrice);
                        booking.HeatingPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.HeatingPrice);


                        booking.HeatingUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.HeatingUnitPrice);
                        booking.CleaningPostVisitUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.CleaningPostVisitUnitPrice);
                        booking.ExtraLininSetUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.ExtraLininSetUnitPrice);
                        booking.MidVactionCleaningUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.MidVactionCleaningUnitPrice);
                        booking.SwimmingPoolHeatingUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.SwimmingPoolHeatingUnitPrice);
                        booking.TowelsUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.TowelsUnitPrice);
                        booking.FirewoodUnitPrice = cc.ConvertCurrency(baseCurrency, newCurrency,
                            (decimal)booking.FirewoodUnitPrice);

                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }

            }
            catch (Exception ex)
            {
                /*Response.Redirect("http://" + Request.Url.Authority + "/Error/PropertyErrorSelection");*/
                //return RedirectToAction("PropertyErrorSelection", "Error", new { propID = CartBooking.PropertyID });                    
            }


            //CURRENCY MUST BE CONVERTED BEFORE THESE METHODS ARE CALLED

            //call meths to set dates
            booking.SetInitalDepositDate();
            booking.SetInitialDepositAmount();

            booking.SetRentalBalanceDueDate(); //1 month before
            booking.CalculateFinalRentalPaymentAmount(); //extrasSummedPrice + price - deposit


            booking.SetBreakageDepositRemittanceDate();
            booking.SetBreakageDepositRemittanceAmount();//1 month after trip end?
            //booking.SetFinalRentalPayment(); //price - deposit

            booking.SetHomeownerAndPRCComissionAmount(db);

            booking.CreationDate = DateTime.Now;


            booking.Cancelled = false;
            booking.Confirmed = false; //if they pay by paypal later, we can update;




            var refGenService = new ReferenceGenerationService();
            booking.BookingPRCReference = refGenService.GenerateBookingReference(booking, property);


            //if (ModelState.IsValid)
            //{

            db.Bookings.Add(booking);

            if (db.SaveChanges() > 0)
            {

                if (booking.BookingID > 0)
                {
                    booking.BookingPRCReference = refGenService.GenerateBookingReference(booking, property);

                    db.Entry(booking).State = EntityState.Modified;
                    db.SaveChanges();

                }





            }
            return booking;
        }