Example #1
0
        public void AddInvoice()
        {
            IProductService productService = new ProductService(new ProductManager());
            ICustomerService customerService = new CustomerService(new CustomerManager());
            IInvoiceService invoiceService =  new InvoiceService(new InvoiceManager(new ProductManager(), new CustomerManager()));

            var products = productService.GetProducts();
            var customer = customerService.GetCustomers().First();

            var invoice = new Invoice
            {
                Id = Guid.NewGuid(),
                DateTime = DateTimeOffset.Now,
                CustomerId = customer.Id
            };

            var items = products.Select((product, i) =>
                new InvoiceItem
                {
                    Id = Guid.NewGuid(),
                    InvoiceId = invoice.Id,
                    ItemNumber = i + 1,
                    ProductId = product.Id,
                    Price = product.Price,
                    Quantity = 1
                });

            invoiceService.AddInvoice(invoice, items, 0);

            // Check products balances.
            foreach (var product in products)
            {
                Assert.AreEqual<int>(product.QuantityOnHand < 1 ? 0 : product.QuantityOnHand - 1, productService.GetProduct(product.Id).QuantityOnHand);
            }

            // Check customer balance.
            Assert.AreEqual<decimal>(customer.Balance + products.Sum(o => o.Price), customerService.GetCustomer(customer.Id).Balance);
        }
Example #2
0
        public void AddInvoiceWithQuantities()
        {
            IProductService productService = new ProductService(new ProductManager());
            ICustomerService customerService = new CustomerService(new CustomerManager());
            IInvoiceService invoiceService = new InvoiceService(new InvoiceManager(new ProductManager(), new CustomerManager()));

            var customer = customerService.GetCustomers().First();
            var product = productService.GetProducts().First();

            var invoice = new Invoice
            {
                Id = Guid.NewGuid(),
                DateTime = DateTimeOffset.Now,
                CustomerId = customer.Id
            };

            var items = new List<InvoiceItem>
            {
                new InvoiceItem
                {
                    Id = Guid.NewGuid(),
                    InvoiceId = invoice.Id,
                    ItemNumber = 1,
                    ProductId = product.Id,
                    Price = product.Price,
                    Quantity = 3
                }
            };

            invoiceService.AddInvoice(invoice, items, 0);

            // Check products balances.
            Assert.AreEqual<int>(product.QuantityOnHand < 3 ? 0 : product.QuantityOnHand - 3, productService.GetProduct(product.Id).QuantityOnHand);

            // Check customer balance.
            Assert.AreEqual<decimal>(customer.Balance + product.Price * 3, customerService.GetCustomer(customer.Id).Balance);
        }