public OrderSummary PlaceOrder(Order order)
        {
            ValidateOrder(order);

            var orderConfirmation = _orderFulfillmentService.Fulfill(order);

            var customer = _customerRepository.Get(order.CustomerId.Value);

            var netTotal = CalculateNetTotal(order);

            var taxRate = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country)
                          .FirstOrDefault(rate => rate.Description == "Default");

            _emailService.SendOrderConfirmationEmail(orderConfirmation.CustomerId, orderConfirmation.OrderId);

            var orderSummary = new OrderSummary
            {
                OrderNumber = orderConfirmation.OrderNumber,
                OrderId     = orderConfirmation.OrderId,
                CustomerId  = orderConfirmation.CustomerId,
                Taxes       = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country),
                NetTotal    = netTotal,
                Total       = netTotal * (1 + taxRate.Rate)
            };

            return(orderSummary);
        }
        public OrderSummary PlaceOrder(Order order)
        {
            ValidateOrder(order);

            var confirmation = _orderFulfillmentService.Fulfill(order);

            var customer = _customerRepository.Get(confirmation.CustomerId);

            var taxes = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country);

            var netTotal = order.OrderItems.Sum(c => c.Quantity * c.Product.Price);

            var orderSummary = new OrderSummary
            {
                CustomerId  = confirmation.CustomerId,
                OrderNumber = confirmation.OrderNumber,
                OrderId     = confirmation.OrderId,
                NetTotal    = netTotal,
                Taxes       = taxes,
            };

            _emailService.SendOrderConfirmationEmail(orderSummary.CustomerId, orderSummary.OrderId);

            return(orderSummary);
        }
        public OrderSummary PlaceOrder(Order order)
        {
            CheckIfOrderIsValid(order);

            var confirmation = _orderFulfillmentService.Fulfill(order);

            var customer = _customerRepository.Get(_customerId);

            var taxList = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country);

            var netTotal = order.OrderItems.Sum(orderItem => orderItem.Quantity * orderItem.Product.Price);

            var taxEntries = taxList as IList <TaxEntry> ?? taxList.ToList();

            var total = taxEntries.Sum(taxEntry => netTotal * taxEntry.Rate);

            _emailService.SendOrderConfirmationEmail(_customerId, confirmation.OrderId);

            return(new OrderSummary()
            {
                CustomerId = _customerId,
                OrderNumber = confirmation.OrderNumber,
                OrderId = confirmation.OrderId,
                OrderItems = order.OrderItems,
                NetTotal = netTotal,
                Taxes = taxEntries,
                Total = total,
                EstimatedDeliveryDate = DateTime.Now.AddDays(7).Date
            });
        }
        public void Init()
        {
            _productRepository       = Substitute.For <IProductRepository>();
            _orderFulfillmentService = Substitute.For <IOrderFulfillmentService>();
            _taxRateService          = Substitute.For <ITaxRateService>();
            _customerRepository      = Substitute.For <ICustomerRepository>();
            _emailService            = Substitute.For <IEmailService>();

            _orderService = new OrderService(_productRepository, _orderFulfillmentService, _taxRateService,
                                             _customerRepository, _emailService);

            _orderConfirmation = new OrderConfirmation {
                CustomerId = 42, OrderId = 12, OrderNumber = "OneTwoThree"
            };
            _taxEntryList = new List <TaxEntry>
            {
                new TaxEntry {
                    Description = "Default", Rate = (decimal)0.2
                },
                new TaxEntry {
                    Description = "High", Rate = (decimal)0.5
                }
            };
            _customer = new Customer {
                CustomerId = 2, PostalCode = "12345", Country = "USA"
            };

            _taxRateService.GetTaxEntries(_customer.PostalCode, _customer.Country).Returns(_taxEntryList);
            _customerRepository.Get(_customer.CustomerId.Value).Returns(_customer);
        }
Ejemplo n.º 5
0
        // probably dont want to use something like this
        private Order ValidOrderSetup()
        {
            _customerRepository.Get(Arg.Any <int>())
            .Returns(CreateCustomer(PostalCode, Country));

            _productRepository.IsInStock(Arg.Any <string>()).Returns(true);

            _orderFulfillmentService.Fulfill(Arg.Any <Order>())
            .Returns(new OrderConfirmation()
            {
                OrderNumber = "orderNum1", OrderId = 123, CustomerId = 30
            });

            _taxRateService.GetTaxEntries(Arg.Any <string>(), Arg.Any <string>()).Returns(CreateTaxEntryList());
            return(CreateValidOrder());
        }
Ejemplo n.º 6
0
        public OrderSummary PlaceOrder(Order order)
        {
            if (order.OrderItemsAreUnique() == false)
            {
                throw new OrderItemsAreNotUniqueException();
            }

            if (ProductsAreInStock(order, _productRepository) == false)
            {
                throw new OrderItemsAreNotInStockException();
            }

            OrderConfirmation      orderConfirmation = _orderFulfillmentService.Fulfill(order);
            IEnumerable <TaxEntry> taxEntries        = _taxRateService.GetTaxEntries(_postalCode, _country);

            decimal netTotal   = order.GetOrderTotal();
            decimal orderTotal = netTotal + GetTotalTax(taxEntries);

            OrderSummary orderSummary = new OrderSummary
            {
                OrderId     = orderConfirmation.OrderId,
                OrderNumber = orderConfirmation.OrderNumber,
                CustomerId  = orderConfirmation.CustomerId,
                OrderItems  = order.OrderItems,
                NetTotal    = netTotal,
                Taxes       = taxEntries,
                Total       = orderTotal
            };

            _emailService.SendOrderConfirmationEmail(orderConfirmation.CustomerId, orderConfirmation.OrderId);

            return(orderSummary);
        }
        public void BeforeEach()
        {
            _productRepo = Substitute.For<IProductRepository>();
            _orderFulfillmentService = Substitute.For<IOrderFulfillmentService>();
            _customerRepository = Substitute.For<ICustomerRepository>();
            _taxRateService = Substitute.For<ITaxRateService>();
            _emailService = Substitute.For<IEmailService>();

            _subject = new OrderService(_orderFulfillmentService,
                _customerRepository,
                _taxRateService,
                _emailService);

            _bestCustomer = new Customer
            {
                CustomerId = 42,
                PostalCode = "12345",
                Country = "Merica"
            };

            _listOfTaxEntries = new List<TaxEntry>
            {
                new TaxEntry {Description = "High Tax", Rate = (decimal) 0.60},
                new TaxEntry {Description = "Low Tax", Rate = (decimal) 0.10}
            };

            _orderConfirmation = new OrderConfirmation
            {
                OrderId = 1234,
                OrderNumber = "hello"
            };
            _customerRepository.Get(_bestCustomer.CustomerId.Value).Returns(_bestCustomer);
            _taxRateService.GetTaxEntries(_bestCustomer.PostalCode, _bestCustomer.Country).Returns(_listOfTaxEntries);
        }
Ejemplo n.º 8
0
        private List <TaxEntry> CreateListOfTaxEntries(Customer customer)
        {
            List <TaxEntry> listOfTaxEntries = (List <TaxEntry>)_taxRateService.GetTaxEntries(
                customer.PostalCode,
                customer.Country);

            return(listOfTaxEntries);
        }
        public OrderSummary PlaceOrder(Order order)
        {
            // Check duplicate SKU
            if (order.HasNoDuplicateSku() == false)
            {
                throw new ArgumentException(DuplicateSkuError);
            }

            // Get customer information
            int customerId = order.CustomerId ?? -1;

            if (customerId == -1)
            {
                throw new ArgumentException(NoCustomerIdError);
            }
            Customer customer = _customerService.Get(customerId);

            // Get tax rate
            IEnumerable <TaxEntry> tax = _taxService.GetTaxEntries(customer.PostalCode, customer.Country);
            decimal totalTax           = 0;

            foreach (TaxEntry entry in tax)
            {
                totalTax += entry.Rate;
            }

            // Check product stock and calculate totals
            decimal netTotal = 0;
            decimal total    = 0;

            foreach (OrderItem items in order.OrderItems)
            {
                if (_productRepo.IsInStock(items.Product.Sku) == false)
                {
                    throw new ArgumentException(NoStockError);
                }

                netTotal += items.Product.Price * items.Quantity;
            }
            total = totalTax / 100 * netTotal;

            // All checks pass, get confirmation properties
            OrderConfirmation confirmation = _fulfillmentService.Fulfill(order);

            // Create new summary with required information filled in
            OrderSummary summary = new OrderSummary
            {
                OrderId     = confirmation.OrderId,
                OrderNumber = confirmation.OrderNumber,
                NetTotal    = netTotal,
                Total       = total
            };

            _emailService.SendOrderConfirmationEmail(customerId, summary.OrderId);

            return(summary);
        }
Ejemplo n.º 10
0
        public OrderSummary PlaceOrder(Order order)
        {
            var orderItems   = order.OrderItems;
            var orderSummary = new OrderSummary();

            if (AreOrderItemsUnique(orderItems) && AllOrderItemsInStock(orderItems))
            {
                var confirmation = _orderFulfillmentService.Fulfill(order);

                var customer = _customerRepository.Get((int)order.CustomerId);
                if (customer == null)
                {
                    throw new Exception("customer does not exist");
                }

                var taxEntries = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country);
                var taxrate    = TotalTaxRate(taxEntries);
                var netTotal   = NetTotal(orderItems);
                var ordertotal = OrderTotal(netTotal, taxrate);


                orderSummary = new OrderSummary
                {
                    OrderId     = confirmation.OrderId,
                    CustomerId  = (int)customer.CustomerId,
                    OrderNumber = confirmation.OrderNumber,
                    OrderItems  = orderItems,
                    NetTotal    = netTotal,
                    Total       = ordertotal,
                    Taxes       = taxEntries
                };

                _emailService.SendOrderConfirmationEmail((int)customer.CustomerId, confirmation.OrderId);
            }
            else if (AllOrderItemsInStock(orderItems))
            {
                throw new SKUsNotUniqueException();
            }
            else if (AreOrderItemsUnique(orderItems))
            {
                throw new ProductsNotInStockException();
            }
            else
            {
                throw new SKUsNotUniqueAndProductNotInStockException();
            }


            return(orderSummary);
        }
Ejemplo n.º 11
0
        public OrderSummary PlaceOrder(Order order)
        {
            decimal orderNetTotal = 0.0M;

            foreach (var orderItem in order.OrderItems)
            {
                bool check = _productRepository.IsInStock("laptop");
                if (!_productRepository.IsInStock("laptop"))
                {
                    ExceptionReasons.Add("There is not enough stock available for the product " + orderItem.Product.Sku + " to complete the order");
                }
                orderNetTotal += orderItem.Product.Price * orderItem.Quantity;
            }

            var customer = _customerRepository.Get((int)order.CustomerId);

            if (customer == null)
            {
                ExceptionReasons.Add("Customer not found");
                throw new OrderPlacementValidationException(ExceptionReasons);
            }

            var taxEntryList = _taxRateService.GetTaxEntries(customer.PostalCode, customer.Country);

            if (taxEntryList == null)
            {
                ExceptionReasons.Add("Tax Entry for the specified Postal Code: " + customer.PostalCode + " and Country: " + customer.Country + " was invalid");
                throw new OrderPlacementValidationException(ExceptionReasons);
            }

            var total = taxEntryList.Sum(t => t.Rate * orderNetTotal);

            var orderConf = _orderFulfillmentService.Fulfill(order);

            var orderSummary = new OrderSummary
            {
                CustomerId            = orderConf.CustomerId,
                EstimatedDeliveryDate = DateTime.Now,
                NetTotal    = orderNetTotal,
                OrderId     = orderConf.OrderId,
                OrderItems  = order.OrderItems,
                OrderNumber = orderConf.OrderNumber,
                Total       = total,
                Taxes       = taxEntryList
            };

            _emailService.SendOrderConfirmationEmail(orderSummary.CustomerId, orderSummary.OrderId);

            return(orderSummary);
        }
Ejemplo n.º 12
0
        public void OrderService_PlaceOrder_OrderSummaryHasTaxes()
        {
            //Arrange
            var order = Get_Order_WithoutDuplicates();

            _productRepo.AllItemsAreStocked();
            _orderFulfillment.SetOrderFullfillmentReturn(order);

            //Act
            var orderSummary = _sut.PlaceOrder(order);
            var customer     = _customerRepository.Get(1);

            //Assert
            orderSummary.Taxes.ShouldBe(_taxRateService.GetTaxEntries(customer.PostalCode, customer.Country));
        }
 public static void SetTaxRateServiceReturn(this ITaxRateService taxRate)
 {
     taxRate.GetTaxEntries(Arg.Any <string>(), Arg.Any <string>()).Returns(new List <TaxEntry>
     {
         new TaxEntry
         {
             Description = "State Tax",
             Rate        = 5.6m
         },
         new TaxEntry
         {
             Description = "Federal Tax",
             Rate        = 8.2m
         }
     });
 }
        public void BeforeEach()
        {
            _productRepo             = Substitute.For <IProductRepository>();
            _orderFulfillmentService = Substitute.For <IOrderFulfillmentService>();
            _customerRepository      = Substitute.For <ICustomerRepository>();
            _taxRateService          = Substitute.For <ITaxRateService>();
            _emailService            = Substitute.For <IEmailService>();

            _subject = new OrderService(_orderFulfillmentService,
                                        _customerRepository,
                                        _taxRateService,
                                        _emailService);

            _bestCustomer = new Customer
            {
                CustomerId = 42,
                PostalCode = "12345",
                Country    = "Merica"
            };

            _listOfTaxEntries = new List <TaxEntry>
            {
                new TaxEntry {
                    Description = "High Tax", Rate = (decimal)0.60
                },
                new TaxEntry {
                    Description = "Low Tax", Rate = (decimal)0.10
                }
            };

            _orderConfirmation = new OrderConfirmation
            {
                OrderId     = 1234,
                OrderNumber = "hello"
            };
            _customerRepository.Get(_bestCustomer.CustomerId.Value).Returns(_bestCustomer);
            _taxRateService.GetTaxEntries(_bestCustomer.PostalCode, _bestCustomer.Country).Returns(_listOfTaxEntries);
        }
Ejemplo n.º 15
0
 private static IEnumerable <TaxEntry> GetTaxes(ITaxRateService taxRateService, Customer customer)
 {
     return(taxRateService.GetTaxEntries(customer.PostalCode, customer.Country));
 }