コード例 #1
0
ファイル: OrderService.cs プロジェクト: kilonet/elfam
        public AddOrderResult AddOrder(Cart cart, User user, OrderShippingInfo orderInfo)
        {
            if (cart == null)
                throw new ArgumentException("cart is null");
            if (cart.Items == null)
                throw new ArgumentException("cart items is null");
            if (user == null)
                throw new ArgumentException("user is null");
            if (orderInfo == null)
                throw new ArgumentException("orderInfo is null");

            // construct order
            var orderLines = new List<OrderLine>();
            var orderOutcomes = new List<Outcome>();
            foreach (CartItem item in cart.Items)
            {
                IList<Income> incomes = FindOldestIncomesForProduct(item.Product, item.Quantity);
                int quantity = item.Quantity;
                var outcomes = new List<Outcome>();
                foreach (Income income in incomes)
                {
                    int n = Math.Min(quantity, income.QuantityCurrent);
                    income.QuantityCurrent -= n;
                    _daoTemplate.Save(income);
                    Outcome outcome = new Outcome(item.Product.Price, n, income, user.Discount + cart.CurrentDiscount());
                    quantity -= n;
                    outcomes.Add(outcome);
                }
                orderOutcomes.AddRange(outcomes);
                orderLines.Add(new OrderLine(item.Product, item.Quantity, outcomes));
            }
            Order order = new Order(orderOutcomes, orderLines);
            order.Comment = orderInfo.Comment ?? "";
            DeliverPrices prices = _daoTemplate.FindAll<DeliverPrices>()[0];
            if (orderInfo.PaymentType == PaymentType.OnPost)
            {
                order.DeliverPrice = prices.PostWhenReceived(cart.SummDiscount());
            }
            else
            {
                order.DeliverPrice = GetPrice(orderInfo.DeliverType);
            }
            order.CopyFrom(orderInfo);
            order.User = user;
            order.Discount = user.Discount + cart.CurrentDiscount();
            _daoTemplate.Save(order);
            order.Uid = _daoTemplate.FindByID<UniqueId>(order.Id).Uid;
            _daoTemplate.Save(order);

            return new AddOrderResult(){Order = order};
        }
コード例 #2
0
ファイル: OrderServiceTest.cs プロジェクト: kilonet/elfam
        public void TestRevertOrder()
        {
            var incomeSumBefore = _incomesForProduct.Sum(x => x.QuantityCurrent);

            var outcome1 = new Outcome(_product.Price, 10, _incomesForProduct[0], 0);
            var outcome2 = new Outcome(_product.Price, 5, _incomesForProduct[1], 0);
            var outcomes = new List<Outcome>() {outcome1, outcome2};
            var orderLine = new OrderLine(_product, 15, outcomes);

            _incomesForProduct[0].QuantityCurrent -= 10;
            _incomesForProduct[0].QuantityCurrent -= 5;

            Order order = new Order(outcomes, new List<OrderLine>(){orderLine});

            Assert.True(order.Outcomes().Sum(x => x.Quantity) == 15);

            _orderService.RevertOrder(order);

            Assert.True(_incomesForProduct.Sum(x => x.QuantityCurrent) == incomeSumBefore);
            Assert.True(order.Outcomes().Count() == 0);
            Assert.True(order.Status.Equals(OrderStatus.Revoked));
        }