Beispiel #1
0
        public async Task CreateBudgetAccountAsync_WhenCalled_CreatesBudgetAccount()
        {
            IAccountingRepository sut = CreateSut();

            DateTime today = DateTime.Today;

            IAccounting accounting = await sut.GetAccountingAsync(WithExistingAccountingNumber(), today);

            IBudgetAccountGroup[] budgetAccountGroupCollection = (await sut.GetBudgetAccountGroupsAsync()).ToArray();

            IBudgetAccount budgetAccount = new BudgetAccount(accounting, WithExistingAccountNumberForBudgetAccount(), _fixture.Create <string>(), budgetAccountGroupCollection[_random.Next(0, budgetAccountGroupCollection.Length - 1)])
            {
                Description = _fixture.Create <string>()
            };

            decimal income   = _random.Next(50, 70) * 1000;
            decimal expenses = _random.Next(25, 35) * 1000;

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-3), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-2), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(-1), income, expenses));

            income   += _random.Next(5, 10) * 1000;
            expenses += _random.Next(5, 10) * 1000;

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today, income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(1), income, expenses));
            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(2), income, expenses));

            budgetAccount.BudgetInfoCollection.Add(CreateBudgetInfo(budgetAccount, today.AddMonths(3), 0M, 0M));

            IBudgetAccount result = await sut.CreateBudgetAccountAsync(budgetAccount);

            Assert.That(result, Is.Not.Null);
        }
Beispiel #2
0
        public ActionResult DeleteConfirmed(int id, int acctId)
        {
            Transaction transaction = db.Transactions.Find(id);

            decimal amount = transaction.Amount * -1;

            // get account balance and reconciled balance
            decimal balance      = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).Balance;
            decimal reconBalance = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).ReconciledBalance;

            // adjust balance
            balance      += amount;
            reconBalance += amount;

            // update balance
            BudgetAccount acctToUpdate = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId);

            acctToUpdate.Balance = balance;

            if (transaction.Reconciled == true)
            {
                acctToUpdate.ReconciledBalance = reconBalance;
            }

            db.Entry(acctToUpdate).State = EntityState.Modified;

            db.Transactions.Remove(transaction);
            db.SaveChanges();
            return(RedirectToAction("Index", new { acctId }));
        }
Beispiel #3
0
        internal static IBudgetAccount ToDomain(this BudgetAccountModel budgetAccountModel, IAccounting accounting, MapperCache mapperCache, IConverter accountingModelConverter)
        {
            NullGuard.NotNull(budgetAccountModel, nameof(budgetAccountModel))
            .NotNull(accounting, nameof(accounting))
            .NotNull(mapperCache, nameof(mapperCache))
            .NotNull(accountingModelConverter, nameof(accountingModelConverter));

            lock (mapperCache.SyncRoot)
            {
                IBudgetAccount budgetAccount = budgetAccountModel.Resolve(mapperCache.BudgetAccountDictionary);
                if (budgetAccount != null)
                {
                    return(budgetAccount);
                }

                IBudgetAccountGroup budgetAccountGroup = accountingModelConverter.Convert <BudgetAccountGroupModel, IBudgetAccountGroup>(budgetAccountModel.BudgetAccountGroup);

                budgetAccount = new BudgetAccount(accounting, budgetAccountModel.AccountNumber, budgetAccountModel.BasicAccount.AccountName, budgetAccountGroup)
                {
                    Description = budgetAccountModel.BasicAccount.Description,
                    Note        = budgetAccountModel.BasicAccount.Note
                };
                budgetAccountModel.CopyAuditInformationTo(budgetAccount);
                budgetAccount.SetDeletable(budgetAccountModel.Deletable);

                mapperCache.BudgetAccountDictionary.Add(budgetAccountModel.BudgetAccountIdentifier, budgetAccount);

                accounting.BudgetAccountCollection.Add(budgetAccount);

                if (budgetAccountModel.BudgetInfos != null)
                {
                    budgetAccount.BudgetInfoCollection.Populate(budgetAccount,
                                                                budgetAccountModel.BudgetInfos
                                                                .Where(budgetInfoModel => budgetInfoModel.Convertible() &&
                                                                       (budgetInfoModel.YearMonth.Year < budgetAccountModel.StatusDateForInfos.Year ||
                                                                        budgetInfoModel.YearMonth.Year == budgetAccountModel.StatusDateForInfos.Year &&
                                                                        budgetInfoModel.YearMonth.Month <= budgetAccountModel.StatusDateForInfos.Month))
                                                                .Select(budgetInfoModel => budgetInfoModel.ToDomain(budgetAccount))
                                                                .ToArray(),
                                                                budgetAccountModel.StatusDate,
                                                                budgetAccountModel.StatusDateForInfos);
                }

                if (budgetAccountModel.PostingLines != null)
                {
                    budgetAccount.PostingLineCollection.Add(budgetAccountModel.PostingLines
                                                            .Where(postingLineModel => postingLineModel.Convertible() &&
                                                                   postingLineModel.PostingDate >= budgetAccountModel.GetFromDateForPostingLines() &&
                                                                   postingLineModel.PostingDate < budgetAccountModel.GetToDateForPostingLines(1))
                                                            .Select(postingLineModel => postingLineModel.ToDomain(accounting, budgetAccount, mapperCache, accountingModelConverter))
                                                            .Where(postingLine => budgetAccount.PostingLineCollection.Contains(postingLine) == false)
                                                            .ToArray());
                }

                return(budgetAccount);
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            // Delete Transactions
            //Transaction transactions = db.Transactions.SelectMany(t=> t.AccountId)
            // Delete account
            BudgetAccount budgetAccount = db.BudgetAccounts.Find(id);

            db.BudgetAccounts.Remove(budgetAccount);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "Id,HouseholdId,Name,Balance,ReconciledBalance")] BudgetAccount budgetAccount)
 {
     if (ModelState.IsValid)
     {
         db.Entry(budgetAccount).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.HouseholdId = new SelectList(db.Household, "Id", "Name", budgetAccount.HouseholdId);
     return(View(budgetAccount));
 }
Beispiel #6
0
        public ActionResult Create([Bind(Include = "Id,AccountId,Amount,AbsAmount,Reconciled,AbsReconciledAmount,Date,Description,CategoryId")] Transaction transaction, int acctId)
        {
            if (ModelState.IsValid)
            {
                transaction.AccountId = acctId;
                transaction.AbsAmount = transaction.Amount;
                var     catId        = transaction.CategoryId;
                var     exp          = false;
                decimal reconBalance = 0;

                // determine if income or expense transaction
                if (catId.HasValue)
                {
                    exp = db.Categories.FirstOrDefault(c => c.Id == catId).Expense;
                }

                // if expense and amount > 0, then reverse sign
                if (exp == true && transaction.Amount > 0)
                {
                    transaction.Amount *= -1;
                }

                // update balance
                var balance = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).Balance;
                reconBalance = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).ReconciledBalance;

                balance += transaction.Amount;

                // if transaction just reconciled, then calculate reconciled balance
                if (transaction.Reconciled == true)
                {
                    reconBalance += transaction.Amount;
                }
                ;

                // NEED TO UPDATE ACCOUNT WITH NEW BALANCE
                BudgetAccount acctToUpdate = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId);
                acctToUpdate.Balance           = balance;
                acctToUpdate.ReconciledBalance = reconBalance;
                db.Entry(acctToUpdate).State   = EntityState.Modified;

                db.Transactions.Add(transaction);
                db.SaveChanges();
                return(RedirectToAction("Index", new { acctId }));
            }

            ViewBag.Name       = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).Name;
            ViewBag.AccountId  = new SelectList(db.BudgetAccounts, "Id", "Name", acctId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", transaction.CategoryId);
            return(View(transaction));
        }
        // GET: BudgetAccounts/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BudgetAccount budgetAccount = db.BudgetAccounts.Find(id);

            if (budgetAccount == null)
            {
                return(HttpNotFound());
            }
            return(View(budgetAccount));
        }
        public ActionResult Create([Bind(Include = "Id,Name,Balance,ReconciledBalance")] BudgetAccount budgetAccount)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId();

                budgetAccount.HouseholdId = int.Parse(User.Identity.GetHouseholdId());

                db.BudgetAccounts.Add(budgetAccount);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //ViewBag.HouseholdId = new SelectList(db.Household, "Id", "Name", budgetAccount.HouseholdId);
            return(View(budgetAccount));
        }
        // GET: BudgetAccounts/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BudgetAccount budgetAccount = db.BudgetAccounts.Find(id);

            if (budgetAccount == null)
            {
                return(HttpNotFound());
            }

            var userId = User.Identity.GetUserId();
            var hhId   = int.Parse(User.Identity.GetHouseholdId());

            ViewBag.Household = db.Household.First(h => h.Id == hhId).Name;
            return(View(budgetAccount));
        }
Beispiel #10
0
        public override IBudgetAccount ToDomain(IAccountingRepository accountingRepository)
        {
            NullGuard.NotNull(accountingRepository, nameof(accountingRepository));

            IAccounting         accounting         = GetAccountingAsync(accountingRepository).GetAwaiter().GetResult();
            IBudgetAccountGroup budgetAccountGroup = GetBudgetAccountGroupAsync(accountingRepository).GetAwaiter().GetResult();

            IBudgetAccount budgetAccount = new BudgetAccount(accounting, AccountNumber, AccountName, budgetAccountGroup)
            {
                Description = Description,
                Note        = Note
            };

            IBudgetInfo[] budgetInfoCollection = (BudgetInfoCollection ?? Array.Empty <IBudgetInfoCommand>())
                                                 .AsParallel()
                                                 .Select(budgetInfo => budgetInfo.ToDomain(budgetAccount))
                                                 .ToArray();
            budgetAccount.BudgetInfoCollection.Add(budgetInfoCollection);

            return(budgetAccount);
        }
 public BudgetAccountDTO ToDTO(BudgetAccount model)
 {
     return(new BudgetAccountDTO
     {
         Id = model.Id,
         AccountNumber = model.AccountNumber,
         Year = model.Year,
         BudgetStatus = model.BudgetStatus,
         YearAmount = model.YearAmount,
         JanuaryAmount = model.JanuaryAmount,
         FebruaryAmount = model.FebruaryAmount,
         MarchAmount = model.MarchAmount,
         AprilAmount = model.AprilAmount,
         MayAmount = model.MayAmount,
         JuneAmount = model.JuneAmount,
         JulyAmount = model.JulyAmount,
         AugustAmount = model.AugustAmount,
         SeptemberAmount = model.SeptemberAmount,
         OctoberAmount = model.OctoberAmount,
         NovemberAmount = model.NovemberAmount,
         DecemberAmount = model.DecemberAmount
     });
 }
Beispiel #12
0
        public ActionResult Edit([Bind(Include = "Id,AccountId,Amount,AbsAmount,ReconciledAmount,Date,Description,CategoryId")] int acctId, Transaction transaction)
        {
            if (ModelState.IsValid)
            {
                var     userId        = User.Identity.GetUserId();
                var     catId         = transaction.CategoryId;
                var     exp           = false;
                decimal balance       = 0;
                decimal reconBalance  = 0;
                decimal oldAmount     = 0;
                decimal amtDifference = 0;

                Transaction oldTransaction = new Transaction();

                // get tempdata amount
                if (TempData["Transaction"] != null)
                {
                    oldTransaction = TempData["Transaction"] as Transaction;
                }

                // determine if transaction amount changed and if so, reverse sign
                if (oldTransaction.Amount != transaction.Amount)
                {
                    amtDifference = amtDifference + (oldTransaction.Amount - transaction.Amount);
                    oldAmount     = oldTransaction.Amount * -1;
                }

                transaction.AbsAmount = transaction.Amount;

                // determine if income or expense transaction
                if (catId.HasValue)
                {
                    exp = db.Categories.FirstOrDefault(c => c.Id == catId).Expense;
                }

                // if expense and amount > 0, then reverse sign
                if (exp == true && transaction.Amount > 0)
                {
                    transaction.Amount *= -1;
                }

                // get account balance and reconciled balance
                balance      = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).Balance;
                reconBalance = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId).ReconciledBalance;

                // calculate balance if transaction amount changed and not reconciled
                if (oldTransaction.Amount != transaction.Amount)
                {
                    balance += oldAmount;
                    balance += transaction.Amount;
                }

                // if transaction just reconciled, then calculate reconciled balance
                if (transaction.Reconciled == true)
                {
                    reconBalance += oldAmount;
                    reconBalance += transaction.Amount;
                }
                ;

                // NEED TO UPDATE ACCOUNT WITH NEW BALANCE
                BudgetAccount acctToUpdate = db.BudgetAccounts.FirstOrDefault(a => a.Id == acctId);
                acctToUpdate.Balance           = balance;
                acctToUpdate.ReconciledBalance = reconBalance;

                db.Entry(acctToUpdate).State = EntityState.Modified;

                transaction.UpdateByUserId = userId;
                transaction.Updated        = DateTimeOffset.Now;

                db.Entry(transaction).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index", new { acctId }));
            }

            ViewBag.Balance      = db.BudgetAccounts.FirstOrDefault(a => a.Id == transaction.AccountId).Balance;
            ViewBag.reconBalance = db.BudgetAccounts.FirstOrDefault(a => a.Id == transaction.AccountId).ReconciledBalance;
            ViewBag.Name         = db.BudgetAccounts.FirstOrDefault(a => a.Id == transaction.AccountId).Name;
            ViewBag.AccountId    = new SelectList(db.BudgetAccounts, "Id", "Name", acctId);
            ViewBag.CategoryId   = new SelectList(db.Categories, "Id", "Name", transaction.CategoryId);
            return(View(transaction));
        }