public IEnumerable <ControlTotalResult> GetControlTotals(System.Web.Http.OData.Query.ODataQueryOptions <FinancialBatch> queryOptions = null)
        {
            var financialBatchQuery = new FinancialBatchService(this.Service.Context as Rock.Data.RockContext).Queryable();

            financialBatchQuery = queryOptions.ApplyTo(financialBatchQuery) as IOrderedQueryable <FinancialBatch>;

            var batchControlTotalsQuery = financialBatchQuery.SelectMany(a => a.Transactions).Where(a => a.BatchId.HasValue).GroupBy(a => a.BatchId.Value).Select(a => new
            {
                BatchId = a.Key,
                TransactionTotalAmounts = a.Select(x => x.TransactionDetails.Sum(d => (decimal?)d.Amount))
            });

            var batchControlTotalsList = batchControlTotalsQuery.ToList();

            var controlTotalsList = batchControlTotalsList.Select(a => new ControlTotalResult
            {
                FinancialBatchId   = a.BatchId,
                ControlTotalCount  = a.TransactionTotalAmounts.Count(),
                ControlTotalAmount = a.TransactionTotalAmounts.Sum(x => (decimal?)x) ?? 0
            }).ToList();

            return(controlTotalsList);
        }
        public IQueryable GetByLocation(int geofenceGroupTypeId, int groupTypeId, int locationId, System.Web.Http.OData.Query.ODataQueryOptions <Group> queryOptions)
        {
            var fenceGroups = new List <Group>();

            // Get the location record
            var rockContext = (RockContext)Service.Context;
            var location    = new LocationService(rockContext).Get(locationId);

            // If location was valid and address was geocoded successfully
            if (location != null && location.GeoPoint != null)
            {
                // Find all the groupLocation records ( belonging to groups of the "geofenceGroupType" )
                // where the geofence surrounds the location
                var groupLocationService = new GroupLocationService(rockContext);
                foreach (var fenceGroupLocation in groupLocationService
                         .Queryable("Group,Location").AsNoTracking()
                         .Where(gl =>
                                gl.Group.GroupTypeId == geofenceGroupTypeId &&
                                gl.Location.GeoFence != null &&
                                location.GeoPoint.Intersects(gl.Location.GeoFence))
                         .ToList())
                {
                    var fenceGroup = fenceGroups.FirstOrDefault(g => g.Id == fenceGroupLocation.GroupId);
                    if (fenceGroup == null)
                    {
                        fenceGroup = fenceGroupLocation.Group;
                        fenceGroups.Add(fenceGroup);
                    }
                    fenceGroupLocation.Group = null;

                    // Find all the group groupLocation records ( with group of the "groupTypeId" ) that have a location
                    // within the fence
                    foreach (var group in Service
                             .Queryable("Schedule,GroupLocations.Location").AsNoTracking()
                             .Where(g =>
                                    g.GroupTypeId == groupTypeId &&
                                    g.GroupLocations.Any(gl =>
                                                         gl.Location.GeoPoint != null &&
                                                         gl.Location.GeoPoint.Intersects(fenceGroupLocation.Location.GeoFence))))
                    {
                        // Remove any other group locations that do not belong to fence
                        foreach (var gl in group.GroupLocations.ToList())
                        {
                            if (gl.Location.GeoPoint == null ||
                                !gl.Location.GeoPoint.Intersects(fenceGroupLocation.Location.GeoFence))
                            {
                                group.GroupLocations.Remove(gl);
                            }
                            else
                            {
                                // Calculate distance
                                double meters = gl.Location.GeoPoint.Distance(location.GeoPoint) ?? 0.0D;
                                gl.Location.SetDistance(meters * Location.MilesPerMeter);
                            }
                        }

                        fenceGroup.Groups.Add(group);
                    }
                }
            }

            // manually apply any OData parameters to the InMemory Query
            var qryResults = queryOptions.ApplyTo(fenceGroups.AsQueryable());

            return(qryResults);
        }
        /// <summary>
        /// Gets the collection of records for the requested entity set and specified OData query options.
        /// </summary>
        /// <param name="model"><see cref="IEdmModel"/></param>
        /// <param name="entitySetName">Name of the entity set</param>
        /// <param name="queryOptions"><see cref="System.Web.Http.OData.Query.ODataQueryOptions"/></param>
        /// <param name="querySettings"><see cref="System.Web.Http.OData.Query.ODataQuerySettings"/></param>
        /// <param name="request"><see cref="HttpRequestMessage"/></param>
        /// <returns><see cref="EdmEntityObjectCollection"/></returns>
        public virtual EdmEntityObjectCollection SelectMultiple(IEdmModel model, string entitySetName, System.Web.Http.OData.Query.ODataQueryOptions queryOptions, System.Web.Http.OData.Query.ODataQuerySettings querySettings, HttpRequestMessage request)
        {
            var edmEntitySchemaType             = model.FindDeclaredType(string.Format("{0}.{1}", NamespaceName, entitySetName));
            var edmEntityType                   = edmEntitySchemaType as IEdmEntityType;
            var entityListIdProperty            = edmEntityType.FindProperty("list-id") as IEdmStructuralProperty;
            var entityListIdString              = entityListIdProperty.DefaultValueString;
            var viewIdProperty                  = edmEntityType.FindProperty("view-id") as IEdmStructuralProperty;
            var viewIdString                    = viewIdProperty.DefaultValueString;
            var entityPermissionEnabledProperty =
                edmEntityType.FindProperty("entity-permissions-enabled") as IEdmStructuralProperty;
            bool entityPermissionsEnabled;

            bool.TryParse(entityPermissionEnabledProperty.DefaultValueString, out entityPermissionsEnabled);

            Guid viewId;

            Guid.TryParse(viewIdString, out viewId);
            var view        = GetView(viewId);
            var viewColumns = view.Columns.ToList();
            var fetch       = Fetch.Parse(view.FetchXml);

            queryOptions.ApplyTo(querySettings, fetch);
            var serviceContext = this.Dependencies.GetServiceContext();

            // If Entity Permissions on the view was enabled then restrict add entity Permissions to Fetch
            if (entityPermissionsEnabled)
            {
                var crmEntityPermissionProvider = new CrmEntityPermissionProvider();
                var perm = crmEntityPermissionProvider.TryApplyRecordLevelFiltersToFetch(
                    serviceContext,
                    CrmEntityPermissionRight.Read,
                    fetch);

                // Ensure the user has permissions to request read access to the entity.
                if (!perm.PermissionGranted && !perm.GlobalPermissionGranted)
                {
                    ADXTrace.Instance.TraceWarning(
                        TraceCategory.Exception,
                        string.Format(
                            "Access to oData, with the entity set name of '{0}', has been denied for the user with the following webroles: '{1}' due to entity permissions. Grant the webroles read access to the entityset if this was not an error.",
                            entitySetName,
                            string.Join("', '", crmEntityPermissionProvider.CurrentUserRoleNames)));
                    throw new SecurityAccessDeniedException();
                }
            }

            var dataSchemaType          = model.FindDeclaredType(string.Format("{0}.{1}", NamespaceName, entitySetName));
            var dataEntityType          = dataSchemaType as IEdmEntityType;
            var dataEntityTypeReference = new EdmEntityTypeReference(dataEntityType, true);
            var collection = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(dataEntityTypeReference), true));
            var entityReferenceSchemaType           = model.FindDeclaredType(string.Format("{0}.{1}", NamespaceName, "EntityReference"));
            var entityReferenceComplexType          = entityReferenceSchemaType as IEdmComplexType;
            var entityReferenceComplexTypeReference = new EdmComplexTypeReference(entityReferenceComplexType, true);
            var optionSetSchemaType           = model.FindDeclaredType(string.Format("{0}.{1}", NamespaceName, "OptionSet"));
            var optionSetComplexType          = optionSetSchemaType as IEdmComplexType;
            var optionSetComplexTypeReference = new EdmComplexTypeReference(optionSetComplexType, true);
            var entityCollection = fetch.Execute(serviceContext as IOrganizationService);

            if (entityCollection == null)
            {
                return(collection);
            }

            var records = entityCollection.Entities;

            foreach (var record in records)
            {
                var entityObject = BuildEdmEntityObject(record, view, viewColumns, dataEntityTypeReference, entityReferenceComplexTypeReference, optionSetComplexTypeReference, entityListIdString, viewIdString);
                collection.Add(entityObject);
            }

            if (entityCollection.MoreRecords && querySettings.PageSize.HasValue && querySettings.PageSize > 0)
            {
                var nextPageLink = ODataQueryOptionExtensions.GetNextPageLink(request, querySettings.PageSize.Value);
                request.SetNextPageLink(nextPageLink);
            }

            if (entityCollection.TotalRecordCount > 0)
            {
                request.SetInlineCount(entityCollection.TotalRecordCount);
            }

            return(collection);
        }