public IEnumerable<Models.Account> GetAccounts(AccountType accountType = null, DateTime? searchDate = null, int? parentAccount = null, bool? permanentAccountsOnly = null)
        {
            return this.repository.SearchAccounts(accountType != null ? accountType.Id : (int?)null, parentAccount,
                searchDate, permanentAccountsOnly).ToList();

            //IQueryable<vw_AccountWithStatusInfo> statusInfo = this.repository.GetAccountStatusInfo();

            //if(accountType != null)
            //    statusInfo = statusInfo.Where(i => i.AccountType == accountType.Id);

            //if(searchDate != null)
            //    statusInfo = statusInfo.Where(i => i.OpenDate != null && i.OpenDate <= searchDate
            //        && (i.CloseDate >= searchDate || i.CloseDate == null));

            //if(parentAccount != null)
            //    statusInfo = statusInfo.Where(i => i.ParentAccountId == parentAccount.Id);

            //if(permanentAccountsOnly != null)
            //    statusInfo = statusInfo.Where(i => i.IsPermenantAccount == permanentAccountsOnly);

            //IQueryable<Account> result = statusInfo.Join(this.repository.GetAccounts(),
            //    i => i.Id,
            //    a => a.Id,
            //    (i, a) => a);

            //return result.ToList();
        }
        public Models.Account OpenAccount(string name, AccountType type, DateTime openDate, Models.Account parentAccount = null)
        {
            if (this.repository.GetAccounts().Any(a => a.Name.ToUpper() == name.ToUpper()))
                throw new ArgumentException("An account by that name already exists");

            if(type == null)
                throw new ArgumentException("Invalid account type");

            Models.AccountStatusType openType = this.repository.GetAccountStatusTypes()
                    .FirstOrDefault(t => t.Name.ToUpper() == AccountStatus.Open.ToString().ToUpper());
            Models.AccountStatusType closeType = this.repository.GetAccountStatusTypes()
                .FirstOrDefault(t => t.Name.ToUpper() == AccountStatus.Close.ToString().ToUpper());

            if(parentAccount != null)
            {
                Models.AccountStatu parentOpen = this.repository.GetAccountStatuses()
                    .Where(s => s.AccountStatusTypeId == openType.Id
                        && s.AccountId == parentAccount.Id && s.StatusDateTime <= openDate)
                    .OrderByDescending(s => s.StatusDateTime)
                    .FirstOrDefault();
                Models.AccountStatu parentClose = this.repository.GetAccountStatuses()
                    .Where(s => s.AccountStatusTypeId == closeType.Id
                        && s.AccountId == parentAccount.Id)
                    .OrderByDescending(s => s.StatusDateTime)
                    .FirstOrDefault();

                if (parentOpen == null || (parentClose != null && parentClose.StatusDateTime < openDate))
                    throw new ArgumentException("Parent account was not open at the time this account was opened");

                if (parentAccount.AccountTypeId != type.Id)
                    throw new ArgumentException("Parent account type must match child account type");
            }

            Account result = this.repository.CreateAccount();
            result.Name = name;
            result.AccountType = type;

            if (parentAccount != null)
            {
                result.ParentAccountId = parentAccount.Id;
            }

            AccountStatu accountOpening = this.repository.CreateAccountStatus();
            accountOpening.StatusDateTime = openDate;

            openType.AccountStatus.Add(accountOpening);
            result.AccountStatus.Add(accountOpening);

            return result;
        }