2024-06-06 13:26:40 +03:30
|
|
|
|
namespace Netina.Core.EntityServices.OrderHandlers;
|
2023-12-31 19:55:22 +03:30
|
|
|
|
|
2024-07-01 21:08:32 +03:30
|
|
|
|
public class CalculateOrderCommandHandler(IRepositoryWrapper repositoryWrapper, IMediator mediator,ISettingService settingService) : IRequestHandler<CalculateOrderCommand,Order>
|
2023-12-31 19:55:22 +03:30
|
|
|
|
{
|
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)
|
|
|
|
|
|
{
|
2024-07-01 21:08:32 +03:30
|
|
|
|
var setting = await settingService.GetSettingAsync("ShopSetting", cancellationToken);
|
|
|
|
|
|
double taxesFee = 9;
|
|
|
|
|
|
if (setting is ShopSetting shopSetting)
|
|
|
|
|
|
taxesFee = shopSetting.TaxesFee;
|
2024-06-06 22:52:49 +03:30
|
|
|
|
var order = await mediator.Send(new GetOrderQuery(request.OrderId), cancellationToken);
|
2023-12-31 19:55:22 +03:30
|
|
|
|
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;
|
2024-09-29 12:31:08 +03:30
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!order.DiscountCode.IsNullOrEmpty())
|
|
|
|
|
|
discountCodePrice += await mediator.Send(new CalculateOrderDiscountCommand(order.DiscountCode, order),
|
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception )
|
|
|
|
|
|
{
|
|
|
|
|
|
order.RemoveDiscount();
|
|
|
|
|
|
}
|
2025-02-11 14:22:42 +03:30
|
|
|
|
|
|
|
|
|
|
if (totalProductPrice > 5000000)
|
|
|
|
|
|
deliveryPrice = 0;
|
2024-07-01 21:08:32 +03:30
|
|
|
|
var taxesPrice = (((totalProductPrice - (discountCodePrice + productDiscountPrice)) + totalPackingPrice + servicePrice) / 100) * taxesFee;
|
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;
|
2024-06-06 22:52:49 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Order>().Update(order);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
2023-12-31 19:55:22 +03:30
|
|
|
|
return order;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|