Esempio n. 1
0
        public List <OrderDetail> CreateOrderDetail(List <OrderDetail> orderDetails)
        {
            db.Configuration.LazyLoadingEnabled   = false;
            db.Configuration.ProxyCreationEnabled = false;
            foreach (var orderDetail in orderDetails)
            {
                var inStock = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p.UnitsInStock).FirstOrDefault();
                if (orderDetail.Quantity <= inStock)
                {
                    var product = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p).FirstOrDefault();
                    inStock -= orderDetail.Quantity;
                    product.UnitsInStock = inStock;
                }
                else
                {
                    var product = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p).FirstOrDefault();
                    orderDetail.Quantity -= inStock;
                    product.UnitsInStock  = 0;
                    db.SaveChanges();
                }
                db.insertOrderDetail(orderDetail.OrderID, orderDetail.ProductID, orderDetail.Quantity, orderDetail.Packaged, orderDetail.PackagedBy);
            }

            return(orderDetails);
        }
Esempio n. 2
0
        public List <OrderDetail> CreateOrderDetail(List <OrderDetail> orderDetails)
        {
            db.Configuration.LazyLoadingEnabled   = false;
            db.Configuration.ProxyCreationEnabled = false;

            //OrderDetail od = new OrderDetail
            //{
            //    OrderID = oid,
            //    ProductID = pid,
            //    Quantity = q,
            //    Packaged = false,
            //    PackagedBy = null
            //};

            //db.Orders.Add(o);
            foreach (var orderDetail in orderDetails)
            {
                var inStock = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p.UnitsInStock).FirstOrDefault();
                if (orderDetail.Quantity <= inStock)
                {
                    var product = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p).FirstOrDefault();
                    inStock -= orderDetail.Quantity;
                    product.UnitsInStock = inStock;
                }
                else
                {
                    var product = db.Products.Where(p => p.ProductID == orderDetail.ProductID).Select(p => p).FirstOrDefault();
                    orderDetail.Quantity -= inStock;
                    product.UnitsInStock  = 0;
                    db.SaveChanges();
                }
                db.insertOrderDetail(orderDetail.OrderID, orderDetail.ProductID, orderDetail.Quantity, orderDetail.Packaged, orderDetail.PackagedBy);
            }
            //db.insertOrderDetail(orderDetails., pid, q, false, null);

            return(orderDetails);
        }