public async Task <IActionResult> Create([Bind("VendorId,Name,Phone,Email,PointOfContact")] Vendor vendor)
        {
            if (ModelState.IsValid)
            {
                _context.Add(vendor);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(vendor));
        }
        public async Task <IActionResult> Create([Bind("WildeRoverItemId,Name,Par,Have,Type, SubType")] WildeRoverItem wildeRoverItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(wildeRoverItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(wildeRoverItem));
        }
        public async Task <IActionResult> Create(VendorItem model)
        {
            if (ModelState.IsValid)
            {
                if (model.WildeRoverItemId == 0)
                {
                    ModelState.AddModelError("WildeRoverItemId", "You must select a house item.");
                    return(View(model));
                }

                if (model.VendorId == 0)
                {
                    ModelState.AddModelError("VendorId", "You must select a vendor.");
                    return(View(model));
                }

                _context.VendorItem.Add(model);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "VendorItems"));
            }

            return(View(model));
        }
        public async Task <IActionResult> Submit(int?id, OrderSubmitViewModel osvm)
        {
            if (id == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //get OrderSummary
                    var order = await(from log in _context.OrderLog.Include("OrderList.Item.VendorItems.Vendor")
                                      where log.OrderSummaryId == id
                                      select log).SingleOrDefaultAsync();

                    if (order == null)
                    {
                        return(NotFound());                //Validate
                    }
                    //====EMAIL VENDORS HERE===================================



                    //=========================================================

                    //Update OrderSummary
                    order.Completed = true;
                    order.Total     = osvm.Total;

                    _context.OrderLog.Update(order);   //update context

                    await _context.SaveChangesAsync(); //save context

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
        public async Task <IActionResult> EditSave(InventoryAreaEditViewModel model)
        {
            if (model == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Get area from context
                    var area = await(from a in _context.InventoryAreas.Include("ItemSlots.WildeRoverItem")
                                     where a.InventoryAreaId == model.InventoryAreaId
                                     select a).SingleOrDefaultAsync();

                    if (area == null)
                    {
                        return(NotFound());               //Validate
                    }
                    //Create dictionary of ItemSlots for O(1) updates
                    var slotDict = area.ItemSlots.ToDictionary(t => t.InventorySlotId);

                    //Loop through model.ItemSlots and update context
                    int i = 0;
                    while (i < model.SlotDisplayCount || i < area.ItemSlots.Count)
                    {
                        if (i < area.ItemSlots.Count && i < model.SlotDisplayCount)  //Update
                        {
                            var temp = slotDict[model.SlotList[i].InventorySlotId];
                            if (model.SlotList[i].WildeRoverItemId == 0)
                            {
                                temp.WildeRoverItemId = null;
                            }
                            else
                            {
                                temp.WildeRoverItemId = model.SlotList[i].WildeRoverItemId;
                            }

                            _context.Slots.Update(temp);
                        }
                        else if (i < model.SlotDisplayCount && i >= area.ItemSlots.Count)  //Add
                        {
                            InventorySlot newSlot = new InventorySlot();
                            newSlot.InventoryAreaId = area.InventoryAreaId;
                            newSlot.Slot            = i + 1;
                            if (model.SlotList[i].WildeRoverItemId != 0)
                            {
                                newSlot.WildeRoverItemId = model.SlotList[i].WildeRoverItemId;
                            }

                            _context.Slots.Add(newSlot);
                        }
                        else if (i >= model.SlotDisplayCount && i < area.ItemSlots.Count)  //Remove
                        {
                            var toRemove = area.ItemSlots[i];
                            area.ItemSlots.RemoveAt(i);

                            _context.Slots.Remove(toRemove);
                        }

                        i++;
                    }

                    await _context.SaveChangesAsync();  //Save context

                    return(RedirectToAction("Details", new { id = model.InventoryAreaId }));
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> Submit(int?id, InventorySummarySubmitViewModel isvm)
        {
            //Validate
            if (id == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //save itemCounts

                    //Get InventorySummary
                    var summary = await(from log in _context.InventoryLog.Include("InventoryAreaLogs.Inventory.Item")
                                        where log.InventorySummaryId == isvm.InventorySummaryId
                                        select log).SingleOrDefaultAsync();

                    //Create Dictionary of Items for O(n) tallying
                    var items = await(from i in _context.WildeRoverItem
                                      select i).ToDictionaryAsync(t => t.WildeRoverItemId);

                    //Reset item Have values
                    foreach (var item in items)
                    {
                        item.Value.Have = 0;
                    }

                    //Save Inventory counts to Item Have
                    foreach (var log in summary.InventoryAreaLogs)
                    {
                        foreach (var item in log.Inventory)
                        {
                            WildeRoverItem temp = items[item.WildeRoverItemId];
                            temp.Have += item.Count;

                            _context.WildeRoverItem.Update(temp);  //Update context
                        }
                    }

                    //Change LastEdited
                    var user = await _userManager.GetUserAsync(User);

                    if (user == null)
                    {
                        throw new InvalidOperationException();
                    }

                    summary.LastEdited = user.FullName;

                    _context.InventoryLog.Update(summary);

                    //====EMAIL PARTIES HERE===================================



                    //=========================================================

                    //Update InventorySummary status
                    summary.Submitted = true;
                    _context.InventoryLog.Update(summary);

                    await _context.SaveChangesAsync();  //Save context
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }

                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Save(OrderViewModel ovm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Create dictionary of orderlist for O(1) access
                    var itemCountDict = _order.OrderList.ToDictionary(t => t.WildeRoverItemId);

                    foreach (var itemCount in ovm.OrderList)
                    {
                        //Find existing itemCount from _order

                        ItemCount temp;
                        if (itemCountDict.TryGetValue(itemCount.WildeRoverItemId, out temp)) //ItemCount found
                        {
                            if (itemCount.Count > 0)                                         //something is ordered
                            {
                                //Update ItemCount in context
                                temp.Count = itemCount.Count;
                                _context.ItemCounts.Update(temp);
                            }
                            else  //Item found but now its count is 0
                            {
                                //Remove ItemCount from context
                                _order.OrderList.Remove(temp);
                                _context.ItemCounts.Remove(temp);
                            }
                        }
                        else  //ItemCount does not exist in context
                        {
                            if (itemCount.Count > 0)
                            {
                                //Create ItemCount
                                temp = new ItemCount();
                                temp.OrderSummary     = _order;
                                temp.OrderSummaryId   = _order.OrderSummaryId;
                                temp.WildeRoverItemId = itemCount.WildeRoverItemId;
                                temp.Count            = itemCount.Count;

                                //Add to context
                                _context.ItemCounts.Add(temp);

                                //Add to OrderSummary
                                _order.OrderList.Add(temp);
                            }
                        }
                    }

                    //Update Last Edited
                    var user = await _userManager.GetUserAsync(User);

                    if (user == null)
                    {
                        throw new InvalidOperationException();
                    }

                    _order.LastEdited = user.FullName;

                    //Update context
                    _context.OrderLog.Update(_order);
                    await _context.SaveChangesAsync();  //save context
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }

                return(RedirectToAction("Index", "OrderSummary"));
            }

            return(View(ovm));
        }
Esempio n. 8
0
        public async Task <IActionResult> FrontHouseInventorySave(FrontHouseInventoryViewModel fhivm)
        {
            if (fhivm == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Update ItemCounts
                    foreach (var ic in fhivm.Inventory)
                    {
                        //get corresponding itemCount from context
                        var temp = await(from i in _context.ItemCounts
                                         where i.ItemCountId == ic.ItemCountId
                                         select i).SingleOrDefaultAsync();

                        if (temp == null)
                        {
                            continue;
                        }

                        temp.Count = ic.Count;            //update count

                        _context.ItemCounts.Update(temp); //update context
                    }

                    //Update Save Time

                    //get area log from context
                    var areaLog = await(from a in _context.InventoryAreaLogs
                                        where a.InventoryAreaInventoryLogId == fhivm.Log.InventoryAreaid
                                        select a).SingleOrDefaultAsync();

                    areaLog.Date = DateTime.Now;
                    _context.InventoryAreaLogs.Update(areaLog);

                    //Update Inventory Summary last edited user
                    var inventorySummary = await(from log in _context.InventoryLog
                                                 where log.InventorySummaryId == areaLog.InventorySummaryId
                                                 select log).SingleOrDefaultAsync();

                    var user = await _userManager.GetUserAsync(User);

                    inventorySummary.LastEdited = user.FullName;
                    _context.InventoryLog.Update(inventorySummary);

                    //Save context
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }

                return(RedirectToAction("FrontHouseIndex"));
                //return RedirectToAction("FrontHouseInventory", new { id = fhivm.Log.InventoryAreaid, loadFromContext = true });
            }

            return(RedirectToAction("FrontHouseInventory", new { id = fhivm.Log.InventoryAreaid, loadFromContext = true }));
        }