Пример #1
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            _currencyTypes   = new Dictionary <int, string>();
            _creditCardTypes = new Dictionary <int, string>();

            // If configured for a registration and registration is null, return
            int registrationEntityTypeId = EntityTypeCache.Read(typeof(Rock.Model.Registration)).Id;

            if (ContextTypesRequired.Any(e => e.Id == registrationEntityTypeId) && _registration == null)
            {
                return;
            }

            // 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 rockContext = new RockContext();
            var qry         = new FinancialTransactionService(rockContext).Queryable();

            // Transaction Types
            var transactionTypeValueIdList = GetAttributeValue("TransactionTypes").SplitDelimitedValues().AsGuidList().Select(a => DefinedValueCache.Read(a)).Where(a => a != null).Select(a => a.Id).ToList();

            if (transactionTypeValueIdList.Any())
            {
                qry = qry.Where(t => transactionTypeValueIdList.Contains(t.TransactionTypeValueId));
            }

            // 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 = _canEdit;
                }
                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 if (_registration != null)
            {
                qry = qry
                      .Where(t => t.TransactionDetails
                             .Any(d =>
                                  d.EntityTypeId.HasValue &&
                                  d.EntityTypeId.Value == registrationEntityTypeId &&
                                  d.EntityId.HasValue &&
                                  d.EntityId.Value == _registration.Id));

                gTransactions.IsDeleteEnabled = false;
            }
            else    // Person
            {
                // otherwise set the selection based on filter settings
                if (_person != null)
                {
                    // get the transactions for the person or all the members in the person's giving group (Family)
                    qry = qry.Where(t => t.AuthorizedPersonAlias.Person.GivingId == _person.GivingId);
                }

                // 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
                var accountIds = (gfTransactions.GetUserPreference("Account") ?? "").SplitDelimitedValues().AsIntegerList().Where(a => a > 0).ToList();
                {
                    if (accountIds.Any())
                    {
                        qry = qry.Where(t => t.TransactionDetails.Any(d => accountIds.Contains(d.AccountId) || (d.Account.ParentAccountId.HasValue && accountIds.Contains(d.Account.ParentAccountId.Value))));
                    }
                }

                // 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.FinancialPaymentDetail != null && t.FinancialPaymentDetail.CurrencyTypeValueId == currencyTypeId);
                }

                // Credit Card Type
                int creditCardTypeId = int.MinValue;
                if (int.TryParse(gfTransactions.GetUserPreference("Credit Card Type"), out creditCardTypeId))
                {
                    qry = qry.Where(t => t.FinancialPaymentDetail != null && t.FinancialPaymentDetail.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
            {
                // Default sort by Id if the transations are seen via the batch,
                // otherwise sort by descending date time.
                if (ContextTypesRequired.Any(e => e.Id == batchEntityTypeId))
                {
                    qry = qry.OrderBy(t => t.Id);
                }
                else
                {
                    qry = qry.OrderByDescending(t => t.TransactionDateTime).ThenByDescending(t => t.Id);
                }
            }

            var lTransactionImageField = gTransactions.ColumnsOfType <RockLiteralField>().FirstOrDefault(a => a.ID == "lTransactionImage");
            var summaryField           = gTransactions.ColumnsOfType <RockBoundField>().FirstOrDefault(a => a.DataField == "Summary");
            var showImages             = bddlOptions.SelectedValue.AsIntegerOrNull() == 1;

            if (lTransactionImageField != null)
            {
                lTransactionImageField.Visible = showImages;
            }

            if (summaryField != null)
            {
                summaryField.Visible = !showImages;
            }

            if (showImages)
            {
                qry = qry.Include(a => a.Images);
            }

            gTransactions.SetLinqDataSource(qry.AsNoTracking());
            gTransactions.DataBind();

            if (_batch == null &&
                _scheduledTxn == null &&
                _registration == null &&
                _person == null)
            {
                pnlSummary.Visible = true;

                // No context - show account summary
                var qryTransactionDetails = qry.SelectMany(a => a.TransactionDetails);
                var qryFinancialAccount   = new FinancialAccountService(rockContext).Queryable();
                var accountSummaryQry     = qryTransactionDetails.GroupBy(a => a.AccountId).Select(a => new
                {
                    AccountId   = a.Key,
                    TotalAmount = (decimal?)a.Sum(d => d.Amount)
                }).Join(qryFinancialAccount, k1 => k1.AccountId, k2 => k2.Id, (td, fa) => new { td.TotalAmount, fa.Name, fa.Order })
                                            .OrderBy(a => a.Order);

                var summaryList      = accountSummaryQry.ToList();
                var grandTotalAmount = (summaryList.Count > 0) ? summaryList.Sum(a => a.TotalAmount ?? 0) : 0;
                lGrandTotal.Text             = grandTotalAmount.FormatAsCurrency();
                rptAccountSummary.DataSource = summaryList.Select(a => new { a.Name, TotalAmount = a.TotalAmount.FormatAsCurrency() }).ToList();
                rptAccountSummary.DataBind();
            }
            else
            {
                pnlSummary.Visible = false;
            }
        }