Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public async Task<IPagedList<Order>> Handle(GetOrderQuery request, CancellationT
if (!string.IsNullOrEmpty(request.OrderTagId))
query = query.Where(o => o.OrderTags.Any(y => y == request.OrderTagId));

if (!string.IsNullOrEmpty(request.CustomerCurrencyCode))
query = query.Where(o => o.CustomerCurrencyCode == request.CustomerCurrencyCode);

if (!string.IsNullOrWhiteSpace(request.PurchaseOrderNumber))
query = query.WherePurchaseOrderNumber(request.PurchaseOrderNumber);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class GetOrderQuery : IRequest<IPagedList<Order>>
public int PageIndex { get; set; } = 0;
public int PageSize { get; set; } = int.MaxValue;
public string OrderTagId { get; set; } = "";
public string CustomerCurrencyCode { get; set; } = "";
public string PurchaseOrderNumber { get; set; } = null;
public bool OverdueOnly { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ public async Task Handle_FiltersByCustomerAndPages()
Assert.AreEqual("NEWEST", result[0].Code);
}

[TestMethod]
public async Task Handle_FiltersByCustomerCurrency()
{
await _repository.Collection.InsertOneAsync(new Order { CustomerCurrencyCode = "EUR", Code = "A" });
await _repository.Collection.InsertOneAsync(new Order { CustomerCurrencyCode = "USD", Code = "B" });

var result = await _handler.Handle(new GetOrderQuery { CustomerCurrencyCode = "EUR", PageSize = 1 },
CancellationToken.None);

Assert.AreEqual(1, result.TotalCount);
Assert.AreEqual("A", result[0].Code);
}

[TestMethod]
public async Task Handle_FiltersByPurchaseOrderNumber()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Business.Core.Queries.Checkout.Orders;
using Grand.Domain;
using Grand.Domain.Directory;
using Grand.Domain.Orders;
using Grand.Domain.Stores;
using Grand.Infrastructure.Caching;
using Grand.Mediator;
using Grand.Web.AdminShared.Models.Directory;
using Grand.Web.AdminShared.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -22,6 +26,7 @@ public class CurrencyViewModelServiceTests
private Mock<ISettingService> _settingServiceMock;
private Mock<IRequestTranslationService> _translationServiceMock;
private Mock<ICacheBase> _cacheBaseMock;
private Mock<IMediator> _mediatorMock;
private CurrencyViewModelService _service;

[TestInitialize]
Expand All @@ -33,14 +38,18 @@ public void Setup()
_settingServiceMock = new Mock<ISettingService>();
_translationServiceMock = new Mock<IRequestTranslationService>();
_cacheBaseMock = new Mock<ICacheBase>();
_mediatorMock = new Mock<IMediator>();
_mediatorMock.Setup(m => m.Send(It.IsAny<GetOrderQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new PagedList<Order>());

_service = new CurrencyViewModelService(
_currencyServiceMock.Object,
_storeServiceMock.Object,
_currencySettings,
_settingServiceMock.Object,
_translationServiceMock.Object,
_cacheBaseMock.Object);
_cacheBaseMock.Object,
_mediatorMock.Object);
}

[TestMethod]
Expand Down Expand Up @@ -207,4 +216,20 @@ public async Task ValidateCurrencyDelete_NotPrimaryAndNotLast_CanDelete()
Assert.IsTrue(canDelete);
Assert.AreEqual(string.Empty, message);
}

[TestMethod]
public async Task ValidateCurrencyDelete_UsedByOrders_CannotDelete()
{
_currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), It.IsAny<string>()))
.ReturnsAsync(new List<Currency> { new() { Id = "currency-1" }, new() { Id = "currency-2" } });
_mediatorMock.Setup(m => m.Send(It.Is<GetOrderQuery>(q => q.CustomerCurrencyCode == "EUR"), It.IsAny<CancellationToken>()))
.ReturnsAsync(new PagedList<Order>(new[] { new Order() }, 0, 1));
_translationServiceMock.Setup(t => t.GetResource("Admin.Configuration.Currencies.CantDeleteUsedByOrders"))
.Returns("Used by orders");

var (canDelete, message) = await _service.ValidateCurrencyDelete(new Currency { Id = "currency-1", CurrencyCode = "EUR" });

Assert.IsFalse(canDelete);
Assert.AreEqual("Used by orders", message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Business.Core.Queries.Checkout.Orders;
using Grand.Domain.Directory;
using Grand.Infrastructure.Caching;
using Grand.Mediator;
using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Directory;
Expand All @@ -20,6 +22,7 @@ public class CurrencyViewModelService : ICurrencyViewModelService
private readonly ISettingService _settingService;
private readonly IRequestTranslationService _translationService;
private readonly ICacheBase _cacheBase;
private readonly IMediator _mediator;

#endregion

Expand All @@ -30,14 +33,16 @@ public CurrencyViewModelService(ICurrencyService currencyService,
CurrencySettings currencySettings,
ISettingService settingService,
IRequestTranslationService translationService,
ICacheBase cacheBase)
ICacheBase cacheBase,
IMediator mediator)
{
_currencyService = currencyService;
_storeService = storeService;
_currencySettings = currencySettings;
_settingService = settingService;
_translationService = translationService;
_cacheBase = cacheBase;
_mediator = mediator;
}

#endregion
Expand Down Expand Up @@ -129,6 +134,11 @@ public virtual async Task MarkAsPrimaryStoreCurrency(string id)
if (currency.Id == _currencySettings.PrimaryExchangeRateCurrencyId)
return (false, _translationService.GetResource("Admin.Configuration.Currencies.CantDeleteExchange"));

//an order keeps the code of the currency it was placed in: its pages, refunds and loyalty points need it
if (!string.IsNullOrEmpty(currency.CurrencyCode) &&
(await _mediator.Send(new GetOrderQuery { CustomerCurrencyCode = currency.CurrencyCode, PageSize = 1 })).TotalCount > 0)
return (false, _translationService.GetResource("Admin.Configuration.Currencies.CantDeleteUsedByOrders"));

var allCurrencies = await _currencyService.GetAllCurrencies();
if (allCurrencies.Count == 1 && allCurrencies[0].Id == currency.Id)
return (false, "At least one published currency is required.");
Expand Down
Binary file modified src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2829,4 +2829,7 @@
<Resource Name="Admin.Settings.Customer.SignOutOnAllDevices" Area="Admin">
<Value>Sign out on all devices</Value>
</Resource>
<Resource Name="Admin.Configuration.Currencies.CantDeleteUsedByOrders" Area="Admin">
<Value>Orders were placed in this currency, so it can't be deleted. Unpublish it instead.</Value>
</Resource>
</Language>
Loading