protected void btnExportToFinancialEdge_Click(object sender, EventArgs e)
        {
            if (_financialBatch != null)
            {
                var rockContext = new RockContext();
                var feJournal   = new FEJournal();

                var items = feJournal.GetGlEntries(rockContext, _financialBatch, GetAttributeValue("JournalType"));

                if (items.Count > 0)
                {
                    //
                    // Set session for export file
                    //
                    feJournal.SetFinancialEdgeSessions(items, _financialBatch.Id.ToString());

                    //
                    // vars we need now
                    //
                    var financialBatch = new FinancialBatchService(rockContext).Get(_batchId);
                    var changes        = new History.HistoryChangeList();

                    //
                    // Close Batch if we're supposed to
                    //
                    if (GetAttributeValue("CloseBatch").AsBoolean())
                    {
                        History.EvaluateChange(changes, "Status", financialBatch.Status, BatchStatus.Closed);
                        financialBatch.Status = BatchStatus.Closed;
                    }

                    //
                    // Set Date Exported
                    //
                    financialBatch.LoadAttributes();
                    var oldDate = financialBatch.GetAttributeValue("rocks.kfs.FinancialEdge.DateExported");
                    var newDate = RockDateTime.Now;
                    History.EvaluateChange(changes, "Date Exported", oldDate, newDate.ToString());

                    //
                    // Save the changes
                    //
                    rockContext.WrapTransaction(() =>
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(FinancialBatch),
                                Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(),
                                financialBatch.Id,
                                changes);
                        }
                    });

                    financialBatch.SetAttributeValue("rocks.kfs.FinancialEdge.DateExported", newDate);
                    financialBatch.SaveAttributeValue("rocks.kfs.FinancialEdge.DateExported", rockContext);
                }
            }

            Response.Redirect(Request.RawUrl);
        }
        /// <summary>
        /// Handles the Click event of the btnExportToFinancialEdge control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnExportToFinancialEdge_Click(object sender, EventArgs e)
        {
            var selectedBatches = new List <int>();

            gBatchesToExport.SelectedKeys.ToList().ForEach(b => selectedBatches.Add(b.ToString().AsInteger()));

            if (selectedBatches.Any())
            {
                var feJournal = new FEJournal();
                var items     = new List <JournalEntryLine>();

                var rockContext     = new RockContext();
                var batchService    = new FinancialBatchService(rockContext);
                var batchesToUpdate = new List <FinancialBatch>();

                var exportedBatches = batchService.Queryable()
                                      .WhereAttributeValue(rockContext, a => a.Attribute.Key == "rocks.kfs.FinancialEdge.DateExported" && (a.Value != null && a.Value != ""))
                                      .Select(b => b.Id)
                                      .ToList();

                batchesToUpdate = batchService.Queryable()
                                  .Where(b =>
                                         selectedBatches.Contains(b.Id) &&
                                         !exportedBatches.Contains(b.Id))
                                  .ToList();

                foreach (var batch in batchesToUpdate)
                {
                    var changes = new History.HistoryChangeList();
                    History.EvaluateChange(changes, "Status", batch.Status, BatchStatus.Closed);

                    string errorMessage;

                    if (batch.IsAutomated && batch.Status == BatchStatus.Pending)
                    {
                        errorMessage = string.Format("{0} is an automated batch and the status can not be modified when the status is pending. The system will automatically set this batch to OPEN when all transactions have been downloaded.", batch.Name);
                        maWarningDialog.Show(errorMessage, ModalAlertType.Warning);
                        return;
                    }

                    batch.Status = BatchStatus.Closed;

                    if (!batch.IsValid)
                    {
                        string message = string.Format("Unable to update status for the selected batches.<br/><br/>{0}", batch.ValidationResults.AsDelimited("<br/>"));
                        maWarningDialog.Show(message, ModalAlertType.Warning);
                        return;
                    }

                    batch.LoadAttributes();

                    var newDate = string.Empty;

                    var oldDate = batch.GetAttributeValue("rocks.kfs.FinancialEdge.DateExported");
                    newDate = RockDateTime.Now.ToString();
                    History.EvaluateChange(changes, "Date Exported", oldDate, newDate.ToString());

                    items.AddRange(feJournal.GetGlEntries(rockContext, batch, tbJournalType.Text));

                    HistoryService.SaveChanges(
                        rockContext,
                        typeof(FinancialBatch),
                        Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(),
                        batch.Id,
                        changes,
                        false);

                    batch.SetAttributeValue("rocks.kfs.FinancialEdge.DateExported", newDate);
                    batch.SaveAttributeValue("rocks.kfs.FinancialEdge.DateExported", rockContext);
                }

                rockContext.SaveChanges();

                feJournal.SetFinancialEdgeSessions(items, RockDateTime.Now.ToString("yyyyMMdd_HHmmss"));

                NavigateToPage(this.RockPage.Guid, new Dictionary <string, string>());
            }
            else
            {
                nbError.Text = string.Format("There were not any batches selected.");
                nbError.NotificationBoxType = NotificationBoxType.Warning;
                nbError.Visible             = true;
            }
        }