/// <summary>
        /// Searches all transaction tables to see if there is a stewardship
        ///   batch that already exists which appears to match the one
        ///   currently being processed.
        /// </summary>
        /// <param name="AICHLedgerNumber">ICH Ledger number</param>
        /// <param name="ABatchRef">Reference of current stewardship batch</param>
        /// <param name="AYear">Year to which current stewardship batch applies</param>
        /// <param name="AMonth">Month to which current stewardship batch applies</param>
        /// <param name="ACurrentPeriod">Current period of ICH ledger</param>
        /// <param name="ACurrentYear">Current year of ICH ledger</param>
        /// <param name="ADBTransaction">Current database transaction</param>
        /// <returns>The batch number of the matching batch or 0 if there is no match.</returns>
        public int FindMatchingStewardshipBatch(int AICHLedgerNumber,
            string ABatchRef,
            int AYear,
            int AMonth,
            int ACurrentPeriod,
            int ACurrentYear,
            ref TDBTransaction ADBTransaction
            )
        {
            int BatchNumber = 0;

            bool MatchFound = false;

            /*cOldStyleDescription = REPLACE(REPLACE(pcBatchDescription, STRING(piYear) + " ", ""), " (Run " + STRING(piRunNumber) + ")", "").*/

            /* Old reference format (prior to this program being available) was xxyy where xx was the fund number
             * and yy was the period number. Therefore we need to strip off the final 3 characters to convert
             * from the new style reference to the old style reference (those 3 characters are the run number).
             * We need to do this because some of the batches we will search through will be in old format. */
            string OldStyleReference = ABatchRef.Substring(0, ABatchRef.Length - 3);
            int OldStyleReferenceLen = OldStyleReference.Length;

            /* Note: In the queries below we need to check the length of the reference as well because of the
             * 3 digit fund numbers (eg. Central America Period 12 would have reference 2012 while NAA Period 2 would be
             * 20112) */

            /* look in current period */
            ATransactionTable TransTable = new ATransactionTable();
            ATransactionRow TemplateRow = (ATransactionRow)TransTable.NewRowTyped(false);

            TemplateRow.LedgerNumber = AICHLedgerNumber;
            TemplateRow.TransactionStatus = true;

            StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "=" });

            ATransactionTable TransactionTable = ATransactionAccess.LoadUsingTemplate(TemplateRow, operators, null, ADBTransaction);
            ATransactionRow TransactionRow = null;

            string TransRef;
            int TransRefLen;

            for (int i = 0; i < TransactionTable.Count; i++)
            {
                TransactionRow = (ATransactionRow)TransactionTable.Rows[i];

                TransRef = TransactionRow.Reference;
                TransRefLen = TransRef.Length;

                if ((TransRef.Substring(0, OldStyleReferenceLen) == OldStyleReference)
                    && ((TransRefLen == OldStyleReferenceLen) || (TransRefLen == (OldStyleReferenceLen + 3))))
                {
                    BatchNumber = TransactionRow.BatchNumber;
                    MatchFound = true;
                    break;
                }
            }

            if (!MatchFound)
            {
#if TODO

                /* look in previous periods (need to compare date of transactions to the month of the stewardship
                 *             because there may be a batch present which was for last years stewardship - eg. Dec 2007
                 *             Stewardship for US may have been processed in Jan 2008) */
                AThisYearOldTransactionTable YearOldTransTable = new AThisYearOldTransactionTable();
                AThisYearOldTransactionRow TemplateRow2 = (AThisYearOldTransactionRow)YearOldTransTable.NewRowTyped(false);

                TemplateRow2.LedgerNumber = AICHLedgerNumber;
                TemplateRow2.TransactionStatus = true;

                StringCollection operators2 = StringHelper.InitStrArr(new string[] { "=", "=" });

                AThisYearOldTransactionTable ThisYearOldTransactionTable = AThisYearOldTransactionAccess.LoadUsingTemplate(TemplateRow2,
                    operators2,
                    null,
                    ADBTransaction);
                AThisYearOldTransactionRow ThisYearOldTransactionRow = null;

                string OldTransRef;
                int OldTransRefLen;

                for (int i = 0; i < ThisYearOldTransactionTable.Count; i++)
                {
                    ThisYearOldTransactionRow = (AThisYearOldTransactionRow)ThisYearOldTransactionTable.Rows[i];

                    OldTransRef = ThisYearOldTransactionRow.Reference;
                    OldTransRefLen = OldTransRef.Length;

                    if ((OldTransRef.Substring(0, OldStyleReferenceLen) == OldStyleReference)
                        && ((OldTransRefLen == OldStyleReferenceLen) || (OldTransRefLen == (OldStyleReferenceLen + 3)))
                        && ((AMonth <= ThisYearOldTransactionRow.TransactionDate.Month) || (AMonth > ACurrentPeriod)))
                    {
                        BatchNumber = ThisYearOldTransactionRow.BatchNumber;
                        MatchFound = true;
                        break;
                    }
                }

                /* look in previous years (need to make sure that you only match batches that are stewardships
                 * for the same year as the one being processed). */
                if (!MatchFound && (AYear < ACurrentYear))
                {
                    APreviousYearTransactionTable YearPreviousTransTable = new APreviousYearTransactionTable();
                    APreviousYearTransactionRow TemplateRow3 = (APreviousYearTransactionRow)YearPreviousTransTable.NewRowTyped(false);

                    TemplateRow3.LedgerNumber = AICHLedgerNumber;
                    TemplateRow3.TransactionStatus = true;

                    StringCollection operators3 = StringHelper.InitStrArr(new string[] { "=", "=" });

                    APreviousYearTransactionTable PreviousYearTransactionTable = APreviousYearTransactionAccess.LoadUsingTemplate(TemplateRow3,
                        operators3,
                        null,
                        ADBTransaction);
                    APreviousYearTransactionRow PreviousYearTransactionRow = null;

                    string PreviousTransRef;
                    int PreviousTransRefLen;

                    for (int i = 0; i < PreviousYearTransactionTable.Count; i++)
                    {
                        PreviousYearTransactionRow = (APreviousYearTransactionRow)PreviousYearTransactionTable.Rows[i];

                        PreviousTransRef = PreviousYearTransactionRow.Reference;
                        PreviousTransRefLen = PreviousTransRef.Length;

                        if ((PreviousTransRef.Substring(0, OldStyleReferenceLen) == OldStyleReference)
                            && ((PreviousTransRefLen == OldStyleReferenceLen) || (PreviousTransRefLen == (OldStyleReferenceLen + 3)))
                            && (PreviousYearTransactionRow.TransactionDate.Month >= AMonth)
                            && (AMonth > ACurrentPeriod)
                            && (PreviousYearTransactionRow.TransactionDate.Year == AYear))
                        {
                            BatchNumber = PreviousYearTransactionRow.BatchNumber;
                            break;
                        }
                    }
                }
#endif
            }

            return BatchNumber;
        }
        /// <summary>
        /// Checks the specified stewardship batch to see whether the summary transactions
        ///    match the amounts specified.
        /// </summary>
        /// <param name="AICHLedgerNumber">ICH Ledger number</param>
        /// <param name="ABatchNumber">The batch number of the batch to check</param>
        /// <param name="ACostCentre">Fund to which stewardship batch applies</param>
        /// <param name="AIncomeAmount">Income total to check against</param>
        /// <param name="AExpenseAmount">Expense total to check against</param>
        /// <param name="ATransferAmount">Transfer total to check against</param>
        /// <param name="ADBTransaction">Current database transaction</param>
        /// <returns>True if transasctions match, false otherwise</returns>
        public bool SummaryStewardshipTransactionsMatch(int AICHLedgerNumber,
            int ABatchNumber,
            string ACostCentre,
            decimal AIncomeAmount,
            decimal AExpenseAmount,
            decimal ATransferAmount,
            ref TDBTransaction ADBTransaction)
        {
            decimal ExistingIncExpTotal;

            ABatchTable BatchTable = ABatchAccess.LoadByPrimaryKey(AICHLedgerNumber, ABatchNumber, ADBTransaction);
            ABatchRow BatchRow = (ABatchRow)BatchTable.Rows[0];

            if (BatchRow != null)
            {
                /* look for summary transaction for transfers */
                ATransactionTable TransTable = new ATransactionTable();
                ATransactionRow TemplateRow = (ATransactionRow)TransTable.NewRowTyped(false);

                TemplateRow.LedgerNumber = AICHLedgerNumber;
                TemplateRow.BatchNumber = ABatchNumber;
                TemplateRow.CostCentreCode = ACostCentre;
                TemplateRow.AccountCode = MFinanceConstants.ICH_ACCT_DEPOSITS_WITH_ICH;                 //i.e. 8540

                StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                ATransactionTable TransactionTable = ATransactionAccess.LoadUsingTemplate(TemplateRow, operators, null, ADBTransaction);
                ATransactionRow TransactionRow = (ATransactionRow)TransactionTable.Rows[0];

                if (TransactionRow != null)
                {
                    if (TransactionRow.TransactionAmount != ATransferAmount)
                    {
                        DBAccess.GDBAccessObj.RollbackTransaction();
                        return false;
                    }
                }

                /* find the summary transactions for income and expense and sum them */
                ExistingIncExpTotal = 0;

                ATransactionTable TransTable2 = new ATransactionTable();
                ATransactionRow TemplateRow2 = (ATransactionRow)TransTable2.NewRowTyped(false);

                TemplateRow2.LedgerNumber = AICHLedgerNumber;
                TemplateRow2.BatchNumber = ABatchNumber;
                TemplateRow2.CostCentreCode = ACostCentre;
                TemplateRow2.AccountCode = MFinanceConstants.ICH_ACCT_LOCAL_LEDGER;                 //i.e. 8520

                StringCollection operators2 = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                ATransactionTable TransactionTable2 = ATransactionAccess.LoadUsingTemplate(TemplateRow2, operators2, null, ADBTransaction);
                ATransactionRow TransactionRow2 = null;

                for (int i = 0; i < TransactionTable2.Count; i++)
                {
                    TransactionRow2 = (ATransactionRow)TransactionTable2.Rows[i];

                    ExistingIncExpTotal += TransactionRow2.TransactionAmount;
                }

                DBAccess.GDBAccessObj.RollbackTransaction();
                return ExistingIncExpTotal == (AIncomeAmount + AExpenseAmount);
            }

#if TODO

            /* now check previous periods if batch wasn't in current period */
            AThisYearOldBatchTable ThisYearOldBatchTable = AThisYearOldBatchAccess.LoadByPrimaryKey(AICHLedgerNumber, ABatchNumber, ADBTransaction);
            AThisYearOldBatchRow ThisYearOldBatchRow = (AThisYearOldBatchRow)ThisYearOldBatchTable.Rows[0];

            if (ThisYearOldBatchRow != null)
            {
                /* look for summary transaction for transfers */
                AThisYearOldTransactionTable ThisYearOldTransTable = new AThisYearOldTransactionTable();
                AThisYearOldTransactionRow TemplateRow3 = (AThisYearOldTransactionRow)ThisYearOldTransTable.NewRowTyped(false);

                TemplateRow3.LedgerNumber = AICHLedgerNumber;
                TemplateRow3.BatchNumber = ABatchNumber;
                TemplateRow3.CostCentreCode = ACostCentre;
                TemplateRow3.AccountCode = MFinanceConstants.ICH_ACCT_DEPOSITS_WITH_ICH;                 //i.e. 8540

                StringCollection operators3 = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                AThisYearOldTransactionTable ThisYearOldTransactionTable = AThisYearOldTransactionAccess.LoadUsingTemplate(TemplateRow3,
                    operators3,
                    null,
                    ADBTransaction);
                AThisYearOldTransactionRow ThisYearOldTransactionRow = (AThisYearOldTransactionRow)ThisYearOldTransactionTable.Rows[0];

                if (ThisYearOldTransactionRow != null)
                {
                    if (ThisYearOldTransactionRow.TransactionAmount != ATransferAmount)
                    {
                        DBAccess.GDBAccessObj.RollbackTransaction();
                        return false;
                    }
                }

                /* find the summary transactions for income and expense and sum them */
                ExistingIncExpTotal = 0;

                AThisYearOldTransactionTable ThisYearOldTransTable4 = new AThisYearOldTransactionTable();
                AThisYearOldTransactionRow TemplateRow4 = (AThisYearOldTransactionRow)ThisYearOldTransTable4.NewRowTyped(false);

                TemplateRow4.LedgerNumber = AICHLedgerNumber;
                TemplateRow4.BatchNumber = ABatchNumber;
                TemplateRow4.CostCentreCode = ACostCentre;
                TemplateRow4.AccountCode = MFinanceConstants.ICH_ACCT_LOCAL_LEDGER;                 //i.e. 8520

                StringCollection operators4 = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                AThisYearOldTransactionTable ThisYearOldTransactionTable2 = AThisYearOldTransactionAccess.LoadUsingTemplate(TemplateRow4,
                    operators4,
                    null,
                    ADBTransaction);
                AThisYearOldTransactionRow ThisYearOldTransactionRow2 = null;

                for (int i = 0; i < ThisYearOldTransactionTable2.Count; i++)
                {
                    ThisYearOldTransactionRow2 = (AThisYearOldTransactionRow)ThisYearOldTransactionTable2.Rows[i];

                    ExistingIncExpTotal += ThisYearOldTransactionRow2.TransactionAmount;
                }

                DBAccess.GDBAccessObj.RollbackTransaction();
                return ExistingIncExpTotal == (AIncomeAmount + AExpenseAmount);
            }

            /* now check previous years if batch wasn't in current year */
            APreviousYearBatchTable PreviousYearBatchTable = APreviousYearBatchAccess.LoadByPrimaryKey(AICHLedgerNumber, ABatchNumber, ADBTransaction);
            APreviousYearBatchRow PreviousYearBatchRow = (APreviousYearBatchRow)PreviousYearBatchTable.Rows[0];

            if (PreviousYearBatchRow != null)
            {
                /* look for summary transaction for transfers */
                APreviousYearTransactionTable PreviousYearTransTable = new APreviousYearTransactionTable();
                APreviousYearTransactionRow TemplateRow5 = (APreviousYearTransactionRow)PreviousYearTransTable.NewRowTyped(false);

                TemplateRow5.LedgerNumber = AICHLedgerNumber;
                TemplateRow5.BatchNumber = ABatchNumber;
                TemplateRow5.CostCentreCode = ACostCentre;
                TemplateRow5.AccountCode = MFinanceConstants.ICH_ACCT_DEPOSITS_WITH_ICH; //i.e. 8540

                StringCollection operators5 = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                APreviousYearTransactionTable PreviousYearTransactionTable = APreviousYearTransactionAccess.LoadUsingTemplate(TemplateRow5,
                    operators5,
                    null,
                    ADBTransaction);
                APreviousYearTransactionRow PreviousYearTransactionRow = (APreviousYearTransactionRow)PreviousYearTransactionTable.Rows[0];

                if (PreviousYearTransactionRow != null)
                {
                    if (PreviousYearTransactionRow.TransactionAmount != ATransferAmount)
                    {
                        DBAccess.GDBAccessObj.RollbackTransaction();
                        return false;
                    }
                }

                /* find the summary transactions for income and expense and sum them */
                ExistingIncExpTotal = 0;

                APreviousYearTransactionTable PreviousYearTransTable4 = new APreviousYearTransactionTable();
                APreviousYearTransactionRow TemplateRow6 = (APreviousYearTransactionRow)PreviousYearTransTable4.NewRowTyped(false);

                TemplateRow6.LedgerNumber = AICHLedgerNumber;
                TemplateRow6.BatchNumber = ABatchNumber;
                TemplateRow6.CostCentreCode = ACostCentre;
                TemplateRow6.AccountCode = MFinanceConstants.ICH_ACCT_LOCAL_LEDGER; //i.e. 8520

                StringCollection operators6 = StringHelper.InitStrArr(new string[] { "=", "=", "=", "=" });

                APreviousYearTransactionTable PreviousYearTransactionTable2 = APreviousYearTransactionAccess.LoadUsingTemplate(TemplateRow6,
                    operators6,
                    null,
                    ADBTransaction);
                APreviousYearTransactionRow PreviousYearTransactionRow2 = null;

                for (int i = 0; i < PreviousYearTransactionTable2.Count; i++)
                {
                    PreviousYearTransactionRow2 = (APreviousYearTransactionRow)PreviousYearTransactionTable2.Rows[i];

                    ExistingIncExpTotal += PreviousYearTransactionRow2.TransactionAmount;
                }

                DBAccess.GDBAccessObj.RollbackTransaction();
                return ExistingIncExpTotal == (AIncomeAmount + AExpenseAmount);
            }
#endif

            DBAccess.GDBAccessObj.RollbackTransaction();
            return false;
        }
        } // Generate StewardshipBatch From ReportFile

        /// <summary>
        /// Looks at the specified batch and works out whether it is a duplicate
        ///       of the stewardship currently being processed (defined by the other
        ///       parameters). There are a number of possibilities from definite duplicate,
        ///       through possible duplicate to definitely not a duplicate, depending on
        ///       comparison of run numbers and comparison of amount totals. The
        ///       procedure works out which it is and writes to the log file appropriately.
        /// </summary>
        /// <param name="ALedgerNumber">ICH Ledger number</param>
        /// <param name="ABatchNumber">The number of the batch to check</param>
        /// <param name="ARunNumber">The run number of the stewardship being processed</param>
        /// <param name="AYear">The year of the stewardship being processed</param>
        /// <param name="AFileName">The name of the stewardship file being processed</param>
        /// <param name="AFromCostCentre">Fund number to which the stewardship applies</param>
        /// <param name="AFromCostCentreName">Name of the fund</param>
        /// <param name="APeriodName">Name of the period to which the stewardship applies (ie. Month)</param>
        /// <param name="ATotalIncome">Total amount of income transactions in stewardship being processed</param>
        /// <param name="ATotalExpense">Total amount of expense transactions in stewardship being processed</param>
        /// <param name="ATotalTransfer">Total amount of transfer transactions in stewardship being processed</param>
        /// <param name="ALogWriter">TextWriter for log file</param>
        /// <param name="ADBTransaction">Current database transaction</param>
        /// <param name="AProcessFile">Set to True if processing of the stewardship should go ahead or False if it should be rejected (ie. if a duplicate or possible duplicate</param>
        private void DealWithMatchingStewardshipBatch(int ALedgerNumber, int ABatchNumber, int ARunNumber,
            int AYear, string AFileName, string AFromCostCentre,
            string AFromCostCentreName, string APeriodName, decimal ATotalIncome,
            decimal ATotalExpense, decimal ATotalTransfer, ref TextWriter ALogWriter, ref TDBTransaction ADBTransaction, out bool AProcessFile)
        {
            string LogMessage = string.Empty;

            AProcessFile = false;

            int BatchRunNumber = 0;

            /*
             *          Possible scenarios:
             *
             *          matching batch exists, both are run 0                           - full duplicate
             *          matching batch exists, original is run 0 new one is run xxx     - possible duplicate if totals identical (return flag to indicate that totals
             *                                                                                                                    need to be compared)
             *          matching batch exists, original is run xxx new one is run 0     - not valid
             *          matching batch exists, original is run xxx new one is run xxx   - full duplicate
             *          matching batch exists, original is run xxx new one is run yyy   - not duplicate
             *          matching batch exists, original is run ? new one is run 0       - full duplicate
             *          matching batch exists, original is run ? new one is run xxx     - possible duplicate if totals identical (return flag to indicate that totals
             */

            /* check if batch is in current period and do the appropriate comparisons if it is (based around
             * the scenarios above) */
            ABatchTable BatchTable = ABatchAccess.LoadByPrimaryKey(ALedgerNumber, ABatchNumber, ADBTransaction);
            ABatchRow BatchRow = (ABatchRow)BatchTable.Rows[0];

            if (BatchRow != null)
            {
                if (ARunNumber == 0)
                {
                    if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense, ATotalTransfer,
                            ref ADBTransaction))
                    {
                        LogMessage =
                            String.Format(Catalog.GetString(
                                    "Stewardship Batch for {0} {1} {2} already exists (Batch {3}) and data is identical. {4} will be skipped."),
                                AFromCostCentreName, APeriodName, AYear, ABatchNumber, AFileName);
                        ALogWriter.WriteLine(LogMessage);
                        AProcessFile = false;
                    }
                    else
                    {
                        LogMessage =
                            String.Format(Catalog.GetString(
                                    "Stewardship Batch for {0} {1} {2} already exists (Batch {3}) and data has changed. {4} will be skipped."),
                                AFromCostCentreName, APeriodName, AYear, ABatchNumber, AFileName);
                        ALogWriter.WriteLine(LogMessage);
                        AProcessFile = false;
                    }
                }
                else
                {
                    ATransactionTable TransTable = new ATransactionTable();
                    ATransactionRow TemplateRow = (ATransactionRow)TransTable.NewRowTyped(false);

                    TemplateRow.LedgerNumber = ALedgerNumber;
                    TemplateRow.BatchNumber = ABatchNumber;

                    StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "=" });

                    ATransactionTable TransactionTable = ATransactionAccess.LoadUsingTemplate(TemplateRow, operators, null, ADBTransaction);
                    ATransactionRow TransactionRow = (ATransactionRow)TransactionTable.Rows[0];

                    if (TransactionTable.Count > 0)
                    {
                        string TransRowRef = TransactionRow.Reference;
                        BatchRunNumber = Convert.ToInt32(TransRowRef.Substring(TransRowRef.Length - 3, 3));

                        if (ARunNumber == BatchRunNumber)
                        {
                            if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                    ATotalTransfer, ref ADBTransaction))
                            {
                                LogMessage =
                                    String.Format(Catalog.GetString(
                                            "Stewardship Batch for {0} {1} {2} already exists (Batch {3}) and data is identical. {4} will be skipped."),
                                        AFromCostCentreName, APeriodName, AYear, ABatchNumber, AFileName);
                                ALogWriter.WriteLine(LogMessage);
                                AProcessFile = false;
                            }
                            else
                            {
                                LogMessage =
                                    String.Format(Catalog.GetString(
                                            "Stewardship Batch for {0} {1} {2} already exists (Batch {3}) and data has changed. {4} will be skipped."),
                                        AFromCostCentreName, APeriodName, AYear, ABatchNumber, AFileName);
                                ALogWriter.WriteLine(LogMessage);
                                AProcessFile = false;
                            }

                            return;
                        }
                        else if (BatchRunNumber == 0)
                        {
                            if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                    ATotalTransfer, ref ADBTransaction))
                            {
                                LogMessage =
                                    String.Format(Catalog.GetString(
                                            "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and is run number 0. File {5} is for run number {6} but the data is identical to the existing batch so it will not be processed."),
                                        AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                        TransactionRow.TransactionDate.ToShortDateString(), AFileName, ARunNumber);
                                ALogWriter.WriteLine(LogMessage);
                                AProcessFile = false;
                            }
                            else
                            {
                                AProcessFile = true;
                            }
                        }
                        else
                        {
                            AProcessFile = true;
                        }
                    }
                }

                return;
            }

#if TODO

            /* if the batch wasn't in the current period try previous periods. In this case we can't assume
             * that the transaction reference will be in the "new style" (ie. LLLPPRRR). It might be in the
             * old style (ie. LLLPP). We are able to do more checks where it is in the new style as we can
             * get hold of the run number */

            AThisYearOldTransactionTable OldTransTable = new AThisYearOldTransactionTable();
            AThisYearOldTransactionRow TemplateRow2 = (AThisYearOldTransactionRow)OldTransTable.NewRowTyped(false);

            TemplateRow2.LedgerNumber = ALedgerNumber;
            TemplateRow2.BatchNumber = ABatchNumber;

            StringCollection operators2 = StringHelper.InitStrArr(new string[] { "=", "=" });

            AThisYearOldTransactionTable OldTransactionTable = AThisYearOldTransactionAccess.LoadUsingTemplate(TemplateRow2,
                operators2,
                null,
                ADBTransaction);
            AThisYearOldTransactionRow OldTransactionRow = (AThisYearOldTransactionRow)OldTransactionTable.Rows[0];

            if (OldTransactionTable.Count > 0)
            {
                string OldTransRowRef = OldTransactionRow.Reference;
                //BatchRunNumber = Convert.ToInt32(TransRowRef.Substring(TransRowRef.Length - 3, 3));

                if (OldTransRowRef.Length < 7)
                {
                    if (ARunNumber == 0)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data is identical. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data has changed. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                    }
                    else
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) but the run number is unknown. File {5} is for run number {6} but the data is identical to the existing batch so it will not be processed."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName, ARunNumber);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            AProcessFile = true;
                        }
                    }
                }
                else
                {
                    BatchRunNumber = Convert.ToInt32(OldTransRowRef.Substring(OldTransRowRef.Length - 3, 3));

                    if (ARunNumber == BatchRunNumber)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data is identical. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data has changed. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                    }
                    else if (ARunNumber < BatchRunNumber)
                    {
                        LogMessage =
                            String.Format(Catalog.GetString(
                                    "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}).  The run number on that batch ({5}) is higher than the run number on the file being processed and therefore {6} will be skipped."),
                                AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                OldTransactionRow.TransactionDate.ToShortDateString(), BatchRunNumber, AFileName);
                        ALogWriter.WriteLine(LogMessage);
                        AProcessFile = false;
                    }
                    else if (BatchRunNumber == 0)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and is run number 0. File {5} is for run number {6} but the data is identical to the existing batch so it will not be processed."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    OldTransactionRow.TransactionDate.ToShortDateString(), AFileName, ARunNumber);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            AProcessFile = true;
                        }
                    }
                    else
                    {
                        AProcessFile = true;
                    }
                }

                return;
            }

            /* if we still haven't found the batch try previous years */
            APreviousYearTransactionTable PreviousTransTable = new APreviousYearTransactionTable();
            APreviousYearTransactionRow TemplateRow3 = (APreviousYearTransactionRow)PreviousTransTable.NewRowTyped(false);

            TemplateRow3.LedgerNumber = ALedgerNumber;
            TemplateRow3.BatchNumber = ABatchNumber;

            StringCollection operators3 = StringHelper.InitStrArr(new string[] { "=", "=" });

            APreviousYearTransactionTable PreviousTransactionTable = APreviousYearTransactionAccess.LoadUsingTemplate(TemplateRow3,
                operators3,
                null,
                ADBTransaction);
            APreviousYearTransactionRow PreviousTransactionRow = (APreviousYearTransactionRow)PreviousTransactionTable.Rows[0];

            if (PreviousTransactionTable.Count > 0)
            {
                string PreviousTransRowRef = PreviousTransactionRow.Reference;
                //BatchRunNumber = Convert.ToInt32(TransRowRef.Substring(TransRowRef.Length - 3, 3));

                if (PreviousTransRowRef.Length < 7)
                {
                    if (ARunNumber == 0)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data is identical. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data has changed. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                    }
                    else
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) but the run number is unknown. File {5} is for run number {6} but the data is identical to the existing batch so it will not be processed."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName, ARunNumber);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            AProcessFile = true;
                        }
                    }
                }
                else
                {
                    BatchRunNumber = Convert.ToInt32(PreviousTransRowRef.Substring(PreviousTransRowRef.Length - 3, 3));

                    if (ARunNumber == BatchRunNumber)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data is identical. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and data has changed. {5} will be skipped."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                    }
                    else if (ARunNumber < BatchRunNumber)
                    {
                        LogMessage =
                            String.Format(Catalog.GetString(
                                    "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}).  The run number on that batch ({5}) is higher than the run number on the file being processed and therefore {6} will be skipped."),
                                AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                PreviousTransactionRow.TransactionDate.ToShortDateString(), BatchRunNumber, AFileName);
                        ALogWriter.WriteLine(LogMessage);
                        AProcessFile = false;
                    }
                    else if (BatchRunNumber == 0)
                    {
                        if (SummaryStewardshipTransactionsMatch(ALedgerNumber, ABatchNumber, AFromCostCentre, ATotalIncome, ATotalExpense,
                                ATotalTransfer, ref ADBTransaction))
                        {
                            LogMessage =
                                String.Format(Catalog.GetString(
                                        "Stewardship Batch for {0} {1} {2} already exists (Batch {3}, date {4}) and is run number 0. File {5} is for run number {6} but the data is identical to the existing batch so it will not be processed."),
                                    AFromCostCentreName, APeriodName, AYear, ABatchNumber,
                                    PreviousTransactionRow.TransactionDate.ToShortDateString(), AFileName, ARunNumber);
                            ALogWriter.WriteLine(LogMessage);
                            AProcessFile = false;
                        }
                        else
                        {
                            AProcessFile = true;
                        }
                    }
                    else
                    {
                        AProcessFile = true;
                    }
                }
            }
#endif
        }