Skip to content

Commit

Permalink
#4697 Fix incorrect filtering of tier prices by customer roles
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanovM committed May 13, 2020
1 parent 4c4be6f commit 3d70084
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
49 changes: 20 additions & 29 deletions src/Libraries/Nop.Services/Catalog/ProductService.cs
Expand Up @@ -419,7 +419,7 @@ public virtual void InsertProduct(Product product)

//insert
_productRepository.Insert(product);

//event notification
_eventPublisher.EntityInserted(product);
}
Expand All @@ -435,7 +435,7 @@ public virtual void UpdateProduct(Product product)

//update
_productRepository.Update(product);

//event notification
_eventPublisher.EntityUpdated(product);
}
Expand All @@ -451,7 +451,7 @@ public virtual void UpdateProducts(IList<Product> products)

//update
_productRepository.Update(products);

//event notification
foreach (var product in products)
{
Expand Down Expand Up @@ -777,7 +777,7 @@ where categoryIds.Contains(pc.CategoryId)
}
//return products
var totalRecords = pTotalRecords.Value != DBNull.Value ? Convert.ToInt32(pTotalRecords.Value) : 0;

return new PagedList<Product>(products, pageIndex, pageSize, totalRecords);
}

Expand Down Expand Up @@ -943,7 +943,7 @@ public virtual void UpdateProductReviewTotals(Product product)
join p in _productRepository.Table on pac.ProductId equals p.Id
where
//filter by combinations with stock quantity less than the minimum
pac.StockQuantity < pac.NotifyAdminForQuantityBelow &&
pac.StockQuantity < pac.NotifyAdminForQuantityBelow &&
//filter by products with tracking inventory by attributes
p.ManageInventoryMethodId == (int)ManageInventoryMethod.ManageStockByAttributes &&
//ignore deleted products
Expand Down Expand Up @@ -1692,7 +1692,7 @@ public virtual int ReverseBookedInventory(Product product, ShipmentItem shipment
var pwi = _productWarehouseInventoryRepository.Table.FirstOrDefault(wi => wi.ProductId == product.Id && wi.WarehouseId == shipmentItem.WarehouseId);
if (pwi == null)
return 0;

var shipment = _shipmentRepository.ToCachedGetById(shipmentItem.ShipmentId);

//not shipped yet? hence "BookReservedInventory" method was not invoked
Expand Down Expand Up @@ -1989,22 +1989,13 @@ public virtual IList<TierPrice> GetTierPrices(Product product, Customer customer
return null;

//get actual tier prices
var actualTierPrices = GetTierPricesByProduct(product.Id).OrderBy(price => price.Quantity)
return GetTierPricesByProduct(product.Id)
.OrderBy(price => price.Quantity)
.FilterByStore(storeId)
.FilterByCustomerRole(_catalogSettings.IgnoreAcl ? Array.Empty<int>() : _customerService.GetCustomerRoleIds(customer))
.FilterByDate()
.RemoveDuplicatedQuantities();

if (!_catalogSettings.IgnoreAcl)
{
var customerRoleIds = _customerService.GetCustomerRoleIds(customer);

actualTierPrices = actualTierPrices.Where(tierPrice =>
!tierPrice.CustomerRoleId.HasValue ||
tierPrice.CustomerRoleId.Value == 0 ||
customerRoleIds.Contains(tierPrice.CustomerRoleId.Value));
}

return actualTierPrices.ToList();
.RemoveDuplicatedQuantities()
.ToList();
}

/// <summary>
Expand All @@ -2027,7 +2018,7 @@ public virtual void DeleteTierPrice(TierPrice tierPrice)
throw new ArgumentNullException(nameof(tierPrice));

_tierPriceRepository.Delete(tierPrice);

//event notification
_eventPublisher.EntityDeleted(tierPrice);
}
Expand Down Expand Up @@ -2055,7 +2046,7 @@ public virtual void InsertTierPrice(TierPrice tierPrice)
throw new ArgumentNullException(nameof(tierPrice));

_tierPriceRepository.Insert(tierPrice);

//event notification
_eventPublisher.EntityInserted(tierPrice);
}
Expand All @@ -2070,7 +2061,7 @@ public virtual void UpdateTierPrice(TierPrice tierPrice)
throw new ArgumentNullException(nameof(tierPrice));

_tierPriceRepository.Update(tierPrice);

//event notification
_eventPublisher.EntityUpdated(tierPrice);
}
Expand Down Expand Up @@ -2256,7 +2247,7 @@ public virtual void UpdateProductPicture(ProductPicture productPicture)
query = query.Where(pr => pr.StoreId == storeId);
if (productId > 0)
query = query.Where(pr => pr.ProductId == productId);

query = from productReview in query
join product in _productRepository.Table on productReview.ProductId equals product.Id
where
Expand Down Expand Up @@ -2452,7 +2443,7 @@ public virtual void UpdateProductReview(ProductReview productReview)

//update
_productReviewRepository.Update(productReview);

//event notification
_eventPublisher.EntityUpdated(productReview);
}
Expand Down Expand Up @@ -2524,7 +2515,7 @@ public virtual void DeleteProductWarehouseInventory(ProductWarehouseInventory pw
throw new ArgumentNullException(nameof(pwi));

_productWarehouseInventoryRepository.Delete(pwi);

_eventPublisher.EntityDeleted(pwi);
}

Expand Down Expand Up @@ -2672,9 +2663,9 @@ public virtual void ClearDiscountProductMapping(Discount discount)
_eventPublisher.EntityDeleted(pdcm.dcm);

//update "HasDiscountsApplied" property
UpdateHasDiscountsApplied(pdcm.product);
}
}
UpdateHasDiscountsApplied(pdcm.product);
}
}

/// <summary>
/// Get a discount-product mapping records by product identifier
Expand Down
21 changes: 21 additions & 0 deletions src/Libraries/Nop.Services/Catalog/TierPriceExtensions.cs
Expand Up @@ -24,6 +24,27 @@ public static IEnumerable<TierPrice> FilterByStore(this IEnumerable<TierPrice> s
return source.Where(tierPrice => tierPrice.StoreId == 0 || tierPrice.StoreId == storeId);
}

/// <summary>
/// Filter tier prices by customer roles
/// </summary>
/// <param name="source">Tier prices</param>
/// <param name="customerRoleIds">Customer role identifiers</param>
/// <returns>Filtered tier prices</returns>
public static IEnumerable<TierPrice> FilterByCustomerRole(this IEnumerable<TierPrice> source, int[] customerRoleIds)
{
if (source == null)
throw new ArgumentNullException(nameof(source));

if (customerRoleIds == null)
throw new ArgumentNullException(nameof(customerRoleIds));

if (!customerRoleIds.Any())
return source;

return source.Where(tierPrice =>
!tierPrice.CustomerRoleId.HasValue || tierPrice.CustomerRoleId == 0 || customerRoleIds.Contains(tierPrice.CustomerRoleId.Value));
}

/// <summary>
/// Remove duplicated quantities (leave only an tier price with minimum price)
/// </summary>
Expand Down

0 comments on commit 3d70084

Please sign in to comment.