Пример #1
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            btnExportToShelbyFinancials.Text = GetAttributeValue("ButtonText");
            var monthsBack     = GetAttributeValue("MonthsBack").AsInteger() * -1;
            var firstBatchDate = RockDateTime.Now.AddMonths(monthsBack);

            var batchIdList         = new List <int>();
            var filteredBatchIdList = new List <int>();
            var batchesToExport     = new List <BatchData>();

            using (var rockContextBatches = new RockContext())
            {
                var batchService = new FinancialBatchService(rockContextBatches);
                var qry          = batchService
                                   .Queryable().AsNoTracking()
                                   .Where(b => b.BatchStartDateTime.HasValue)
                                   .Where(b => b.BatchStartDateTime >= firstBatchDate)
                                   .Where(b => b.ControlAmount == b.Transactions.Sum(t => t.TransactionDetails.Sum(d => d.Amount)));

                string dateRangeValue = gfBatchesToExportFilter.GetUserPreference("Date Range");
                if (!string.IsNullOrWhiteSpace(dateRangeValue))
                {
                    var drp = new DateRangePicker();
                    drp.DelimitedValues = dateRangeValue;
                    if (drp.LowerValue.HasValue)
                    {
                        qry = qry.Where(b => b.BatchStartDateTime >= drp.LowerValue.Value);
                    }

                    if (drp.UpperValue.HasValue)
                    {
                        var endOfDay = drp.UpperValue.Value.AddDays(1);
                        qry = qry.Where(b => b.BatchStartDateTime < endOfDay);
                    }
                }

                var status = gfBatchesToExportFilter.GetUserPreference("Status").ConvertToEnumOrNull <BatchStatus>();
                if (status.HasValue)
                {
                    qry = qry.Where(b => b.Status == status);
                }

                SortProperty sortProperty = gBatchesToExport.SortProperty;
                if (sortProperty != null)
                {
                    qry = qry.Sort(sortProperty);
                }
                else
                {
                    qry = qry
                          .OrderByDescending(b => b.BatchStartDateTime)
                          .ThenBy(b => b.Name);
                }

                batchIdList = qry.Select(b => b.Id).ToList();
            }

            using (var rockContextAttributeValues = new RockContext())
            {
                var dateExportedAttributeId = AttributeCache.Get("4B6576DD-82F6-419F-8DF0-467D2636822D".AsGuid()).Id;    //rocks.kfs.ShelbyFinancials.DateExported

                foreach (var batchId in batchIdList)
                {
                    var attributeValue = new AttributeValueService(rockContextAttributeValues).GetByAttributeIdAndEntityId(dateExportedAttributeId, batchId);
                    if (attributeValue == null || attributeValue.ValueAsDateTime == null)
                    {
                        filteredBatchIdList.Add(batchId);
                    }
                }
            }

            using (var rockContextBatchData = new RockContext())
            {
                foreach (var batchId in filteredBatchIdList)
                {
                    var batch = new FinancialBatchService(rockContextBatchData).Get(batchId);

                    batchesToExport.Add(new BatchData
                    {
                        Id = batch.Id,
                        BatchStartDateTime = batch.BatchStartDateTime,
                        Name         = batch.Name,
                        Status       = batch.Status.ToString(),
                        Note         = batch.Note,
                        Transactions = batch.Transactions.Count,
                        Total        = batch.GetTotalTransactionAmount(rockContextBatchData)
                    });
                }
            }

            gBatchesToExport.DataSource   = batchesToExport;
            gBatchesToExport.ObjectList   = ((List <BatchData>)gBatchesToExport.DataSource).ToDictionary(b => b.Id.ToString(), v => v as object);
            gBatchesToExport.EntityTypeId = EntityTypeCache.Get <FinancialBatch>().Id;

            gBatchesToExport.DataBind();
        }