protected void Page_Load(object sender, EventArgs e) { FillCountryRegionOptionsHtml(); if (!IsPostBack) { LoadShippingTransactions(); int?deleteRateId = WA.Parser.ToInt(Request.QueryString["deleteRate"]); if (deleteRateId.HasValue) { ShippingServiceRate toDelete = new ShippingServiceRate(); if (toDelete.LoadByPrimaryKey(deleteRateId.Value)) { toDelete.MarkAsDeleted(); toDelete.Save(); } } short?deleteId = WA.Parser.ToShort(Request.QueryString["delete"]); if (deleteId.HasValue) { DataModel.ShippingServiceRateType toDelete = new DataModel.ShippingServiceRateType(); if (toDelete.LoadByPrimaryKey(deleteId.Value)) { toDelete.MarkAsDeleted(); toDelete.Save(); } } chkShippingTablesEnabled.Checked = shippingTablesService.IsEnabled; // FEDEX chkFedExEnabled.Checked = fedExService.IsEnabled; Dictionary <string, string> fedexSettings = fedExService.GetSettingsDictionary(); txtFedExAccountNum.Text = fedexSettings.TryGetValueOrEmpty("accountNumber"); txtFedExMeterNum.Text = fedexSettings.TryGetValueOrEmpty("meterNumber"); txtFedExSmartPostHub.Text = fedexSettings.TryGetValueOrEmpty("smartPostHubId"); txtFedExApiKey.Text = fedexSettings.TryGetValueOrEmpty("apiKey"); txtFedExApiPassword.Text = fedexSettings.TryGetValueOrEmpty("apiPassword"); chkFedExIsTestGateway.Checked = WA.Parser.ToBool(fedexSettings.TryGetValueOrEmpty("isTestGateway")).GetValueOrDefault(false); ddlLabelStockType.SelectedValue = fedexSettings.TryGetValueOrDefault("labelStockType", "0"); // UPS chkUpsEnabled.Checked = upsService.IsEnabled; Dictionary <string, string> upsSettings = upsService.GetSettingsDictionary(); txtUpsUserId.Text = upsSettings.TryGetValueOrEmpty("userId"); txtUpsPassword.Text = upsSettings.TryGetValueOrEmpty("password"); txtUpsAccountNumber.Text = upsSettings.TryGetValueOrEmpty("accountNumber"); txtUpsAccessKey.Text = upsSettings.TryGetValueOrEmpty("accessKey"); chkUpsIsTestGateway.Checked = WA.Parser.ToBool(upsSettings.TryGetValueOrEmpty("isTestGateway")).GetValueOrDefault(false); LoadShippingRates(); } }
private void SaveShippingRows() { const string inputPrefix = "shippingRow"; Regex rxInputName = new Regex(@"shippingRow\[(\d+)\]\[(.*?)\]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); Dictionary <int, Dictionary <string, string[]> > shipMethodIdToNameValues = new Dictionary <int, Dictionary <string, string[]> >(); foreach (string key in Request.Form) { Match m = rxInputName.Match(key); if (m.Success) { int? shipMethodId = WA.Parser.ToInt(m.Groups[1].Value); string fieldName = m.Groups[2].Value; if (shipMethodId.HasValue) { if (!shipMethodIdToNameValues.ContainsKey(shipMethodId.Value)) { shipMethodIdToNameValues[shipMethodId.Value] = new Dictionary <string, string[]>(); } shipMethodIdToNameValues[shipMethodId.Value][fieldName] = Request.Form.GetValues(key); } } } // delete existing rates for this service (we'll re-add them below) shippingTablesService.GetAllRateTypes().ForEach(rt => rt.DeleteAllRates()); //--- Save the dictionary structure to the database foreach (var methods in shipMethodIdToNameValues) { int rateTypeId = methods.Key; //ShippingServiceRateCollection.DeleteAllRates(shippingMethodId); string[] countryRegionValues = methods.Value["ddlCountryRegion"]; string[] minWeightValues = methods.Value["minWeight"]; string[] maxWeightValues = methods.Value["maxWeight"]; string[] costValues = methods.Value["cost"]; for (int i = 0; i < countryRegionValues.Length; i++) { string countryValue = ""; string regionValue = ""; decimal?minWeight = null; decimal?maxWeight = null; decimal?cost = null; string[] parts = countryRegionValues[i].Split('|'); if (parts.Length == 2) { // Country AND Region countryValue = parts[0]; regionValue = parts[1]; } else if (parts.Length == 1) { // Country only countryValue = parts[0]; } if (countryValue.ToLower() == "none") { countryValue = ""; } minWeight = WA.Parser.ToDecimal(minWeightValues[i]); maxWeight = WA.Parser.ToDecimal(maxWeightValues[i]); cost = WA.Parser.ToDecimal(costValues[i]); if (cost.HasValue) { // add the rate to the db ShippingServiceRate newRate = new ShippingServiceRate(); newRate.RateTypeId = (short)rateTypeId; newRate.CountryCode = countryValue; newRate.Region = regionValue; newRate.WeightMin = minWeight.GetValueOrDefault(0); newRate.WeightMax = maxWeight.GetValueOrDefault(99999999); newRate.Cost = cost; newRate.Save(); } } } }
public IList <IShippingRate> GetRates(IPostalAddress senderAddress, IPostalAddress recipientAddress, IList <IShipmentPackageDetail> packageDetails) { var shippingRates = new List <IShippingRate>(); if (packageDetails.Count != 0) { decimal cartTotalProductWeight = packageDetails.Sum(p => p.Weight); var rateTypes = dbShippingService.GetEnabledRateTypes(); foreach (var rateType in rateTypes) { decimal optionCost = 0; //---- Determine cost by total weight of products in the cart List <ShippingServiceRate> ratesByWeight = rateType.GetRates(); //AddressInfo shippingAddress = destination; List <ShippingServiceRate> exactRanges = ratesByWeight.FindAll(w => cartTotalProductWeight >= w.WeightMin.GetValueOrDefault(0) && cartTotalProductWeight <= w.WeightMax.GetValueOrDefault(0)); if (exactRanges.Count > 0) { decimal?shipCostByLocation = null; //--- Check for most-specific: country AND region ShippingServiceRate rateWeight = exactRanges.Find( r => r.CountryCode == recipientAddress.CountryCode && r.Region == recipientAddress.Region); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } else { //--- Check for next-specific: country only (empty region) rateWeight = exactRanges.Find( r => r.CountryCode == recipientAddress.CountryCode && string.IsNullOrEmpty(r.Region)); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } else { //--- Check for least-specific: empty/default (empty country, empty region) rateWeight = exactRanges.Find( r => string.IsNullOrEmpty(r.CountryCode) && string.IsNullOrEmpty(r.Region)); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } } } if (shipCostByLocation.HasValue) { //---- Add AdditionalHandlingFee for any products that have additional shipping costs decimal additionalHandlingFee = packageDetails.Sum(p => p.AdditionalHandlingFee); optionCost = shipCostByLocation.Value + additionalHandlingFee; shippingRates.Add(new ShippingRate() { ServiceType = rateType.Name, Rate = optionCost }); } } } shippingRates.Sort((left, right) => left.Rate.CompareTo(right.Rate)); } return(shippingRates); }
private List <ShippingOption> GetShippingOptionEstimates(AddressInfo origin, AddressInfo destination, List <vCartItemProductInfo> cartProducts, IEnumerable <ShippingServiceRateType> rateTypes) { List <ShippingOption> shippingOptions = new List <ShippingOption>(); //var rateTypes = service.GetEnabledRateTypes(); foreach (var rateType in rateTypes) { decimal optionCost = 0; // special case: we have a cart with only Downloadable items, so no shipping cost if (!cartProducts.TrueForAll(cp => cp.DeliveryMethod == ProductDeliveryMethod.Downloaded)) { //---- Determine cost by total weight of products in the cart List <ShippingServiceRate> ratesByWeight = rateType.GetRates(); decimal cartTotalProductWeight = cartProducts.Sum(p => p.GetWeightForQuantity()); AddressInfo shippingAddress = destination; List <ShippingServiceRate> exactRanges = ratesByWeight.FindAll(w => cartTotalProductWeight >= w.WeightMin.GetValueOrDefault(0) && cartTotalProductWeight <= w.WeightMax.GetValueOrDefault(0)); if (exactRanges.Count > 0) { decimal?shipCostByLocation = null; //--- Check for most-specific: country AND region ShippingServiceRate rateWeight = exactRanges.Find(r => r.CountryCode == shippingAddress.Country && r.Region == shippingAddress.Region); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } else { //--- Check for next-specific: country only (empty region) rateWeight = exactRanges.Find(r => r.CountryCode == shippingAddress.Country && string.IsNullOrEmpty(r.Region)); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } else { //--- Check for least-specific: empty/default (empty country, empty region) rateWeight = exactRanges.Find(r => string.IsNullOrEmpty(r.CountryCode) && string.IsNullOrEmpty(r.Region)); if (rateWeight != null) { shipCostByLocation = rateWeight.Cost.GetValueOrDefault(0); } } } if (shipCostByLocation.HasValue) { //---- Add amounts for any products that have additional shipping costs decimal additionalShipCost = cartProducts.Sum(p => (p.ProductShippingAdditionalFeePerItem * p.Quantity)).GetValueOrDefault(0); optionCost = shipCostByLocation.Value + additionalShipCost; shippingOptions.Add(new ShippingOption() { ProviderType = providerType, Name = rateType.Name, DisplayName = rateType.DisplayName, Cost = optionCost }); } } } } shippingOptions.Sort((left, right) => left.Cost.Value.CompareTo(right.Cost.Value)); return(shippingOptions); }