Skip to content

Commit

Permalink
Resolves #1184 Sort Current shopping carts & Current wishlists by Sho…
Browse files Browse the repository at this point in the history
…ppingCartItem.CreatedOn
  • Loading branch information
mgesing committed Sep 20, 2018
1 parent 2323c5f commit 21b31ea
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 82 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* (Perf) Significantly increased query performance for products with a lot of category assignments (> 10).
* Debitoor: Partially update customer instead of full update to avoid all fields being overwritten
* #1479 Show in messages the delivery time at the time of purchase
* #1184 Sort Current shopping carts & Current wishlists by ShoppingCartItem.CreatedOn.

### Bugfixes
* In a multi-store environment, multiple topics with the same system name cannot be resolved reliably.
Expand Down
71 changes: 46 additions & 25 deletions src/Libraries/SmartStore.Services/Customers/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
Expand All @@ -14,56 +13,55 @@
using SmartStore.Core.Domain.Forums;
using SmartStore.Core.Domain.Orders;
using SmartStore.Core.Domain.Shipping;
using SmartStore.Core.Localization;
using SmartStore.Core.Fakes;
using SmartStore.Core.Localization;
using SmartStore.Core.Logging;
using SmartStore.Data.Caching;
using SmartStore.Services.Common;
using SmartStore.Services.Localization;
using SmartStore.Core.Logging;
using SmartStore.Services.Messages;

namespace SmartStore.Services.Customers
{
public partial class CustomerService : ICustomerService
public partial class CustomerService : ICustomerService
{
private readonly IRepository<Customer> _customerRepository;
private readonly IRepository<CustomerRole> _customerRoleRepository;
private readonly IRepository<GenericAttribute> _gaRepository;
private readonly IRepository<RewardPointsHistory> _rewardPointsHistoryRepository;
private readonly IRepository<ShoppingCartItem> _shoppingCartItemRepository;
private readonly IGenericAttributeService _genericAttributeService;
private readonly RewardPointsSettings _rewardPointsSettings;
private readonly ICommonServices _services;
private readonly HttpContextBase _httpContext;
private readonly IUserAgent _userAgent;
private readonly CustomerSettings _customerSettings;
private readonly Lazy<IMessageModelProvider> _messageModelProvider;
private readonly Lazy<IGdprTool> _gdprTool;

public CustomerService(
IRepository<Customer> customerRepository,
IRepository<CustomerRole> customerRoleRepository,
IRepository<GenericAttribute> gaRepository,
IRepository<RewardPointsHistory> rewardPointsHistoryRepository,
IRepository<ShoppingCartItem> shoppingCartItemRepository,
IGenericAttributeService genericAttributeService,
RewardPointsSettings rewardPointsSettings,
ICommonServices services,
HttpContextBase httpContext,
IUserAgent userAgent,
CustomerSettings customerSettings,
Lazy<IMessageModelProvider> messageModelProvider,
Lazy<IGdprTool> gdprTool)
{
_customerRepository = customerRepository;
_customerRoleRepository = customerRoleRepository;
_gaRepository = gaRepository;
_rewardPointsHistoryRepository = rewardPointsHistoryRepository;
_shoppingCartItemRepository = shoppingCartItemRepository;
_genericAttributeService = genericAttributeService;
_rewardPointsSettings = rewardPointsSettings;
_services = services;
_httpContext = httpContext;
_userAgent = userAgent;
_customerSettings = customerSettings;
_messageModelProvider = messageModelProvider;
_gdprTool = gdprTool;

T = NullLocalizer.Instance;
Expand All @@ -80,7 +78,41 @@ public virtual IPagedList<Customer> SearchCustomers(CustomerSearchQuery q)
{
Guard.NotNull(q, nameof(q));

var query = _customerRepository.Table;
var isOrdered = false;
IQueryable<Customer> query = null;

if (q.OnlyWithCart)
{
var cartItemQuery = _shoppingCartItemRepository.TableUntracked.Expand(x => x.Customer);

if (q.CartType.HasValue)
{
cartItemQuery = cartItemQuery.Where(x => x.ShoppingCartTypeId == (int)q.CartType.Value);
}

var groupQuery =
from sci in cartItemQuery
group sci by sci.CustomerId into grp
select grp
.OrderByDescending(x => x.CreatedOnUtc)
.Select(x => new
{
x.Customer,
x.CreatedOnUtc
})
.FirstOrDefault();

// We have to sort again because of paging.
query = groupQuery
.OrderByDescending(x => x.CreatedOnUtc)
.Select(x => x.Customer);

isOrdered = true;
}
else
{
query = _customerRepository.Table;
}

if (q.Email.HasValue())
{
Expand Down Expand Up @@ -184,23 +216,12 @@ public virtual IPagedList<Customer> SearchCustomers(CustomerSearchQuery q)
.Select(z => z.Customer);
}

if (q.OnlyWithCart)
{
int? sctId = null;
if (q.CartType.HasValue)
{
sctId = (int)q.CartType.Value;
}

query = q.CartType.HasValue ?
query.Where(c => c.ShoppingCartItems.Where(x => x.ShoppingCartTypeId == sctId).Count() > 0) :
query.Where(c => c.ShoppingCartItems.Count() > 0);
}

query = query.OrderByDescending(c => c.CreatedOnUtc);
if (!isOrdered)
{
query = query.OrderByDescending(c => c.CreatedOnUtc);
}

var customers = new PagedList<Customer>(query, q.PageIndex, q.PageSize);

return customers;
}

Expand All @@ -217,7 +238,7 @@ public virtual IPagedList<Customer> GetAllCustomersByPasswordFormat(PasswordForm
return customers;
}

public virtual IPagedList<Customer> GetOnlineCustomers(DateTime lastActivityFromUtc, int[] customerRoleIds, int pageIndex, int pageSize)
public virtual IPagedList<Customer> GetOnlineCustomers(DateTime lastActivityFromUtc, int[] customerRoleIds, int pageIndex, int pageSize)
{
var q = new CustomerSearchQuery
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using SmartStore.Services.Catalog;
using SmartStore.Services.Customers;
using SmartStore.Services.Helpers;
using SmartStore.Services.Localization;
using SmartStore.Services.Security;
using SmartStore.Services.Stores;
using SmartStore.Services.Tax;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Framework.Security;
Expand All @@ -19,45 +17,32 @@ namespace SmartStore.Admin.Controllers
[AdminAuthorize]
public class ShoppingCartController : AdminControllerBase
{
#region Fields

private readonly ICustomerService _customerService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly IPriceFormatter _priceFormatter;
private readonly IStoreService _storeService;
private readonly ITaxService _taxService;
private readonly IPriceCalculationService _priceCalculationService;
private readonly IPermissionService _permissionService;
private readonly ILocalizationService _localizationService;
#endregion

#region Constructors

public ShoppingCartController(ICustomerService customerService,
IDateTimeHelper dateTimeHelper, IPriceFormatter priceFormatter,
IStoreService storeService, ITaxService taxService,
IPriceCalculationService priceCalculationService,
IPermissionService permissionService, ILocalizationService localizationService)
public ShoppingCartController(
ICustomerService customerService,
IDateTimeHelper dateTimeHelper,
IPriceFormatter priceFormatter,
ITaxService taxService,
IPriceCalculationService priceCalculationService)
{
this._customerService = customerService;
this._dateTimeHelper = dateTimeHelper;
this._priceFormatter = priceFormatter;
this._storeService = storeService;
this._taxService = taxService;
this._priceCalculationService = priceCalculationService;
this._permissionService = permissionService;
this._localizationService = localizationService;
_customerService = customerService;
_dateTimeHelper = dateTimeHelper;
_priceFormatter = priceFormatter;
_taxService = taxService;
_priceCalculationService = priceCalculationService;
}

#endregion

#region Methods

//shopping carts
public ActionResult CurrentCarts()
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (!Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
return AccessDeniedView();
}

return View();
}
Expand All @@ -67,7 +52,7 @@ public ActionResult CurrentCarts(GridCommand command)
{
var gridModel = new GridModel<ShoppingCartModel>();

if (_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
var q = new CustomerSearchQuery
{
Expand All @@ -77,14 +62,15 @@ public ActionResult CurrentCarts(GridCommand command)
PageSize = command.PageSize
};

var customers = _customerService.SearchCustomers(q);
var guestStr = T("Admin.Customers.Guest").Text;
var customers = _customerService.SearchCustomers(q);

gridModel.Data = customers.Select(x =>
{
return new ShoppingCartModel
{
CustomerId = x.Id,
CustomerEmail = x.IsGuest() ? T("Admin.Customers.Guest").Text : x.Email,
CustomerEmail = x.IsGuest() ? guestStr : x.Email,
TotalItems = x.CountProductsInCart(ShoppingCartType.ShoppingCart)
};
});
Expand All @@ -109,15 +95,15 @@ public ActionResult GetCartDetails(int customerId)
{
var gridModel = new GridModel<ShoppingCartItemModel>();

if (_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
var customer = _customerService.GetCustomerById(customerId);
decimal taxRate;
var customer = _customerService.GetCustomerById(customerId);
var cart = customer.GetCartItems(ShoppingCartType.ShoppingCart);

gridModel.Data = cart.Select(sci =>
{
decimal taxRate;
var store = _storeService.GetStoreById(sci.Item.StoreId);
var store = Services.StoreService.GetStoreById(sci.Item.StoreId);

var sciModel = new ShoppingCartItemModel
{
Expand All @@ -126,12 +112,13 @@ public ActionResult GetCartDetails(int customerId)
ProductId = sci.Item.ProductId,
Quantity = sci.Item.Quantity,
ProductName = sci.Item.Product.Name,
ProductTypeName = sci.Item.Product.GetProductTypeLabel(_localizationService),
ProductTypeName = sci.Item.Product.GetProductTypeLabel(Services.Localization),
ProductTypeLabelHint = sci.Item.Product.ProductTypeLabelHint,
UnitPrice = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Item.Product, _priceCalculationService.GetUnitPrice(sci, true), out taxRate)),
Total = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Item.Product, _priceCalculationService.GetSubTotal(sci, true), out taxRate)),
UpdatedOn = _dateTimeHelper.ConvertToUserTime(sci.Item.UpdatedOnUtc, DateTimeKind.Utc)
};

return sciModel;
});

Expand All @@ -150,12 +137,12 @@ public ActionResult GetCartDetails(int customerId)
};
}


//wishlists
public ActionResult CurrentWishlists()
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (!Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
return AccessDeniedView();
}

return View();
}
Expand All @@ -165,7 +152,7 @@ public ActionResult CurrentWishlists(GridCommand command)
{
var gridModel = new GridModel<ShoppingCartModel>();

if (_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
var q = new CustomerSearchQuery
{
Expand All @@ -175,14 +162,15 @@ public ActionResult CurrentWishlists(GridCommand command)
PageSize = command.PageSize
};

var customers = _customerService.SearchCustomers(q);
var guestStr = T("Admin.Customers.Guest").Text;
var customers = _customerService.SearchCustomers(q);

gridModel.Data = customers.Select(x =>
{
return new ShoppingCartModel
{
CustomerId = x.Id,
CustomerEmail = x.IsGuest() ? T("Admin.Customers.Guest").Text : x.Email,
CustomerEmail = x.IsGuest() ? guestStr : x.Email,
TotalItems = x.CountProductsInCart(ShoppingCartType.Wishlist)
};
});
Expand All @@ -207,15 +195,15 @@ public ActionResult GetWishlistDetails(int customerId)
{
var gridModel = new GridModel<ShoppingCartItemModel>();

if (_permissionService.Authorize(StandardPermissionProvider.ManageOrders))
if (Services.Permissions.Authorize(StandardPermissionProvider.ManageOrders))
{
var customer = _customerService.GetCustomerById(customerId);
decimal taxRate;
var customer = _customerService.GetCustomerById(customerId);
var cart = customer.GetCartItems(ShoppingCartType.Wishlist);

gridModel.Data = cart.Select(sci =>
{
decimal taxRate;
var store = _storeService.GetStoreById(sci.Item.StoreId);
var store = Services.StoreService.GetStoreById(sci.Item.StoreId);

var sciModel = new ShoppingCartItemModel
{
Expand All @@ -224,12 +212,13 @@ public ActionResult GetWishlistDetails(int customerId)
ProductId = sci.Item.ProductId,
Quantity = sci.Item.Quantity,
ProductName = sci.Item.Product.Name,
ProductTypeName = sci.Item.Product.GetProductTypeLabel(_localizationService),
ProductTypeName = sci.Item.Product.GetProductTypeLabel(Services.Localization),
ProductTypeLabelHint = sci.Item.Product.ProductTypeLabelHint,
UnitPrice = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Item.Product, _priceCalculationService.GetUnitPrice(sci, true), out taxRate)),
Total = _priceFormatter.FormatPrice(_taxService.GetProductPrice(sci.Item.Product, _priceCalculationService.GetSubTotal(sci, true), out taxRate)),
UpdatedOn = _dateTimeHelper.ConvertToUserTime(sci.Item.UpdatedOnUtc, DateTimeKind.Utc)
};

return sciModel;
});

Expand All @@ -247,7 +236,5 @@ public ActionResult GetWishlistDetails(int customerId)
Data = gridModel
};
}

#endregion
}
}
Loading

0 comments on commit 21b31ea

Please sign in to comment.