Shipments BuildShipments(RealTimeRateCalculationContext context) { var useDistributors = context.CartItems.HasDistributorComponents() && AppLogic.AppConfigBool("RTShipping.MultiDistributorCalculation"); var freeShippingAllowsRateSelection = AppLogic.AppConfigBool("FreeShippingAllowsRateSelection"); var shipments = new Shipments { HasDistributorItems = useDistributors, }; // Get ship separately cart items var packageId = 1; foreach (var cartItem in context.CartItems.Where(ci => ci.IsShipSeparately)) { // Only calculate rates for products that require shipping if (cartItem.IsDownload || cartItem.IsSystem || !cartItem.Shippable || GiftCard.ProductIsEmailGiftCard(cartItem.ProductID) || (cartItem.FreeShipping && !freeShippingAllowsRateSelection)) { continue; } var packages = new Packages(); ApplyDestinationForAddress(packages, context.ShipmentAddress); if (useDistributors && cartItem.DistributorID > 0) { ApplyOriginForDistributor(packages, cartItem.DistributorID); } packages.AddPackage(new Package(cartItem) { PackageId = packageId++, }); shipments.Add(packages); } // Now get all itmes that do not ship separately, but group them into shipments by distributor. // Note that distributor ID 0 will be all of the items without a distributor. var maxDistributorId = useDistributors ? DB.GetSqlN("select max(DistributorID) N from ProductDistributor") : 0; for (int distributorId = 0; distributorId <= maxDistributorId; distributorId++) { var remainingItemsWeight = 0m; var remainingItemsInsuranceValue = 0m; foreach (var cartItem in context.CartItems.Where(ci => !ci.IsShipSeparately)) { // Only calculate rates for products that require shipping if (cartItem.IsDownload || cartItem.IsSystem || !cartItem.Shippable || GiftCard.ProductIsEmailGiftCard(cartItem.ProductID) || (cartItem.FreeShipping && !freeShippingAllowsRateSelection)) { continue; } if (useDistributors && cartItem.DistributorID != distributorId) { continue; } var weight = cartItem.Weight; if (weight == 0m) { weight = AppLogic.AppConfigUSDecimal("RTShipping.DefaultItemWeight"); } if (weight == 0m) { weight = 0.5m; // must have SOMETHING to use! } remainingItemsWeight += (weight * cartItem.Quantity); remainingItemsInsuranceValue += (cartItem.Price * cartItem.Quantity); } if (remainingItemsWeight == 0m) { continue; } var package = new Package { PackageId = packageId++, Weight = remainingItemsWeight + AppLogic.AppConfigUSDecimal("RTShipping.PackageExtraWeight"), Insured = AppLogic.AppConfigBool("RTShipping.Insured"), InsuredValue = remainingItemsInsuranceValue, }; var packages = new Packages(); ApplyDestinationForAddress(packages, context.ShipmentAddress); if (useDistributors && distributorId != 0) { ApplyOriginForDistributor(packages, distributorId); } packages.AddPackage(package); shipments.Add(packages); } return(shipments); }
protected ShippingMethods GetRates() { decimal ShippingHandlingExtraFee = AppLogic.AppConfigNativeDecimal("ShippingHandlingExtraFee"); bool IsShipSeparately = GetProductIsShipSeparately(ProductID); RTShipping realTimeShipping = new RTShipping(); Decimal MarkupPercent = AppLogic.AppConfigUSDecimal("RTShipping.MarkupPercent"); // Set shipment info decimal ProductWeight = GetVariantWeight(VariantID); decimal ProductPrice = AppLogic.GetVariantPrice(VariantID); realTimeShipping.ShipmentWeight = ProductWeight * Quantity; if (realTimeShipping.ShipmentWeight == System.Decimal.Zero) { realTimeShipping.ShipmentWeight = AppLogic.AppConfigUSDecimal("RTShipping.DefaultItemWeight"); // force a default. } //Create Shipments Collection Shipments shipments = new Shipments(); // Create Packages Collection Packages shipment = new Packages(); int PackageID = 1; shipments = new Shipments(); shipment = new Packages(); // Set pickup type shipment.PickupType = AppLogic.AppConfig("RTShipping.UPS.UPSPickupType"); // RTShipping.PickupTypes.UPSCustomerCounter.ToString(); if (AppLogic.AppConfig("RTShipping.UPS.UPSPickupType").Length == 0) { shipment.PickupType = RTShipping.PickupTypes.UPSCustomerCounter.ToString(); } // Set destination address of this package group: Address sa = new Address(); sa.LoadFromDB(ThisCustomer.PrimaryShippingAddressID); shipment.DestinationCity = sa.City; shipment.DestinationStateProvince = sa.State; shipment.DestinationZipPostalCode = sa.Zip; shipment.DestinationCountryCode = AppLogic.GetCountryTwoLetterISOCode(sa.Country); shipment.DestinationResidenceType = sa.ResidenceType; realTimeShipping.DestinationResidenceType = shipment.DestinationResidenceType; // now override them with what is passed in here: shipment.DestinationCountryCode = AppLogic.GetCountryTwoLetterISOCode(Country); shipment.DestinationStateProvince = State; shipment.DestinationZipPostalCode = PostalCode; if (IsShipSeparately) { for (int i = 1; i <= Quantity; i++) { Package p = new Package(); p.PackageId = PackageID; PackageID = PackageID + 1; String Dimensions = String.Empty; // not supported here p.Weight = ProductWeight; if (p.Weight == System.Decimal.Zero) { p.Weight = 0.5M; // must have SOMETHING to use! } p.Weight += AppLogic.AppConfigUSDecimal("RTShipping.PackageExtraWeight"); p.Insured = AppLogic.AppConfigBool("RTShipping.Insured"); p.InsuredValue = ProductPrice; shipment.AddPackage(p); realTimeShipping.ShipmentValue += ProductPrice; p = null; } } else { Package p = new Package(); p.PackageId = PackageID; PackageID = PackageID + 1; String Dimensions = String.Empty; p.Weight = ProductWeight * Quantity; if (p.Weight == System.Decimal.Zero) { p.Weight = 0.5M; // must have SOMETHING to use! } p.Weight += AppLogic.AppConfigUSDecimal("RTShipping.PackageExtraWeight"); p.Insured = AppLogic.AppConfigBool("RTShipping.Insured"); p.InsuredValue = ProductPrice * Quantity; shipment.AddPackage(p); p = null; realTimeShipping.ShipmentValue = ProductPrice * Quantity; } shipments.AddPackages(shipment); // Get carriers string carriers = String.Empty; if (shipment.DestinationCountryCode.Equals("US", StringComparison.InvariantCultureIgnoreCase)) { carriers = AppLogic.AppConfig("RTshipping.DomesticCarriers"); } else { carriers = AppLogic.AppConfig("RTshipping.InternationalCarriers"); } if (carriers.Length == 0) { carriers = AppLogic.AppConfig("RTShipping.ActiveCarrier"); } // Get result type RTShipping.ResultType format = RTShipping.ResultType.CollectionList; String RTShipRequest = String.Empty; String RTShipResponse = String.Empty; decimal ShippingTaxRate = System.Decimal.Zero; // NOT supported here return((ShippingMethods)realTimeShipping.GetRates(shipments, carriers, format, "ShippingMethod", "ShippingMethod", ShippingTaxRate, out RTShipRequest, out RTShipResponse, ShippingHandlingExtraFee, (decimal)MarkupPercent, realTimeShipping.ShipmentValue)); }