public static Money ConvertCurrency(Money money, Currency currency) { var retVal = !CurrencyFormatter.CanBeConverted(money.Currency, currency) ? money : CurrencyFormatter.ConvertCurrency(money, currency); return(retVal); }
/// <summary> /// Processes the shipments. /// </summary> private void ProcessShipments() { ShippingMethodDto methods = ShippingManager.GetShippingMethods(/*Thread.CurrentThread.CurrentUICulture.Name*/ String.Empty); OrderGroup order = OrderGroup; // request rates, make sure we request rates not bound to selected delivery method foreach (OrderForm form in order.OrderForms) { foreach (Shipment shipment in form.Shipments) { bool processThisShipment = true; string discountName = "@ShipmentSkipRateCalc"; // If you find the shipment discount which represents if (shipment.Discounts.Cast <ShipmentDiscount>().Any(x => x.ShipmentId == shipment.ShipmentId && x.DiscountName.Equals(discountName))) { processThisShipment = false; } if (!processThisShipment) { continue; } ShippingMethodDto.ShippingMethodRow row = methods.ShippingMethod.FindByShippingMethodId(shipment.ShippingMethodId); // If shipping method is not found, set it to 0 and continue if (row == null) { Logger.Info(String.Format("Total shipment is 0 so skip shipment calculations.")); shipment.ShipmentTotal = 0; continue; } // Check if package contains shippable items, if it does not use the default shipping method instead of the one specified Logger.Debug(String.Format("Getting the type \"{0}\".", row.ShippingOptionRow.ClassName)); Type type = Type.GetType(row.ShippingOptionRow.ClassName); if (type == null) { throw new TypeInitializationException(row.ShippingOptionRow.ClassName, null); } Logger.Debug(String.Format("Creating instance of \"{0}\".", type.Name)); IShippingGateway provider = null; var orderMarket = ServiceLocator.Current.GetInstance <IMarketService>().GetMarket(order.MarketId); if (orderMarket != null) { provider = (IShippingGateway)Activator.CreateInstance(type, orderMarket); } else { provider = (IShippingGateway)Activator.CreateInstance(type); } Logger.Debug(String.Format("Calculating the rates.")); string message = String.Empty; ShippingRate rate = provider.GetRate(row.ShippingMethodId, shipment, ref message); if (rate != null) { Logger.Debug(String.Format("Rates calculated.")); // check if shipment currency is convertable to Billing currency, and then convert it if (!CurrencyFormatter.CanBeConverted(rate.Money, order.BillingCurrency)) { Logger.Debug(String.Format("Cannot convert selected shipping's currency({0}) to current currency({1}).", rate.Money.Currency.CurrencyCode, order.BillingCurrency)); throw new Exception(String.Format("Cannot convert selected shipping's currency({0}) to current currency({1}).", rate.Money.Currency.CurrencyCode, order.BillingCurrency)); } else { Money convertedRate = CurrencyFormatter.ConvertCurrency(rate.Money, order.BillingCurrency); shipment.ShipmentTotal = convertedRate.Amount; } } else { Warnings[String.Concat("NoShipmentRateFound-", shipment.ShippingMethodName)] = String.Concat("No rates have been found for ", shipment.ShippingMethodName); Logger.Debug(String.Format("No rates have been found.")); } } } }