/// <summary> /// Get rate by weight and by total /// </summary> /// <param name="subTotal">Subtotal</param> /// <param name="weight">Weight</param> /// <param name="shippingMethodId">Shipping method ID</param> /// <param name="storeId">Store ID</param> /// <param name="warehouseId">Warehouse ID</param> /// <param name="countryId">Country ID</param> /// <param name="stateProvinceId">State/Province ID</param> /// <param name="zip">Zip code</param> /// <returns>Rate</returns> private decimal?GetRate(decimal subTotal, decimal weight, int shippingMethodId, int storeId, int warehouseId, int countryId, int stateProvinceId, string zip) { var shippingByWeightByTotalRecord = _shippingByWeightByTotalService.FindRecords(shippingMethodId, storeId, warehouseId, countryId, stateProvinceId, zip, weight, subTotal); if (shippingByWeightByTotalRecord == null) { if (_fixedByWeightByTotalSettings.LimitMethodsToCreated) { return(null); } return(decimal.Zero); } //additional fixed cost var shippingTotal = shippingByWeightByTotalRecord.AdditionalFixedCost; //charge amount per weight unit if (shippingByWeightByTotalRecord.RatePerWeightUnit > decimal.Zero) { var weightRate = Math.Max(weight - shippingByWeightByTotalRecord.LowerWeightLimit, decimal.Zero); shippingTotal += shippingByWeightByTotalRecord.RatePerWeightUnit * weightRate; } //percentage rate of subtotal if (shippingByWeightByTotalRecord.PercentageRateOfSubtotal > decimal.Zero) { shippingTotal += Math.Round((decimal)((((float)subTotal) * ((float)shippingByWeightByTotalRecord.PercentageRateOfSubtotal)) / 100f), 2); } return(Math.Max(shippingTotal, decimal.Zero)); }
public IActionResult RateByWeightByTotalList(ConfigurationModel searchModel, ConfigurationModel filter) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageShippingSettings)) { return(AccessDeniedDataTablesJson()); } //var records = _shippingByWeightService.GetAll(command.Page - 1, command.PageSize); var records = _shippingByWeightService.FindRecords( pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize, storeId: filter.SearchStoreId, warehouseId: filter.SearchWarehouseId, countryId: filter.SearchCountryId, stateProvinceId: filter.SearchStateProvinceId, zip: filter.SearchZip, shippingMethodId: filter.SearchShippingMethodId, weight: null, orderSubtotal: null ); var gridModel = new ShippingByWeightByTotalListModel().PrepareToGrid(searchModel, records, () => { return(records.Select(record => { var model = new ShippingByWeightByTotalModel { Id = record.Id, StoreId = record.StoreId, StoreName = _storeService.GetStoreById(record.StoreId)?.Name ?? "*", WarehouseId = record.WarehouseId, WarehouseName = _shippingService.GetWarehouseById(record.WarehouseId)?.Name ?? "*", ShippingMethodId = record.ShippingMethodId, ShippingMethodName = _shippingService.GetShippingMethodById(record.ShippingMethodId)?.Name ?? "Unavailable", CountryId = record.CountryId, CountryName = _countryService.GetCountryById(record.CountryId)?.Name ?? "*", StateProvinceId = record.StateProvinceId, StateProvinceName = _stateProvinceService.GetStateProvinceById(record.StateProvinceId)?.Name ?? "*", WeightFrom = record.WeightFrom, WeightTo = record.WeightTo, OrderSubtotalFrom = record.OrderSubtotalFrom, OrderSubtotalTo = record.OrderSubtotalTo, AdditionalFixedCost = record.AdditionalFixedCost, PercentageRateOfSubtotal = record.PercentageRateOfSubtotal, RatePerWeightUnit = record.RatePerWeightUnit, LowerWeightLimit = record.LowerWeightLimit, Zip = !string.IsNullOrEmpty(record.Zip) ? record.Zip : "*" }; var htmlSb = new StringBuilder("<div>"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource("Plugins.Shipping.FixedByWeightByTotal.Fields.WeightFrom"), model.WeightFrom); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource("Plugins.Shipping.FixedByWeightByTotal.Fields.WeightTo"), model.WeightTo); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.OrderSubtotalFrom"), model.OrderSubtotalFrom); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.OrderSubtotalTo"), model.OrderSubtotalTo); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.AdditionalFixedCost"), model.AdditionalFixedCost); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.RatePerWeightUnit"), model.RatePerWeightUnit); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.LowerWeightLimit"), model.LowerWeightLimit); htmlSb.Append("<br />"); htmlSb.AppendFormat("{0}: {1}", _localizationService.GetResource( "Plugins.Shipping.FixedByWeightByTotal.Fields.PercentageRateOfSubtotal"), model.PercentageRateOfSubtotal); htmlSb.Append("</div>"); model.DataHtml = htmlSb.ToString(); return model; })); }); return(Json(gridModel)); }
/// <summary> /// Gets available shipping options /// </summary> /// <param name="getShippingOptionRequest">A request for getting shipping options</param> /// <returns>Represents a response of getting shipping rate options</returns> public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest) { if (getShippingOptionRequest == null) { throw new ArgumentNullException(nameof(getShippingOptionRequest)); } var response = new GetShippingOptionResponse(); if (getShippingOptionRequest.Items == null || !getShippingOptionRequest.Items.Any()) { response.AddError("No shipment items"); return(response); } //choose the shipping rate calculation method if (_fixedByWeightByTotalSettings.ShippingByWeightByTotalEnabled) { //shipping rate calculation by products weight if (getShippingOptionRequest.ShippingAddress == null) { response.AddError("Shipping address is not set"); return(response); } var storeId = getShippingOptionRequest.StoreId != 0 ? getShippingOptionRequest.StoreId : _storeContext.CurrentStore.Id; var countryId = getShippingOptionRequest.ShippingAddress.CountryId ?? 0; var stateProvinceId = getShippingOptionRequest.ShippingAddress.StateProvinceId ?? 0; var warehouseId = getShippingOptionRequest.WarehouseFrom?.Id ?? 0; var zip = getShippingOptionRequest.ShippingAddress.ZipPostalCode; //get subtotal of shipped items var subTotal = decimal.Zero; foreach (var packageItem in getShippingOptionRequest.Items) { if (_shippingService.IsFreeShipping(packageItem.ShoppingCartItem)) { continue; } subTotal += _shoppingCartService.GetSubTotal(packageItem.ShoppingCartItem); } //get weight of shipped items (excluding items with free shipping) var weight = _shippingService.GetTotalWeight(getShippingOptionRequest, ignoreFreeShippedItems: true); foreach (var shippingMethod in _shippingService.GetAllShippingMethods(countryId)) { int?transitDays = null; var rate = decimal.Zero; var shippingByWeightByTotalRecord = _shippingByWeightByTotalService.FindRecords( shippingMethod.Id, storeId, warehouseId, countryId, stateProvinceId, zip, weight, subTotal); if (shippingByWeightByTotalRecord == null) { if (_fixedByWeightByTotalSettings.LimitMethodsToCreated) { continue; } } else { rate = GetRate(shippingByWeightByTotalRecord, subTotal, weight); transitDays = shippingByWeightByTotalRecord.TransitDays; } response.ShippingOptions.Add(new ShippingOption { Name = _localizationService.GetLocalized(shippingMethod, x => x.Name), Description = _localizationService.GetLocalized(shippingMethod, x => x.Description), Rate = rate, TransitDays = transitDays }); } } else { //shipping rate calculation by fixed rate var restrictByCountryId = getShippingOptionRequest.ShippingAddress?.CountryId; response.ShippingOptions = _shippingService.GetAllShippingMethods(restrictByCountryId).Select(shippingMethod => new ShippingOption { Name = _localizationService.GetLocalized(shippingMethod, x => x.Name), Description = _localizationService.GetLocalized(shippingMethod, x => x.Description), Rate = GetRate(shippingMethod.Id), TransitDays = GetTransitDays(shippingMethod.Id) }).ToList(); } return(response); }