/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { _currencyTypes = new Dictionary<int,string>(); _creditCardTypes = new Dictionary<int,string>(); // If configured for a person and person is null, return int personEntityTypeId = EntityTypeCache.Read( "Rock.Model.Person" ).Id; if ( ContextTypesRequired.Any( e => e.Id == personEntityTypeId ) && _person == null ) { return; } // If configured for a batch and batch is null, return int batchEntityTypeId = EntityTypeCache.Read( "Rock.Model.FinancialBatch" ).Id; if ( ContextTypesRequired.Any( e => e.Id == batchEntityTypeId ) && _batch == null ) { return; } // If configured for a batch and batch is null, return int scheduledTxnEntityTypeId = EntityTypeCache.Read( "Rock.Model.FinancialScheduledTransaction" ).Id; if ( ContextTypesRequired.Any( e => e.Id == scheduledTxnEntityTypeId ) && _scheduledTxn == null ) { return; } // Qry var qry = new FinancialTransactionService( new RockContext() ) .Queryable( "AuthorizedPersonAlias.Person,ProcessedByPersonAlias.Person" ); // Set up the selection filter if ( _batch != null ) { // If transactions are for a batch, the filter is hidden so only check the batch id qry = qry.Where( t => t.BatchId.HasValue && t.BatchId.Value == _batch.Id ); // If the batch is closed, do not allow any editing of the transactions if ( _batch.Status != BatchStatus.Closed && _canEdit ) { gTransactions.IsDeleteEnabled = true; } else { gTransactions.IsDeleteEnabled = false; } } else if ( _scheduledTxn != null ) { // If transactions are for a batch, the filter is hidden so only check the batch id qry = qry.Where( t => t.ScheduledTransactionId.HasValue && t.ScheduledTransactionId.Value == _scheduledTxn.Id ); gTransactions.IsDeleteEnabled = false; } else // Person { // otherwise set the selection based on filter settings if ( _person != null ) { qry = qry.Where( t => t.AuthorizedPersonAlias.PersonId == _person.Id ); } // Date Range var drp = new DateRangePicker(); drp.DelimitedValues = gfTransactions.GetUserPreference( "Date Range" ); if ( drp.LowerValue.HasValue ) { qry = qry.Where( t => t.TransactionDateTime >= drp.LowerValue.Value ); } if ( drp.UpperValue.HasValue ) { DateTime upperDate = drp.UpperValue.Value.Date.AddDays( 1 ); qry = qry.Where( t => t.TransactionDateTime < upperDate ); } // Amount Range var nre = new NumberRangeEditor(); nre.DelimitedValues = gfTransactions.GetUserPreference( "Amount Range" ); if ( nre.LowerValue.HasValue ) { qry = qry.Where( t => t.TransactionDetails.Sum( d => d.Amount ) >= nre.LowerValue.Value ); } if ( nre.UpperValue.HasValue ) { qry = qry.Where( t => t.TransactionDetails.Sum( d => d.Amount ) <= nre.UpperValue.Value ); } // Transaction Code string transactionCode = gfTransactions.GetUserPreference( "Transaction Code" ); if ( !string.IsNullOrWhiteSpace( transactionCode ) ) { qry = qry.Where( t => t.TransactionCode == transactionCode.Trim() ); } // Account Id int accountId = int.MinValue; if ( int.TryParse( gfTransactions.GetUserPreference( "Account" ), out accountId ) ) { qry = qry.Where( t => t.TransactionDetails.Any( d => d.AccountId == accountId ) ); } // Transaction Type int transactionTypeId = int.MinValue; if ( int.TryParse( gfTransactions.GetUserPreference( "Transaction Type" ), out transactionTypeId ) ) { qry = qry.Where( t => t.TransactionTypeValueId == transactionTypeId ); } // Currency Type int currencyTypeId = int.MinValue; if ( int.TryParse( gfTransactions.GetUserPreference( "Currency Type" ), out currencyTypeId ) ) { qry = qry.Where( t => t.CurrencyTypeValueId == currencyTypeId ); } // Credit Card Type int creditCardTypeId = int.MinValue; if ( int.TryParse( gfTransactions.GetUserPreference( "Credit Card Type" ), out creditCardTypeId ) ) { qry = qry.Where( t => t.CreditCardTypeValueId == creditCardTypeId ); } // Source Type int sourceTypeId = int.MinValue; if ( int.TryParse( gfTransactions.GetUserPreference( "Source Type" ), out sourceTypeId ) ) { qry = qry.Where( t => t.SourceTypeValueId == sourceTypeId ); } } SortProperty sortProperty = gTransactions.SortProperty; if ( sortProperty != null ) { if ( sortProperty.Property == "TotalAmount" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { qry = qry.OrderBy( t => t.TransactionDetails.Sum( d => (decimal?)d.Amount ) ?? 0.00M ); } else { qry = qry.OrderByDescending( t => t.TransactionDetails.Sum( d => (decimal?)d.Amount ) ?? 0.0M ); } } else { qry = qry.Sort( sortProperty ); } } else { qry = qry.OrderBy( t => t.Id ); } // Row Limit int? rowLimit = gfTransactions.GetUserPreference( "Row Limit" ).AsIntegerOrNull(); if ( rowLimit.HasValue ) { qry = qry.Take( rowLimit.Value ); } gTransactions.DataSource = qry.AsNoTracking().ToList(); gTransactions.DataBind(); }