public async Task <IList <ProductOrder> > CreateAsync(IList <ProductWithCharmsOrderDto> productsWithCharmsOrder)
        {
            var productsIds = productsWithCharmsOrder.Select(s => s.ProductId).Distinct().ToList();
            var charmsIds   = productsWithCharmsOrder.SelectMany(s => s.Charms).Select(s => s.CharmId).ToList();
            var products    = await productQueryService.GetRawByIdsAsync(productsIds);

            var enabledDiscounts = await productCategoryDiscountQueryService.GetRawActiveAsync();

            var productsWithDiscounts = productCategoryDiscountQueryService.ApplyDiscountsToProducts(products, enabledDiscounts);
            var productOrders         = new List <ProductOrder>();

            foreach (var productWithCharmsOrder in productsWithCharmsOrder)
            {
                var productOrder        = new ProductOrder(productWithCharmsOrder.Amount, productWithCharmsOrder.Comment);
                var productWithDiscount = productsWithDiscounts.First(x => x.Product.Id == productWithCharmsOrder.ProductId);
                productOrder.AddProduct(productWithDiscount.Product);

                var finalPrice = productWithDiscount.Product.Price;

                if (productWithDiscount.ProductCategoryDiscount != null)
                {
                    productOrder.AddProductCategoryDiscount(productWithDiscount.ProductCategoryDiscount);
                    var discountValue = finalPrice * (productWithDiscount.ProductCategoryDiscount.PercentValue / 100.0M);
                    finalPrice -= discountValue;
                }

                productOrder.SetCurrentProductPrice(productWithDiscount.Product.Price);
                productOrder.SetFinalPrice(finalPrice);
                productOrders.Add(productOrder);
            }

            return(productOrders);
        }
예제 #2
0
        public async Task <IList <ProductOrder> > CreateAsync(IList <ProductWithCharmsOrderDto> productsWithCharmsOrder)
        {
            var productsIds = productsWithCharmsOrder.Select(s => s.ProductId);
            var charmsIds   = productsWithCharmsOrder.SelectMany(s => s.Charms).Select(s => s.CharmId).ToList();
            var products    = await productQueryService.GetRawByIdsAsync(productsIds);

            var charms = await charmQueryService.GetRawByIdsAsync(charmsIds);

            var enabledDiscounts = await productCategoryDiscountQueryService.GetRawActiveAsync();

            var productsDiscounts = productCategoryDiscountQueryService.ApplyDiscountsToProducts(products, enabledDiscounts);
            var productOrders     = new List <ProductOrder>();

            foreach (var productWithCharmsOrder in productsWithCharmsOrder)
            {
                var productOrder = new ProductOrder(productWithCharmsOrder.Amount, productWithCharmsOrder.Comment);

                var product = products.SingleOrDefault(s => s.Id == productWithCharmsOrder.ProductId);
                if (product == null)
                {
                    throw new StreetwoodException(ErrorCode.OrderProductsNotFound);
                }

                productOrder.AddProduct(product);

                var discount   = productsDiscounts.FirstOrDefault(s => s.Item1 == product.Id).Item2;
                var finalPrice = product.Price;

                productOrder.AddProductCategoryDiscount(discount);

                if (productWithCharmsOrder.HaveCharms)
                {
                    if (!product.AcceptCharms)
                    {
                        throw new StreetwoodException(ErrorCode.ProductNotAcceptCharms);
                    }

                    var productOrderCharms = CreateProductOrderCharms(productWithCharmsOrder.Charms, charms);
                    var charmsPrice        = productOrderCharms.Sum(s => s.CurrentPrice);

                    productOrder.AddProductOrderCharms(productOrderCharms);

                    // we subtract one charm because first is free
                    charmsPrice -= productOrderCharms.First().CurrentPrice;

                    if (discount != null)
                    {
                        var discountValue = (finalPrice + charmsPrice) * (discount.PercentValue / 100.0M);
                        finalPrice = (finalPrice + charmsPrice) - discountValue;
                    }
                    else
                    {
                        finalPrice += charmsPrice;
                    }

                    productOrder.SetCharmsPrice(charmsPrice);
                }
                else if (discount != null)
                {
                    var discountValue = finalPrice * (discount.PercentValue / 100.0M);
                    finalPrice -= discountValue;
                }

                productOrder.SetCurrentProductPrice(product.Price);
                productOrder.SetFinalPrice(finalPrice);

                productOrders.Add(productOrder);
            }

            return(productOrders);
        }