public async Task <ActionResult> Cancel(int?id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Invoice invoice = await db.Invoices.FindAsync(id);

            if (invoice == null)
            {
                return(HttpNotFound());
            }

            invoice.InvoiceStatus = (InvoiceStatus)3;

            ReplenishProducts(invoice.Order);

            await db.SaveChangesAsync();

            ProductsHub.BroadcastData();

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            if (User.IsInRole("Validated User") || User.IsInRole("Unvalidated User"))
            {
                return(RedirectToAction("Warning", "Home", new { message = "ACCESS DENIED" }));
            }
            Product product = await db.Products.FindAsync(id);

            db.Products.Remove(product);
            await db.SaveChangesAsync();

            ProductsHub.BroadcastData();
            return(RedirectToAction("Index"));
        }
        public void ReplenishProducts(Order order)
        {
            // start by getting all productInOrder data
            var productInOrders = db.ProductInOrders.Where(x => x.OrderID == order.OrderID).Include(x => x.Product);


            foreach (var item in productInOrders)
            {
                Product product = item.Product;
                if (product.UnitsInStock == 0)
                {
                    product.ProductStatus = (ProductStatus)0;
                }
                product.UnitsInStock += item.Quantity;
            }

            db.SaveChanges();
            ProductsHub.BroadcastData();
        }
        public async Task <ActionResult> Edit([Bind(Include = "ProductID,ProductName,Description,ProductStatus,ProductCategoryID,ProductPrice,ProductUnitOfMeasurementID,UnitsInStock")] Product product)
        {
            if (User.IsInRole("Validated User") || User.IsInRole("Unvalidated User"))
            {
                return(RedirectToAction("Warning", "Home", new { message = "ACCESS DENIED" }));
            }
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                product.ProductStatus   = (product.UnitsInStock > 0) ? (ProductStatus)0 : (ProductStatus)1;
                await db.SaveChangesAsync();

                ProductsHub.BroadcastData();
                return(RedirectToAction("Index"));
            }
            ViewBag.ProductCategoryID          = new SelectList(db.ProductCategories, "ProductCategoryID", "CategoryName", product.ProductCategoryID);
            ViewBag.ProductUnitOfMeasurementID = new SelectList(db.ProductUnitOfMeasurements, "ProductUnitOfMeasurementID", "UnitName", product.ProductUnitOfMeasurementID);
            return(View(product));
        }
        public async Task <ActionResult> Create([Bind(Include = "ProductID,ProductName,Description,ProductStatus,ProductCategoryID,ProductPrice,ProductUnitOfMeasurementID,UnitsInStock")] Product product)
        {
            if (ModelState.IsValid)
            {
                if (product.ProductStatus < (ProductStatus)2)
                {
                    product.ProductStatus = (product.UnitsInStock > 0) ? (ProductStatus)0 : (ProductStatus)1;
                }
                db.Products.Add(product);

                await db.SaveChangesAsync();

                ProductsHub.BroadcastData();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductCategoryID          = new SelectList(db.ProductCategories, "ProductCategoryID", "CategoryName", product.ProductCategoryID);
            ViewBag.ProductUnitOfMeasurementID = new SelectList(db.ProductUnitOfMeasurements, "ProductUnitOfMeasurementID", "UnitName", product.ProductUnitOfMeasurementID);
            return(View(product));
        }
        private bool UpdateProducts(Order activeOrder)
        {
            // start by getting all productInOrder data
            var productInOrders = db.ProductInOrders.Where(x => x.OrderID == activeOrder.OrderID).Include(x => x.Product);


            // check if all products are available in the quantities needed
            bool allProductsAvailable = true;

            foreach (var item in productInOrders)
            {
                if (item.Quantity > item.Product.UnitsInStock)
                {
                    allProductsAvailable = false;
                }
            }

            if (!allProductsAvailable)
            {
                return(false);   //if a single product isn't available we go back to the action with a false
            }
            // Now i want to update all products with new quantities

            foreach (var item in productInOrders)
            {
                Product product = item.Product;
                product.UnitsInStock -= item.Quantity;
                if (product.UnitsInStock == 0)
                {
                    product.ProductStatus = (ProductStatus)1;
                }
            }

            db.SaveChanges();
            ProductsHub.BroadcastData();

            return(true);
        }