/// <summary> /// Find the best price given the price break /// </summary> /// <param name="desiredCurrency">The 3 character currency code</param> /// <param name="offer">The offer to search within</param> /// <param name="qty">The quantity to search for (i.e. QTY required)</param> /// <param name="ignoreMoq">Indicates if the MOQ should be considered when looking up pricing</param> /// <returns>The minimum price available for the specified QTY, in Current-Culture string format</returns> public static string OffersMinPrice(string desiredCurrency, ApiV4Schema.PartOffer offer, int qty, bool ignoreMoq) { if (string.IsNullOrEmpty(desiredCurrency) || (offer == null)) { return(string.Empty); } double priceMin = double.MaxValue; if (offer.prices.Keys.Contains(desiredCurrency)) { string prices = offer.prices[desiredCurrency]; if (!string.IsNullOrEmpty(prices)) { // Get price info var priceInfo = JsonConvert.DeserializeObject <dynamic>(prices); bool mouserSpecialCase = ((offer.seller.name == "Mouser") && (priceInfo.Count == 1) && (priceInfo[0][0] == 1)); for (int i = 0; (priceInfo != null) && (i < priceInfo.Count); i++) { try { int priceTupleQty = Convert.ToInt32(priceInfo[i][0], CultureInfo.CreateSpecificCulture("en-US")); double priceTuplePrice = Convert.ToDouble(priceInfo[i][1], CultureInfo.CreateSpecificCulture("en-US")); if (mouserSpecialCase) { if (qty >= priceTupleQty) { priceMin = priceTuplePrice; } } else if (qty >= priceTupleQty) { if (ignoreMoq || (string.IsNullOrEmpty(offer.moq) || (qty >= Convert.ToInt32(offer.moq, CultureInfo.CreateSpecificCulture("en-US"))))) { if (priceTuplePrice < priceMin) { priceMin = priceTuplePrice; } } } } catch (FormatException) { /* Do nothing */ } catch (OverflowException) { /* Do nothing */ } } } } if (priceMin == double.MaxValue) { return(string.Empty); } else { return(priceMin.ToString("F5", CultureInfo.CurrentCulture)); } }
/// <summary> /// Find the preferred currency, or whatever else /// </summary> /// <param name="desiredCurrency">The desired currency to get the price breaks from</param> /// <param name="offer">The offer to search within</param> /// <returns>Currency string</returns> public static string FindPreferredCurrency(string desiredCurrency, ApiV4Schema.PartOffer offer) { string ret = string.Empty; foreach (string currency in offer.prices.Keys) { string prices = offer.prices[currency]; if (!string.IsNullOrEmpty(prices)) { if (currency == desiredCurrency) { // We found at least one price with the specified currency, so return it. return(desiredCurrency); } else { // Well, we didn't find what we were looking for, but at least give something. ret = currency; } } } return(ret); }