public virtual async Task <Order> GenerateAsync(Guid customerUserId, CreateOrderDto input, Dictionary <Guid, ProductDto> productDict, Dictionary <Guid, ProductDetailDto> productDetailDict) { var effectiveCurrency = await GetEffectiveCurrencyAsync(); var orderLines = new List <OrderLine>(); foreach (var inputOrderLine in input.OrderLines) { orderLines.Add(await GenerateOrderLineAsync( input, inputOrderLine, productDict, productDetailDict, effectiveCurrency)); } var productTotalPrice = orderLines.Select(x => x.TotalPrice).Sum(); var paymentExpireIn = orderLines.Select(x => productDict[x.ProductId].GetSkuPaymentExpireIn(x.ProductSkuId)) .Min(); var totalPrice = productTotalPrice; var totalDiscount = orderLines.Select(x => x.TotalDiscount).Sum(); var order = new Order( id: _guidGenerator.Create(), tenantId: _currentTenant.Id, storeId: input.StoreId, customerUserId: customerUserId, currency: effectiveCurrency.Code, productTotalPrice: productTotalPrice, totalDiscount: totalDiscount, totalPrice: totalPrice, actualTotalPrice: totalPrice - totalDiscount, customerRemark: input.CustomerRemark, paymentExpiration: paymentExpireIn.HasValue ? _clock.Now.Add(paymentExpireIn.Value) : null ); input.MapExtraPropertiesTo(order, MappingPropertyDefinitionChecks.Destination); await AddOrderExtraFeesAsync(order, customerUserId, input, productDict, effectiveCurrency); order.SetOrderLines(orderLines); order.SetOrderNumber(await _orderNumberGenerator.CreateAsync(order)); return(order); }
public virtual async Task <Order> GenerateAsync(Guid customerUserId, CreateOrderDto input, Dictionary <Guid, ProductDto> productDict) { var orderLines = new List <OrderLine>(); foreach (var inputOrderLine in input.OrderLines) { orderLines.Add(await GenerateOrderLineAsync(input, inputOrderLine, productDict)); } var storeCurrency = await GetStoreCurrencyAsync(input.StoreId); if (orderLines.Any(x => x.Currency != storeCurrency)) { throw new CurrencyIsLimitException(storeCurrency); } var productTotalPrice = orderLines.Select(x => x.TotalPrice).Sum(); var totalPrice = productTotalPrice; var totalDiscount = orderLines.Select(x => x.TotalDiscount).Sum(); var order = new Order( id: _guidGenerator.Create(), tenantId: _currentTenant.Id, storeId: input.StoreId, customerUserId: customerUserId, currency: storeCurrency, productTotalPrice: productTotalPrice, totalDiscount: totalDiscount, totalPrice: totalPrice, actualTotalPrice: totalPrice - totalDiscount, customerRemark: input.CustomerRemark); input.MapExtraPropertiesTo(order, MappingPropertyDefinitionChecks.Destination); await AddOrderExtraFeesAsync(order, customerUserId, input, productDict); order.SetOrderLines(orderLines); order.SetOrderNumber(await _orderNumberGenerator.CreateAsync(order)); return(order); }