Skip to content

Commit

Permalink
#1918 Add filters by dates on the discount list page
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanovM committed Dec 27, 2017
1 parent 11931a6 commit 71fda1c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 65 deletions.
82 changes: 45 additions & 37 deletions src/Libraries/Nop.Services/Discounts/DiscountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,42 +214,49 @@ public virtual Discount GetDiscountById(int discountId)
/// <summary>
/// Gets all discounts
/// </summary>
/// <param name="discountType">Discount type; null to load all discount</param>
/// <param name="couponCode">Coupon code to find (exact match)</param>
/// <param name="discountName">Discount name</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="discountType">Discount type; pass null to load all records</param>
/// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
/// <param name="discountName">Discount name; pass null or empty to load all records</param>
/// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
/// <param name="startDateUtc">Discount start date; pass null to load all records</param>
/// <param name="endDateUtc">Discount end date; pass null to load all records</param>
/// <returns>Discounts</returns>
public virtual IList<Discount> GetAllDiscounts(DiscountType? discountType = null,
string couponCode = "", string discountName = "", bool showHidden = false)
string couponCode = null, string discountName = null, bool showHidden = false,
DateTime? startDateUtc = null, DateTime? endDateUtc = null)
{
var query = _discountRepository.Table;

if (!showHidden)
{
//The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
//That's why we pass the date value
//The function 'CurrentUtcDateTime' is not supported by SQL Server Compact, that's why we pass the date value
var nowUtc = DateTime.UtcNow;
query = query.Where(d =>
(!d.StartDateUtc.HasValue || d.StartDateUtc <= nowUtc)
&& (!d.EndDateUtc.HasValue || d.EndDateUtc >= nowUtc));
query = query.Where(discount =>
(!discount.StartDateUtc.HasValue || discount.StartDateUtc <= nowUtc) &&
(!discount.EndDateUtc.HasValue || discount.EndDateUtc >= nowUtc));
}

//filter by dates
if (startDateUtc.HasValue)
query = query.Where(discount => !discount.StartDateUtc.HasValue || discount.StartDateUtc >= startDateUtc.Value);
if (endDateUtc.HasValue)
query = query.Where(discount => !discount.EndDateUtc.HasValue || discount.EndDateUtc <= endDateUtc.Value);

//filter by coupon code
if (!string.IsNullOrEmpty(couponCode))
{
query = query.Where(d => d.CouponCode == couponCode);
}
query = query.Where(discount => discount.CouponCode == couponCode);

//filter by name
if (!string.IsNullOrEmpty(discountName))
{
query = query.Where(d => d.Name.Contains(discountName));
}
query = query.Where(discount => discount.Name.Contains(discountName));

//filter by type
if (discountType.HasValue)
{
var discountTypeId = (int) discountType.Value;
query = query.Where(d => d.DiscountTypeId == discountTypeId);
}
query = query.Where(discount => discount.DiscountTypeId == (int)discountType.Value);

query = query.OrderBy(d => d.Name);
query = query.OrderBy(discount => discount.Name).ThenBy(discount => discount.Id);

var discounts = query.ToList();
return discounts;
return query.ToList();
}

/// <summary>
Expand Down Expand Up @@ -281,21 +288,21 @@ public virtual void UpdateDiscount(Discount discount)
//event notification
_eventPublisher.EntityUpdated(discount);
}

#endregion

#region Discounts (caching)

/// <summary>
/// Gets all discounts (cachable models)
/// </summary>
/// <param name="discountType">Discount type; null to load all discount</param>
/// <param name="couponCode">Coupon code to find (exact match)</param>
/// <param name="discountName">Discount name</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="discountType">Discount type; pass null to load all records</param>
/// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
/// <param name="discountName">Discount name; pass null or empty to load all records</param>
/// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
/// <returns>Discounts</returns>
public virtual IList<DiscountForCaching> GetAllDiscountsForCaching(DiscountType? discountType = null,
string couponCode = "", string discountName = "", bool showHidden = false)
string couponCode = null, string discountName = null, bool showHidden = false)
{
//we cache discounts between requests. Otherwise, they will be loaded for almost each HTTP request
//we have to use the following workaround with cachable model (DiscountForCaching) because
Expand All @@ -304,19 +311,20 @@ public virtual void UpdateDiscount(Discount discount)
//we load all discounts, and filter them using "discountType" parameter later (in memory)
//we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
//that's why let's access the database only once
var key = string.Format(DiscountEventConsumer.DISCOUNT_ALL_KEY, showHidden, couponCode, discountName);
var result = _cacheManager.Get(key, () =>
var cacheKey = string.Format(DiscountEventConsumer.DISCOUNT_ALL_KEY,
showHidden, couponCode ?? string.Empty, discountName ?? string.Empty);
var discounts = _cacheManager.Get(cacheKey, () =>
{
var discounts = GetAllDiscounts(null, couponCode, discountName, showHidden);
return discounts.Select(d => d.MapDiscount()).ToList();
return GetAllDiscounts(couponCode: couponCode, discountName: discountName, showHidden: showHidden)
.Select(discount => discount.MapDiscount()).ToList();
});

//we know that this method is usually inkoved multiple times
//that's why we filter discounts by type on the application layer
if (discountType.HasValue)
{
result = result.Where(d => d.DiscountType == discountType.Value).ToList();
}
return result;
discounts = discounts.Where(discount => discount.DiscountType == discountType.Value).ToList();

return discounts;
}

/// <summary>
Expand Down
28 changes: 16 additions & 12 deletions src/Libraries/Nop.Services/Discounts/IDiscountService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Discounts;
Expand Down Expand Up @@ -28,13 +29,16 @@ public partial interface IDiscountService
/// <summary>
/// Gets all discounts
/// </summary>
/// <param name="discountType">Discount type; null to load all discount</param>
/// <param name="couponCode">Coupon code to find (exact match)</param>
/// <param name="discountName">Discount name</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="discountType">Discount type; pass null to load all records</param>
/// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
/// <param name="discountName">Discount name; pass null or empty to load all records</param>
/// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
/// <param name="startDateUtc">Discount start date; pass null to load all records</param>
/// <param name="endDateUtc">Discount end date; pass null to load all records</param>
/// <returns>Discounts</returns>
IList<Discount> GetAllDiscounts(DiscountType? discountType = null,
string couponCode = "", string discountName = "", bool showHidden = false);
IList<Discount> GetAllDiscounts(DiscountType? discountType = null,
string couponCode = null, string discountName = null, bool showHidden = false,
DateTime ? startDateUtc = null, DateTime? endDateUtc = null);

/// <summary>
/// Inserts a discount
Expand All @@ -55,13 +59,13 @@ public partial interface IDiscountService
/// <summary>
/// Gets all discounts (cachable models)
/// </summary>
/// <param name="discountType">Discount type; null to load all discount</param>
/// <param name="couponCode">Coupon code to find (exact match)</param>
/// <param name="discountName">Discount name</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="discountType">Discount type; pass null to load all records</param>
/// <param name="couponCode">Coupon code to find (exact match); pass null or empty to load all records</param>
/// <param name="discountName">Discount name; pass null or empty to load all records</param>
/// <param name="showHidden">A value indicating whether to show expired and not started discounts</param>
/// <returns>Discounts</returns>
IList<DiscountForCaching> GetAllDiscountsForCaching(DiscountType? discountType = null,
string couponCode = "", string discountName = "", bool showHidden = false);
string couponCode = null, string discountName = null, bool showHidden = false);

/// <summary>
/// Get category identifiers to which a discount is applied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11910,6 +11910,18 @@
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchDiscountType.Hint">
<Value>Search by discount type.</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchEndDate">
<Value>End date</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchEndDate.Hint">
<Value>The end date for the search.</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchStartDate">
<Value>Start date</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchStartDate.Hint">
<Value>The start date for the search.</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.Requirements">
<Value>Requirements</Value>
</LocaleResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,25 @@ public virtual IActionResult List(DiscountListModel model, DataSourceRequest com
if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
return AccessDeniedKendoGridJson();

DiscountType? discountType = null;
if (model.SearchDiscountTypeId > 0)
discountType = (DiscountType)model.SearchDiscountTypeId;
var discounts = _discountService.GetAllDiscounts(discountType,
model.SearchDiscountCouponCode,
model.SearchDiscountName,
true);
var discountType = model.SearchDiscountTypeId > 0 ? (DiscountType?)model.SearchDiscountTypeId : null;
var startDateUtc = model.SearchStartDate.HasValue ?
(DateTime?)_dateTimeHelper.ConvertToUtcTime(model.SearchStartDate.Value, _dateTimeHelper.CurrentTimeZone) : null;
var endDateUtc = model.SearchEndDate.HasValue ?
(DateTime?)_dateTimeHelper.ConvertToUtcTime(model.SearchEndDate.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1) : null;

var discounts = _discountService.GetAllDiscounts(discountType, model.SearchDiscountCouponCode,
model.SearchDiscountName, true, startDateUtc, endDateUtc);

var gridModel = new DataSourceResult
{
Data = discounts.PagedForCommand(command).Select(x =>
Data = discounts.PagedForCommand(command).Select(discount =>
{
var discountModel = x.ToModel();
discountModel.DiscountTypeName = x.DiscountType.GetLocalizedEnum(_localizationService, _workContext);
discountModel.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode;
discountModel.TimesUsed = _discountService.GetAllDiscountUsageHistory(x.Id, pageSize: 1).TotalCount;
var discountModel = discount.ToModel();
discountModel.DiscountTypeName = discount.DiscountType.GetLocalizedEnum(_localizationService, _workContext);
discountModel.PrimaryStoreCurrencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)?.CurrencyCode;
discountModel.TimesUsed = _discountService.GetAllDiscountUsageHistory(discount.Id, pageSize: 1).TotalCount;
return discountModel;
}),
Total = discounts.Count
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.Rendering;
using Nop.Web.Framework.Mvc.ModelBinding;
using Nop.Web.Framework.Mvc.Models;
Expand All @@ -21,5 +23,13 @@ public DiscountListModel()
[NopResourceDisplayName("Admin.Promotions.Discounts.List.SearchDiscountType")]
public int SearchDiscountTypeId { get; set; }
public IList<SelectListItem> AvailableDiscountTypes { get; set; }

[NopResourceDisplayName("Admin.Promotions.Discounts.List.SearchStartDate")]
[UIHint("DateNullable")]
public DateTime? SearchStartDate { get; set; }

[NopResourceDisplayName("Admin.Promotions.Discounts.List.SearchEndDate")]
[UIHint("DateNullable")]
public DateTime? SearchEndDate { get; set; }
}
}
22 changes: 20 additions & 2 deletions src/Presentation/Nop.Web/Areas/Admin/Views/Discount/List.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@
<nop-select asp-for="SearchDiscountTypeId" asp-items="Model.AvailableDiscountTypes"/>
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<nop-label asp-for="SearchStartDate"/>
</div>
<div class="col-md-8">
<nop-editor asp-for="SearchStartDate"/>
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<nop-label asp-for="SearchEndDate"/>
</div>
<div class="col-md-8">
<nop-editor asp-for="SearchEndDate"/>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="button" id="search-discounts" class="btn btn-primary btn-search">
Expand Down Expand Up @@ -168,13 +184,15 @@
$("#search-discounts").click();
return false;
}
});
});
function additionalData() {
var data = {
SearchDiscountTypeId: $('#@Html.IdFor(model => model.SearchDiscountTypeId)').val(),
SearchDiscountCouponCode: $('#@Html.IdFor(model => model.SearchDiscountCouponCode)').val(),
SearchDiscountName: $('#@Html.IdFor(model => model.SearchDiscountName)').val()
SearchDiscountName: $('#@Html.IdFor(model => model.SearchDiscountName)').val(),
SearchStartDate: $('#@Html.IdFor(model => model.SearchStartDate)').val(),
SearchEndDate: $('#@Html.IdFor(model => model.SearchEndDate)').val(),
};
addAntiForgeryToken(data);
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public new void SetUp()
[Test]
public void Can_get_all_discount()
{
var discounts = _discountService.GetAllDiscounts(null);
var discounts = _discountService.GetAllDiscounts();
discounts.ShouldNotBeNull();
(discounts.Any()).ShouldBeTrue();
}
Expand Down
12 changes: 12 additions & 0 deletions upgradescripts/4.00-4.10 (under development)/upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ set @resources='
<LocaleResource Name="Admin.Configuration.Settings.Catalog.DefaultViewMode.Hint">
<Value>Choose the default view mode for catalog pages.</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchEndDate">
<Value>End date</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchEndDate.Hint">
<Value>The end date for the search.</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchStartDate">
<Value>Start date</Value>
</LocaleResource>
<LocaleResource Name="Admin.Promotions.Discounts.List.SearchStartDate.Hint">
<Value>The start date for the search.</Value>
</LocaleResource>
</Language>
'

Expand Down

0 comments on commit 71fda1c

Please sign in to comment.