public static List <ShippingRule> ToFlatRateModel(this IQueryable <shippingRule> methods, Currency currency, out string everywhereElseCost) { var ordered = methods.OrderBy(x => x.country); var rules = new List <ShippingRule>(); everywhereElseCost = ""; foreach (var entry in ordered) { // skip states and non zero match values if (!string.IsNullOrEmpty(entry.state) || entry.matchvalue != 0) { continue; } // exclude everywhere else entry if (!entry.country.HasValue) { everywhereElseCost = entry.cost.ToString("n" + currency.decimalCount); continue; } var country = Country.GetCountry(entry.country.Value); var rule = new ShippingRule { id = entry.id.ToString(), cost = entry.cost.ToString("n" + currency.decimalCount), country = country.name, countryid = entry.country.Value, currency = currency.code }; rules.Add(rule); } return(rules); }
public static IEnumerable <ShippingRule> ToModel(this RateResponse response, Currency currency) { response.ValidateResponse(); var rules = new List <ShippingRule>(); var order = response.Order[0]; foreach (var quote in order.Quotes) { var method = new ShippingRule { id = quote.method, name = quote.Service, cost = quote.Cost.value.ToString(), currency = quote.Cost.currency }; if (currency.code != "USD") { method.cost = CurrencyConverter.Instance.Convert("USD", currency.code, quote.Cost.value).ToString("n" + currency. decimalCount); } var min = quote.DeliveryEstimate.Minimum.value; var max = quote.DeliveryEstimate.Maximum.value; var timeunit = quote.DeliveryEstimate.Minimum.units; if (timeunit == "days") { timeunit = "day"; } else { Syslog.Write(string.Format("Unknown timeunit: {0}", timeunit)); } if (min == max) { if (min == 1) { method.deliveryEstimate = string.Format("1 {0}", timeunit.Substring(0, timeunit.Length)); } else { method.deliveryEstimate = string.Format("{0} {1}s", min, timeunit); } } else { method.deliveryEstimate = string.Format("{0} to {1} {2}s", min, max, timeunit); } rules.Add(method); } return(rules); }
public static List <ShippingGroup> ToModel(this IQueryable <shippingRule> rules, Currency currency, bool ismetric) { // sort first var ordered = rules.OrderBy(x => x.country).ThenBy(x => x.state); var groups = new List <ShippingGroup>(); int? countryid = null; string statename = ""; ShippingGroup countrygroup = null; ShippingGroup stategroup = null; foreach (var rule in ordered) { string countryname = "Everywhere Else"; if (rule.country.HasValue) { var topcountry = Country.GetCountry(rule.country.Value); countryname = topcountry.name; } // if countryid is null then this is the first time // if not the same then is new country group if (!countryid.HasValue || rule.country != countryid.Value) { /////////// new country countrygroup = new ShippingGroup(); countrygroup.name = countryname; groups.Add(countrygroup); statename = ""; } countryid = rule.country; if (string.IsNullOrEmpty(rule.state)) { ////////////// country level var shippingRule = new ShippingRule { id = rule.id.ToString(), cost = rule.cost.ToString("n" + currency.decimalCount), countryid = rule.country, country = countryname, matchValue = rule.matchvalue.ToString("n2"), name = rule.name, ruleType = (RuleType)rule.ruletype }; shippingRule.matchValue = rule.matchvalue.ToMatchValueDisplay(shippingRule.ruleType, currency, ismetric); countrygroup.AddRule(shippingRule); } else { if (string.IsNullOrEmpty(statename) || rule.state != statename) { ////// new state stategroup = new ShippingGroup(); stategroup.name = rule.state.ToStateName(countryid.Value.ToString()); statename = rule.state; countrygroup.AddSubGroup(stategroup); } /////////// state level var shippingRule = new ShippingRule { id = rule.id.ToString(), cost = rule.cost.ToString("n" + currency.decimalCount), countryid = rule.country, state = rule.state, matchValue = rule.matchvalue.ToString("n2"), name = rule.name, ruleType = (RuleType)rule.ruletype }; shippingRule.country = "Everywhere Else"; if (rule.country.HasValue) { shippingRule.country = Country.GetCountry(rule.country.Value).name; } shippingRule.matchValue = rule.matchvalue.ToMatchValueDisplay(shippingRule.ruleType, currency, ismetric); stategroup.AddRule(shippingRule); } } return(groups); }
public void AddRule(ShippingRule rule) { rules.Add(rule); }