public object removeCustomerOrderProduct(int productID, int customerorderID, int quantity)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    Customer_Order customerorder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                    if (customerorder != null)
                    {
                        Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == customerorder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                        if (backlog_Product != null)
                        {
                            backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder - quantity);
                            db.SaveChanges();

                            Product_Order_Line product_Order = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == customerorder.CustomerOrderID).FirstOrDefault();
                            if (product_Order != null)
                            {
                                db.Product_Order_Line.Remove(product_Order);
                                db.SaveChanges();

                                toReturn.Product_Order_Line = product_Order;
                            }
                        }
                        else
                        {
                            toReturn.Error = "Container Not Found";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Order Not Found";
                    }
                }
                else
                {
                    toReturn.Error = "Product Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Product Removal Unsuccessful";
            }

            return(toReturn);
        }
        public object cancelCustomerOrder(int customerorderID)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                //get sale
                Customer_Order newCustomerOrder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                if (newCustomerOrder != null)
                {
                    //get container
                    Container container = db.Containers.Where(x => x.ContainerID == newCustomerOrder.ContainerID).FirstOrDefault();



                    //get list of products in Sale
                    List <Product_Order_Line> product_Order_line = newCustomerOrder.Product_Order_Line.ToList();

                    if (container != null)
                    {
                        if (product_Order_line != null)
                        {
                            foreach (var prod in product_Order_line)
                            {
                                Product product = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault();
                                if (product != null)
                                {
                                    Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == newCustomerOrder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                                    if (backlog_Product != null)
                                    {
                                        backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder + prod.PLQuantity);
                                        db.SaveChanges();

                                        Product_Order_Line product_Order_Line = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == newCustomerOrder.CustomerOrderID).FirstOrDefault();
                                        if (product_Order_Line != null)
                                        {
                                            db.Product_Order_Line.Remove(product_Order_Line);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                                else
                                {
                                    toReturn.Error = "Product Not Found";
                                }
                            }

                            toReturn.Message = "Order Cancelled";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Container Not Found";
                    }
                }


                else
                {
                    toReturn.Error = "Cancel Failed: Order Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Customer Order Cancellation Unsuccessfully Completed";
            }

            return(toReturn);
        }
        public object addCustomerOrderProduct(int productID, int customerorderID, int quantity)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    Customer_Order customerorder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                    if (customerorder != null)
                    {
                        Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == customerorder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                        if (backlog_Product != null)
                        {
                            backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder + quantity);
                            db.SaveChanges();

                            Product_Order_Line product_Order_Line = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == customerorder.CustomerOrderID).FirstOrDefault();
                            if (product_Order_Line == null)
                            {
                                Product_Order_Line newProduct_Order_Line = new Product_Order_Line();
                                newProduct_Order_Line.ProductID       = product.ProductID;
                                newProduct_Order_Line.Product         = product;
                                newProduct_Order_Line.CustomerOrderID = customerorder.CustomerOrderID;
                                newProduct_Order_Line.Customer_Order  = customerorder;
                                newProduct_Order_Line.PLQuantity      = quantity;
                                db.Product_Order_Line.Add(newProduct_Order_Line);
                                db.SaveChanges();

                                toReturn.Product_Order_Line = db.Product_Order_Line.ToList().LastOrDefault();
                            }
                            else
                            {
                                product_Order_Line.PLQuantity = product_Order_Line.PLQuantity + quantity;
                                db.SaveChanges();

                                toReturn.Product_Order_Line = product_Order_Line;
                            }
                        }
                        else
                        {
                            toReturn.Error = "Container Not Found";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Order Not Found";
                    }
                }
                else
                {
                    toReturn.Error = "Product Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Product Add Unsuccessful";
            }

            return(toReturn);
        }
        public dynamic placeOrder(Customer_Order order)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            toReturn.newOrder = new ExpandoObject();
            string newOrderNo = "";

            try
            {
                //Get Product Order Line Details from order
                List <Product_Order_Line> productList = order.Product_Order_Line.ToList();

                if (order != null && productList != null)
                {
                    Customer              customer     = db.Customers.Where(x => x.CustomerID == order.CustomerID).FirstOrDefault();
                    User                  user         = db.Users.Where(x => x.UserID == order.UserID).FirstOrDefault();
                    Container             con          = db.Containers.Where(x => x.ContainerID == order.ContainerID).FirstOrDefault();
                    Customer_Order_Status order_Status = db.Customer_Order_Status.Where(x => x.CODescription == "Placed").FirstOrDefault();

                    //save customer order details
                    Customer_Order customerOrder = new Customer_Order();
                    customerOrder.Customer = customer;
                    customerOrder.Customer_Order_Status = order_Status;
                    customerOrder.User         = user;
                    customerOrder.Container    = con;
                    customerOrder.CusOrdNumber = order.CusOrdNumber;
                    customerOrder.CusOrdDate   = DateTime.Now;


                    db.Customer_Order.Add(customerOrder);
                    db.SaveChanges();

                    //Get The Saved Order details form the db
                    Customer_Order placedOrder = db.Customer_Order.ToList().LastOrDefault();

                    if (placedOrder != null)
                    {
                        //Add the Product_Order_Line Records for each product
                        foreach (var prod in productList)
                        {
                            Product            product   = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault();
                            Product_Order_Line orderProd = new Product_Order_Line();
                            orderProd.Customer_Order = placedOrder;
                            orderProd.Product        = product;
                            orderProd.PLQuantity     = prod.PLQuantity;

                            db.Product_Order_Line.Add(orderProd);
                            db.SaveChanges();
                        }

                        //Get the placed Orders Order Number
                        newOrderNo = placedOrder.CusOrdNumber;
                    }

                    else
                    {
                        toReturn.Message = "Something went wrong adding the products!";
                    }

                    //Set the return Objects
                    toReturn.newOrder = searchByOrderNo(newOrderNo);
                    toReturn.Message  = "Success! Order was placed successfully and email confirmation sent.";
                }
                else
                {
                    toReturn.Message = " Null Parameters Received";
                }
            }

            catch (Exception error)
            {
                toReturn.Message = error.Message;
            }

            return(toReturn);
        }