public ActionResult Create(AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
        {
            if (ModelState.IsValid)
            {
                AccountingTransaction newAccountingTransaction = new AccountingTransaction();
                AccountingPeriodOpeningClosingTransaction accountingPeriodOpeningClosingTransaction = null;

                newAccountingTransaction = accountingTransactionBaseViewModel.AccountingTransaction;
                newAccountingTransaction.AccountTransactionID = Guid.NewGuid();

                SetupAccountForTransaction(ref newAccountingTransaction, accountingTransactionBaseViewModel);

                CheckAndUpdateTheDonorTransactionSource(ref newAccountingTransaction, accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource);

                if (accountingTransactionBaseViewModel.IsTransactionOpeningClosingTransaction)
                {
                    accountingPeriodOpeningClosingTransaction = new AccountingPeriodOpeningClosingTransaction();

                    AccountingPeriodTimeFrame pf = unitOfWork.AccountingPeriodTimeFrameRepository.GetByID(accountingTransactionBaseViewModel.AccountingPeriodTimeFrameID);
                    newAccountingTransaction.TransactionDate = accountingTransactionBaseViewModel.IsTransactionOpening ? pf.StartDate : pf.EndDate;

                    accountingPeriodOpeningClosingTransaction.IsTransactionOpening = accountingTransactionBaseViewModel.IsTransactionOpening;
                    accountingPeriodOpeningClosingTransaction.AccountingPeriodTimeFrameID = accountingTransactionBaseViewModel.AccountingPeriodTimeFrameID;
                    accountingPeriodOpeningClosingTransaction.AccountTransactionID = newAccountingTransaction.AccountTransactionID;
                    accountingPeriodOpeningClosingTransaction.Active = true;
                }

                SetupTagInfosForAccount(ref accountingTransactionBaseViewModel);

                newAccountingTransaction = unitOfWork.AccountingTransactionRepository.Insert(newAccountingTransaction);
                unitOfWork.Save();

                if (accountingTransactionBaseViewModel.IsTransactionOpeningClosingTransaction)
                {
                    accountingPeriodOpeningClosingTransaction.AccountTransactionID = newAccountingTransaction.AccountTransactionID;
                    unitOfWork.AccountingPeriodOpeningClosingTransactionRepository.Insert(accountingPeriodOpeningClosingTransaction);
                    unitOfWork.Save();
                }

                if (accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.DonationDonorEntityID != null)
                {
                    return RedirectToAction("Index", "Donor");
                }
                else if (accountingTransactionBaseViewModel.IsTransactionOpeningClosingTransaction)
                {
                    return RedirectToAction("Index", "AccountingPeriodOpeningClosingTransaction");
                }
                else if (newAccountingTransaction.IsTemplateTransaction)
                {
                    return RedirectToAction("Index", "AccountingTransactionTemplate");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }

            return View(AccountingTransactionBaseViewModelBaseSetup(accountingTransactionBaseViewModel));
        }
 private void SetupAccountingTemplate(ref AccountingTransactionBaseViewModel accountingTransactionAccountViewModel)
 {
     accountingTransactionAccountViewModel.ReoccurrenceFrames = unitOfWork.EventReoccurrenceFrameRepository.ReoccurrenceFrameKeyValueList();
 }
 private void SetupAccountingTemplateForDection(ref AccountingTransactionBaseViewModel vm)
 {
     vm.AccountingTransaction.Amount = 0;
     vm.AccountingTransaction.UseCurrentBatchAccountAmount = true;
 }
 private void SetupTagsForSelection(ref AccountingTransactionBaseViewModel accountingTransactionAccountViewModel)
 {
     accountingTransactionAccountViewModel.AvailableTags = unitOfWork.TagInfoRepository.TagInfoMultiSelectList(accountingTransactionAccountViewModel.AccountingTransaction.TagInfoes);
 }
        private void SetupTagInfosForAccount(ref AccountingTransactionBaseViewModel accountingTransactionAccountViewModel)
        {
            int[] selectedTags = accountingTransactionAccountViewModel.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();

            accountingTransactionAccountViewModel.AccountingTransaction.TagInfoes.Clear();

            foreach (var selectedTagInfo in selectedTagInfos)
            {
                accountingTransactionAccountViewModel.AccountingTransaction.TagInfoes.Add(selectedTagInfo);
            }
        }
 private void InitializeCreditDebitAccountsForTransaction(ref AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
 {
     accountingTransactionBaseViewModel.AccountsToCredit = SetupSelectedAccountsSelectListFromTransaction(accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionAccountingTransactionAccounts.Where(x => x.IsTransactionCredit));
     accountingTransactionBaseViewModel.AccountsToDebit = SetupSelectedAccountsSelectListFromTransaction(accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionAccountingTransactionAccounts.Where(x => !x.IsTransactionCredit));
 }
        private AccountingTransactionBaseViewModel AccountingTransactionBaseViewModelBaseSetup(AccountingTransactionBaseViewModel accountingTransactionBaseViewModel = null)
        {
            //AccountingTransaction accountingTransaction = null;

            if (accountingTransactionBaseViewModel == null)
            {
                accountingTransactionBaseViewModel = new AccountingTransactionBaseViewModel();
            }

            if (accountingTransactionBaseViewModel.AccountingTransaction == null)
            {
                accountingTransactionBaseViewModel.AccountingTransaction = new AccountingTransaction();
            }

            if (accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource == null)
            {
                accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource = new AccountingTransactionSource();
            }

            //accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AvailableVendors = unitOfWork.AccountingVendorRepository.VendorsSelectList(
            //    new AccountingVendor() { AccountingVendorID = accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AccountingVendorID.GetValueOrDefault()});

            //accountingTransactionBaseViewModel.AccountsToCredit = unitOfWork.AccountingTransactionAccountRepository.AccountingAccountsMultiSelectList(
            //    accountingTransactionBaseViewModel.AccountsToCredit.Select<SelectListItem, AccountingTransactionAccount>(x => new AccountingTransactionAccount() { AccountTransactionAccountID = Convert.ToInt32(x.Value)}));

            accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AvailableVendors = unitOfWork.AccountingVendorRepository
                .VendorsSelectList(new AccountingVendor() { AccountingVendorID = accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AccountingVendorID.GetValueOrDefault() });

            accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.AvailablePersons = unitOfWork.PersonPrimaryInfoRepository.PeopleSelectList(accountingTransactionBaseViewModel.AccountingTransaction.AccountingTransactionSource.PersonPrimaryInfoID.GetValueOrDefault());

            //accountingTransactionBaseViewModel.AccountsToCredit = unitOfWork.AccountingTransactionAccountRepository.AccountingAccountsMultiSelectList();
            //accountingTransactionBaseViewModel.AccountsToDebit = unitOfWork.AccountingTransactionAccountRepository.AccountingAccountsMultiSelectList();

            accountingTransactionBaseViewModel.AccountsToCredit = SetupAccountsSelectList(accountingTransactionBaseViewModel.AccountsToCredit);
            accountingTransactionBaseViewModel.AccountsToDebit = SetupAccountsSelectList(accountingTransactionBaseViewModel.AccountsToDebit);

            if (User.IsInRole(AppRole.DonationManager))
            {
                accountingTransactionBaseViewModel.ShowDonorName = true;
            }

            SetupTagsForSelection(ref accountingTransactionBaseViewModel);

            if (accountingTransactionBaseViewModel.AccountingTransaction.IsTemplateTransaction)
            {
                SetupAccountingTemplate(ref accountingTransactionBaseViewModel);
                accountingTransactionBaseViewModel.ShowReoccurringFields = true;
            }

            return accountingTransactionBaseViewModel;
        }
        private void RestrictAccountsToOnlyDonationAccounts(ref AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
        {
            //x.TagInfoes.Any(t => t.TagInfoID == 2)
            IEnumerable<AccountingTransactionAccount> donationAccounts = unitOfWork.AccountingTransactionAccountRepository.Get(x => x.IsDonation);

            accountingTransactionBaseViewModel.AccountsToCredit = accountingTransactionBaseViewModel.AccountsToCredit
                .Where(x => donationAccounts.Any(a => a.AccountTransactionAccountID == Convert.ToInt32(x.Value)))
                .Select(x => new SelectListItem() { Text = x.Text, Value = x.Value, Selected = donationAccounts.Any(a => a.AccountTransactionAccountID == Convert.ToInt32(x.Value) && a.IsDefaultCredit) })
                .ToList();
            accountingTransactionBaseViewModel.AccountsToDebit = accountingTransactionBaseViewModel.AccountsToDebit
                .Where(x => donationAccounts.Any(a => a.AccountTransactionAccountID == Convert.ToInt32(x.Value)))
                .Select(x => new SelectListItem() { Text = x.Text, Value = x.Value, Selected = donationAccounts.Any(a => a.AccountTransactionAccountID == Convert.ToInt32(x.Value) && a.IsDefaultDebit) })
                .ToList();
        }
        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);
            }

        }
 private void SetupOpeningClosingTransaction(AccountingTransactionBaseViewModel accountingTransactionBaseViewModel)
 {
     accountingTransactionBaseViewModel.AccountingPeriodTimeFrameSelectList = unitOfWork.AccountingPeriodTimeFrameRepository.AccountingPeriodTimeFrameSelectList(accountingTransactionBaseViewModel.AccountingPeriodTimeFrameID);
 }
        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));
        }
        public ActionResult Edit(Guid accountTransactionID)
        {
            AccountingTransactionBaseViewModel accountingTransactionBaseViewModel = new AccountingTransactionBaseViewModel();

            accountingTransactionBaseViewModel.AccountingTransaction = unitOfWork.AccountingTransactionRepository.GetByID(accountTransactionID);

            InitializeCreditDebitAccountsForTransaction(ref accountingTransactionBaseViewModel);

            return View(AccountingTransactionBaseViewModelBaseSetup(accountingTransactionBaseViewModel));
        }