private void SetupAccountForTransaction(ref AccountingTransaction accountingTransaction, AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
        {
            int[] AccountsToCreditIDs = accountingTransactionBaseViewModel.AccountsToCredit.Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray<int>();
            int[] AccountsToDebitIDs = accountingTransactionBaseViewModel.AccountsToDebit.Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray<int>();

            IEnumerable<AccountingTransactionAccount> accountsSelected = unitOfWork.AccountingTransactionAccountRepository.Get(x => AccountsToCreditIDs.Contains(x.AccountTransactionAccountID) || AccountsToDebitIDs.Contains(x.AccountTransactionAccountID)).ToArray();

            foreach (var account in accountsSelected)
            {
                AccountingTransactionAccountingTransactionAccount newAccountingTransactionAccountingTransactionAccount = new AccountingTransactionAccountingTransactionAccount();
                newAccountingTransactionAccountingTransactionAccount.IsTransactionCredit = AccountsToCreditIDs.Contains(account.AccountTransactionAccountID);
                newAccountingTransactionAccountingTransactionAccount.AccountTransactionID = accountingTransaction.AccountTransactionID;
                newAccountingTransactionAccountingTransactionAccount.AccountTransactionAccountID = account.AccountTransactionAccountID;
                accountingTransaction.AccountingTransactionAccountingTransactionAccounts.Add(newAccountingTransactionAccountingTransactionAccount);
            }

        }
        public ActionResult Edit(AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.context.AccountingTransactions.Attach(accountingTransactionBaseViewModel.AccountingTransaction);

                int? selectedVendor = accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AccountingVendorID;

                unitOfWork.context.Entry(accountingTransactionBaseViewModel.AccountingTransaction).Collection(x => x.AccountingTransactionAccountingTransactionAccounts).Load();
                unitOfWork.context.Entry(accountingTransactionBaseViewModel.AccountingTransaction).Collection(x => x.TagInfoes).Load();

                accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionAccountingTransactionAccounts.Clear();

                int[] AccountsToCreditIDs = accountingTransactionBaseViewModel.AccountsToCredit.Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray<int>();
                int[] AccountsToDebitIDs = accountingTransactionBaseViewModel.AccountsToDebit.Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray<int>();

                IEnumerable<AccountingTransactionAccount> accountsSelected = unitOfWork.AccountingTransactionAccountRepository.Get(x => AccountsToCreditIDs.Contains(x.AccountTransactionAccountID) || AccountsToDebitIDs.Contains(x.AccountTransactionAccountID)).ToArray();
                IEnumerable<AccountingTransactionAccountingTransactionAccount> accountsCurrent = accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionAccountingTransactionAccounts;

                foreach (var account in accountsSelected)
                {
                    AccountingTransactionAccountingTransactionAccount transactionAccount = new AccountingTransactionAccountingTransactionAccount();
                    transactionAccount.IsTransactionCredit = AccountsToCreditIDs.Contains(account.AccountTransactionAccountID);
                    transactionAccount.AccountTransactionID = accountingTransactionBaseViewModel.AccountingTransaction.AccountTransactionID;
                    transactionAccount.AccountTransactionAccountID = account.AccountTransactionAccountID;

                    accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionAccountingTransactionAccounts.Add(transactionAccount);
                }

                //Remove the donor from transaction if transaction not from a donor
                if (!accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.IsFromADonor)
                {
                    accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.DonationDonorEntity = null;
                    accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.DonationDonorEntityID = null;
                }

                //Setup tags
                //int[] selectedTags = accountingTransactionBaseViewModel.AvailableTags.Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToArray<int>();

                //IEnumerable<TagInfo> selectedTagInfos = unitOfWork.TagInfoRepository.Get(x => selectedTags.Contains(x.TagInfoID)).ToArray();

                //accountingTransactionBaseViewModel.AccountingTransaction.TagInfoes.Clear();

                //foreach (var selectedTagInfo in selectedTagInfos)
                //{
                //    accountingTransactionBaseViewModel.AccountingTransaction.TagInfoes.Add(selectedTagInfo);
                //}
                SetupTagInfosForAccount(ref accountingTransactionBaseViewModel);

                unitOfWork.context.Entry(accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource).State = System.Data.Entity.EntityState.Modified;
                //unitOfWork.context.Entry(accountingTransactionBaseViewModel.AccountingTransaction.TagInfoes).State = System.Data.Entity.EntityState.Modified;

                unitOfWork.AccountingTransactionRepository.Update(accountingTransactionBaseViewModel.AccountingTransaction);
                unitOfWork.Save();

                if (accountingTransactionBaseViewModel.AccountingTransaction.IsTemplateTransaction)
                {
                    return RedirectToAction("Index", "AccountingTransactionTemplate");
                }
                else
                {
                    return RedirectToAction("Index");
                }

            }
            return View(AccountingTransactionBaseViewModelBaseSetup(accountingTransactionBaseViewModel));
        }