public void Save(TransactionDTO dto)
        {
            var account = _accounts.Get(dto.Account_AccountID);

            var transaction = _transactions.Get(dto.TransactionID);

            if (transaction != null)
            {
                dto.MapTo(transaction);
                _transactions.Update(transaction);
            }
            else
            {
                transaction = _transactions.Add(dto.MapTo<Transaction>());
                dto.TransactionID = transaction.TransactionID;
            }

            // Update the transaction description history
            var history = account.TransactionDescriptionHistory;
            var historyList = new List<string>();
            // Turn the history string into a list
            if (!string.IsNullOrWhiteSpace(history))
                historyList = history.Split('|').ToList();
            // Add the description from the transaction we just added to the history
            historyList.Add(dto.Description);
            // Re-flatten the history list (removing any duplicates) and save it against the account
            account.TransactionDescriptionHistory = string.Join("|", historyList.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray());
            _accounts.Update(account);
        }
        public ActionResult CreateTransfer(ProfileViewModel profile, IndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                GetModelData(model, profile.UserID, model.Account_AccountID);
                model.Tab = DashboardTab.Transfers;
                return View("Index", model);
            }

            model.Tab = DashboardTab.Transfers;
            var sourceAccount = _accountServices.Get(model.SourceAccountID);
            var destinationAccount =  _accountServices.Get(model.DestinationAccountID);

            // Do Transfer
            var guid = Guid.NewGuid().ToString();

            // Make sure the amount is not negative
            if(model.Amount < 0)
                model.Amount *= -1;

            var sourceTransaction = new TransactionDTO {
                TransferGUID = guid,
                Account_AccountID = model.SourceAccountID,
                Date = model.Date,
                Category_CategoryID = model.Category_CategoryID,
                Amount = (model.Amount * -1),
                Description = "Transfer to " + destinationAccount.Name,
                Note = model.Note
            };

            var destinationTransaction = new TransactionDTO {
                TransferGUID = guid,
                Account_AccountID = model.DestinationAccountID,
                Date = model.Date,
                // Don't set the category of the destination because we can't know what it should be
                // Category_CategoryID = model.Category_CategoryID,
                Amount = model.Amount,
                Description = "Transfer from " + sourceAccount.Name,
                Note = model.Note
            };

            _transactionServices.Save(sourceTransaction);
            _transactionServices.Save(destinationTransaction);

            // Clear the cache of anything that depends upon transactions
            _cache.InvalidateAllWithDependency(_cachingHelpers.GetDependencyKey(CachingDependency.Transaction));
            // Clear the user because current balance comes from User.Accounts property
            _cache.InvalidateAllWithDependency(_cachingHelpers.GetDependencyKey(CachingDependency.User));

            var encodedPageState = EncryptionHelpers.EncodeReturnParameters(model.Account_AccountID, model.Type, model.Tab);

            return RedirectToRoute("Home-State", new { state = encodedPageState });
        }
 public void Save(TransactionDTO dto)
 {
     _transactionServices.Save(dto);
 }