示例#1
0
        public BookingExtraSelection CreateBookingExtraSelection(BookingExtraSelection bookingExtraSelection, BookingExtra extra, Customer theCustomer, PortugalVillasContext db)
        {
            bookingExtraSelection.BESCurrencyConversionSymbol = theCustomer.PreferredCurrencySymbol;
            bookingExtraSelection.BESPreferredCurrency        = theCustomer.PreferredCurrency;

            var cc = new CurrencyConverterController();


            long?currentBookingTypeID = bookingExtraSelection.GetBookingExtraTypeIDFromBookingExtraSelection();

            bookingExtraSelection.CustomerID = theCustomer.CustomerID;

            //the price already needs to be assigned
            bookingExtraSelection.BESPrice = BookingExtraSelection.GetBookingExtraPrice(bookingExtraSelection, db);
            bookingExtraSelection.BESExtraServicesPrice = BookingExtraSelection.CalculateBookingExtraAdditionalCostsAndAssignToThisBooking(bookingExtraSelection, db);

            bookingExtraSelection.BESTotalServicesPrice = BookingExtraSelection.GetBookingExtraTotalServicesPrice(bookingExtraSelection, db);


            bookingExtraSelection.WhenCreated = DateTime.Now;

            //calc number of guests
            if (bookingExtraSelection.NumberOfGuests == null || bookingExtraSelection.NumberOfGuests == 0)
            {
                bookingExtraSelection.CalculateNoOfGuests();
            }

            //if not UK need to do currency conversion
            if (theCustomer.Country.ToLower() != "united kingdom" && ConfigurationManager.AppSettings["defaultCurrency"] == "GBP")
            {
                var baseCurrency = "GBP";
                var newCurrency  = "EUR";

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

                try
                {
                    bookingExtraSelection.BESPrice = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESPrice);
                    bookingExtraSelection.BESCurrencyConversionPrice = bookingExtraSelection.BESPrice;
                    bookingExtraSelection.BESExtraServicesPrice      = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESExtraServicesPrice);
                    bookingExtraSelection.BESTotalServicesPrice      = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESTotalServicesPrice);



                    //set exchange rate
                    bookingExtraSelection.BESCurrencyConversionSymbol = exchangeRateFromDB.CurrencyExchangeSymbol;
                    bookingExtraSelection.BESCurrencyExchangeRate     = exchangeRateFromDB.CurrencyExchangeRate;
                    bookingExtraSelection.BESPreferredCurrency        = "EUR";
                }
                catch (Exception)
                {
                    throw;
                }
            }

            else if (ConfigurationManager.AppSettings["defaultCurrency"] == "USD")
            {
                var baseCurrency = "GBP";
                var newCurrency  = "USD";

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

                try
                {
                    bookingExtraSelection.BESPrice = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESPrice);
                    bookingExtraSelection.BESCurrencyConversionPrice = bookingExtraSelection.BESPrice;
                    bookingExtraSelection.BESExtraServicesPrice      = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESExtraServicesPrice);
                    bookingExtraSelection.BESTotalServicesPrice      = cc.ConvertCurrency(baseCurrency, newCurrency, (decimal)bookingExtraSelection.BESTotalServicesPrice);


                    //set exchange rate
                    bookingExtraSelection.BESCurrencyConversionSymbol = exchangeRateFromDB.CurrencyExchangeSymbol;
                    bookingExtraSelection.BESCurrencyExchangeRate     = exchangeRateFromDB.CurrencyExchangeRate;
                    bookingExtraSelection.BESPreferredCurrency        = "USD";
                }
                catch (Exception)
                {
                    throw;
                }
            }

            //generate reference
            var refGenService = new ReferenceGenerationService();

            bookingExtraSelection.BookingExtraPRCReference = refGenService.GenerateBESReference(bookingExtraSelection, extra);


            if (ModelState.IsValid)
            {
                db.BookingExtraSelections.Add(bookingExtraSelection);
                if (db.SaveChanges() > 0)
                {
                    //generate reference with ID
                    bookingExtraSelection.BookingExtraPRCReference = refGenService.GenerateBESReference(bookingExtraSelection, extra);

                    db.Entry(bookingExtraSelection).State = EntityState.Modified;
                    db.SaveChanges();
                    return(bookingExtraSelection);
                }
            }
            throw new Exception();
        }