示例#1
0
        public void ClosePlanTest()
        {
            const string accountName = "ExpenseManagerAccount01";
            const string typeName    = "Food";
            var          account     = new AccountModel
            {
                Badges = new List <AccountBadgeModel>(),
                Costs  = new List <CostInfoModel>(),
                Name   = accountName
            };
            var type = new CostTypeModel
            {
                Name         = typeName,
                CostInfoList = new EditableList <CostInfoModel>(),
                Account      = account
            };
            var plan = new PlanModel
            {
                Description  = "I want money for food!",
                PlanType     = PlanTypeModel.Save,
                PlannedMoney = 10000,
                IsCompleted  = false,
            };

            using (
                var db =
                    new ExpenseDbContext(
                        Effort.DbConnectionFactory.CreatePersistent(TestInstaller.ExpenseManagerTestDbConnection)))
            {
                db.Accounts.Add(account);
                db.CostTypes.Add(type);
                db.SaveChanges();
                var accountId = account.Id;
                plan.AccountId   = accountId;
                plan.PlannedType = type;
                db.Plans.Add(plan);
                db.SaveChanges();
                var item = new CostInfoModel()
                {
                    Description          = "bread",
                    AccountId            = accountId,
                    TypeId               = type.Id,
                    IsIncome             = true,
                    Money                = 10001,
                    Created              = DateTime.Now.Subtract(new TimeSpan(100, 0, 0, 0)),
                    Account              = db.Accounts.Find(accountId),
                    Type                 = db.CostTypes.Find(type.Id),
                    Periodicity          = PeriodicityModel.None,
                    PeriodicMultiplicity = 3
                };
                db.CostInfos.Add(item);
                db.SaveChanges();
            }
            var closeable = _balanceFacade.ListAllCloseablePlans(account.Id);
            var balance   = _balanceFacade.GetBalance(account.Id);

            Assert.IsTrue(closeable.Count == 1, "Plan not found as closeable");
            _balanceFacade.ClosePlan(closeable.Single());
            Assert.IsTrue(balance == _expenseFacade.ListItems(null, null, null, null, null, null, null, false, null).Single().Money + _balanceFacade.GetBalance(account.Id));
        }
        /// <summary>
        /// Marks given plan as finished
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Close([FromForm] Guid id)
        {
            var plan    = _balanceFacade.GetPlan(id);
            var account = CurrentAccountProvider.GetCurrentAccount(HttpContext.User);

            if (plan == null || plan.AccountId != account.Id)
            {
                return(RedirectWithError(ExpenseManagerResource.PlanNotClosedDoesntExist));
            }

            try
            {
                _balanceFacade.ClosePlan(plan);
            }
            catch (Exception)
            {
                return(RedirectWithError(ExpenseManagerResource.PlanNotClosed));
            }
            return(RedirectToAction("Index", new { successMessage = ExpenseManagerResource.PlanClosed }));
        }