Exemple #1
0
        private void OnCartChanged(object sender, EventArgs e)
        {
            StartIfNew();

            _transaction.TotalPrice = Cart.TotalPrice;

            // set products to transaction
            var cartProducts = Cart.Products
                               .Select(x => new
            {
                x.Product,
                x.Quantity,
            })
                               .ToArray();

            // serialization can be slow but no new thread is created since sometimes sync issues appear (empty products in completed transaction)
            // todo: hypothesis above should be proved
            var transactionProducts = cartProducts
                                      .Select(x => EkTransactionProduct.FromProduct(x.Product.EkProduct, x.Product.GetDescription(), x.Quantity))
                                      .ToArray();

            _transaction.SetProducts(transactionProducts);
            _receiptData.Products = cartProducts
                                    .Select(x => new ReceiptDataProduct(x.Product, x.Quantity))
                                    .ToArray();
        }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task <Order> GetOrderAsync(int orderId)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            if (orderId % 2 == 0)
            {
                throw EntityNotFoundException.Create <Order>(orderId);
            }

            var now       = DateTime.Now;
            var createdOn = new DateTime(now.Year, now.Month, now.Day, 12, 05, 35);

            var order = new Order()
            {
                Id                     = orderId,
                KioskId                = 1,
                CreatedOnLocalTime     = createdOn,
                PreferableLanguageCode = "ru",
                Products               = Enumerable.Range(1, 3)
                                         .Select(x =>
                {
                    var source = x % 2 == 0
                                    ? EkProductSourceEnum.OmegaAutoBiz
                                    : EkProductSourceEnum.AllegroPl;
                    var product = new EkTransactionProduct()
                    {
                        Key    = new EkProductKey(source, x.ToString()).ToKey(),
                        Source = source,
                        Name   = new MultiLanguageString()
                        {
                            [Languages.RussianCode] = $"Продукт {x}",
                        },
                        Description = new MultiLanguageString()
                        {
                            [Languages.RussianCode] = $"Описание продукта {x}",
                        },
                        BasePrice             = 125 * x,
                        BasePriceCurrencyCode = source == EkProductSourceEnum.AllegroPl
                                            ? "PLN"
                                            : "UAH",
                        Quantity = x,
                    };

                    if (source == EkProductSourceEnum.AllegroPl)
                    {
                        product.Price                = Math.Ceiling(product.BasePrice * 7.93m * 1.5m);
                        product.PriceCurrencyCode    = "UAH";
                        product.PriceCalculationInfo = "BasePrice x 7.93 (exchange rate) x 1.2";
                    }
                    else
                    {
                        product.Price                = Math.Ceiling(product.BasePrice * 1.2m);
                        product.PriceCurrencyCode    = product.BasePriceCurrencyCode;
                        product.PriceCalculationInfo = "BasePrice x 1.2";
                    }

                    return(product);
                })
                                         .ToArray(),
                Customer = new EkCustomerInfo()
                {
                    FullName = "Тимчик Вадим",
                    Phone    = "+380977861272",
                },
                Delivery = new EkDeliveryInfo()
                {
                    Type    = EkDeliveryTypeEnum.Courier,
                    Address = new EkTransactionAddress()
                    {
                        City         = "Киев",
                        AddressLine1 = "ул. Предславинская 30, оф. 25",
                    },
                },
                ReceiptNumber = $"XXX-{orderId}",
            };

            order.TotalPrice             = order.Products.Sum(x => x.Price * x.Quantity);
            order.TotalPriceCurrencyCode = "UAH";

            return(order);
        }