public int UpdateCurrencyExchangeRates()
        {

            try
            {
                //new up everything
                IMailService mailerService = new MaintainanceMailer();
                CurrencyExchangeService ces = new CurrencyExchangeService(mailerService);
                

                //get all the currencies
                using (var _db = new PortVillasContext())
                {

                    var currencies = _db.CurrencyExchanges.ToList();

                    foreach (var currency in currencies)
                    {
                        ces.UpdateCurrency(currency);
                    }

                }

                return 0;
            }
            catch (Exception ex)
            {

                
                
            }
            return -1;


        }
        public static List <BookingExternal> GetExternalBookingsForAProperty(long propertyID)
        {
            using (var db = new PortVillasContext())
            {
                var bookings = db.BookingExternals.ToList();

                return(bookings);
            }
        }
 public static List <AirportDestination> GetAllAirportDestinations()
 {
     {
         using (var portCon = new PortVillasContext())
         {
             return(portCon.AirportDestinations.ToList());
         }
     }
 }
        public ActionResult GetACustomer(int CustomerID)
        {
            using (var _db = new PortVillasContext())
            {
                var data = _db.Customers.Where(x => x.CustomerID == CustomerID).FirstOrDefault();
                ViewData["currentCustomer"] = data;

                return PartialView("CustomerEdit", data);
            }
        }
 public static List<AirportDestination> GetAllAirportDestinations()
 {
     {
         using (var portCon = new PortVillasContext())
         {
             return portCon.AirportDestinations.ToList();
         }
         
     }
 }
        public static List<BookingExternal> GetExternalBookingsForAProperty(long propertyID)
        {
            using (var db = new PortVillasContext())
            {

                var bookings = db.BookingExternals.ToList();

                return bookings;

            }
        }
        public ActionResult Edit(Customer customer)
        {

            if (ModelState.IsValid)
            {
                using (var db = new PortVillasContext())
                {
                    db.Entry(customer).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return RedirectToAction("Index");
        }
        //
        // GET: /TestAjax/

        public ActionResult Index()
        {
            using (var _db = new PortVillasContext())
            {
                var data = _db.Customers.ToList();

                ViewData["currentCustomer"] = _db.Customers.Where(x => x.CustomerID == 1).FirstOrDefault();


                return View(data);
            }


        }
        public static List <Booking> GetExternalBookingsAsBookings(long propertyID)
        {
            using (var db = new PortVillasContext())
            {
                var            bookings = db.BookingExternals.Where(x => x.PropertyID == propertyID).ToList();
                List <Booking> cal      = new List <Booking>();

                foreach (var bookingExternal in bookings)
                {
                    cal.Add(new Booking
                    {
                        StartDate  = bookingExternal.StartDate,
                        EndDate    = bookingExternal.EndDate,
                        PropertyID = bookingExternal.PropertyID,
                        Confirmed  = true,
                        Cancelled  = false
                    });
                }

                return(cal);
            }
        }
        public static List<Booking> GetExternalBookingsAsBookings(long propertyID)
        {
            using (var db = new PortVillasContext())
            {

                var bookings = db.BookingExternals.Where(x=>x.PropertyID == propertyID).ToList();
                List<Booking> cal = new List<Booking>();

                foreach (var bookingExternal in bookings)
                {
                       cal.Add(new Booking
                       {
                           StartDate = bookingExternal.StartDate,
                           EndDate =  bookingExternal.EndDate,
                           PropertyID = bookingExternal.PropertyID,
                           Confirmed = true,
                           Cancelled = false
                       });
                }

                return cal;
            }
        }
        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 void TestCurrencyService()
        {
            //new up everything
            IMailService mailerService = new MaintainanceMailer();
            CurrencyExchangeService ces = new CurrencyExchangeService(mailerService);

            //send a cheeky testmail


            //get all the currencies
            using (var _db = new PortVillasContext())
            {

                var currencies = _db.CurrencyExchanges.ToList();

                foreach (var currency in currencies)
                {
                    ces.UpdateCurrency(currency);
                }

            }

        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();


            var binder = new BootstrapVillas.CustomModelBinders.DateTimeUKModelBinder();
            ModelBinders.Binders.Add(typeof(DateTime), binder);
            ModelBinders.Binders.Add(typeof(DateTime?), binder);


            Database.SetInitializer<PortugalVillasContext>(null);
            Database.SetInitializer<UsersContext>(null);

            //membership provider
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("PortugalVillasContext", "UserProfile", "UserId", "UserName",
                    true);
            }

            //update currency
            try
            {
                var autoMate = new PRCAutomations();
                var ok = autoMate.UpdateCurrencyExchangeRates();

            }
            catch (Exception ex)
            {

                //ignore
            }

            //get a token for the translator every 500 seconds and store it in the viewbag
            Timer translateTimer = new Timer(600000);
            translateTimer.Elapsed += translateTimer_Elapsed;
            Application["TranslateToken"] = GetBingAPITranslationToken();
            translateTimer.Enabled = true;
            GC.KeepAlive(translateTimer);


            //update currency every hr  
            Timer currencyUpdateTimer = new Timer(3600000);
            currencyUpdateTimer.Elapsed += currencyUpdateTimer_Elapsed;
            currencyUpdateTimer.Enabled = true;
            GC.KeepAlive(currencyUpdateTimer);


            InitCache();


            var defaultCurrencyEnum = (CurrencyEnum)Enum.Parse(typeof(CurrencyEnum), ConfigurationManager.AppSettings["defaultCurrency"]);
            var currencies = new List<CurrencyExchange>();

            using (var db = new PortVillasContext())
            {
                currencies = db.CurrencyExchanges.ToList();
            }


            //structuremap
            IContainer container = new Container(cfg =>
            {
                cfg.Scan(x => x.WithDefaultConventions());

                cfg.For<ICurrencyConvert>()
                    .Singleton()
                    .Use(() => CurrencyConverterFactory.GetCurrencyConverter(defaultCurrencyEnum, currencies));

            }
            
            );
            DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));


        }
            protected void TestSetup()
            {
                db = new PortVillasContext();

            }
        public ActionResult ProcessForm(Customer obj)
        {


            using (var _db = new PortVillasContext())
            {
                var data = _db.Customers.Where(x => x.CustomerID == obj.CustomerID).SingleOrDefault();

                ViewData["currentCustomer"] = data;

                return PartialView("CustomerEdit", data);
            }



        }