public JsonResult ActivityList()
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                List<ActivityModel> list = new List<ActivityModel>();

                foreach (var a in dm.Activities)//.Where(z => z.Active == true))
                {
                    ActivityModel atemp = new ActivityModel();
                    atemp.Id = a.Id;
                    atemp.Description = a.Description;
                    atemp.Points = a.Points;
                    atemp.Active = a.Active;
                    atemp.UnitId = a.Unit.Id;
                    atemp.Quantity = a.Quantity;
                    list.Add(atemp);
                }
                return Json(new { Result = "OK", Records = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateActivity(ActivityModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                Activity newActivity = new Activity();
                newActivity.Points = model.Points;
                newActivity.Description = model.Description;
                newActivity.Unit = dm.Units.Single(z => z.Id == model.UnitId);
                newActivity.Quantity = model.Quantity;
                dm.AddToActivities(newActivity);
                dm.SaveChanges();

                model.Id = newActivity.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateActivityLog(ActivityLogModel model)
        {
            var dm = new JourListDMContainer();
            var log = new ActivityLog();
            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);
            if (member == null)
                return JsonError("You don't exist");

            var journal = member.Journals.Single(z => z.Id == model.JourId);
            if (journal == null)
                return JsonError("This journal doesn't belong to you");

            log.Activity = dm.Activities.Single(z => z.Id == model.ActId);
            log.Quantity = model.Quantity;
            // TODO: Hyperlink & Notes should be updated to default to an
            //       empty string in the databaseif the value are null,
            //       the EM has been updated, but the database needs to
            //       be updated to reflect the default value of "".
            log.Notes = model.Notes;
            log.Hyperlink = model.Hyperlink;
            log.Unit = dm.Units.Single(z => z.Id == model.UnitId);
            log.Journal = journal;
            dm.ActivityLogs.AddObject(log);
            dm.SaveChanges();
            //            model.Points = log.Activity.Points;
            //            model.TotalPoints = (int)Math.Round(log.Activity.Points * log.Quantity);
            model.LogId = log.Id;
            //            return Json(model);
            return Json(new { Result = "OK", Record = model });
            //            return JsonError("Not Done");
        }
 public ActivityLogModel(long LogId, string memberName)
 {
     JourListDMContainer dm = new JourListDMContainer();
     var mem = dm.Members.SingleOrDefault(z => z.Name == memberName);
     var log = dm.ActivityLogs.Single(z => z.Id == LogId);
     if (log.Journal.Member.Name != memberName)
         throw new Exception("Log does not belong to user");
     set(log, log.Activity);
 }
 public JsonResult ItemCategories()
 {
     try
     {
         JourListDMContainer dm = new JourListDMContainer();
         var list = dm.ItemCategories.Select(z => new { DisplayText = z.Description, Value = z.Id });
         return Json(new { Result = "OK", Options = list });
     }
     catch (Exception e)
     {
         return Json(new { Result = "ERROR", Message = e.Message });
     }
 }
        public JsonResult DeleteActivityLog(long LogId)
        {
            JourListDMContainer dm = new JourListDMContainer();

            if (User.Identity.IsAuthenticated != true)
                return JsonError("Please log in");

            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);
            if (member == null)
                return JsonError("You do not exist");

            var log = dm.ActivityLogs.SingleOrDefault(z => z.Id == LogId);
            if (log == null)
                return JsonError("That log does not exist");

            if (log.Journal.Member != member)
                return JsonError("This log does not belong to you");

            dm.ActivityLogs.DeleteObject(log);

            dm.SaveChanges();

            return JsonOk("Deleted");
        }
        public JsonResult InventoryItems()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You need to log in to see your items."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                var list = dm.Members.SingleOrDefault(z=>z.Name == User.Identity.Name)
                                     .Inventories.Select(z => new { DisplayText = z.Item.Description, Value = z.Item.Id })
                                                 .OrderByDescending(z => z.DisplayText);
                return Json(new { Result = "OK", Options = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
 public JsonResult StandardItems()
 {
     try
     {
         JourListDMContainer dm = new JourListDMContainer();
         var list = dm.Items.OfType<PersonalItem>().Select(z => new { DisplayText = z.Description, Value = z.Id })
                            .OrderByDescending(z => z.DisplayText);
         return Json(new { Result = "OK", Options = list });
     }
     catch (Exception e)
     {
         return Json(new { Result = "ERROR", Message = e.Message });
     }
 }
        public JsonResult UpdateItemCategory(ItemCategoryModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                          "Please correct it and try again."
                    });
                }

                if (!User.IsInRole("Officer"))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "You do not have the authority to update this item category."
                    });
                }

                JourListDMContainer dm = new JourListDMContainer();

                ItemCategory item = dm.ItemCategories.Single(z => z.Id == model.Id);
                item.Description = model.Description;
                item.Active = model.Active;
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult UpdateItem(ItemModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                          "Please correct it and try again."
                    });
                }

                if (!User.Identity.IsAuthenticated)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "You need to log in to add update an item."
                    });
                }

                if (!User.IsInRole("Officer"))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "You do not have the authority to create a new inventory item."
                    });
                }

                JourListDMContainer dm = new JourListDMContainer();

                Item item = dm.Items.OfType<StandardItem>().Single(z => z.Id == model.Id);

                if (item == null)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "The item was not modified since it does not exist."
                    });
                }

                item.Description = model.Description;
                //if (model.Hyperlink != null) item.Hyperlink = model.Hyperlink;
                //if (model.Barcode != null) item.Barcode = model.Barcode;
                item.ItemCategory = dm.ItemCategories.Single(z => z.Id == model.CategoryId);
                item.UnitType = dm.UnitTypes.Single(z => z.Id == model.UnitTypeId);
                item.Active = model.Active;

                dm.SaveChanges(); dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateInventoryAction(InventoryActionModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to create a new inventory action."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                InventoryAction item = new InventoryAction();
                item.Description = model.Description;
                dm.AddToInventoryActions(item);
                dm.SaveChanges();

                model.Id = item.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        // POST: /DataManagement/UnitTypeList
        public JsonResult UnitTypeList()
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                List<UnitTypeModel> list = new List<UnitTypeModel>();

                foreach (var a in dm.UnitTypes.Where(z => z.Active == true))
                {
                    UnitTypeModel item = new UnitTypeModel();
                    item.Id = a.Id;
                    item.Description = a.Description;
                    item.Active = a.Active;
                    list.Add(item);
                }

                return Json(new { Result = "OK", Records = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        // POST: /DataManagement/RecurrenceIntervalList
        public JsonResult RecurrenceIntervalList()
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                List<RecurrenceIntervalModel> list = new List<RecurrenceIntervalModel>();

                foreach (var a in dm.RecurrenceIntervals.Where(z => z.Active == true))
                {
                    RecurrenceIntervalModel item = new RecurrenceIntervalModel();
                    item.Id = a.Id;
                    item.Description = a.Description;
                    item.Minutes = a.Minutes;
                    item.Active = a.Active;
                    list.Add(item);
                }

                return Json(new { Result = "OK", Records = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateUnitType(UnitTypeModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to create a new unit type."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                UnitType newUnitType = new UnitType();
                newUnitType.Description = model.Description;
                dm.AddToUnitTypes(newUnitType);
                dm.SaveChanges();

                model.Id = newUnitType.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult PopularActivities(string Day, short MaxResults = 20)
        {
            JourListDMContainer dm = new JourListDMContainer();
            List<ActivityModel> list = new List<ActivityModel>();
            List<ActivityLog> omit = new List<ActivityLog>(0);

            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);
            if (User.Identity.IsAuthenticated == false || member == null)
                return JsonError("You do not exist.  No activities for you!");

            var date = Day == null ? DateTime.Today : DateTime.Parse(Day);

            // Get a journal entry for this day if it exists
            var jour = member.Journals.SingleOrDefault(z => z.EntryDate == date);

            // Get the exisitng activity logs if the journal did exist
            if (jour != null)
                omit = jour.ActivityLogs.ToList();

            // Get the member's most popular activities
            var logs = from al in dm.ActivityLogs
                       where al.Journal.Member.Name == member.Name
                       group al by al.Activity.Id into j
                       orderby j.GroupBy(z => z.Activity.Id).Count() descending
                       select j;

            // If they haven't completed enough activities, get common popular activities
            if (logs.Count() - omit.Count() < MaxResults)
                logs.Concat(from al in dm.ActivityLogs
                            group al by al.Activity.Id into j
                            orderby j.GroupBy(z => z.Activity.Id).Count() descending
                            select j);

            // Add everything to the list
            foreach (var group in logs)
            {
                var act = group.First().Activity;
                if (omit.Count(z => z.Activity.Id == act.Id) == 0)
                    list.Add(new ActivityModel(act));
                if (list.Count >= MaxResults)
                    break;
            }

            // Fill up any empty space with random activities
            foreach (var act in dm.Activities)
            {
                if (omit.Count(z => z.Activity.Id == act.Id) == 0)
                    list.Add(new ActivityModel(act));
                if (list.Count >= MaxResults)
                    break;
            }

            // Return
            return JsonOk(list.Distinct(new Tools.Tools.KeyEqualityComparer<ActivityModel>(z => z.Id)));
        }
        public JsonResult Save(JournalModel model)
        {
            if (User.Identity.IsAuthenticated == false)
                return JsonError("You are not authenticated");
            JourListDMContainer dm = new JourListDMContainer();

            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);
            if(member == null)
                return JsonError("You are not registered in the system");

            var journal = member.Journals.SingleOrDefault(z => z.Id == model.JourId);
            if (journal == null)
                return JsonError("You can't save a journal that doesn't exist. Or at least, technically you could by creating a new one... but that should have already been done.  If it hasn't already been done then you're working outside the system.");

            journal.Weight = model.Weight;
            journal.HeartRate = model.HeartRate;
            journal.Story = model.Story;
            journal.Encrypted = model.Encrypted;

            dm.SaveChanges();
            model.JourId = journal.Id;
            return JsonOk(model);
        }
        public JsonResult DeleteTypeUnit(int id)
        {
            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to delete this unit type."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                // Grab the unit object
                UnitType t = dm.UnitTypes.Single(z => z.Id == id);

                // Delete the object only if there are no references
                if (t.Units.Count < 1 )
                    dm.UnitTypes.DeleteObject(t);

                // Otherwise just deactivate so we don't break any historical records
                else
                    t.Active = false;

                // Save the changes
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult DeleteRecurrenceInterval(int id)
        {
            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority delete this interval."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                // Grab the item
                RecurrenceInterval item = dm.RecurrenceIntervals.Single(z => z.Id == id);

                // Delete the object only if there are no references
                if (item.Goals.Count < 1)
                    dm.RecurrenceIntervals.DeleteObject(item);

                // Otherwise just deactivate so we don't break any historical records
                else
                    item.Active = false;

                // Save the changes
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public TransactionModel(int actionId = -1, string user = null, long invid = -1, bool allunits=false)
        {
            JourListDMContainer dm = new JourListDMContainer();
            TransactionModel model = this;
            Inventory inv = null;
            IEnumerable<Unit> units = dm.Units;
            // Verify the member
            if (user != null)
            {
                Member member = dm.Members.SingleOrDefault(z => z.Name == user);
                if (member == null) throw new Exception("User not authenticated");

                // Get the item if the jourId is specified
                inv = member.Inventories.FirstOrDefault(z => z.Id == invid);

                if (inv == null) inv = member.Inventories.FirstOrDefault(z => z.ShoppingList != null);

                // If that didn't work just grab the first item from the inventory
                if (inv == null) inv = member.Inventories.FirstOrDefault();

                // If that didn't work then they get nothing
                if (inv != null)
                {
                    model.InvId = inv.Id;
                    model.Description = inv.Item.Description;
                    model.UnitId = inv.Unit.Id;

                    InventoryLog log;
                    if ((log = inv.InventoryLogs.FirstOrDefault()) != null)
                        model.Cost = log.Cost;
                    else model.Cost = 0;

                    if(allunits == false)
                        units = inv.Unit.UnitType.Units;

                    if (inv.ShoppingList != null)
                    {
                        model.Quantity = inv.ShoppingList.QuantityNeeded;
                        model.Size = inv.ShoppingList.SizeNeeded;
                    }
                    else
                    {
                        model.Quantity = 0;
                        model.Size = 0;
                    }
                }

            }
            if(inv == null)
            {
                model.InvId = -1;
                model.Description = "";
                model.Quantity = 0;
                model.Size = 0;
                model.sUnitId = "";
                model.Cost = 0;
            }

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in units)
            {
                SelectListItem li = new SelectListItem();
                li.Text = item.Description;
                li.Value = item.Id.ToString();
                if (inv != null && item.Id == inv.Unit.Id)
                    li.Selected = true;
                list.Add(li);
            }

            model.UnitOptions = new SelectList(list, "Value", "Text", model.sUnitId);

            if (actionId != -1)
                model.ActionId = actionId;
        }
        public JsonResult DeleteItem(int id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You need to log in to delete an item."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to delete an item."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                // Grab the item
                Item item = dm.Items.OfType<StandardItem>().Single(z => z.Id == id);

                if (item == null)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "The item for deletion does not exist!"
                    });
                }

                // Delete the object only if there are no references
                if (item.Inventories.Count < 1)
                    dm.Items.DeleteObject(item);

                // Otherwise just deactivate so we don't break any historical records
                else
                    item.Active = false;

                // Save the changes
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult DeleteActivity(int id)
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                // Grab the goal
                Activity a = dm.Activities.Single(z => z.Id == id);

                // Delete the object only if there are no references
                if (a.ActivityLogs.Count < 1)
                    dm.Activities.DeleteObject(a);

                // Otherwise just deactivate so we don't break any historical records
                else
                    a.Active = false;

                // Save the changes
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateItem(ItemModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            if (!User.Identity.IsAuthenticated)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You need to log in to add an item."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to create a new inventory item."
                });
            }
            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                StandardItem item = new StandardItem();

                item.Description = model.Description;
                //if (model.Hyperlink != null) item.Hyperlink = model.Hyperlink;
                //if (model.Barcode != null) item.Barcode = model.Barcode;
                item.ItemCategory = dm.ItemCategories.Single(z => z.Id == model.CategoryId);
                item.UnitType = dm.UnitTypes.Single(z => z.Id == model.UnitTypeId);

                dm.AddToItems(item);
                dm.SaveChanges();

                model.Id = item.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public ActivityLogModel(long ActId)
        {
            JourListDMContainer dm = new JourListDMContainer();
            var act = dm.Activities.Single(z => z.Id == ActId);

            set(null, act);

            // TODO: Complete member initialization
            this.Quantity = 0;
            this.unitid = act.Unit.Id;
        }
        public JsonResult CreateUnit(UnitModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to create a new unit."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                Unit unit = new Unit();

                unit.UnitType = dm.UnitTypes.Single(z => z.Id == model.UnitTypeId);
                unit.Description = model.Description;
                unit.ConversionFactor = model.ConversionFactor;
                unit.Abbreviation = model.Abbreviation;

                if (model.ConversionFactor == 1)
                {
                    if (unit.UnitType.DefaultUnit != null && unit.UnitType.DefaultUnit.Active)
                        return Json(new
                        {
                            Result = "ERROR",
                            Message = "A default unit with conversion factor of 1 already exists."
                        });

                    dm.AddToUnits(unit);
                    dm.SaveChanges();

                    unit.UnitType.DefaultUnit = unit;
                }
                else
                {
                    dm.AddToUnits(unit);
                }

                dm.SaveChanges();

                model.Id = unit.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult DeleteUnit(int id)
        {
            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to delete this unit."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                // Grab the goal
                Unit u = dm.Units.Single(z => z.Id == id);

                if (u.UnitType.DefaultUnit == u)
                {
                    u.UnitType.DefaultUnit = null;
                }

                // Delete the object only if there are no references
                if (u.ActivityLogs.Count < 1 &&  u.UnitType.DefaultUnit != u && u.Inventories.Count < 1)
                    dm.Units.DeleteObject(u);

                // Otherwise just deactivate so we don't break any historical records
                else
                    u.Active = false;

                // Save the changes
                dm.SaveChanges();

                return Json(new { Result = "OK" });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult CreateReccurenceInterval(RecurrenceIntervalModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "Form is not valid! " +
                    "Please correct it and try again."
                });
            }

            if (!User.IsInRole("Officer"))
            {
                return Json(new
                {
                    Result = "ERROR",
                    Message = "You do not have the authority to create a new interval."
                });
            }

            try
            {
                JourListDMContainer dm = new JourListDMContainer();

                RecurrenceInterval item = new RecurrenceInterval();
                item.Description = model.Description;
                item.Minutes = model.Minutes;
                dm.AddToRecurrenceIntervals(item);
                dm.SaveChanges();

                model.Id = item.Id;

                return Json(new { Result = "OK", Record = model });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult UpdateActivityLog(ActivityLogModel model)
        {
            JourListDMContainer dm = new JourListDMContainer();
            if (User.Identity.IsAuthenticated != true)
                return JsonError("Please log in");

            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);
            if ( member == null)
                return JsonError("You do not exist");

            var log = dm.ActivityLogs.SingleOrDefault(z => z.Id == model.LogId);
            if ( log == null )
                return JsonError("That log does not exist");

            if (log.Journal.Member != member)
                return JsonError("This log does not belong to you");

            var unit = dm.Units.SingleOrDefault(z => z.Id == model.UnitId);
            if (unit == null)
                return JsonError("A unit by that ID does not exist");
            if (unit.UnitType != log.Activity.Unit.UnitType)
                return JsonError("Your unit is not the same type as the activity's");

            log.Unit = unit;
            log.Hyperlink = model.Hyperlink;
            log.Notes = model.Notes;
            log.Quantity = model.Quantity;

            dm.SaveChanges();

            return JsonOk(model);
        }
        // POST: /DataManagement/InventoryActionList
        public JsonResult InventoryActionList()
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                List<InventoryActionModel> list = new List<InventoryActionModel>();

                foreach (var a in dm.InventoryActions)
                {
                    InventoryActionModel item = new InventoryActionModel();
                    item.Id = a.Id;
                    item.Description = a.Description;
                    list.Add(item);
                }

                return Json(new { Result = "OK", Records = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }
        public JsonResult GetActivityLogs(string Day)
        {
            if (User.Identity.IsAuthenticated == false)
                return JsonError("You are not authenticated");
            DateTime date;
            if (Day == null)
                date = DateTime.Today;
            else
                date = DateTime.Parse(Day);

            var dm = new JourListDMContainer();
            var list = new List<ActivityLogModel>();
            var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name);

            if (member == null)
                return JsonError("You don't exist");

            var journal = member.Journals.SingleOrDefault(z => z.EntryDate == date);

            if (journal == null)
                return JsonOk("");

            foreach (var log in journal.ActivityLogs)
                list.Add(new ActivityLogModel(log.Id, User.Identity.Name));

            return JsonOk(list);
        }
        // POST: /DataManagement/ItemList
        public JsonResult ItemList(FormCollection collection)
        {
            try
            {
                JourListDMContainer dm = new JourListDMContainer();
                List<ItemModel> list = new List<ItemModel>();

                foreach (var a in dm.Items.OfType<StandardItem>().Where(z => z.Active == true))
                {
                    ItemModel item = new ItemModel();
                    item.Id = a.Id;
                    item.Description = a.Description;
                    //item.Hyperlink = a.Hyperlink;
                    //item.Barcode = a.Barcode;
                    item.CategoryId = a.ItemCategory.Id;
                    item.UnitTypeId = a.UnitType.Id;
                    item.Active = a.Active;
                    list.Add(item);
                }

                return Json(new { Result = "OK", Records = list });
            }
            catch (Exception e)
            {
                return Json(new { Result = "ERROR", Message = e.Message });
            }
        }