Esempio n. 1
0
        public void RenderPage()
        {
            var                   pairs                 = GetAttributeValue("GroupIds").ToKeyValuePairList();
            RockContext           rockContext           = new RockContext();
            GroupMemberService    groupMemberService    = new GroupMemberService(rockContext);
            AttributeValueService attributeValueService = new AttributeValueService(rockContext);
            List <GroupMember>    members               = new List <GroupMember>();

            foreach (var pair in pairs)
            {
                var volunteerId = pair.Key.AsInteger();
                var volunteers  = groupMemberService.GetByGroupIdAndPersonId(volunteerId, CurrentPerson.Id);
                foreach (var volunteer in volunteers)
                {
                    //Add the volunteer and any other volunteers
                    members.Add(volunteer);
                    volunteer.LoadAttributes();
                    var campGroupName   = volunteer.GetAttributeValue(GetAttributeValue("VolunteerGroupKey"));
                    var otherVolunteers = groupMemberService.GetByGroupId(volunteer.GroupId);
                    var volunterGroupAttributeValues = attributeValueService.GetByAttributeId(volunteer.Attributes[GetAttributeValue("VolunteerGroupKey")].Id).ToList();

                    foreach (var otherVolunteer in otherVolunteers)
                    {
                        if (volunterGroupAttributeValues.Where(av => av.EntityId == otherVolunteer.Id && av.Value == campGroupName).Any())
                        {
                            members.Add(otherVolunteer);
                        }
                    }

                    //Now add matching students
                    var students = groupMemberService.GetByGroupId((( string )pair.Value).AsInteger());
                    if (students.Any())
                    {
                        var firstStudent = students.FirstOrDefault();
                        firstStudent.LoadAttributes();
                        var studentGroupAttributeValues = attributeValueService.GetByAttributeId(firstStudent.Attributes[GetAttributeValue("StudentGroupKey")].Id).ToList();
                        foreach (var student in students)
                        {
                            if (studentGroupAttributeValues.Where(av => av.EntityId == student.Id && av.Value == campGroupName).Any())
                            {
                                members.Add(student);
                            }
                        }
                    }
                }
            }
            members = members.DistinctBy(gm => gm.PersonId).ToList();
            var enabledCommands = GetAttributeValue("EnabledLavaCommands");
            var mergeObjects    = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, CurrentPerson);

            mergeObjects.Add("GroupMembers", members);
            ltOutput.Text = GetAttributeValue("Lava").ResolveMergeFields(mergeObjects, enabledCommands);
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, Model.WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.Families.Where(f => f.Selected).FirstOrDefault();
                if (family != null)
                {
                    var service = new GroupMemberService(rockContext);
                    foreach (var groupMember in service.GetByGroupId(family.Group.Id).AsNoTracking().ToList())
                    {
                        if (!family.People.Any(p => p.Person.Id == groupMember.PersonId))
                        {
                            var person = new CheckInPerson();
                            person.Person       = groupMember.Person.Clone(false);
                            person.FamilyMember = true;
                            family.People.Add(person);
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Processes for family.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="family">The family.</param>
        /// <param name="preventInactivePeople">if set to <c>true</c> [prevent inactive people]. Usually get from CurrentCheckInState.CheckInType.PreventInactivePeople</param>
        /// <returns></returns>
        public static bool ProcessForFamily(RockContext rockContext, CheckInFamily family, bool preventInactivePeople)
        {
            var service = new GroupMemberService(rockContext);

            var people = service.GetByGroupId(family.Group.Id).AsNoTracking();

            if (preventInactivePeople)
            {
                var dvInactive = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE.AsGuid());
                if (dvInactive != null)
                {
                    people = people.Where(m => m.Person.RecordStatusValueId != dvInactive.Id);
                }
            }

            foreach (var groupMember in people.ToList())
            {
                if (!family.People.Any(p => p.Person.Id == groupMember.PersonId))
                {
                    var person = new CheckInPerson();
                    person.Person       = groupMember.Person.Clone(false);
                    person.FamilyMember = true;
                    family.People.Add(person);
                }
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, Model.WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.CurrentFamily;
                if (family != null)
                {
                    var service = new GroupMemberService(rockContext);

                    var people = service.GetByGroupId(family.Group.Id).AsNoTracking();
                    if (checkInState.CheckInType != null && checkInState.CheckInType.PreventInactivePeopele)
                    {
                        var dvInactive = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE.AsGuid());
                        if (dvInactive != null)
                        {
                            people = people.Where(m => m.Person.RecordStatusValueId != dvInactive.Id);
                        }
                    }

                    foreach (var groupMember in people.ToList())
                    {
                        if (!family.People.Any(p => p.Person.Id == groupMember.PersonId))
                        {
                            var person = new CheckInPerson();
                            person.Person       = groupMember.Person.Clone(false);
                            person.FamilyMember = true;
                            family.People.Add(person);
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }
Esempio n. 5
0
        public DataSet GetContributionTransactions(int groupId, int?personId, [FromBody] ContributionStatementOptions options)
        {
            var qry = Get().Where(a => a.TransactionDateTime >= options.StartDate);

            if (options.EndDate.HasValue)
            {
                qry = qry.Where(a => a.TransactionDateTime < options.EndDate.Value);
            }

            var transactionTypeContribution = Rock.Web.Cache.DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid());

            if (transactionTypeContribution != null)
            {
                int transactionTypeContributionId = transactionTypeContribution.Id;
                qry = qry.Where(a => a.TransactionTypeValueId == transactionTypeContributionId);
            }

            if (personId.HasValue)
            {
                // get transactions for a specific person
                qry = qry.Where(a => a.AuthorizedPersonAlias.PersonId == personId.Value);
            }
            else
            {
                // get transactions for all the persons in the specified group that have specified that group as their GivingGroup
                GroupMemberService groupMemberService = new GroupMemberService(( RockContext )Service.Context);
                var personIdList = groupMemberService.GetByGroupId(groupId).Where(a => a.Person.GivingGroupId == groupId).Select(s => s.PersonId).ToList();

                qry = qry.Where(a => personIdList.Contains(a.AuthorizedPersonAlias.PersonId));
            }

            if (options.AccountIds != null)
            {
                qry = qry.Where(a => a.TransactionDetails.Any(x => options.AccountIds.Contains(x.AccountId)));
            }

            var selectQry = qry.Select(a => new
            {
                a.TransactionDateTime,
                CurrencyTypeValueName = a.FinancialPaymentDetail != null ? a.FinancialPaymentDetail.CurrencyTypeValue.Value : string.Empty,
                a.Summary,
                Details = a.TransactionDetails.Select(d => new
                {
                    d.AccountId,
                    AccountName = d.Account.Name,
                    d.Summary,
                    d.Amount
                }).OrderBy(x => x.AccountName),
            }).OrderBy(a => a.TransactionDateTime);

            DataTable dataTable = new DataTable("contribution_transactions");

            dataTable.Columns.Add("TransactionDateTime", typeof(DateTime));
            dataTable.Columns.Add("CurrencyTypeValueName");
            dataTable.Columns.Add("Summary");
            dataTable.Columns.Add("Amount", typeof(decimal));
            dataTable.Columns.Add("Details", typeof(DataTable));

            var list = selectQry.ToList();

            dataTable.BeginLoadData();
            foreach (var fieldItems in list)
            {
                DataTable detailTable = new DataTable("transaction_details");
                detailTable.Columns.Add("AccountId", typeof(int));
                detailTable.Columns.Add("AccountName");
                detailTable.Columns.Add("Summary");
                detailTable.Columns.Add("Amount", typeof(decimal));
                var transactionDetails = fieldItems.Details.ToList();

                // remove any Accounts that were not included (in case there was a mix of included and not included accounts in the transaction)
                if (options.AccountIds != null)
                {
                    transactionDetails = transactionDetails.Where(a => options.AccountIds.Contains(a.AccountId)).ToList();
                }

                foreach (var detail in transactionDetails)
                {
                    var detailArray = new object[] {
                        detail.AccountId,
                        detail.AccountName,
                        detail.Summary,
                        detail.Amount
                    };

                    detailTable.Rows.Add(detailArray);
                }

                var itemArray = new object[] {
                    fieldItems.TransactionDateTime,
                    fieldItems.CurrencyTypeValueName,
                    fieldItems.Summary,
                    transactionDetails.Sum(a => a.Amount),
                    detailTable
                };

                dataTable.Rows.Add(itemArray);
            }

            dataTable.EndLoadData();

            DataSet dataSet = new DataSet();

            dataSet.Tables.Add(dataTable);

            return(dataSet);
        }
Esempio n. 6
0
        public StatementGeneratorRecipientResult GetStatementGeneratorRecipientResult(int groupId, int?personId, Guid?locationGuid, [FromBody] StatementGeneratorOptions options)
        {
            if (options == null)
            {
                throw new Exception("StatementGenerationOption options must be specified");
            }

            if (options.LayoutDefinedValueGuid == null)
            {
                throw new Exception("LayoutDefinedValueGuid option must be specified");
            }

            var result = new StatementGeneratorRecipientResult();

            result.GroupId  = groupId;
            result.PersonId = personId;

            using (var rockContext = new RockContext())
            {
                var financialTransactionQry = this.GetFinancialTransactionQuery(options, rockContext, false);
                var financialPledgeQry      = GetFinancialPledgeQuery(options, rockContext, false);

                var    personList = new List <Person>();
                Person person     = null;
                if (personId.HasValue)
                {
                    person = new PersonService(rockContext).Queryable().Include(a => a.Aliases).Where(a => a.Id == personId.Value).FirstOrDefault();
                    personList.Add(person);
                }
                else
                {
                    // get transactions for all the persons in the specified group that have specified that group as their GivingGroup
                    GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                    personList = groupMemberService.GetByGroupId(groupId).Where(a => a.Person.GivingGroupId == groupId).Select(s => s.Person).Include(a => a.Aliases).ToList();
                    person     = personList.FirstOrDefault();
                }

                if (options.ExcludeOptedOutIndividuals == true && !options.DataViewId.HasValue)
                {
                    int?doNotSendGivingStatementAttributeId = AttributeCache.Get(Rock.StatementGenerator.SystemGuid.Attribute.PERSON_DO_NOT_SEND_GIVING_STATEMENT.AsGuid())?.Id;
                    if (doNotSendGivingStatementAttributeId.HasValue)
                    {
                        var personIds         = personList.Select(a => a.Id).ToList();
                        var optedOutPersonQry = new AttributeValueService(rockContext).Queryable().Where(a => a.AttributeId == doNotSendGivingStatementAttributeId);
                        if (personIds.Count == 1)
                        {
                            int entityPersonId = personIds[0];
                            optedOutPersonQry = optedOutPersonQry.Where(a => a.EntityId == entityPersonId);
                        }
                        else
                        {
                            optedOutPersonQry = optedOutPersonQry.Where(a => personIds.Contains(a.EntityId.Value));
                        }

                        var optedOutPersonIds = optedOutPersonQry
                                                .Select(a => new
                        {
                            PersonId = a.EntityId.Value,
                            a.Value
                        }).ToList().Where(a => a.Value.AsBoolean() == true).Select(a => a.PersonId).ToList();

                        if (optedOutPersonIds.Any())
                        {
                            bool givingLeaderOptedOut = personList.Any(a => optedOutPersonIds.Contains(a.Id) && a.GivingLeaderId == a.Id);

                            var remaingPersonIds = personList.Where(a => !optedOutPersonIds.Contains(a.Id)).ToList();

                            if (givingLeaderOptedOut || !remaingPersonIds.Any())
                            {
                                // If the giving leader opted out, or if there aren't any people in the giving statement that haven't opted out, return NULL and OptedOut = true
                                result.OptedOut = true;
                                result.Html     = null;
                                return(result);
                            }
                        }
                    }
                }

                var personAliasIds = personList.SelectMany(a => a.Aliases.Select(x => x.Id)).ToList();
                if (personAliasIds.Count == 1)
                {
                    var personAliasId = personAliasIds[0];
                    financialTransactionQry = financialTransactionQry.Where(a => a.AuthorizedPersonAliasId.Value == personAliasId);
                }
                else
                {
                    financialTransactionQry = financialTransactionQry.Where(a => personAliasIds.Contains(a.AuthorizedPersonAliasId.Value));
                }

                var financialTransactionsList = financialTransactionQry
                                                .Include(a => a.FinancialPaymentDetail)
                                                .Include(a => a.FinancialPaymentDetail.CurrencyTypeValue)
                                                .Include(a => a.TransactionDetails)
                                                .Include(a => a.TransactionDetails.Select(x => x.Account))
                                                .OrderBy(a => a.TransactionDateTime).ToList();

                foreach (var financialTransaction in financialTransactionsList)
                {
                    if (options.TransactionAccountIds != null)
                    {
                        // remove any Accounts that were not included (in case there was a mix of included and not included accounts in the transaction)
                        financialTransaction.TransactionDetails = financialTransaction.TransactionDetails.Where(a => options.TransactionAccountIds.Contains(a.AccountId)).ToList();
                    }

                    financialTransaction.TransactionDetails = financialTransaction.TransactionDetails.OrderBy(a => a.Account.Order).ThenBy(a => a.Account.Name).ToList();
                }

                var lavaTemplateValue      = DefinedValueCache.Get(options.LayoutDefinedValueGuid.Value);
                var lavaTemplateLava       = lavaTemplateValue.GetAttributeValue("LavaTemplate");
                var lavaTemplateFooterLava = lavaTemplateValue.GetAttributeValue("FooterHtml");

                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null, null, new Lava.CommonMergeFieldsOptions {
                    GetLegacyGlobalMergeFields = false, GetDeviceFamily = false, GetOSFamily = false, GetPageContext = false, GetPageParameters = false, GetCampuses = true, GetCurrentPerson = true
                });
                mergeFields.Add("LavaTemplate", lavaTemplateValue);

                mergeFields.Add("PersonList", personList);
                mergeFields.Add("StatementStartDate", options.StartDate);
                var humanFriendlyEndDate = options.EndDate.HasValue ? options.EndDate.Value.AddDays(-1) : RockDateTime.Now.Date;
                mergeFields.Add("StatementEndDate", humanFriendlyEndDate);

                var familyTitle = Rock.Data.RockUdfHelper.ufnCrm_GetFamilyTitle(rockContext, personId, groupId, null, false, !options.ExcludeInActiveIndividuals);

                mergeFields.Add("Salutation", familyTitle);

                Location mailingAddress;

                if (locationGuid.HasValue)
                {
                    // get the location that was specified for the recipient
                    mailingAddress = new LocationService(rockContext).Get(locationGuid.Value);
                }
                else
                {
                    // for backwards compatibility, get the first address
                    IQueryable <GroupLocation> groupLocationsQry = GetGroupLocationQuery(rockContext);
                    mailingAddress = groupLocationsQry.Where(a => a.GroupId == groupId).Select(a => a.Location).FirstOrDefault();
                }

                mergeFields.Add("MailingAddress", mailingAddress);

                if (mailingAddress != null)
                {
                    mergeFields.Add("StreetAddress1", mailingAddress.Street1);
                    mergeFields.Add("StreetAddress2", mailingAddress.Street2);
                    mergeFields.Add("City", mailingAddress.City);
                    mergeFields.Add("State", mailingAddress.State);
                    mergeFields.Add("PostalCode", mailingAddress.PostalCode);
                    mergeFields.Add("Country", mailingAddress.Country);
                }
                else
                {
                    mergeFields.Add("StreetAddress1", string.Empty);
                    mergeFields.Add("StreetAddress2", string.Empty);
                    mergeFields.Add("City", string.Empty);
                    mergeFields.Add("State", string.Empty);
                    mergeFields.Add("PostalCode", string.Empty);
                    mergeFields.Add("Country", string.Empty);
                }

                var transactionDetailListAll = financialTransactionsList.SelectMany(a => a.TransactionDetails).ToList();

                if (options.HideRefundedTransactions && transactionDetailListAll.Any(a => a.Amount < 0))
                {
                    var allRefunds = transactionDetailListAll.SelectMany(a => a.Transaction.Refunds).ToList();
                    foreach (var refund in allRefunds)
                    {
                        foreach (var refundedOriginalTransactionDetail in refund.OriginalTransaction.TransactionDetails)
                        {
                            // remove the refund's original TransactionDetails from the results
                            if (transactionDetailListAll.Contains(refundedOriginalTransactionDetail))
                            {
                                transactionDetailListAll.Remove(refundedOriginalTransactionDetail);
                                foreach (var refundDetailId in refund.FinancialTransaction.TransactionDetails.Select(a => a.Id))
                                {
                                    // remove the refund's transaction from the results
                                    var refundDetail = transactionDetailListAll.FirstOrDefault(a => a.Id == refundDetailId);
                                    if (refundDetail != null)
                                    {
                                        transactionDetailListAll.Remove(refundDetail);
                                    }
                                }
                            }
                        }
                    }
                }

                if (options.HideCorrectedTransactions && transactionDetailListAll.Any(a => a.Amount < 0))
                {
                    // Hide transactions that are corrected on the same date. Transactions that have a matching negative dollar amount on the same date and same account will not be shown.

                    // get a list of dates that have at least one negative transaction
                    var transactionsByDateList = transactionDetailListAll.GroupBy(a => a.Transaction.TransactionDateTime.Value.Date).Select(a => new
                    {
                        Date = a.Key,
                        TransactionDetails = a.ToList()
                    })
                                                 .Where(a => a.TransactionDetails.Any(x => x.Amount < 0))
                                                 .ToList();


                    foreach (var transactionsByDate in transactionsByDateList)
                    {
                        foreach (var negativeTransaction in transactionsByDate.TransactionDetails.Where(a => a.Amount < 0))
                        {
                            // find the first transaction that has an amount that matches the negative amount (on the same day and same account)
                            // and make sure the matching transaction doesn't already have a refund associated with it
                            var correctedTransactionDetail = transactionsByDate.TransactionDetails
                                                             .Where(a => (a.Amount == (-negativeTransaction.Amount) && a.AccountId == negativeTransaction.AccountId) && !a.Transaction.Refunds.Any())
                                                             .FirstOrDefault();
                            if (correctedTransactionDetail != null)
                            {
                                // if the transaction was corrected, remove it, and also remove the associated correction (the negative one) transaction
                                transactionDetailListAll.Remove(correctedTransactionDetail);
                                transactionDetailListAll.Remove(negativeTransaction);
                            }
                        }
                    }
                }

                List <FinancialTransactionDetail> transactionDetailListCash    = transactionDetailListAll;
                List <FinancialTransactionDetail> transactionDetailListNonCash = new List <FinancialTransactionDetail>();

                if (options.CurrencyTypeIdsCash != null)
                {
                    // NOTE: if there isn't a FinancialPaymentDetail record, assume it is Cash
                    transactionDetailListCash = transactionDetailListCash.Where(a =>
                                                                                (a.Transaction.FinancialPaymentDetailId == null) ||
                                                                                (a.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.HasValue && options.CurrencyTypeIdsCash.Contains(a.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.Value))).ToList();
                }

                if (options.CurrencyTypeIdsNonCash != null)
                {
                    transactionDetailListNonCash = transactionDetailListAll.Where(a =>
                                                                                  a.Transaction.FinancialPaymentDetailId.HasValue &&
                                                                                  a.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.HasValue &&
                                                                                  options.CurrencyTypeIdsNonCash.Contains(a.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.Value)).ToList();
                }

                // Add Merge Fields for Transactions for custom Statements that might want to organize the output by Transaction instead of TransactionDetail
                var transactionListCash    = transactionDetailListCash.GroupBy(a => a.Transaction).Select(a => a.Key).ToList();
                var transactionListNonCash = transactionDetailListNonCash.GroupBy(a => a.Transaction).Select(a => a.Key).ToList();
                mergeFields.Add("Transactions", transactionListCash);
                mergeFields.Add("TransactionsNonCash", transactionListNonCash);

                // Add the standard TransactionDetails and TransactionDetailsNonCash that the default Rock templates use
                mergeFields.Add("TransactionDetails", transactionDetailListCash);
                mergeFields.Add("TransactionDetailsNonCash", transactionDetailListNonCash);

                mergeFields.Add("TotalContributionAmount", transactionDetailListCash.Sum(a => a.Amount));
                mergeFields.Add("TotalContributionCount", transactionDetailListCash.Count());

                mergeFields.Add("TotalContributionAmountNonCash", transactionDetailListNonCash.Sum(a => a.Amount));
                mergeFields.Add("TotalContributionCountNonCash", transactionDetailListNonCash.Count());

                mergeFields.Add("AccountSummary", transactionDetailListCash.GroupBy(t => t.Account.Name).Select(s => new { AccountName = s.Key, Total = s.Sum(a => a.Amount), Order = s.Max(a => a.Account.Order) }).OrderBy(s => s.Order));
                mergeFields.Add("AccountSummaryNonCash", transactionDetailListNonCash.GroupBy(t => t.Account.Name).Select(s => new { AccountName = s.Key, Total = s.Sum(a => a.Amount), Order = s.Max(a => a.Account.Order) }).OrderBy(s => s.Order));

                if (options.PledgesAccountIds.Any())
                {
                    var pledgeList = financialPledgeQry
                                     .Where(p => p.PersonAliasId.HasValue && personAliasIds.Contains(p.PersonAliasId.Value))
                                     .Include(a => a.Account)
                                     .OrderBy(a => a.Account.Order)
                                     .ThenBy(a => a.Account.PublicName)
                                     .ToList();

                    var pledgeSummaryByPledgeList = pledgeList
                                                    .Select(p => new
                    {
                        p.Account,
                        Pledge = p
                    })
                                                    .ToList();

                    //// Pledges but organized by Account (in case more than one pledge goes to the same account)
                    //// NOTE: In the case of multiple pledges to the same account (just in case they accidently or intentionally had multiple pledges to the same account)
                    ////  -- Date Range
                    ////    -- StartDate: Earliest StartDate of all the pledges for that account
                    ////    -- EndDate: Lastest EndDate of all the pledges for that account
                    ////  -- Amount Pledged: Sum of all Pledges to that account
                    ////  -- Amount Given:
                    ////    --  The sum of transaction amounts to that account between
                    ////      -- Start Date: Earliest Start Date of all the pledges to that account
                    ////      -- End Date: Whatever is earlier (Statement End Date or Pledges' End Date)
                    var pledgeSummaryList = pledgeSummaryByPledgeList.GroupBy(a => a.Account).Select(a => new PledgeSummary
                    {
                        Account    = a.Key,
                        PledgeList = a.Select(x => x.Pledge).ToList()
                    }).ToList();

                    // add detailed pledge information
                    if (pledgeSummaryList.Any())
                    {
                        int statementPledgeYear = options.StartDate.Value.Year;

                        List <int> pledgeCurrencyTypeIds = null;
                        if (options.CurrencyTypeIdsCash != null)
                        {
                            pledgeCurrencyTypeIds = options.CurrencyTypeIdsCash;
                            if (options.PledgesIncludeNonCashGifts && options.CurrencyTypeIdsNonCash != null)
                            {
                                pledgeCurrencyTypeIds = options.CurrencyTypeIdsCash.Union(options.CurrencyTypeIdsNonCash).ToList();
                            }
                        }

                        foreach (var pledgeSummary in pledgeSummaryList)
                        {
                            DateTime adjustedPledgeEndDate = pledgeSummary.PledgeEndDate.Value.Date;
                            if (pledgeSummary.PledgeEndDate.Value.Date < DateTime.MaxValue.Date)
                            {
                                adjustedPledgeEndDate = pledgeSummary.PledgeEndDate.Value.Date.AddDays(1);
                            }

                            if (options.EndDate.HasValue)
                            {
                                if (adjustedPledgeEndDate > options.EndDate.Value)
                                {
                                    adjustedPledgeEndDate = options.EndDate.Value;
                                }
                            }

                            var pledgeFinancialTransactionDetailQry = new FinancialTransactionDetailService(rockContext).Queryable().Where(t =>
                                                                                                                                           t.Transaction.AuthorizedPersonAliasId.HasValue && personAliasIds.Contains(t.Transaction.AuthorizedPersonAliasId.Value) &&
                                                                                                                                           t.Transaction.TransactionDateTime >= pledgeSummary.PledgeStartDate &&
                                                                                                                                           t.Transaction.TransactionDateTime < adjustedPledgeEndDate);

                            if (options.PledgesIncludeChildAccounts)
                            {
                                // If PledgesIncludeChildAccounts = true, we'll include transactions to those child accounts as part of the pledge (but only one level deep)
                                pledgeFinancialTransactionDetailQry = pledgeFinancialTransactionDetailQry.Where(t =>
                                                                                                                t.AccountId == pledgeSummary.AccountId
                                                                                                                ||
                                                                                                                (t.Account.ParentAccountId.HasValue && t.Account.ParentAccountId == pledgeSummary.AccountId)
                                                                                                                );
                            }
                            else
                            {
                                pledgeFinancialTransactionDetailQry = pledgeFinancialTransactionDetailQry.Where(t => t.AccountId == pledgeSummary.AccountId);
                            }

                            if (pledgeCurrencyTypeIds != null)
                            {
                                pledgeFinancialTransactionDetailQry = pledgeFinancialTransactionDetailQry.Where(t =>
                                                                                                                t.Transaction.FinancialPaymentDetailId.HasValue &&
                                                                                                                t.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.HasValue && pledgeCurrencyTypeIds.Contains(t.Transaction.FinancialPaymentDetail.CurrencyTypeValueId.Value));
                            }

                            pledgeSummary.AmountGiven = pledgeFinancialTransactionDetailQry.Sum(t => ( decimal? )t.Amount) ?? 0;
                        }
                    }

                    // Pledges ( organized by each Account in case an account is used by more than one pledge )
                    mergeFields.Add("Pledges", pledgeSummaryList);
                }

                mergeFields.Add("Options", options);

                var currentPerson = this.GetPerson();
                result.Html = lavaTemplateLava.ResolveMergeFields(mergeFields, currentPerson);
                if (!string.IsNullOrEmpty(lavaTemplateFooterLava))
                {
                    result.FooterHtml = lavaTemplateFooterLava.ResolveMergeFields(mergeFields, currentPerson);
                }

                result.Html = result.Html.Trim();
            }

            return(result);
        }
Esempio n. 7
0
        public DataSet GetContributionTransactions(int groupId, int?personId, [FromBody] Rock.Net.RestParameters.ContributionStatementOptions options)
        {
            var user = CurrentUser();

            if (user == null)
            {
                // unable to determine user
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            if (!new FinancialTransaction().IsAuthorized("View", user.Person))
            {
                // user can't view FinancialTransactions
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            var qry = Get()
                      .Where(a => a.TransactionDateTime >= options.StartDate)
                      .Where(a => a.TransactionDateTime < (options.EndDate ?? DateTime.MaxValue));

            if (personId.HasValue)
            {
                // get transactions for a specific person
                qry = qry.Where(a => a.AuthorizedPersonId == personId.Value);
            }
            else
            {
                // get transactions for all the persons in the specified group that have specified that group as their GivingGroup
                GroupMemberService groupMemberService = new GroupMemberService();
                var personIdList = groupMemberService.GetByGroupId(groupId).Where(a => a.Person.GivingGroupId == groupId).Select(s => s.PersonId).ToList();

                qry = qry.Where(a => personIdList.Contains(a.AuthorizedPersonId.Value));
            }

            if (options.AccountIds != null)
            {
                qry = qry.Where(a => options.AccountIds.Contains(a.TransactionDetails.FirstOrDefault().AccountId));
            }

            var selectQry = qry.Select(a => new
            {
                a.TransactionDateTime,
                CurrencyTypeValueName = a.CurrencyTypeValue.Name,
                a.Summary,
                Account = a.TransactionDetails.FirstOrDefault().Account,
                a.Amount
            }).OrderBy(a => a.TransactionDateTime);

            DataTable dataTable = new DataTable("contribution_transactions");

            dataTable.Columns.Add("TransactionDateTime", typeof(DateTime));
            dataTable.Columns.Add("CurrencyTypeValueName");
            dataTable.Columns.Add("Summary");
            dataTable.Columns.Add("AccountId", typeof(int));
            dataTable.Columns.Add("AccountName");
            dataTable.Columns.Add("Amount", typeof(decimal));

            var list = selectQry.ToList();

            dataTable.BeginLoadData();
            foreach (var fieldItems in list)
            {
                var itemArray = new object[] {
                    fieldItems.TransactionDateTime,
                    fieldItems.CurrencyTypeValueName,
                    fieldItems.Summary,
                    fieldItems.Account.Id,
                    fieldItems.Account.Name,
                    fieldItems.Amount
                };

                dataTable.Rows.Add(itemArray);
            }

            dataTable.EndLoadData();

            DataSet dataSet = new DataSet();

            dataSet.Tables.Add(dataTable);

            return(dataSet);
        }
        public DataSet GetContributionTransactions(int groupId, int?personId, [FromBody] Rock.Net.RestParameters.ContributionStatementOptions options)
        {
            var qry = Get()
                      .Where(a => a.TransactionDateTime >= options.StartDate)
                      .Where(a => a.TransactionDateTime < (options.EndDate ?? DateTime.MaxValue));

            if (personId.HasValue)
            {
                // get transactions for a specific person
                qry = qry.Where(a => a.AuthorizedPersonAlias.PersonId == personId.Value);
            }
            else
            {
                // get transactions for all the persons in the specified group that have specified that group as their GivingGroup
                GroupMemberService groupMemberService = new GroupMemberService((RockContext)Service.Context);
                var personIdList = groupMemberService.GetByGroupId(groupId).Where(a => a.Person.GivingGroupId == groupId).Select(s => s.PersonId).ToList();

                qry = qry.Where(a => personIdList.Contains(a.AuthorizedPersonAlias.PersonId));
            }

            if (options.AccountIds != null)
            {
                qry = qry.Where(a => options.AccountIds.Contains(a.TransactionDetails.FirstOrDefault().AccountId));
            }

            var selectQry = qry.Select(a => new
            {
                a.TransactionDateTime,
                CurrencyTypeValueName = a.CurrencyTypeValue.Value,
                a.Summary,
                Details = a.TransactionDetails.Select(d => new
                {
                    d.AccountId,
                    AccountName = d.Account.Name,
                    a.Summary,
                    d.Amount
                }).OrderBy(x => x.AccountName),
            }).OrderBy(a => a.TransactionDateTime);

            DataTable dataTable = new DataTable("contribution_transactions");

            dataTable.Columns.Add("TransactionDateTime", typeof(DateTime));
            dataTable.Columns.Add("CurrencyTypeValueName");
            dataTable.Columns.Add("Summary");
            dataTable.Columns.Add("Amount", typeof(decimal));
            dataTable.Columns.Add("Details", typeof(DataTable));

            var list = selectQry.ToList();

            dataTable.BeginLoadData();
            foreach (var fieldItems in list)
            {
                DataTable detailTable = new DataTable("transaction_details");
                detailTable.Columns.Add("AccountId", typeof(int));
                detailTable.Columns.Add("AccountName");
                detailTable.Columns.Add("Summary");
                detailTable.Columns.Add("Amount", typeof(decimal));
                foreach (var detail in fieldItems.Details)
                {
                    var detailArray = new object[] {
                        detail.AccountId,
                        detail.AccountName,
                        detail.Summary,
                        detail.Amount
                    };

                    detailTable.Rows.Add(detailArray);
                }

                var itemArray = new object[] {
                    fieldItems.TransactionDateTime,
                    fieldItems.CurrencyTypeValueName,
                    fieldItems.Summary,
                    fieldItems.Details.Sum(a => a.Amount),
                    detailTable
                };

                dataTable.Rows.Add(itemArray);
            }

            dataTable.EndLoadData();

            DataSet dataSet = new DataSet();

            dataSet.Tables.Add(dataTable);

            return(dataSet);
        }