public ActionResult AddDetail(Models.LedgerAddDetailModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theDetail = new LedgerDetail();
         theDetail.Amount = model.Amount;
         theDetail.Legder_LedgerId = model.LedgerId;
         theDetail.Memo = model.Memo;
         theDetail.Payor = model.Payor;
         theDetail.PaySource = model.PaySource;
         theDetail.Category = model.Category;
         theDetail.When = model.When;
         theLedger.LedgerDetails.Add(theDetail);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id=model.LedgerId });
     }
 }
 public ActionResult AddEditor(Models.LedgerAddEditorModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theUser = context.UserProfiles.FirstOrDefault(u => u.UserName == model.EditorName);
         if (theUser == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such User!", Message = "The user you are attempting to add does not exist in the database.", ReturnAction = "Editors", ReturnRouteValues = new { id= model.LedgerId } });
         }
         if (theLedger.Editors.Any(u => u.UserId == theUser.UserId))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "User Already an Editor!", Message = "The user you are attempting to add is already an editor.", ReturnAction = "Editors", ReturnRouteValues = new { id=model.LedgerId } });
         }
         theLedger.Editors.Add(theUser);
         context.SaveChanges();
         return RedirectToAction("Editors", new { id = model.LedgerId });
     }
 }
        public ActionResult CategoryReport(int id)
        {
            using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
            {
                var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == id);
                if (theLedger == default(Ledger))
                {
                    return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
                }
                else if(theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u=>u.UserName==User.Identity.Name)==default(UserProfile))
                {
                    return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
                }
                Models.CategoryReportModel theReport = new CategoryReportModel();
                theReport.LedgerId = theLedger.LedgerId;
                theReport.LedgerName = theLedger.LedgerName;
                theReport.Total = 0;
                Dictionary<string, CategoryReportCategoryModel> theCategories = new Dictionary<string, CategoryReportCategoryModel>();

                theLedger.LedgerDetails.OrderByDescending(ld => ld.When).ToList().ForEach(ld =>
                {
                    string theCategory = ld.Category;
                    if (!theCategories.ContainsKey(theCategory))
                    {
                        theCategories.Add(theCategory, new CategoryReportCategoryModel());
                        theCategories[theCategory].CategoryName = theCategory;
                        theCategories[theCategory].Details = new List<CategoryReportDetailModel>();
                        theCategories[theCategory].Subtotal = 0;
                    }
                    CategoryReportDetailModel theDetail = new CategoryReportDetailModel();
                    theDetail.LedgerDetailId = ld.LedgerDetailId;
                    theDetail.Amount = ld.Amount;
                    theDetail.Memo = ld.Memo;
                    theDetail.Payor = ld.Payor;
                    theDetail.PaySource = ld.PaySource;
                    theDetail.When = ld.When;
                    theCategories[theCategory].Details.Add(theDetail);
                    theCategories[theCategory].Subtotal += theDetail.Amount;
                    theReport.Total += theDetail.Amount;
                });
                theReport.Categories = theCategories.Values.OrderBy(m=>m.CategoryName).ToList();
                return View(theReport);
            }
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<db00ccd2da5aff4a5983c0a17b010f53a6Entities>(null);

                try
                {
                    using (var context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
 public ActionResult TransferOwnership(LedgerTransferOwnershipModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theOldOwner = theLedger.UserProfile;
         var theNewOwner = context.UserProfiles.FirstOrDefault(u => u.UserName == model.NewOwner);
         if (theNewOwner == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "User Does Not Exist!", Message = "The specified new owner does not exist!", ReturnAction = "Detail", ReturnRouteValues = new { id=model.LedgerId } });
         }
         if (theOldOwner.UserId != theNewOwner.UserId)
         {
             if (!theLedger.Editors.Any(u => u.UserId == theOldOwner.UserId) && model.MakeOldOwnerAnEditor)
             {
                 theLedger.Editors.Add(theOldOwner);
             }
             theLedger.UserProfile = theNewOwner;
             context.SaveChanges();
         }
         return RedirectToAction("Index");
     }
 }
 public ActionResult TransferOwnership(int id)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == id);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         LedgerTransferOwnershipModel theModel = new LedgerTransferOwnershipModel();
         theModel.LedgerId = id;
         theModel.MakeOldOwnerAnEditor = true;
         return View(theModel);
     }
 }
 public ActionResult Rename(Models.LedgerRenameModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.Id);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         theLedger.LedgerName = model.Name;
         context.SaveChanges();
         return RedirectToAction("Index");
     }
 }
 public ActionResult RemoveDetail(int id)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theDetail = context.LedgerDetails.FirstOrDefault(d => d.LedgerDetailId == id);
         if (theDetail == default(LedgerDetail))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger Detail!", Message = "The ledger detail you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == theDetail.Legder_LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         context.LedgerDetails.Remove(theDetail);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id = theLedger.LedgerId });
     }
 }
 public ActionResult Index()
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         Models.LedgerListModel theModel = new LedgerListModel();
         theModel.Details = new List<LedgerListDetailModel>();
         context.Ledgers.Where(l => (l.UserProfile.UserName == User.Identity.Name) || (l.Editors.FirstOrDefault(u=>u.UserName==User.Identity.Name)!=default(UserProfile))).ToList().ForEach(l =>
         {
             LedgerListDetailModel theDetail = new LedgerListDetailModel();
             theDetail.Id = l.LedgerId;
             theDetail.Name = l.LedgerName;
             theDetail.Owner = l.UserProfile.UserName;
             theDetail.HasEntries = l.LedgerDetails.Any();
             theDetail.Editors = l.Editors.Select(u => u.UserName).ToList();
             theModel.Details.Add(theDetail);
         });
         return View(theModel);
     }
 }
 public ActionResult Detail(int id)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == id);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         LedgerModel theModel = new LedgerModel();
         theModel.Id = theLedger.LedgerId;
         theModel.Name = theLedger.LedgerName;
         theModel.Details = new List<LedgerDetailModel>();
         theModel.Owner = theLedger.UserProfile.UserName;
         theModel.Editors = theLedger.Editors.Select(u => u.UserName).ToList();
         theLedger.LedgerDetails.OrderByDescending(i=>i.When.ToString("yyyyMMdd")+i.LedgerDetailId.ToString("X8")).ToList().ForEach(ld => {
             LedgerDetailModel theDetail = new LedgerDetailModel();
             theDetail.DetailId = ld.LedgerDetailId;
             theDetail.When = ld.When;
             theDetail.PaySource = ld.PaySource;
             theDetail.Payor = ld.Payor;
             theDetail.Amount = ld.Amount;
             theDetail.Memo = ld.Memo;
             theDetail.Category = ld.Category;
             theModel.Details.Add(theDetail);
         });
         if (theLedger.LedgerDetails.Any())
         {
             theModel.LatestWhen = theLedger.LedgerDetails.Select(d => d.When).Max();
         }
         else
         {
             theModel.LatestWhen = DateTime.Now;
         }
         return View(theModel);
     }
 }
 public ActionResult Create(Models.LedgerCreateModel model)
 {
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         UserProfile theUser = context.UserProfiles.First(u => u.UserName == User.Identity.Name);
         Ledger theLedger = new Ledger();
         theLedger.LedgerName = model.Name;
         theLedger.User_UserId = theUser.UserId;
         context.Ledgers.Add(theLedger);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id=theLedger.LedgerId });
     }
 }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (db00ccd2da5aff4a5983c0a17b010f53a6Entities db = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }