context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       timop
  //
  // Copyright 2004-2012 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.Data;
  using System.Windows.Forms;
  using System.Drawing;
  
  using GNU.Gettext;
  
  using Ict.Common;
  using Ict.Common.Controls;
  using Ict.Common.Data;
  using Ict.Common.Verification;
  
  using Ict.Petra.Client.MFinance.Logic;
  using Ict.Petra.Client.App.Core.RemoteObjects;
  using Ict.Petra.Client.App.Core;
  using Ict.Petra.Client.MFinance.Gui.Setup;
  
  using Ict.Petra.Shared;
  using Ict.Petra.Shared.MFinance;
  using Ict.Petra.Shared.MFinance.Account.Data;
  using Ict.Petra.Shared.MFinance.GL.Data;
  using Ict.Petra.Shared.MFinance.Validation;
  
  
  namespace Ict.Petra.Client.MFinance.Gui.GL
  {
      public partial class TUC_GLJournals
      {
          /// <summary>
          /// Returns FMainDS
          /// </summary>
          /// <returns></returns>
          public GLBatchTDS JournalFMainDS()
          {
              return FMainDS;
          }
  
          /// <summary>
          /// The current active Batch number
          /// </summary>
          public Int32 FBatchNumber = -1;
  
          /// <summary>
          /// flags if the Journal(s) have finished loading
          /// </summary>
          public bool FJournalsLoaded = false;
  
          private string FLedgerBaseCurrency = string.Empty;
          private Int32 FLedgerNumber = -1;
          private ABatchRow FBatchRow = null;
          private string FBatchStatus = string.Empty;
  
          // Logic Objects
          private TUC_GLJournals_Cancel FCancelLogicObject = null;
  
          /// <summary>Sets the ledger base currency</summary>
          public string LedgerBaseCurrency
          {
              set
              {
                  FLedgerBaseCurrency = value;
              }
          }
  
          /// <summary>
          /// WorkAroundInitialization
          /// </summary>
          public void WorkAroundInitialization()
          {
              grdDetails.DoubleClickCell += new TDoubleClickCellEventHandler(this.ShowTransactionTab);
              btnGetSetExchangeRate.Click += new EventHandler(SetExchangeRateValue);
          }
  
          /// <summary>
          /// load the journals into the grid
          /// </summary>
          /// <param name="ACurrentBatchRow"></param>
          public void LoadJournals(ABatchRow ACurrentBatchRow)
          {
              FJournalsLoaded = false;
  
              FBatchRow = ACurrentBatchRow;
  
              if (FBatchRow == null)
              {
                  return;
              }
  
              Int32 CurrentLedgerNumber = FBatchRow.LedgerNumber;
              Int32 CurrentBatchNumber = FBatchRow.BatchNumber;
              string CurrentBatchStatus = FBatchRow.BatchStatus;
              bool BatchIsUnposted = (FBatchRow.BatchStatus == MFinanceConstants.BATCH_UNPOSTED);
  
              bool FirstRun = (FLedgerNumber != CurrentLedgerNumber);
              bool BatchChanged = (FBatchNumber != CurrentBatchNumber);
              bool BatchStatusChanged = (!BatchChanged && (FBatchStatus != CurrentBatchStatus));
  
              if (FirstRun)
              {
                  FLedgerNumber = CurrentLedgerNumber;
              }
  
              if (BatchChanged)
              {
                  FBatchNumber = CurrentBatchNumber;
              }
  
              if (BatchStatusChanged)
              {
                  FBatchStatus = CurrentBatchStatus;
              }
  
              //Create object to control deletion
              FCancelLogicObject = new TUC_GLJournals_Cancel(FPetraUtilsObject, FLedgerNumber, FMainDS);
  
              //Make sure the current effective date for the Batch is correct
              DateTime BatchDateEffective = FBatchRow.DateEffective;
  
              //Check if need to load Journals
              if (!(FirstRun || BatchChanged || BatchStatusChanged))
              {
                  //Need to reconnect FPrev in some circumstances
                  if (FPreviouslySelectedDetailRow == null)
                  {
                      DataRowView rowView = (DataRowView)grdDetails.Rows.IndexToDataSourceRow(FPrevRowChangedRow);
  
                      if (rowView != null)
                      {
                          FPreviouslySelectedDetailRow = (GLBatchTDSAJournalRow)(rowView.Row);
                      }
                  }
  
                  // The journals are the same and we have loaded them already
                  if (BatchIsUnposted)
                  {
                      if (GetSelectedRowIndex() > 0)
                      {
                          GetDetailsFromControls(GetSelectedDetailRow());
                      }
                  }
              }
              else
              {
                  // a different journal
                  SetJournalDefaultView();
                  FPreviouslySelectedDetailRow = null;
  
                  //Load Journals
                  if (FMainDS.AJournal.DefaultView.Count == 0)
                  {
                      FMainDS.Merge(TRemote.MFinance.GL.WebConnectors.LoadAJournal(FLedgerNumber, FBatchNumber));
                  }
  
                  if (BatchIsUnposted)
                  {
                      if (!dtpDetailDateEffective.Date.HasValue || (dtpDetailDateEffective.Date.Value != BatchDateEffective))
                      {
                          dtpDetailDateEffective.Date = BatchDateEffective;
                      }
                  }
  
                  //Check if DateEffective has changed and then update all
                  foreach (DataRowView drv in FMainDS.AJournal.DefaultView)
                  {
                      AJournalRow jr = (AJournalRow)drv.Row;
  
                      if (jr.DateEffective != BatchDateEffective)
                      {
                          ((TFrmGLBatch)ParentForm).GetTransactionsControl().UpdateTransactionTotals(TFrmGLBatch.eGLLevel.Batch, true);
                          break;
                      }
                  }
  
                  ShowData();
  
                  // Now set up the complete current filter
                  FFilterAndFindObject.FilterPanelControls.SetBaseFilter(FMainDS.AJournal.DefaultView.RowFilter, true);
                  FFilterAndFindObject.ApplyFilter();
              }
  
              int nRowToSelect = 1;
  
              TFrmGLBatch myParentForm = (TFrmGLBatch)ParentForm;
  
              if (myParentForm.InitialBatchFound)
              {
                  DataView myView = (grdDetails.DataSource as DevAge.ComponentModel.BoundDataView).DataView;
  
  
gt:                 for (int counter = 0; (counter < myView.Count); counter++)