Skip to content

Commit

Permalink
Merge pull request #268 from EasyAbp/order-and-refund
Browse files Browse the repository at this point in the history
Introduce `PaymentAmount` to validate refund amounts
  • Loading branch information
gdlcf88 committed Jul 29, 2023
2 parents dbfba52 + a21b3fd commit 77a50a1
Show file tree
Hide file tree
Showing 45 changed files with 7,426 additions and 302 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>4.3.0</Version>
<Version>5.0.0-preview.1</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class OrderDto : ExtensibleFullAuditedEntityDto<Guid>, IOrder

public DateTime? PaidTime { get; set; }

public decimal? PaymentAmount { get; set; }

public DateTime? CompletionTime { get; set; }

public DateTime? CanceledTime { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class OrderExtraFeeDto : IOrderExtraFee
public decimal Fee { get; set; }

public decimal RefundAmount { get; set; }

public decimal? PaymentAmount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ public class OrderLineDto : ExtensibleFullAuditedEntityDto<Guid>, IOrderLine
public int RefundedQuantity { get; set; }

public decimal RefundAmount { get; set; }

public decimal? PaymentAmount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"OrderCustomerRemark": "Customer remark",
"OrderStaffRemark": "Staff remark",
"OrderPaidTime": "Paid time",
"OrderPaymentAmount": "Payment amount",
"OrderCompletionTime": "Completion time",
"OrderCanceledTime": "Canceled time",
"OrderReducedInventoryAfterPlacingTime": "Time of inventory reduced after placing",
Expand All @@ -45,6 +46,9 @@
"OrderLineTotalDiscount": "Total discount",
"OrderLineActualTotalPrice": "Actual total price",
"OrderLineQuantity": "Quantity",
"OrderLineRefundedQuantity": "Refunded quantity",
"OrderLineRefundAmount": "Refund amount",
"OrderLinePaymentAmount": "Payment amount",
"OrderDiscount": "Order discount",
"OrderDiscountOrderId": "Order ID",
"OrderDiscountOrderLineId": "Order line ID",
Expand All @@ -60,6 +64,7 @@
"OrderExtraFeeDisplayName": "Display name",
"OrderExtraFeeFee": "Fee",
"OrderExtraFeeRefundAmount": "Refund amount",
"OrderExtraFeePaymentAmount": "Payment amount",
"EasyAbp.EShop.Orders:UnexpectedCurrency": "Only the specified currency {expectedCurrency} is allowed.",
"EasyAbp.EShop.Orders:OrderLineInvalidQuantity": "Invalid quantity {quantity} for product {productId} (SKU: {productSkuId}).",
"EasyAbp.EShop.Orders:DiscountAmountOverflow": "The discount amount overflows.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"OrderCustomerRemark": "客户备注",
"OrderStaffRemark": "员工备注",
"OrderPaidTime": "支付时间",
"OrderPaymentAmount": "支付金额",
"OrderCompletionTime": "完成时间",
"OrderCanceledTime": "取消时间",
"OrderReducedInventoryAfterPlacingTime": "下单后减库存时间",
Expand All @@ -45,6 +46,9 @@
"OrderLineTotalDiscount": "总折扣",
"OrderLineActualTotalPrice": "折后总价",
"OrderLineQuantity": "数量",
"OrderLineRefundedQuantity": "已退款的数量",
"OrderLineRefundAmount": "已退款金额",
"OrderLinePaymentAmount": "支付金额",
"OrderDiscount": "订单折扣项",
"OrderDiscountOrderId": "订单 ID",
"OrderDiscountOrderLineId": "订单项 ID",
Expand All @@ -60,6 +64,7 @@
"OrderExtraFeeDisplayName": "显示名称",
"OrderExtraFeeFee": "金额",
"OrderExtraFeeRefundAmount": "已退款金额",
"OrderExtraFeePaymentAmount": "支付金额",
"EasyAbp.EShop.Orders:UnexpectedCurrency": "只允许指定的{expectedCurrency}货币",
"EasyAbp.EShop.Orders:OrderLineInvalidQuantity": "产品{productId}(SKU: {productSkuId})的{quantity}数量无效",
"EasyAbp.EShop.Orders:DiscountAmountOverflow": "折扣金额溢出",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"OrderCustomerRemark": "客戶備註",
"OrderStaffRemark": "員工備註",
"OrderPaidTime": "支付時間",
"OrderPaymentAmount": "支付金額",
"OrderCompletionTime": "完成時間",
"OrderCanceledTime": "取消時間",
"OrderReducedInventoryAfterPlacingTime": "下單後減庫存時間",
Expand All @@ -45,6 +46,9 @@
"OrderLineTotalDiscount": "總折扣",
"OrderLineActualTotalPrice": "折後總價",
"OrderLineQuantity": "數量",
"OrderLineRefundedQuantity": "已退款的數量",
"OrderLineRefundAmount": "已退款金額",
"OrderLinePaymentAmount": "支付金額",
"OrderDiscount": "訂單折扣項",
"OrderDiscountOrderId": "訂單 ID",
"OrderDiscountOrderLineId": "訂單項 ID",
Expand All @@ -60,6 +64,7 @@
"OrderExtraFeeDisplayName": "顯示名稱",
"OrderExtraFeeFee": "金額",
"OrderExtraFeeRefundAmount": "已退款金額",
"OrderExtraFeePaymentAmount": "支付金額",
"EasyAbp.EShop.Orders:UnexpectedCurrency": "只允許指定的{expectedCurrency}貨幣",
"EasyAbp.EShop.Orders:OrderLineInvalidQuantity": "產品{productId}(SKU: {productSkuId})的{quantity}數量無效",
"EasyAbp.EShop.Orders:DiscountAmountOverflow": "折扣金額溢出",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public interface IOrder : IMultiStore, IHasExtraProperties

DateTime? PaidTime { get; }

decimal? PaymentAmount { get; }

DateTime? CompletionTime { get; }

DateTime? CanceledTime { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public interface IOrderExtraFee
decimal Fee { get; }

decimal RefundAmount { get; }

decimal? PaymentAmount { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ public interface IOrderLine : IHasExtraProperties, IHasProductGroupDisplayName
int RefundedQuantity { get; }

decimal RefundAmount { get; }

decimal? PaymentAmount { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace EasyAbp.EShop.Orders.Orders
public class OrderEto : ExtensibleObject, IOrder, IFullAuditedObject, IMultiTenant
{
public Guid Id { get; set; }

public Guid? TenantId { get; set; }

public Guid StoreId { get; set; }

public string OrderNumber { get; set; }

public Guid CustomerUserId { get; set; }

public OrderStatus OrderStatus { get; set; }
Expand All @@ -29,29 +29,31 @@ public class OrderEto : ExtensibleObject, IOrder, IFullAuditedObject, IMultiTena
public decimal TotalDiscount { get; set; }

public decimal TotalPrice { get; set; }

public decimal ActualTotalPrice { get; set; }

public decimal RefundAmount { get; set; }

public string CustomerRemark { get; set; }

public string StaffRemark { get; set; }

public Guid? PaymentId { get; set; }

public DateTime? PaidTime { get; set; }

public decimal? PaymentAmount { get; set; }

public DateTime? CompletionTime { get; set; }

public DateTime? CanceledTime { get; set; }

public string CancellationReason { get; set; }

public DateTime? ReducedInventoryAfterPlacingTime { get; set; }

public DateTime? ReducedInventoryAfterPaymentTime { get; set; }

public DateTime? PaymentExpiration { get; set; }

IEnumerable<IOrderLine> IOrder.OrderLines => OrderLines;
Expand All @@ -64,17 +66,17 @@ public class OrderEto : ExtensibleObject, IOrder, IFullAuditedObject, IMultiTena
public List<OrderExtraFeeEto> OrderExtraFees { get; set; }

public DateTime CreationTime { get; set; }

public Guid? CreatorId { get; set; }

public DateTime? LastModificationTime { get; set; }

public Guid? LastModifierId { get; set; }

public bool IsDeleted { get; set; }

public DateTime? DeletionTime { get; set; }

public Guid? DeleterId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class OrderExtraFeeEto : IOrderExtraFee
public decimal Fee { get; set; }

public decimal RefundAmount { get; set; }

public decimal? PaymentAmount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class OrderLineEto : ExtensibleObject, IOrderLine
public Guid Id { get; set; }

public Guid ProductId { get; set; }

public Guid ProductSkuId { get; set; }

public Guid? ProductDetailId { get; set; }

public DateTime ProductModificationTime { get; set; }

public DateTime? ProductDetailModificationTime { get; set; }

public string ProductGroupName { get; set; }

public string ProductGroupDisplayName { get; set; }

public string ProductUniqueName { get; set; }
Expand All @@ -32,23 +32,25 @@ public class OrderLineEto : ExtensibleObject, IOrderLine
public string SkuName { get; set; }

public string SkuDescription { get; set; }

public string MediaResources { get; set; }

public string Currency { get; set; }

public decimal UnitPrice { get; set; }

public decimal TotalPrice { get; set; }

public decimal TotalDiscount { get; set; }

public decimal ActualTotalPrice { get; set; }

public int Quantity { get; set; }

public int RefundedQuantity { get; set; }

public decimal RefundAmount { get; set; }

public decimal? PaymentAmount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<RootNamespace />
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="EasyAbp.EShop.Orders.Application.Tests" />
<InternalsVisibleTo Include="EasyAbp.EShop.Orders.Domain.Tests" />
<InternalsVisibleTo Include="EasyAbp.EShop.Plugins.Promotions.Application.Tests" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NodaMoney" Version="$(NodaMoneyVersion)" />
<PackageReference Include="Volo.Abp.AutoMapper" Version="$(AbpVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EasyAbp.EShop.Orders.Orders;

public interface IMoneyDistributor
{
Task<MoneyDistributionResult<TKey>> DistributeAsync<TKey>(string currency, Dictionary<TKey, decimal> currentAmounts,
decimal totalDistributionAmount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Volo.Abp;

namespace EasyAbp.EShop.Orders.Orders;

public class MoneyDistributionResult<TKey>
{
[NotNull]
public string Currency { get; }

public Dictionary<TKey, decimal> AmountsAfterDistribution { get; }

public Dictionary<TKey, decimal> Distributions { get; }

public MoneyDistributionResult(
[NotNull] string currency,
Dictionary<TKey, decimal> amountsAfterDistribution,
Dictionary<TKey, decimal> distributions)
{
Currency = Check.NotNullOrWhiteSpace(currency, nameof(currency));
AmountsAfterDistribution = Check.NotNull(amountsAfterDistribution, nameof(amountsAfterDistribution));
Distributions = Check.NotNull(distributions, nameof(distributions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NodaMoney;
using Volo.Abp.DependencyInjection;

namespace EasyAbp.EShop.Orders.Orders;

public class MoneyDistributor : IMoneyDistributor, ITransientDependency
{
public virtual Task<MoneyDistributionResult<TKey>> DistributeAsync<TKey>(string currency,
Dictionary<TKey, decimal> currentAmounts, decimal totalDistributionAmount)
{
var distributions = new Dictionary<TKey, decimal>();
var originalAmountSum = currentAmounts.Sum(x => x.Value);
var remainingDistributionAmount = totalDistributionAmount;

foreach (var key in currentAmounts.Keys)
{
var calculatedDistributionAmount = new Money(
currentAmounts[key] / originalAmountSum *
totalDistributionAmount, currency, MidpointRounding.ToZero);

var distributionAmount = currentAmounts[key] + calculatedDistributionAmount.Amount < 0
? currentAmounts[key]
: calculatedDistributionAmount.Amount;

distributions[key] = distributionAmount;
currentAmounts[key] += distributionAmount;
remainingDistributionAmount -= distributionAmount;
}

foreach (var key in currentAmounts.OrderByDescending(x => x.Value).Select(x => x.Key))
{
if (remainingDistributionAmount == decimal.Zero)
{
break;
}

var distributionAmount = currentAmounts[key] + remainingDistributionAmount < 0
? currentAmounts[key]
: remainingDistributionAmount;

distributions[key] += distributionAmount;
currentAmounts[key] += distributionAmount;
remainingDistributionAmount -= distributionAmount;
}

if (remainingDistributionAmount != decimal.Zero)
{
throw new ApplicationException("The MoneyDistributor failed to distribute the remaining");
}

return Task.FromResult(new MoneyDistributionResult<TKey>(currency, currentAmounts, distributions));
}
}
Loading

0 comments on commit 77a50a1

Please sign in to comment.