public async Task <IActionResult> Edit(int id, int barcodeinput, int courseinput, float qtyusedinput)
        {
            if (_context.ChemInventory.Count(M => M.ChemInventoryId == barcodeinput) >= 1)
            {
                ChemLog chemLog = await _context.ChemLog.FirstAsync(m => m.ChemLogId == id);

                ChemInventory temp      = _context.ChemInventory.FirstOrDefault(s => s.ChemInventoryId == barcodeinput);
                float         tempValue = temp.QtyLeft;
                float         used      = chemLog.QtyUsed;
                temp.QtyLeft = tempValue + used - qtyusedinput;
                _context.Entry <ChemInventory>(temp).State = EntityState.Modified;
                _context.SaveChanges();
                chemLog.ChemInventoryId = barcodeinput;
                chemLog.CourseID        = courseinput;
                chemLog.QtyUsed         = qtyusedinput;

                if (id != chemLog.ChemLogId)
                {
                    return(NotFound());
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(chemLog);
                        await _context.SaveChangesAsync();

                        sp_Logging("2-Change", "Edit", "User edited a Log entry where ID= " + id.ToString(), "Success");
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ChemLogExists(chemLog.ChemLogId))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction("Index"));
                }
                ViewData["Barcode"]  = new SelectList(_context.ChemInventory, "ChemInventoryId", "ChemInventoryId", chemLog.ChemInventoryId);
                ViewData["CourseID"] = new SelectList(_context.Course, "CourseID", "NormalizedStr", chemLog.CourseID);
                return(View(chemLog));
            }
            else
            {
                return(View("CheckBarcode"));
            }
        }
        public async Task <IActionResult> Edit(int id, int?formulainput, DateTime dateinput, int?storageinput, int?orderinput, string cat, string lot, float qtyinput, string unitstring, string deptstring)
        {
            ChemInventory chemInventory = await _context.ChemInventory.SingleOrDefaultAsync(p => p.ChemInventoryId == id);

            if (id != chemInventory.ChemInventoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    chemInventory.ChemID     = formulainput;
                    chemInventory.LocationID = storageinput;
                    chemInventory.ExpiryDate = dateinput;
                    chemInventory.OrderID    = orderinput;
                    chemInventory.QtyLeft    = qtyinput;
                    chemInventory.Units      = unitstring;
                    chemInventory.Department = deptstring;
                    chemInventory.CAT        = cat;
                    chemInventory.LOT        = lot;
                    var temp = _context.Locations.First(m => m.LocationID == storageinput);
                    chemInventory.NormalizedLocation = temp.StorageCode;
                    _context.Update(chemInventory);
                    await _context.SaveChangesAsync();

                    sp_Logging("2-Change", "Edit", "User edited a Chemical inventory item where ID= " + id.ToString(), "Success");
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ChemInventoryExists(chemInventory.ChemInventoryId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            ViewData["ChemID"]       = new SelectList(_context.Chemical, "ChemID", "FormulaName", chemInventory.ChemID);
            ViewData["LocationName"] = new SelectList(_context.Locations, "LocationID", "StorageCode", chemInventory.LocationID);
            ViewData["OrderID"]      = new SelectList(_context.Orders, "OrderID", "OrderID", chemInventory.OrderID);
            return(View(chemInventory));
        }
        public async Task <IActionResult> Create(int?formulainput, DateTime dateinput, int?storageinput, int?orderinput, string cat, string lot, float qtyinput, string unitstring, string deptstring)
        {
            ViewData["Formula"]     = formulainput;
            ViewData["ExpiryDate"]  = dateinput;
            ViewData["StorageCode"] = storageinput;
            ViewData["Order"]       = orderinput;
            ViewData["Qty"]         = qtyinput;
            ViewData["Unit"]        = unitstring;
            ViewData["Department"]  = deptstring;
            ViewData["CAT"]         = cat;
            ViewData["LOT"]         = lot;

            ChemInventory chemInventory = null;

            if (ModelState.IsValid)
            {
                //var chemID = _context.Chemical.Where(p => p.Formula == FormulaString).Select(p => p.ChemID);
                //var Chem = _context.Chemical.Where(p => p.Formula == FormulaString);
                //chemInventory.ChemID = await chemID;
                chemInventory            = new ChemInventory();
                chemInventory.ChemID     = formulainput;
                chemInventory.LocationID = storageinput;
                chemInventory.ExpiryDate = dateinput;
                chemInventory.OrderID    = orderinput;
                chemInventory.QtyLeft    = qtyinput;
                chemInventory.Units      = unitstring;
                chemInventory.Department = deptstring;
                chemInventory.CAT        = cat;
                chemInventory.LOT        = lot;
                var temp = _context.Locations.First(m => m.LocationID == storageinput);
                chemInventory.NormalizedLocation = temp.StorageCode;

                _context.Add(chemInventory);
                await _context.SaveChangesAsync();

                sp_Logging("2-Change", "Create", "User created a chemical inventory item where ChemID=" + formulainput + ", OrderID=" + formulainput, "Success");
                return(RedirectToAction("Index"));
            }
            ViewData["ChemID"]       = new SelectList(_context.Chemical, "ChemID", "FormulaName", chemInventory.ChemID);
            ViewData["LocationName"] = new SelectList(_context.Locations, "LocationID", "StorageCode", chemInventory.LocationID);
            ViewData["OrderID"]      = new SelectList(_context.Orders, "OrderID", "OrderID", chemInventory.OrderID);
            return(View(chemInventory));
        }
        public async Task <IActionResult> Edit(int id, int?formulainput, DateTime dateinput, int?storageinput, int?orderinput, string cat, string lot, float qtyinput, string unitstring, string deptstring)
        {
            ChemInventory chemInventory = await _context.ChemInventory.SingleOrDefaultAsync(p => p.ChemInventoryId == id);

            if (id != chemInventory.ChemInventoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                // if the amount of the chemical is changed to zero units,
                //   this places it in the archives table, and deletes it from
                //   the inventory table.
                //   possibly also sends you to the archives table. not sure yet.
                if (qtyinput == 0)
                {
                    ChemInventoryArc chemInventoryArc = null;

                    try
                    {
                        chemInventoryArc            = new ChemInventoryArc();
                        chemInventoryArc.ChemID     = formulainput;
                        chemInventoryArc.LocationID = storageinput;
                        chemInventoryArc.ExpiryDate = dateinput;
                        chemInventoryArc.OrderID    = orderinput;
                        chemInventoryArc.QtyLeft    = qtyinput;
                        chemInventoryArc.Units      = unitstring;
                        chemInventoryArc.Department = deptstring;
                        chemInventoryArc.CAT        = cat;
                        chemInventoryArc.LOT        = lot;
                        var temp = _context.Locations.First(m => m.LocationID == storageinput);
                        chemInventoryArc.NormalizedLocation = temp.StorageCode;
                        _context.Add(chemInventoryArc);
                        await _context.SaveChangesAsync();

                        // This section would change the barcode/PK of the item, inside the archive table,
                        // to match what it was in the inventory table if that was possible.
                        // The only option I see is to replace the table with one where the barcode is an
                        // additional column, or attribute, in the table, and change it using the line
                        // directly below this comment, but placed up above with the other attributes.
                        // ** there may be a method of altering a table's attributes **
                        // Doing this change would requre code changes throughout the files affiliated with chemicals

                        /*                       chemInventoryArc.ChemInventoryIdArc = chemInventory.ChemInventoryId;
                         *                     temp = _context.Locations.First(m => m.LocationID == storageinput);
                         *                     chemInventoryArc.NormalizedLocation = temp.StorageCode;
                         *                     _context.Update(chemInventoryArc);
                         *                     await _context.SaveChangesAsync();
                         */
                        //Sp_Logging("4-Archive", "Edit", "User archived a Chemical inventory item where ID= " + id.ToString(), "Success");

                        await DeleteConfirmed(id);
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ChemInventoryExists(chemInventoryArc.ChemInventoryIdArc))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }

                    /// maybe call DeleteConfirmed(id) to delete the chemical from the inventory
                }
                else
                {
                    try
                    {
                        chemInventory.ChemID     = formulainput;
                        chemInventory.LocationID = storageinput;
                        chemInventory.ExpiryDate = dateinput;
                        chemInventory.OrderID    = orderinput;
                        chemInventory.QtyLeft    = qtyinput;
                        chemInventory.Units      = unitstring;
                        chemInventory.Department = deptstring;
                        chemInventory.CAT        = cat;
                        chemInventory.LOT        = lot;
                        var temp = _context.Locations.First(m => m.LocationID == storageinput);
                        chemInventory.NormalizedLocation = temp.StorageCode;
                        _context.Update(chemInventory);
                        await _context.SaveChangesAsync();

                        Sp_Logging("2-Change", "Edit", "User edited a Chemical inventory item where ID= " + id.ToString(), "Success");
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ChemInventoryExists(chemInventory.ChemInventoryId))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                return(RedirectToAction("Index"));
            }
            ViewData["ChemID"]       = new SelectList(_context.Chemical, "ChemID", "FormulaName", chemInventory.ChemID);
            ViewData["LocationName"] = new SelectList(_context.Locations, "LocationID", "StorageCode", chemInventory.LocationID);
            ViewData["OrderID"]      = new SelectList(_context.Orders, "OrderID", "OrderID", chemInventory.OrderID);
            return(View(chemInventory));
        }