public bool GetPropertyPriceRange() { try{ this.propertyPriceRange = Property.GetPriceRangeForProperty(Property.GetPropertyByID(this.propertyID)); //code to stop propeties without a pricing failing if (this.propertyPriceRange.MinPrice.Equals(0) || this.propertyPriceRange.MaxPrice == null) { this.propertyPriceRange.MinPrice = 1; this.propertyPriceRange.MaxPrice = 1; } //end decimal tempDecimal = (decimal)this.propertyPriceRange.MinPrice; this.propertyPriceRange.MinPrice = Decimal.Round(tempDecimal); tempDecimal = (decimal)this.propertyPriceRange.MaxPrice; this.propertyPriceRange.MaxPrice = Decimal.Round(tempDecimal); return true; } catch (Exception ex) { throw ex; } }
//constructor public PropertySearch(PriceRange theSearchPriceRange = null, long propertyTypeID = 0, long vacationTypeID = 0, string poolType = null, int maxSleeps = 0, int noOfAdults = 0, int noOfChildren = 0, int noOfInfants = 0) { //assign params to members this.TheSearchPriceRange = theSearchPriceRange; this.PropertyTypeID = propertyTypeID; this.VacationTypeID = vacationTypeID; this.PoolType = poolType; this.MaxSleeps = maxSleeps; this.NoOfAdults = noOfAdults; this.NoOfChildren = noOfChildren; this.NoOfInfants = noOfInfants; //build query based on params passed //if not, just query properties }
//////////////////OVERALL IMPLEMENATION NOTES ON METHODS///////////////// /// /// /* * passesdRange1 is what I use as the customer or form range * comparisonRange2 is what I generally get from the database PropertyPricing table */ ///////////END NOTES /* need to check against a specific property's overall range, if there is no booking dates, or a range withing certain months if there are */ //1st method public static bool IsPropertyWithinPriceRange(Property aProperty, PriceRange customerRange) { //check the passed property has has a price range, if not create one if(aProperty.PriceRange == null) aProperty.CreatePriceRange(); //need to check AGAIN if a pricerange has been created - if there's nothing in the DB, it won't be if (aProperty.PriceRange != null) //this is saying - return true if the result is not null, else return false. //LOOK HOW VARS ARE PASSED return IsPriceWithinRange(customerRange, aProperty.PriceRange) != null; else { return false; } }
public bool GetPropertyPriceRange() { try{ this.propertyPriceRange = Property.GetPriceRangeForProperty(Property.GetPropertyByID(this.propertyID)); decimal tempDecimal = (decimal)this.propertyPriceRange.MinPrice; this.propertyPriceRange.MinPrice = Decimal.Round(tempDecimal); tempDecimal = (decimal)this.propertyPriceRange.MaxPrice; this.propertyPriceRange.MaxPrice = Decimal.Round(tempDecimal); return true; } catch (Exception ex) { throw ex; } }
//1st overload - works for a list of ranges, compares every one and returns them in a list if they are within the range of 'passedrange1' public static List <PriceRange> IsPriceWithinRange(PriceRange customerRequestedRange, List <PriceRange> comparisonRange2) { List <PriceRange> pricesInRange = new List <PriceRange>(); foreach (PriceRange priceRange in comparisonRange2) { if (IsPriceWithinRange(customerRequestedRange, priceRange) != null) { pricesInRange.Add(IsPriceWithinRange(customerRequestedRange, priceRange)); } } return(pricesInRange); }
//1st overload - works for a list of ranges, compares every one and returns them in a list if they are within the range of 'passedrange1' public static List<PriceRange> IsPriceWithinRange( PriceRange passedRange1, List<PriceRange> comparisonRange2) { List<PriceRange> pricesInRange = new List<PriceRange>(); foreach(PriceRange priceRange in comparisonRange2) { if (IsPriceWithinRange(passedRange1, priceRange) != null) { pricesInRange.Add(IsPriceWithinRange(passedRange1, priceRange)); } } return pricesInRange; }
public static PriceRange IsPriceWithinRange(PriceRange passedRange1, PriceRange comparisonRange2) { if (passedRange1.MinPrice <= comparisonRange2.MinPrice) { if (passedRange1.MaxPrice >= comparisonRange2.MaxPrice) { return comparisonRange2; } } //check one range against another and return the Range if it's within it (i.e. 'ok') //check the min is higher or equal to the passed in min //if so, check the max is lower or equal to the passed in max //if that passes, return the second range return null; }
public static PriceRange IsPriceWithinRange(PriceRange customerRequestedRange, PriceRange propertyRange) { if (customerRequestedRange.MinPrice <= propertyRange.MaxPrice) { if (customerRequestedRange.MaxPrice >= propertyRange.MinPrice) { return propertyRange; } } //check one range against another and return the Range if it's within it (i.e. 'ok') //check the min is higher or equal to the passed in min //if so, check the max is lower or equal to the passed in max //if that passes, return the second range return null; }
public static PriceRange IsPriceWithinRange(PriceRange customerRequestedRange, PriceRange propertyRange) { if (customerRequestedRange.MinPrice <= propertyRange.MaxPrice) { if (customerRequestedRange.MaxPrice >= propertyRange.MinPrice) { return(propertyRange); } } //check one range against another and return the Range if it's within it (i.e. 'ok') //check the min is higher or equal to the passed in min //if so, check the max is lower or equal to the passed in max //if that passes, return the second range return(null); }
//////////////////OVERALL IMPLEMENATION NOTES ON METHODS///////////////// /// /// /* * passesdRange1 is what I use as the customer or form range * comparisonRange2 is what I generally get from the database PropertyPricing table * */ ///////////END NOTES /* * need to check against a specific property's overall range, * if there is no booking dates, or a range withing certain months if there are */ //1st method public static bool IsPropertyWithinPriceRange(Property aProperty, PriceRange customerRange) { //check the passed property has has a price range, if not create one if (aProperty.PriceRange == null) { aProperty.CreatePriceRange(); } //need to check AGAIN if a pricerange has been created - if there's nothing in the DB, it won't be if (aProperty.PriceRange != null) { //this is saying - return true if the result is not null, else return false. //LOOK HOW VARS ARE PASSED return(IsPriceWithinRange(customerRange, aProperty.PriceRange) != null); } else { return(false); } }
///////////////////////////// //// INSTANCE METHODS ///////////////////////////// //this intialised the pricerange fr the property public void CreatePriceRange() { PortugalVillasContext _db = new PortugalVillasContext(); var checkForPricings = _db.PropertyPricingSeasonalInstances .Where(x => x.PropertyID == PropertyID) .ToList(); if(checkForPricings.Count > 1) { this.PriceRange = GetPriceRangeForProperty(this.PropertyID); } }
//1st Overload of GetPriceRangeForProperty public static PriceRange GetPriceRangeForProperty(long aPropertyID) { PortugalVillasContext _db = new PortugalVillasContext(); long propertyid = aPropertyID; var thePricingMax = _db.PropertyPricingSeasonalInstances .Where(x => x.PropertyID == propertyid) .Select(x => x.Price).Max(); var thePricingMin = _db.PropertyPricingSeasonalInstances .Where(x => x.PropertyID == propertyid) .Select(x => x.Price).Min(); PriceRange thePriceRange = new PriceRange() { //it is weekly pricing MinPrice = (thePricingMin), MaxPrice = (thePricingMax), PropertyID = propertyid }; return thePriceRange; }
//1st Overload of GetPriceRangeForProperty public static PriceRange GetPriceRangeForProperty(long aPropertyID) { PortugalVillasContext _db = new PortugalVillasContext(); long propertyid = aPropertyID; var thePricingMax = _db.PropertyPricings .Where(x => x.PropertyID == propertyid) .Select(x => x.Price).Max(); var thePricingMin = _db.PropertyPricings .Where(x => x.PropertyID == propertyid) .Select(x => x.Price).Min(); PriceRange thePriceRange = new PriceRange() { //it is DAY pricing so divide by seven MinPrice = (thePricingMin / 7), MaxPrice = (thePricingMax / 7), PropertyID = propertyid }; return thePriceRange; }
public ActionResult SearchProperties(FormCollection formCollection, int page = 1, string propertyResultsSort = "", int propertyResultsAmount = 25) { ViewBag.IdentifyPage = "Property Search"; Session["CurrentSearchResultsAmount"] = propertyResultsAmount; Session["CurrentSearchResultsSort"] = propertyResultsSort; //stick values into viewback for DDL update ViewBag.propertyResultsSort = propertyResultsSort; ViewBag.propertyResultsAmount = propertyResultsAmount; //retrive values from post //based on whether there are dates create either a booking search or property search //create new object and populate - then run dynamic instance query on that object int propertyTypeID = 0; int vacationTypeID = 0; string poolType = null; int maxSleeps = 0; int noOfAdults = 0; int noOfChildren = 0; int noOfInfants = 0; DateTime? startDate = null; DateTime? endDate = null; ; try { //parse dates if (formCollection["StartDate"] != "" && formCollection["StartDate"] != null) { startDate = DateTime.ParseExact(formCollection["StartDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture); } if (formCollection["EndDate"] != "" && formCollection["EndDate"] != null) { endDate = DateTime.ParseExact(formCollection["EndDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture); } if (formCollection["StartDate1"] != "" && formCollection["StartDate1"] != null) { startDate = DateTime.ParseExact(formCollection["StartDate1"], "dd/MM/yyyy", CultureInfo.InvariantCulture); } if (formCollection["EndDate1"] != "" && formCollection["EndDate1"] != null) { endDate = DateTime.ParseExact(formCollection["EndDate1"], "dd/MM/yyyy", CultureInfo.InvariantCulture); } //parse property Type ID if (Int32.Parse(formCollection["PropertyType"]) != 0) { propertyTypeID = Int32.Parse(formCollection["PropertyType"]); } //parse region type if (Int32.Parse(formCollection["RegionType"]) != 0) { vacationTypeID = Int32.Parse(formCollection["RegionType"]); } //parse poolType if (formCollection["PoolType"] != "" && formCollection["PoolType"] != null && poolType != "0") { poolType = formCollection["PoolType"]; } //parse max sleeps if (Int32.Parse(formCollection["MaxSleeps"]) != 0) { maxSleeps = Int32.Parse(formCollection["MaxSleeps"]); } //parse no of adults if (Int32.Parse(formCollection["NoOfAdults"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfAdults"]); } //parse no of children if (Int32.Parse(formCollection["NoOfChildren"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfChildren"]); } //parse no of infants if (Int32.Parse(formCollection["NoOfInfants"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfInfants"]); } //parse price range //jsut to account for if we haven't be passed a price String[] splitPrice = "1-5000".Split('-'); if (formCollection["PriceRange"] != "" && formCollection["PriceRange"] != null) { splitPrice = null; splitPrice = formCollection["PriceRange"].Split('-'); } LinkedList<decimal> thePrices = new LinkedList<decimal>(); foreach (var thePrice in splitPrice) { int TryParseResult = 0; //try parse to check it's a number Int32.TryParse(thePrice.Trim().Replace("£", "").Replace("$", "").ToString(), out TryParseResult); var cash = (decimal)TryParseResult; if (TryParseResult != 0) { //IF SYSTEM IS US DOLLARS, NEED TO CONVERT THE NUMBER INTO POUNDS if ((string)ConfigurationManager.AppSettings["defaultCurrency"] != "GBP") { var cc = new CurrencyConverterController(); cash = cc.ConvertCurrency((string)ConfigurationManager.AppSettings["defaultCurrency"], "GBP", (decimal)cash); } thePrices.AddLast(cash); } } //end parse price range PriceRange customerQueryPriceRange = null; if (thePrices.Count > 0) { customerQueryPriceRange = new PriceRange(thePrices.Min(), thePrices.Max()); } else //pass the max range ever { customerQueryPriceRange = new PriceRange(0, 100000); } //END FORM PARSE //Now test if there are dates - if yes - create a booking query. If no, create a property query //create property query PropertySearch newSearch = new PropertySearch() { MaxSleeps = maxSleeps, NoOfAdults = noOfAdults, NoOfChildren = noOfChildren, NoOfInfants = noOfInfants, PoolType = poolType, PropertyTypeID = propertyTypeID, TheSearchPriceRange = customerQueryPriceRange, VacationTypeID = vacationTypeID }; //do the search var searchResults = newSearch.SearchForMatchingProperties(); //if there are dates, pass the search results into the BookingSearch if (startDate.HasValue && endDate.HasValue) { //create booking query searchResults = SearchBookingResultsAndReturnPropetiesWithoutAnOverlappingBooking(searchResults, (DateTime)startDate, (DateTime)endDate); } //for each sort / pager Session["LastSearchResults"] = PagedList.PagedListExtensions.ToPagedList(searchResults, page, searchResults.Count); //for fresh canvas for 'amount' sort Session["InitalCustomerSearchResults"] = PagedList.PagedListExtensions.ToPagedList(searchResults, page, searchResults.Count); //which page are we on of the results Session["LastPropertyPagerPage"] = page; ViewBag.MatchingProperties = PagedList.PagedListExtensions.ToPagedList(searchResults, page, propertyResultsAmount); Session["currentPagedPropertySearch"] = PagedList.PagedListExtensions.ToPagedList(searchResults, page, 500000); //store the last results set in the viewbag, so we can page it if necessary } catch (Exception ex) { Response.Write("Something is wrong in the SearchProperties in the Home Controller when trying to assing the variables " + "from the POST from the search form"); } //return results //create a new enquiry ViewBag.Keywords = "villa rental portugal, portugal silver coast, portugal cheap holidays, cheap holidays to portugal,silver coast portugal, portugal rental cottages, cheap all inclusive holidays to portugal"; ViewBag.Title = "Book your villa in Obidos, Nazare, Foz do Arelho or A-dos-Negros for a seaside holiday or rural townhouse with swimming pool and Internet. All types of villa, apartment are catered for in regions like Foz do Arelho, Obidos, Alfeizerao - Sao Martinho and Reguengo Grande. Stay in the lovely Salir do Porto and visit the Mafra Palace & Obidos during your vacation. With Portugal Rental Cottages, you can be up directly from the airport with our rental extras. While you stay, go on wine tasting tours to the beautiful Sanguinhal Estate and get picked up from the airport with our airport to villa transfer and return."; return View(); }
//helper method for building the query - if the passed param not null //(object or string or zero (int or long or decimal) the public List <Property> SearchForMatchingProperties() { PortugalVillasContext _db = new PortugalVillasContext(); this.propertyQuery = _db.Properties.Include(x => x.PropertyTown); propertyQuery = propertyQuery.Where(x => x.Active == true); //test if each the object is not null or zero then add a where clause if (this.PropertyTypeID.HasValue && this.PropertyTypeID != 0) { propertyQuery = propertyQuery.Where(x => x.PropertyTypeID == this.PropertyTypeID); } if (this.VacationTypeID.HasValue && this.VacationTypeID != 0) { propertyQuery = propertyQuery.Where(x => x.PropertyVacationTypeID == this.VacationTypeID); } if (!String.IsNullOrEmpty(this.PoolType)) { propertyQuery = propertyQuery.Where(x => x.SwimmingPoolType == this.PoolType); } if (this.MaxSleeps != 0) { propertyQuery = propertyQuery.Where(x => x.MaxGuests >= this.MaxSleeps); } //implicit zero else if (MaxSleeps == 0 && ((NoOfAdults + NoOfChildren + NoOfInfants) > 0)) { //there's some people, need a house big enough for them AND infants if they have if (NoOfInfants > 0) { //add infants claaus propertyQuery = propertyQuery.Where(x => x.Cots > 0); } //add where clause for total people var totalPeople = NoOfAdults + NoOfChildren + NoOfInfants; propertyQuery = propertyQuery.Where(x => x.MaxGuests >= totalPeople - NoOfInfants); } //run the search and bring back some properties List <Property> theResultsList = new List <Property>(); theResultsList = propertyQuery.ToList(); if (this.TheSearchPriceRange != null) //check it's not null i.e. that there was a price range passed { if ((this.TheSearchPriceRange.MaxPrice != 2000 | this.TheSearchPriceRange.MinPrice != 1)) { //we want to search on price because a value has been provided, so create price ranges for active properties //NB - NOW done in PriceRange.IsPropertyWithinPriceRange List <Property> theResultsListWithPrice = new List <Property>(); foreach (var property in theResultsList) { if (PriceRange.IsPropertyWithinPriceRange(property, TheSearchPriceRange)) { theResultsListWithPrice.Add(property); } } return(theResultsListWithPrice); } } //no price search, just return the list return(theResultsList); }
public ActionResult SearchProperties(FormCollection formCollection, int page = 1) { //retrive values from post //based on whether there are dates create either a booking search or property search //create new object and populate - then run dynamic instance query on that object int propertyTypeID = 0; int vacationTypeID = 0; string poolType = null; int maxSleeps = 0; int noOfAdults = 0; int noOfChildren = 0; int noOfInfants = 0; DateTime? startDate = null; DateTime? endDate = null; ; try { //parse dates if (formCollection["bookingModalStartDatePicker"] != "" && formCollection["bookingModalStartDatePicker"] != null) { startDate = DateTime.Parse(formCollection["bookingModalStartDatePicker"]); } if (formCollection["EndDate"] != "" && formCollection["EndDate"] != null) { endDate = DateTime.Parse(formCollection["EndDate"]); } //parse property Type ID if (Int32.Parse(formCollection["PropertyType"]) != 0) { propertyTypeID = Int32.Parse(formCollection["PropertyType"]); } //parse region type if (Int32.Parse(formCollection["RegionType"]) != 0) { vacationTypeID = Int32.Parse(formCollection["RegionType"]); } //parse poolType if (formCollection["PoolType"] != "" && formCollection["PoolType"] != null && poolType != "0") { poolType = formCollection["PoolType"]; } //parse max sleeps if (Int32.Parse(formCollection["MaxSleeps"]) != 0) { maxSleeps = Int32.Parse(formCollection["MaxSleeps"]); } //parse no of adults if (Int32.Parse(formCollection["NoOfAdults"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfAdults"]); } //parse no of children if (Int32.Parse(formCollection["NoOfChildren"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfChildren"]); } //parse no of infants if (Int32.Parse(formCollection["NoOfInfants"]) != 0) { maxSleeps = Int32.Parse(formCollection["NoOfInfants"]); } //parse price range String[] splitPrice = "1-1000".Split('-'); if (formCollection["PriceRange"] != "" && formCollection["PriceRange"] != null) { splitPrice = null; splitPrice = formCollection["PriceRange"].Split('-'); } LinkedList<decimal> thePrices = new LinkedList<decimal>(); foreach (var thePrice in splitPrice) { int TryParseResult = 0; //try parse to check it's a number Int32.TryParse(thePrice.Trim().Replace("£", "").ToString(), out TryParseResult); if (TryParseResult != 0) { thePrices.AddLast(Decimal.Parse(thePrice.Trim().Replace("£", ""))); } } //end parse price range PriceRange customerQueryPriceRange = null; if (thePrices.Count > 0) { customerQueryPriceRange = new PriceRange(thePrices.Min(), thePrices.Max()); } else //pass the max range ever { customerQueryPriceRange = new PriceRange(0, 100000); } //END FORM PARSE //Now test if there are dates - if yes - create a booking query. If no, create a property query if (startDate.HasValue && endDate.HasValue) { //create booking query } else { //create property query PropertySearch newSearch = new PropertySearch() { MaxSleeps = maxSleeps, NoOfAdults = noOfAdults, NoOfChildren = noOfChildren, NoOfInfants = noOfInfants, PoolType = poolType, PropertyTypeID = propertyTypeID, TheSearchPriceRange = customerQueryPriceRange, VacationTypeID = vacationTypeID }; //store the last results set in the viewbag, so we can page it if necessary Session["LastSearchResults"] = PagedList.PagedListExtensions.ToPagedList(newSearch.SearchForMatchingProperties(), page, 2000); ViewBag.MatchingProperties = PagedList.PagedListExtensions.ToPagedList(newSearch.SearchForMatchingProperties(), page, 25); } } catch (Exception ex) { Response.Write("Something is wrong in the SearchProperties in the Home Controller when trying to assing the variables " + "from the POST from the search form"); } foreach (string _formData in formCollection) { ViewData[_formData] = formCollection[_formData]; } //return results //create a new enquiry return View(); }
//1st overload - works for a list of ranges, compares every one and returns them in a list if they are within the range of 'passedrange1' public static List<PriceRange> IsPriceWithinRange( PriceRange customerRequestedRange, List<PriceRange> comparisonRange2) { List<PriceRange> pricesInRange = new List<PriceRange>(); foreach(PriceRange priceRange in comparisonRange2) { if (IsPriceWithinRange(customerRequestedRange, priceRange) != null) { pricesInRange.Add(IsPriceWithinRange(customerRequestedRange, priceRange)); } } return pricesInRange; }