2024-01-01 16:18:49 +03:30
|
|
|
|
namespace NetinaShop.Core.EntityServices.OrderHandlers;
|
2023-12-31 19:55:22 +03:30
|
|
|
|
|
|
|
|
|
|
public class CalculateOrderCommandHandler : IRequestHandler<CalculateOrderCommand,Order>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
|
|
2024-04-03 10:40:41 +03:30
|
|
|
|
public CalculateOrderCommandHandler(IRepositoryWrapper repositoryWrapper, IMediator mediator)
|
2023-12-31 19:55:22 +03:30
|
|
|
|
{
|
|
|
|
|
|
_repositoryWrapper = repositoryWrapper;
|
|
|
|
|
|
_mediator = mediator;
|
|
|
|
|
|
}
|
2024-02-10 14:00:31 +03:30
|
|
|
|
|
2023-12-31 19:55:22 +03:30
|
|
|
|
public async Task<Order> Handle(CalculateOrderCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
var order = await _mediator.Send(new GetOrderQuery(request.OrderId), cancellationToken);
|
|
|
|
|
|
if (order.OrderStatus != OrderStatus.OrderBag)
|
|
|
|
|
|
throw new AppException("Order is not in bag status and cant be calculate", ApiResultStatusCode.BadRequest);
|
2024-02-10 14:00:31 +03:30
|
|
|
|
var totalProductPrice = order.OrderProducts.Sum(op => op.ProductFee * op.Count);
|
2023-12-31 19:55:22 +03:30
|
|
|
|
var totalPackingPrice = order.OrderProducts.Sum(op => op.PackingCost);
|
2024-02-13 14:56:55 +03:30
|
|
|
|
|
2024-02-10 14:00:31 +03:30
|
|
|
|
//var servicePrice = _shopSettings.ServiceIsPercent
|
|
|
|
|
|
// ? (totalProductPrice / 100) * _shopSettings.ServiceFee
|
|
|
|
|
|
// : _shopSettings.ServiceFee;
|
2024-02-13 14:56:55 +03:30
|
|
|
|
|
2024-02-10 14:00:31 +03:30
|
|
|
|
var servicePrice = 0;
|
2024-02-13 12:22:58 +03:30
|
|
|
|
var deliveryPrice = order.OrderDelivery?.DeliveryCost ?? 0;
|
2024-02-13 14:56:55 +03:30
|
|
|
|
double productDiscountPrice = order.OrderProducts.Sum(op=>(op.ProductFee - op.ProductFeeWithDiscount) * op.Count);
|
|
|
|
|
|
double discountCodePrice = 0;
|
2023-12-31 19:55:22 +03:30
|
|
|
|
if (!order.DiscountCode.IsNullOrEmpty())
|
2024-02-13 14:56:55 +03:30
|
|
|
|
discountCodePrice += await _mediator.Send(new CalculateOrderDiscountCommand(order.DiscountCode, order),cancellationToken);
|
2023-12-31 19:55:22 +03:30
|
|
|
|
|
2024-02-10 14:00:31 +03:30
|
|
|
|
//var taxesPrice = (((totalProductPrice - discountPrice) + totalPackingPrice + servicePrice) / 100) * _shopSettings.TaxesFee;
|
2024-02-13 14:56:55 +03:30
|
|
|
|
|
2024-02-10 14:00:31 +03:30
|
|
|
|
var taxesPrice = 0;
|
2023-12-31 19:55:22 +03:30
|
|
|
|
|
2024-02-13 14:56:55 +03:30
|
|
|
|
order.SetTotalPrice(totalProductPrice, totalPackingPrice, servicePrice, deliveryPrice, productDiscountPrice, discountCodePrice, taxesPrice);
|
2024-02-12 22:01:15 +03:30
|
|
|
|
order.OrderProducts.Clear();
|
2024-02-13 12:22:58 +03:30
|
|
|
|
order.OrderDelivery = null;
|
2023-12-31 19:55:22 +03:30
|
|
|
|
_repositoryWrapper.SetRepository<Order>().Update(order);
|
|
|
|
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
return order;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|