/// <summary> /// Gets the financial pledge query. /// </summary> /// <param name="options">The options.</param> /// <param name="rockContext">The rock context.</param> /// <param name="usePersonFilters">if set to <c>true</c> [use person filters].</param> /// <returns></returns> private IQueryable <FinancialPledge> GetFinancialPledgeQuery(StatementGeneratorOptions options, RockContext rockContext, bool usePersonFilters) { // pledge information var pledgeQry = new FinancialPledgeService(rockContext).Queryable(); // only include pledges that started *before* the enddate of the statement ( we don't want pledges that start AFTER the statement end date ) if (options.EndDate.HasValue) { pledgeQry = pledgeQry.Where(p => p.StartDate <= options.EndDate.Value); } // also only include pledges that ended *after* the statement start date ( we don't want pledges that ended BEFORE the statement start date ) pledgeQry = pledgeQry.Where(p => p.EndDate >= options.StartDate.Value); // Filter to specified AccountIds (if specified) if (options.PledgesAccountIds == null || !options.PledgesAccountIds.Any()) { // if no PledgeAccountIds where specified, don't include any pledges pledgeQry = pledgeQry.Where(a => false); } else { // NOTE: Only get the Pledges that were specifically pledged to the selected accounts // If the PledgesIncludeChildAccounts = true, we'll include transactions to those child accounts as part of the pledge (but only one level deep) var selectedAccountIds = options.PledgesAccountIds; pledgeQry = pledgeQry.Where(a => a.AccountId.HasValue && selectedAccountIds.Contains(a.AccountId.Value)); } if (usePersonFilters) { if (options.PersonId.HasValue) { // If PersonId is specified, then this statement is for a specific person, so don't do any other filtering string personGivingId = new PersonService(rockContext).Queryable().Where(a => a.Id == options.PersonId.Value).Select(a => a.GivingId).FirstOrDefault(); if (personGivingId != null) { pledgeQry = pledgeQry.Where(a => a.PersonAlias.Person.GivingId == personGivingId); } else { // shouldn't happen, but just in case person doesn't exist pledgeQry = pledgeQry.Where(a => false); } } else { // unless we are using a DataView for filtering, filter based on the IncludeBusiness and ExcludeInActiveIndividuals options if (!options.DataViewId.HasValue) { if (!options.IncludeBusinesses) { int recordTypeValueIdPerson = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_PERSON.AsGuid()).Id; pledgeQry = pledgeQry.Where(a => a.PersonAlias.Person.RecordTypeValueId == recordTypeValueIdPerson); } if (options.ExcludeInActiveIndividuals) { int recordStatusValueIdActive = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid()).Id; pledgeQry = pledgeQry.Where(a => a.PersonAlias.Person.RecordStatusValueId == recordStatusValueIdActive); } // Only include Non-Deceased People even if we are including inactive individuals pledgeQry = pledgeQry.Where(a => a.PersonAlias.Person.IsDeceased == false); } } } return(pledgeQry); }