diff --git a/.nuke/parameters.json b/.nuke/parameters.json index b65c359e1..2bd6f2b50 100644 --- a/.nuke/parameters.json +++ b/.nuke/parameters.json @@ -1,4 +1,4 @@ { "$schema": "./build.schema.json", "Solution": "VirtoCommerce.OrdersModule.sln" -} \ No newline at end of file +} diff --git a/src/VirtoCommerce.OrdersModule.Core/Model/CustomerOrder.cs b/src/VirtoCommerce.OrdersModule.Core/Model/CustomerOrder.cs index 88db4318e..e6ffbcbef 100644 --- a/src/VirtoCommerce.OrdersModule.Core/Model/CustomerOrder.cs +++ b/src/VirtoCommerce.OrdersModule.Core/Model/CustomerOrder.cs @@ -323,6 +323,47 @@ public virtual void ReduceDetails(string responseGroup) } + public virtual void RestoreDetails(CustomerOrder order) + { + TaxPercentRate = order.TaxPercentRate; + ShippingTotalWithTax = order.ShippingTotalWithTax; + PaymentTotalWithTax = order.PaymentTotalWithTax; + DiscountAmount = order.DiscountAmount; + Total = order.Total; + SubTotal = order.SubTotal; + SubTotalWithTax = order.SubTotalWithTax; + ShippingTotal = order.ShippingTotal; + PaymentTotal = order.PaymentTotal; + DiscountTotal = order.DiscountTotal; + DiscountTotalWithTax = order.DiscountTotalWithTax; + TaxTotal = order.TaxTotal; + Sum = order.Sum; + Fee = order.Fee; + FeeTotalWithTax = order.FeeTotalWithTax; + FeeTotal = order.FeeTotal; + FeeWithTax = order.FeeWithTax; + HandlingTotal = order.HandlingTotal; + HandlingTotalWithTax = order.HandlingTotalWithTax; + + foreach (var shipment in order.Shipments ?? Array.Empty()) + { + var targetShipment = Shipments?.FirstOrDefault(x => x.Id == shipment.Id); + targetShipment?.RestoreDetails(shipment); + } + + foreach (var payment in order.InPayments ?? Array.Empty()) + { + var targetPayment = InPayments?.FirstOrDefault(x => x.Id == payment.Id); + targetPayment?.RestoreDetails(payment); + } + + foreach (var item in order.Items ?? Array.Empty()) + { + var targetItem = Items?.FirstOrDefault(x => x.Id == item.Id); + targetItem?.RestoreDetails(item); + } + } + #region ICloneable members public override object Clone() diff --git a/src/VirtoCommerce.OrdersModule.Core/Model/LineItem.cs b/src/VirtoCommerce.OrdersModule.Core/Model/LineItem.cs index 72032c328..3c65c4b82 100644 --- a/src/VirtoCommerce.OrdersModule.Core/Model/LineItem.cs +++ b/src/VirtoCommerce.OrdersModule.Core/Model/LineItem.cs @@ -161,6 +161,7 @@ public virtual void ReduceDetails(string responseGroup) { Discounts = null; } + if (!orderResponseGroup.HasFlag(CustomerOrderResponseGroup.WithPrices)) { Price = 0m; @@ -172,6 +173,16 @@ public virtual void ReduceDetails(string responseGroup) } } + public virtual void RestoreDetails(LineItem item) + { + Price = item.Price; + PriceWithTax = item.PriceWithTax; + DiscountAmount = item.DiscountAmount; + DiscountAmountWithTax = item.DiscountAmountWithTax; + TaxTotal = item.TaxTotal; + TaxPercentRate = item.TaxPercentRate; + } + #region ICloneable members public virtual object Clone() diff --git a/src/VirtoCommerce.OrdersModule.Core/Model/PaymentIn.cs b/src/VirtoCommerce.OrdersModule.Core/Model/PaymentIn.cs index 1617fcdcb..9fb84b928 100644 --- a/src/VirtoCommerce.OrdersModule.Core/Model/PaymentIn.cs +++ b/src/VirtoCommerce.OrdersModule.Core/Model/PaymentIn.cs @@ -110,7 +110,6 @@ public virtual void ReduceDetails(string responseGroup) } if (!orderResponseGroup.HasFlag(CustomerOrderResponseGroup.WithPrices)) { - Price = 0m; PriceWithTax = 0m; DiscountAmount = 0m; @@ -124,6 +123,20 @@ public virtual void ReduceDetails(string responseGroup) } + public virtual void RestoreDetails(PaymentIn payment) + { + Price = payment.Price; + PriceWithTax = payment.PriceWithTax; + DiscountAmount = payment.DiscountAmount; + DiscountAmountWithTax = payment.DiscountAmountWithTax; + Total = payment.Total; + TotalWithTax = payment.TotalWithTax; + TaxTotal = payment.TaxTotal; + TaxPercentRate = payment.TaxPercentRate; + Sum = payment.Sum; + } + + #region ICloneable members public override object Clone() diff --git a/src/VirtoCommerce.OrdersModule.Core/Model/Shipment.cs b/src/VirtoCommerce.OrdersModule.Core/Model/Shipment.cs index 4bd2aa374..67c80436e 100644 --- a/src/VirtoCommerce.OrdersModule.Core/Model/Shipment.cs +++ b/src/VirtoCommerce.OrdersModule.Core/Model/Shipment.cs @@ -141,6 +141,19 @@ public virtual void ReduceDetails(string responseGroup) } + public virtual void RestoreDetails(Shipment shipment) + { + Price = shipment.Price; + PriceWithTax = shipment.PriceWithTax; + DiscountAmount = shipment.DiscountAmount; + DiscountAmountWithTax = shipment.DiscountAmountWithTax; + Total = shipment.Total; + TotalWithTax = shipment.TotalWithTax; + TaxTotal = shipment.TaxTotal; + TaxPercentRate = shipment.TaxPercentRate; + Sum = shipment.Sum; + } + #region ICloneable members public override object Clone() diff --git a/src/VirtoCommerce.OrdersModule.Data/Model/CustomerOrderEntity.cs b/src/VirtoCommerce.OrdersModule.Data/Model/CustomerOrderEntity.cs index bc691dded..bd5c514df 100644 --- a/src/VirtoCommerce.OrdersModule.Data/Model/CustomerOrderEntity.cs +++ b/src/VirtoCommerce.OrdersModule.Data/Model/CustomerOrderEntity.cs @@ -346,33 +346,26 @@ public override void Patch(OperationEntity target) operation.LanguageCode = LanguageCode; operation.IsAnonymous = IsAnonymous; - // Checks whether calculation of sum is needed to pass the result to the property of base class before calling of base.Patch - var needPatchPrices = !(GetNonCalculatablePrices().All(x => x == 0m) && - operation.GetNonCalculatablePrices().Any(x => x != 0m)); - - if (needPatchPrices) - { - operation.Total = Total; - operation.SubTotal = SubTotal; - operation.SubTotalWithTax = SubTotalWithTax; - operation.ShippingTotal = ShippingTotal; - operation.ShippingTotalWithTax = ShippingTotalWithTax; - operation.PaymentTotal = PaymentTotal; - operation.PaymentTotalWithTax = PaymentTotalWithTax; - operation.HandlingTotal = HandlingTotal; - operation.HandlingTotalWithTax = HandlingTotalWithTax; - operation.DiscountTotal = DiscountTotal; - operation.DiscountTotalWithTax = DiscountTotalWithTax; - operation.DiscountAmount = DiscountAmount; - operation.TaxTotal = TaxTotal; - operation.TaxPercentRate = TaxPercentRate; - operation.Fee = Fee; - operation.FeeWithTax = FeeWithTax; - operation.FeeTotal = FeeTotal; - operation.FeeTotalWithTax = FeeTotalWithTax; - operation.HandlingTotal = HandlingTotal; - operation.HandlingTotalWithTax = HandlingTotalWithTax; - } + operation.Total = Total; + operation.SubTotal = SubTotal; + operation.SubTotalWithTax = SubTotalWithTax; + operation.ShippingTotal = ShippingTotal; + operation.ShippingTotalWithTax = ShippingTotalWithTax; + operation.PaymentTotal = PaymentTotal; + operation.PaymentTotalWithTax = PaymentTotalWithTax; + operation.HandlingTotal = HandlingTotal; + operation.HandlingTotalWithTax = HandlingTotalWithTax; + operation.DiscountTotal = DiscountTotal; + operation.DiscountTotalWithTax = DiscountTotalWithTax; + operation.DiscountAmount = DiscountAmount; + operation.TaxTotal = TaxTotal; + operation.TaxPercentRate = TaxPercentRate; + operation.Fee = Fee; + operation.FeeWithTax = FeeWithTax; + operation.FeeTotal = FeeTotal; + operation.FeeTotalWithTax = FeeTotalWithTax; + operation.HandlingTotal = HandlingTotal; + operation.HandlingTotalWithTax = HandlingTotalWithTax; if (!Addresses.IsNullCollection()) { diff --git a/src/VirtoCommerce.OrdersModule.Data/Model/ISupportPartialPriceUpdate.cs b/src/VirtoCommerce.OrdersModule.Data/Model/ISupportPartialPriceUpdate.cs index d346934d9..1b6dd6b83 100644 --- a/src/VirtoCommerce.OrdersModule.Data/Model/ISupportPartialPriceUpdate.cs +++ b/src/VirtoCommerce.OrdersModule.Data/Model/ISupportPartialPriceUpdate.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; namespace VirtoCommerce.OrdersModule.Data.Model { + [Obsolete("This interface is deprecated and will be removed in future versions.", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions/")] public interface ISupportPartialPriceUpdate { void ResetPrices(); diff --git a/src/VirtoCommerce.OrdersModule.Data/Model/LineItemEntity.cs b/src/VirtoCommerce.OrdersModule.Data/Model/LineItemEntity.cs index 606ef3cf0..e3041af1f 100644 --- a/src/VirtoCommerce.OrdersModule.Data/Model/LineItemEntity.cs +++ b/src/VirtoCommerce.OrdersModule.Data/Model/LineItemEntity.cs @@ -317,20 +317,14 @@ public virtual void Patch(LineItemEntity target) target.IsConfigured = IsConfigured; target.IsDiscountAmountRounded = IsDiscountAmountRounded; - // Patch prices if there are non 0 prices in the patching entity, or all patched entity prices are 0 - var isNeedPatch = GetNonCalculatablePrices().Any(x => x != 0m) || target.GetNonCalculatablePrices().All(x => x == 0m); - - if (isNeedPatch) - { - target.TaxPercentRate = TaxPercentRate; - target.Price = Price; - target.DiscountAmount = DiscountAmount; - target.PriceWithTax = PriceWithTax; - target.DiscountAmountWithTax = DiscountAmountWithTax; - target.TaxTotal = TaxTotal; - target.Fee = Fee; - target.FeeWithTax = FeeWithTax; - } + target.TaxPercentRate = TaxPercentRate; + target.Price = Price; + target.DiscountAmount = DiscountAmount; + target.PriceWithTax = PriceWithTax; + target.DiscountAmountWithTax = DiscountAmountWithTax; + target.TaxTotal = TaxTotal; + target.Fee = Fee; + target.FeeWithTax = FeeWithTax; if (!Discounts.IsNullCollection()) { diff --git a/src/VirtoCommerce.OrdersModule.Data/Model/PaymentInEntity.cs b/src/VirtoCommerce.OrdersModule.Data/Model/PaymentInEntity.cs index dd0736796..b98acd6a9 100644 --- a/src/VirtoCommerce.OrdersModule.Data/Model/PaymentInEntity.cs +++ b/src/VirtoCommerce.OrdersModule.Data/Model/PaymentInEntity.cs @@ -268,10 +268,6 @@ public override void Patch(OperationEntity target) if (payment == null) throw new ArgumentException(@"target argument must be of type PaymentInEntity", nameof(target)); - // Patch prices if there are non 0 prices in the patching entity, or all patched entity prices are 0 - var isNeedPatch = GetNonCalculatablePrices().Any(x => x != 0m) || payment.GetNonCalculatablePrices().All(x => x == 0m); - - NeedPatchSum = isNeedPatch; base.Patch(payment); payment.TaxType = TaxType; @@ -291,19 +287,15 @@ public override void Patch(OperationEntity target) payment.CancelReason = CancelReason; payment.VendorId = VendorId; - if (isNeedPatch) - { - payment.Price = Price; - payment.PriceWithTax = PriceWithTax; - payment.DiscountAmount = DiscountAmount; - payment.DiscountAmountWithTax = DiscountAmountWithTax; - payment.TaxPercentRate = TaxPercentRate; - payment.TaxTotal = TaxTotal; - payment.Total = Total; - payment.TotalWithTax = TotalWithTax; - payment.Sum = Sum; - } - + payment.Price = Price; + payment.PriceWithTax = PriceWithTax; + payment.DiscountAmount = DiscountAmount; + payment.DiscountAmountWithTax = DiscountAmountWithTax; + payment.TaxPercentRate = TaxPercentRate; + payment.TaxTotal = TaxTotal; + payment.Total = Total; + payment.TotalWithTax = TotalWithTax; + payment.Sum = Sum; if (!Addresses.IsNullCollection()) { diff --git a/src/VirtoCommerce.OrdersModule.Data/Model/ShipmentEntity.cs b/src/VirtoCommerce.OrdersModule.Data/Model/ShipmentEntity.cs index 2b58c3b0e..c8847b937 100644 --- a/src/VirtoCommerce.OrdersModule.Data/Model/ShipmentEntity.cs +++ b/src/VirtoCommerce.OrdersModule.Data/Model/ShipmentEntity.cs @@ -292,10 +292,6 @@ public override void Patch(OperationEntity target) throw new ArgumentException(@"operation argument must be of type ShipmentEntity", nameof(target)); } - // Patch prices if there are non 0 prices in the patching entity, or all patched entity prices are 0 - var isNeedPatch = GetNonCalculatablePrices().Any(x => x != 0m) || shipmentEntity.GetNonCalculatablePrices().All(x => x == 0m); - - NeedPatchSum = isNeedPatch; base.Patch(target); shipmentEntity.FulfillmentCenterId = FulfillmentCenterId; @@ -321,19 +317,16 @@ public override void Patch(OperationEntity target) shipmentEntity.VendorId = VendorId; shipmentEntity.PickupLocationId = PickupLocationId; - if (isNeedPatch) - { - shipmentEntity.Price = Price; - shipmentEntity.PriceWithTax = PriceWithTax; - shipmentEntity.DiscountAmount = DiscountAmount; - shipmentEntity.DiscountAmountWithTax = DiscountAmountWithTax; - shipmentEntity.TaxPercentRate = TaxPercentRate; - shipmentEntity.TaxTotal = TaxTotal; - shipmentEntity.Total = Total; - shipmentEntity.TotalWithTax = TotalWithTax; - shipmentEntity.Fee = Fee; - shipmentEntity.FeeWithTax = FeeWithTax; - } + shipmentEntity.Price = Price; + shipmentEntity.PriceWithTax = PriceWithTax; + shipmentEntity.DiscountAmount = DiscountAmount; + shipmentEntity.DiscountAmountWithTax = DiscountAmountWithTax; + shipmentEntity.TaxPercentRate = TaxPercentRate; + shipmentEntity.TaxTotal = TaxTotal; + shipmentEntity.Total = Total; + shipmentEntity.TotalWithTax = TotalWithTax; + shipmentEntity.Fee = Fee; + shipmentEntity.FeeWithTax = FeeWithTax; if (!InPayments.IsNullCollection()) { diff --git a/src/VirtoCommerce.OrdersModule.Data/VirtoCommerce.OrdersModule.Data.csproj b/src/VirtoCommerce.OrdersModule.Data/VirtoCommerce.OrdersModule.Data.csproj index 68b690b53..653aeb26a 100644 --- a/src/VirtoCommerce.OrdersModule.Data/VirtoCommerce.OrdersModule.Data.csproj +++ b/src/VirtoCommerce.OrdersModule.Data/VirtoCommerce.OrdersModule.Data.csproj @@ -2,7 +2,7 @@ net8.0 true - 1591 + 1591,VC0011 True true true diff --git a/src/VirtoCommerce.OrdersModule.Web/Controllers/Api/OrderModuleController.cs b/src/VirtoCommerce.OrdersModule.Web/Controllers/Api/OrderModuleController.cs index e8df88de5..be4fe58b2 100644 --- a/src/VirtoCommerce.OrdersModule.Web/Controllers/Api/OrderModuleController.cs +++ b/src/VirtoCommerce.OrdersModule.Web/Controllers/Api/OrderModuleController.cs @@ -38,6 +38,7 @@ using VirtoCommerce.Platform.Core.ChangeLog; using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.Platform.Core.Json; +using VirtoCommerce.Platform.Core.Security; using VirtoCommerce.Platform.Core.Settings; using VirtoCommerce.StoreModule.Core.Model; using VirtoCommerce.StoreModule.Core.Services; @@ -368,6 +369,12 @@ public async Task UpdateOrder([FromBody] CustomerOrder customerOrd }); } + if (!User.HasGlobalPermission(ModuleConstants.Security.Permissions.ReadPrices)) + { + // Restore prices from order if user has no ReadPrices permission and receive the order without prices + customerOrder.RestoreDetails(order); + } + try { await customerOrderService.SaveChangesAsync(new[] { customerOrder }); diff --git a/src/VirtoCommerce.OrdersModule.Web/VirtoCommerce.OrdersModule.Web.csproj b/src/VirtoCommerce.OrdersModule.Web/VirtoCommerce.OrdersModule.Web.csproj index ca5a313a6..5f8ace3a2 100644 --- a/src/VirtoCommerce.OrdersModule.Web/VirtoCommerce.OrdersModule.Web.csproj +++ b/src/VirtoCommerce.OrdersModule.Web/VirtoCommerce.OrdersModule.Web.csproj @@ -23,7 +23,7 @@ - + diff --git a/tests/VirtoCommerce.OrdersModule.Tests/OrderOperationPricePatchingTests.cs b/tests/VirtoCommerce.OrdersModule.Tests/OrderOperationPricePatchingTests.cs index aa2c46bbc..463994d1b 100644 --- a/tests/VirtoCommerce.OrdersModule.Tests/OrderOperationPricePatchingTests.cs +++ b/tests/VirtoCommerce.OrdersModule.Tests/OrderOperationPricePatchingTests.cs @@ -49,7 +49,7 @@ public void PatchPrices_Shipment_NotNeeded(decimal sourcePrice, decimal patchedP sourceOperation.Patch(patchedOperation); // Assert - patchedOperation.Price.Should().Be(patchedPrice); + patchedOperation.Price.Should().Be(sourcePrice); } [Theory] @@ -94,7 +94,7 @@ public void PatchPrices_Payment_NotNeeded(decimal sourcePrice, decimal patchedPr sourceOperation.Patch(patchedOperation); // Assert - patchedOperation.Price.Should().Be(patchedPrice); + patchedOperation.Price.Should().Be(sourcePrice); } [Theory] @@ -139,7 +139,7 @@ public void PatchPrices_LineItem_NotNeeded(decimal sourcePrice, decimal patchedP sourceOperation.Patch(patchedOperation); // Assert - patchedOperation.Price.Should().Be(patchedPrice); + patchedOperation.Price.Should().Be(sourcePrice); } } }