/// <summary>
        /// Binds the yearly summary.
        /// </summary>
        private void BindYearlySummary()
        {
            var givingId = Person.GivingId;

            using (var rockContext = new RockContext())
            {
                DateTime?startDate;
                if (!IsYearlySummaryExpanded)
                {
                    // Only show this current year and last year
                    startDate = RockDateTime.Now.StartOfYear().AddYears(-1);
                }
                else
                {
                    startDate = null;
                }

                var monthlyAccountGivingHistoryList = new FinancialTransactionService(rockContext).GetGivingAutomationMonthlyAccountGivingHistory(givingId, startDate);

                var financialAccounts = new FinancialAccountService(rockContext).Queryable()
                                        .AsNoTracking()
                                        .ToDictionary(k => k.Id, v => v.Name);

                var summaryList = monthlyAccountGivingHistoryList
                                  .GroupBy(a => new { a.Year, a.AccountId })
                                  .Select(t => new SummaryRecord
                {
                    Year        = t.Key.Year,
                    AccountId   = t.Key.AccountId,
                    TotalAmount = t.Sum(d => d.Amount)
                })
                                  .OrderByDescending(a => a.Year)
                                  .ToList();

                var contributionSummaries = new List <ContributionSummary>();
                foreach (var item in summaryList.GroupBy(a => a.Year))
                {
                    var contributionSummary = new ContributionSummary();
                    contributionSummary.Year           = item.Key;
                    contributionSummary.SummaryRecords = new List <SummaryRecord>();
                    foreach (var a in item)
                    {
                        a.AccountName = financialAccounts.ContainsKey(a.AccountId) ? financialAccounts[a.AccountId] : string.Empty;
                        contributionSummary.SummaryRecords.Add(a);
                    }

                    contributionSummary.TotalAmount = item.Sum(a => a.TotalAmount);
                    contributionSummaries.Add(contributionSummary);
                }

                rptYearSummary.DataSource = contributionSummaries;
                rptYearSummary.DataBind();

                // Show the correct button to expand or collapse
                lbShowLessYearlySummary.Visible = IsYearlySummaryExpanded;
                lbShowMoreYearlySummary.Visible = !IsYearlySummaryExpanded;
            }
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var contributionType = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid());

            if (contributionType != null)
            {
                var rockContext = new RockContext();
                var transactionDetailService = new FinancialTransactionDetailService(rockContext);
                var qry = transactionDetailService.Queryable().AsNoTracking()
                          .Where(a =>
                                 a.Transaction.TransactionTypeValueId == contributionType.Id &&
                                 a.Transaction.TransactionDateTime.HasValue);

                var targetPerson = this.ContextEntity <Person>();
                if (targetPerson != null)
                {
                    qry = qry.Where(t => t.Transaction.AuthorizedPersonAlias.Person.GivingId == targetPerson.GivingId);
                }

                List <SummaryRecord> summaryList;

                using (new Rock.Data.QueryHintScope(rockContext, QueryHintType.RECOMPILE))
                {
                    summaryList = qry
                                  .GroupBy(a => new { a.Transaction.TransactionDateTime.Value.Year, a.AccountId })
                                  .Select(t => new SummaryRecord
                    {
                        Year        = t.Key.Year,
                        AccountId   = t.Key.AccountId,
                        TotalAmount = t.Sum(d => d.Amount)
                    }).OrderByDescending(a => a.Year)
                                  .ToList();
                }

                var mergeObjects      = GlobalAttributesCache.GetMergeFields(this.CurrentPerson);
                var financialAccounts = new FinancialAccountService(rockContext).Queryable().Select(a => new { a.Id, a.Name }).ToDictionary(k => k.Id, v => v.Name);

                var yearsMergeObjects = new List <Dictionary <string, object> >();
                foreach (var item in summaryList.GroupBy(a => a.Year))
                {
                    var year         = item.Key;
                    var accountsList = new List <object>();
                    foreach (var a in item)
                    {
                        var accountDictionary = new Dictionary <string, object>();
                        accountDictionary.Add("Account", financialAccounts.ContainsKey(a.AccountId) ? financialAccounts[a.AccountId] : string.Empty);
                        accountDictionary.Add("TotalAmount", a.TotalAmount);
                        accountsList.Add(accountDictionary);
                    }

                    var yearDictionary = new Dictionary <string, object>();
                    yearDictionary.Add("Year", year);
                    yearDictionary.Add("SummaryRows", accountsList);

                    yearsMergeObjects.Add(yearDictionary);
                }

                mergeObjects.Add("Rows", yearsMergeObjects);

                lLavaOutput.Text = string.Empty;
                if (GetAttributeValue("EnableDebug").AsBooleanOrNull().GetValueOrDefault(false))
                {
                    lLavaOutput.Text = mergeObjects.lavaDebugInfo(rockContext);
                }

                string template = GetAttributeValue("LavaTemplate");

                lLavaOutput.Text += template.ResolveMergeFields(mergeObjects).ResolveClientIds(upnlContent.ClientID);
            }
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var contributionType = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid() );
            if ( contributionType != null )
            {
                var rockContext = new RockContext();
                var transactionDetailService = new FinancialTransactionDetailService( rockContext );
                var qry = transactionDetailService.Queryable().AsNoTracking()
                    .Where( a =>
                        a.Transaction.TransactionTypeValueId == contributionType.Id &&
                        a.Transaction.TransactionDateTime.HasValue );

                var targetPerson = this.ContextEntity<Person>();
                if ( targetPerson != null )
                {
                    qry = qry.Where( t => t.Transaction.AuthorizedPersonAlias.Person.GivingId == targetPerson.GivingId );
                }

                List<SummaryRecord> summaryList;

                using ( new Rock.Data.QueryHintScope( rockContext, QueryHintType.RECOMPILE ) )
                {
                    summaryList = qry
                        .GroupBy( a => new { a.Transaction.TransactionDateTime.Value.Year, a.AccountId } )
                        .Select( t => new SummaryRecord
                        {
                            Year = t.Key.Year,
                            AccountId = t.Key.AccountId,
                            TotalAmount = t.Sum( d => d.Amount )
                        } ).OrderByDescending( a => a.Year )
                        .ToList();
                }

                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson );
                var financialAccounts = new FinancialAccountService( rockContext ).Queryable().Select( a => new { a.Id, a.Name } ).ToDictionary( k => k.Id, v => v.Name );

                var yearsMergeObjects = new List<Dictionary<string, object>>();
                foreach ( var item in summaryList.GroupBy( a => a.Year ) )
                {
                    var year = item.Key;
                    var accountsList = new List<object>();
                    foreach ( var a in item )
                    {
                        var accountDictionary = new Dictionary<string, object>();
                        accountDictionary.Add( "Account", financialAccounts.ContainsKey( a.AccountId ) ? financialAccounts[a.AccountId] : string.Empty );
                        accountDictionary.Add( "TotalAmount", a.TotalAmount );
                        accountsList.Add( accountDictionary );
                    }

                    var yearDictionary = new Dictionary<string, object>();
                    yearDictionary.Add( "Year", year );
                    yearDictionary.Add( "SummaryRows", accountsList );

                    yearsMergeObjects.Add( yearDictionary );
                }

                mergeFields.Add( "Rows", yearsMergeObjects );

                lLavaOutput.Text = string.Empty;
                if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) )
                {
                    lLavaOutput.Text = mergeFields.lavaDebugInfo( rockContext );
                }

                string template = GetAttributeValue( "LavaTemplate" );

                lLavaOutput.Text += template.ResolveMergeFields( mergeFields ).ResolveClientIds( upnlContent.ClientID );
            }
        }