public ActionResult AddProducts(WebApp.Models.OrderSale orderSale)
        {
            if (ModelState.IsValid)
            {
                var order = _orderRepo.GetOrderById(orderSale.OrderId);

                var inventory = _locRepo.GetInventory(order.LocationId);

                var product = inventory.Find(p => p.ProductId == orderSale.ProductId);

                if (product.Quantity - orderSale.Quantity < 0)
                {
                    return(RedirectToAction(nameof(Detail), new { OrderId = orderSale.OrderId }));
                }
                else
                {
                    bool isInOrder = order.OrderSales.Any(p => p.Product.ProductId == product.ProductId);

                    var totalPrice = product.Product.Price * orderSale.Quantity;

                    if (isInOrder)
                    {
                        foreach (var prod in order.OrderSales)
                        {
                            if (prod.Product.ProductId == orderSale.ProductId)
                            {
                                var currentOrder = _orderRepo.GetOrderSaleById(prod.ProductId);
                                currentOrder.Quantity += orderSale.Quantity;
                                currentOrder.SalePrice = (decimal)totalPrice;
                                order.Total            = (decimal)totalPrice;
                            }
                        }
                        ;
                    }
                    else
                    {
                        var newOrderSale = new ClassLibrary.Models.OrderSale(orderSale.ProductId, orderSale.ProductName, (decimal)totalPrice, orderSale.Quantity);
                        order.Total += (decimal)totalPrice;
                        _orderRepo.AddProductToOrder(newOrderSale);
                        _orderRepo.UpdateOrder(order);
                    }
                    product.Quantity -= orderSale.Quantity;
                    return(RedirectToAction(nameof(AddProducts), new { id = order.OrderId }));
                }
            }
            return(View());
        }
Beispiel #2
0
        public void AddProductToOrder(ClassLibrary.Models.OrderSale orderSale)
        {
            using var context = new Project0DBContext(_contextOptions);


            var newOrderSale = new OrderSale()
            {
                ProductId   = orderSale.ProductId,
                ProductName = orderSale.ProductName,
                Quantity    = orderSale.Quantity,
                SalePrice   = orderSale.SalePrice
            };

            context.OrderSales.Add(newOrderSale);

            context.SaveChanges();
        }
Beispiel #3
0
        public List <ClassLibrary.Models.OrderSale> GetOrderProducts(ClassLibrary.Models.Order order)
        {
            using var context = new Project0DBContext(_contextOptions);

            var dbOrderSales = context.OrderSales.Where(o => o.OrderId == order.OrderId).Include(o => o.Product).ToList();

            var appOrderSales = new List <ClassLibrary.Models.OrderSale>();

            foreach (var item in dbOrderSales)
            {
                var orderSale = new Product()
                {
                    ProductId = item.Product.ProductId,
                    Name      = item.Product.Name,
                    Price     = item.Product.Price,
                };

                var newOrderSale = new ClassLibrary.Models.OrderSale(item.OrderId, item.ProductName, item.SalePrice, item.Quantity);

                newOrderSale.ProductId = orderSale.ProductId;
                appOrderSales.Add(newOrderSale);
            }
            return(appOrderSales);
        }