private ShippingMethodAndRate GetShippingRateInfo(ShippingMethodDto.ShippingMethodRow row, Shipment shipment) { ShippingMethodAndRate returnRate = null; string nameAndRate = string.Empty; // Check if package contains shippable items, if it does not use the default shipping method instead of the one specified Type type = Type.GetType(row.ShippingOptionRow.ClassName); if (type == null) { throw new TypeInitializationException(row.ShippingOptionRow.ClassName, null); } string outputMessage = string.Empty; IShippingGateway provider = (IShippingGateway)Activator.CreateInstance(type); if (shipment != null) { ShippingRate rate = provider.GetRate(row.ShippingMethodId, shipment, ref outputMessage); nameAndRate = string.Format("{0} : {1}", row.Name, rate.Money.Amount.ToString("C")); returnRate = new ShippingMethodAndRate(row.Name, nameAndRate, rate.Money.Amount, row.ShippingMethodId); } return(returnRate); }
public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf, ICatalogGateway catalogGateway, IShippingGateway shippingGateway, IPromoGateway promoGateway) : base(uow, qrf) { _catalogGateway = catalogGateway; _shippingGateway = shippingGateway; _promoGateway = promoGateway; }
public RequestHandler(ICatalogGateway cgw, IQueryRepositoryFactory qrf, IShippingGateway shippingGateway, IPromoGateway promoGateway) : base(qrf) { _catalogGateway = cgw; _shippingGateway = shippingGateway; _promoGateway = promoGateway; }
public CartServiceImpl(IServiceProvider resolver) { _queryFactory = resolver.GetService <IQueryRepositoryFactory>(); _commandFactory = resolver.GetService <IUnitOfWorkAsync>(); _catalogGateway = resolver.GetService <ICatalogGateway>(); _shippingGateway = resolver.GetService <IShippingGateway>(); _promoGateway = resolver.GetService <IPromoGateway>(); }
/// <summary> /// Processes the shipments. /// </summary> private void ProcessShipments() { ShippingMethodDto methods = ShippingManager.GetShippingMethods(Thread.CurrentThread.CurrentUICulture.Name); 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) { 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); } string message = String.Empty; Logger.Debug(String.Format("Creating instance of \"{0}\".", type.Name)); IShippingGateway provider = (IShippingGateway)Activator.CreateInstance(type); List <LineItem> items = Shipment.GetShipmentLineItems(shipment); Logger.Debug(String.Format("Calculating the rates.")); ShippingRate rate = provider.GetRate(row.ShippingMethodId, items.ToArray(), ref message); if (rate != null) { Logger.Debug(String.Format("Rates calculated.")); shipment.ShipmentTotal = rate.Price; } else { Logger.Debug(String.Format("No rates has been found.")); } } } }
public async Task <Cart> CalculateCartAsync( TaxType taxType, ICatalogGateway catalogGateway, IPromoGateway promoGateway, IShippingGateway shippingGateway) { if (CartItems != null && CartItems?.Count() > 0) { CartItemTotal = 0.0D; foreach (var cartItem in CartItems) { var product = await catalogGateway.GetProductByIdAsync(cartItem.Product.ProductId); if (product == null) { throw new Exception("Could not find product."); } cartItem .FillUpProductInfo(product.Name, product.Price, product.Desc) .ChangePrice(product.Price); CartItemPromoSavings = CartItemPromoSavings + cartItem.PromoSavings * cartItem.Quantity; CartItemTotal = CartItemTotal + cartItem.Product.Price * cartItem.Quantity; } shippingGateway.CalculateShipping(this); } promoGateway.ApplyShippingPromotions(this); switch (taxType) { case TaxType.NoTax: CartTotal = CartItemTotal + ShippingTotal; break; case TaxType.TenPercentage: var cartTotal = CartItemTotal + ShippingTotal; CartTotal = cartTotal * 10 / 100 + cartTotal; break; default: throw new ArgumentOutOfRangeException(nameof(taxType), taxType, null); } return(this); }
protected PriceCalculatorContext(IPromoGateway promoGateway, IShippingGateway shippingGateway) { _promoGateway = promoGateway; _shippingGateway = shippingGateway; }
public NoTaxCaculator(IPromoGateway promoGateway, IShippingGateway shippingGateway) : base(promoGateway, shippingGateway) { }
public TenPercentTaxCalculator(IPromoGateway promoGateway, IShippingGateway shippingGateway) : base(promoGateway, shippingGateway) { }
/// <summary> /// Binds the shipping rates. /// </summary> private void BindShippingRates() { CountryDto country; bool countryFound = false; bool regionFound = false; int shippingCountryId = -1; int shippingStateProvinceId = -1; bool isRestricted; //get the country id for the order address country = CountryManager.GetCountry(OrderAddress.CountryCode, true); if (country.Country != null && country.Country.Rows.Count > 0) { shippingCountryId = country.Country[0].CountryId; countryFound = true; } //get the state id for the order address CountryDto regions = CountryManager.GetCountries(true); if (regions.StateProvince != null && regions.StateProvince.Count > 0) { foreach (CountryDto.StateProvinceRow stateProvinceRow in regions.StateProvince) { if (stateProvinceRow.Name == OrderAddress.State || stateProvinceRow.Name == OrderAddress.RegionCode) { shippingStateProvinceId = stateProvinceRow.StateProvinceId; regionFound = true; break; } } } //Get the list of all shipping methods to be filtered ShippingMethodDto methods = ShippingManager.GetShippingMethods(CMSContext.Current.LanguageName); //Thread.CurrentThread.CurrentUICulture.Name); // filter the list for only methods that apply to this particular cart's shipping address List <ShippingMethodDto.ShippingMethodRow> shippingRows = new List <ShippingMethodDto.ShippingMethodRow>(); if (countryFound || regionFound) { foreach (ShippingMethodDto.ShippingMethodRow method in methods.ShippingMethod.Rows) { isRestricted = false; if (countryFound) { //first check the restricted countries ShippingMethodDto.ShippingCountryRow[] paymentCountryRestrictions = method.GetShippingCountryRows(); if (paymentCountryRestrictions != null && paymentCountryRestrictions.Length > 0) { foreach (ShippingMethodDto.ShippingCountryRow restrictedCountryRow in paymentCountryRestrictions) { if (restrictedCountryRow.CountryId == shippingCountryId) { isRestricted = true; break; } } } } if (regionFound) { //now check for restricted regions ShippingMethodDto.ShippingRegionRow[] restrictedRegionRows = method.GetShippingRegionRows(); if (restrictedRegionRows != null && restrictedRegionRows.Length > 0) { foreach (ShippingMethodDto.ShippingRegionRow restrictedRegionRow in restrictedRegionRows) { if (shippingStateProvinceId == restrictedRegionRow.StateProvinceId) { isRestricted = true; break; } } } } //if the method has not been excluded based on country or region, including in shipping collection if (!isRestricted) { shippingRows.Add(method); } } } else { //just add all of the rows to the generic list foreach (ShippingMethodDto.ShippingMethodRow method in methods.ShippingMethod.Rows) { shippingRows.Add(method); } } // request rates, make sure we request rates not bound to selected delivery method if (_ShippingRates == null) { List <ShippingRate> list = new List <ShippingRate>(); foreach (ShippingMethodDto.ShippingMethodRow row in shippingRows) { // Check if package contains shippable items, if it does not use the default shipping method instead of the one specified Type type = Type.GetType(row.ShippingOptionRow.ClassName); if (type == null) { throw new TypeInitializationException(row.ShippingOptionRow.ClassName, null); } string message = String.Empty; IShippingGateway provider = (IShippingGateway)Activator.CreateInstance(type); List <LineItem> items = new List <LineItem>(); foreach (LineItem lineItem in CartHelper.LineItems) { if (lineItem.ShippingAddressId == OrderAddress.Name.ToString()) { items.Add(lineItem); } } if (items.Count > 0) { list.Add(provider.GetRate(row.ShippingMethodId, items.ToArray(), ref message)); } } _ShippingRates = list.ToArray(); } if (_ShippingRates == null) { return; } string selectedValue = String.Empty; /* * if (this.ShipmentPackage.Details.DeliveryMethod != null) * selectedValue = ShipmentPackage.Details.DeliveryMethod.StoreMethod + ";" + ShipmentPackage.Details.DeliveryMethod.ServiceSpeed; * */ if (ShippingRatesList.Items.Count > 0) { selectedValue = ShippingRatesList.SelectedValue; ShippingRatesList.Items.Clear(); } // Bind control string lowestOptionValue = String.Empty; decimal lowestPrice = Decimal.MinusOne; // set to -1 initially foreach (ShippingRate listItem in _ShippingRates) { ListItem item = new ListItem(String.Format("{1} - {0}", listItem.Name, CurrencyFormatter.FormatCurrency(listItem.Price, listItem.CurrencyCode)), listItem.Id.ToString()); if (selectedValue.CompareTo(item.Value) == 0) { item.Selected = true; } if (listItem.Price < lowestPrice || lowestPrice == Decimal.MinusOne) { lowestPrice = listItem.Price; lowestOptionValue = item.Value; } ShippingRatesList.Items.Add(item); } // Select the least expensive option if none selected yet if (String.IsNullOrEmpty(selectedValue)) { foreach (ListItem listItem in ShippingRatesList.Items) { if (lowestOptionValue.CompareTo(listItem.Value) == 0) { listItem.Selected = true; } } } // if nothing is selected make sure the very first element is selected // also save that selection to the current order if (ShippingRatesList.SelectedIndex < 0 && ShippingRatesList.Items.Count > 0) { ShippingRatesList.SelectedIndex = 0; } // update the selected delivery option UpdateDeliveryMethod(); // Bind it ShippingRatesList.DataBind(); // Hide if no items available if (ShippingRatesList.Items.Count == 0) { this.Visible = false; } else { this.Visible = true; } }
/// <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.")); } } } }