public static void ConsolidateBudgets(Int32 ALedgerNumber, bool AConsolidateAll)
        {
            TDBTransaction transaction  = null;
            Boolean        SubmissionOK = false;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoTransaction(IsolationLevel.Serializable, ref transaction, ref SubmissionOK,
                                                                  delegate
            {
                ALedgerRow LedgerRow = FBudgetTDS.ALedger[0];

                // first clear the old budget from GLMPeriods
                if (AConsolidateAll)
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        BudgetRow.BudgetStatus = false;
                    }

                    foreach (AGeneralLedgerMasterRow GeneralLedgerMasterRow in GLPostingDS.AGeneralLedgerMaster.Rows)
                    {
                        for (int Period = 1; Period <= LedgerRow.NumberOfAccountingPeriods; Period++)
                        {
                            ClearAllBudgetValues(GeneralLedgerMasterRow.GlmSequence, Period);
                        }
                    }
                }
                else
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        if (!BudgetRow.BudgetStatus)
                        {
                            UnPostBudget(BudgetRow, ALedgerNumber);
                        }
                    }
                }

                foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                {
                    if (!BudgetRow.BudgetStatus || AConsolidateAll)
                    {
                        List <ABudgetPeriodRow> budgetPeriods = new List <ABudgetPeriodRow>();

                        FBudgetTDS.ABudgetPeriod.DefaultView.RowFilter = ABudgetPeriodTable.GetBudgetSequenceDBName() + " = " +
                                                                         BudgetRow.BudgetSequence.ToString();

                        foreach (DataRowView rv in FBudgetTDS.ABudgetPeriod.DefaultView)
                        {
                            budgetPeriods.Add((ABudgetPeriodRow)rv.Row);
                        }

                        PostBudget(ALedgerNumber, BudgetRow, budgetPeriods);
                    }
                }

                FinishConsolidateBudget(transaction);


                GLPostingDS.ThrowAwayAfterSubmitChanges = true;
                GLPostingTDSAccess.SubmitChanges(GLPostingDS);
                GLPostingDS.Clear();
                SubmissionOK = true;
            });     // Get NewOrExisting AutoTransaction
        } // Consolidate Budgets
        /// <summary>
        /// Next-Year records will be created, and the Ledger will be moved to the new year.
        /// </summary>
        public override Int32 RunOperation()
        {
            Int32 TempGLMSequence = -1;
            Int32 EntryCount = 0;

            if (!FInfoMode)
            {
                FYearEndOperator.SetNextPeriod();
            }

            try
            {
                foreach (AGeneralLedgerMasterRow GlmRowFrom in FGlmPostingFrom.Rows)
                {
                    ++EntryCount;

                    if (FInfoMode)
                    {
                        continue;
                    }

                    FGlmpFrom.DefaultView.RowFilter = "a_glm_sequence_i=" + GlmRowFrom.GlmSequence;

                    AGeneralLedgerMasterRow GlmRowTo = null;
                    //
                    // If there's not already a GLM row for this Account / Cost Centre,
                    // I need to create one now.
                    //
                    Int32 GlmToRowIdx = GlmTDS.AGeneralLedgerMaster.DefaultView.Find(
                        new Object[] { GlmRowFrom.AccountCode, GlmRowFrom.CostCentreCode }
                        );

                    if (GlmToRowIdx >= 0)
                    {
                        GlmRowTo = (AGeneralLedgerMasterRow)GlmTDS.AGeneralLedgerMaster.DefaultView[GlmToRowIdx].Row;
                    }
                    else        // GLM record Not present - I'll make one now...
                    {
                        GlmRowTo = GlmTDS.AGeneralLedgerMaster.NewRowTyped(true);
                        GlmRowTo.GlmSequence = TempGLMSequence;
                        TempGLMSequence--;
                        GlmRowTo.LedgerNumber = FLedgerInfo.LedgerNumber;
                        GlmRowTo.AccountCode = GlmRowFrom.AccountCode;
                        GlmRowTo.CostCentreCode = GlmRowFrom.CostCentreCode;
                        GlmRowTo.Year = FNewYearNum;

                        GlmTDS.AGeneralLedgerMaster.Rows.Add(GlmRowTo);

                        GlmRowTo.YtdActualBase = GlmRowFrom.YtdActualBase;

                        if (!GlmRowFrom.IsYtdActualForeignNull())
                        {
                            GlmRowTo.YtdActualForeign = GlmRowFrom.YtdActualForeign;
                        }
                    }

                    // If the GlmRowTo row already existed, its GLMP records will also exist,
                    // but I need to update them with data from last year's forward periods.
                    // Otherwise I need to create a clutch of matching GLMP records.

                    // The first (FwdPostingPeriods) rows are copies of last year's Fwd records,
                    // and the remainder have the balances carried forward.


                    Int32 LastPeriodFromLastYear = FGlmpFrom.DefaultView.Count;
                    Decimal ActualBase = GlmRowTo.YtdActualBase;
                    Decimal ActualForeign = 0;
                    Boolean UsingForeign = false;

                    if (!GlmRowTo.IsYtdActualForeignNull())
                    {
                        ActualForeign = GlmRowTo.YtdActualForeign;
                        UsingForeign = true;
                    }

                    for (int PeriodCount = 1;
                         PeriodCount <= FLedgerAccountingPeriods + FLedgerFwdPeriods;
                         PeriodCount++)
                    {
                        AGeneralLedgerMasterPeriodRow glmPeriodRow;

                        if (GlmToRowIdx >= 0) // If there's already a GLM entry, I need to use the existing GLMP row for this period
                        {
                            glmPeriodRow = (AGeneralLedgerMasterPeriodRow)GlmTDS.AGeneralLedgerMasterPeriod.DefaultView[PeriodCount - 1].Row;
                        }
                        else // otherwise I need to create one now.
                        {
                            glmPeriodRow = GlmTDS.AGeneralLedgerMasterPeriod.NewRowTyped(true);
                            glmPeriodRow.GlmSequence = GlmRowTo.GlmSequence;
                            glmPeriodRow.PeriodNumber = PeriodCount;
                            GlmTDS.AGeneralLedgerMasterPeriod.Rows.Add(glmPeriodRow);
                        }

                        if (PeriodCount <= LastPeriodFromLastYear) // For any forward periods, I've already got data...
                        {
                            AGeneralLedgerMasterPeriodRow GlmFromRow =
                                (AGeneralLedgerMasterPeriodRow)FGlmpFrom.DefaultView[PeriodCount - 1].Row;
                            ActualBase = GlmFromRow.ActualBase;

                            if (!GlmFromRow.IsActualForeignNull())
                            {
                                ActualForeign = GlmFromRow.ActualForeign;
                                UsingForeign = true;
                            }
                        }

                        glmPeriodRow.ActualBase = ActualBase;

                        if (UsingForeign)
                        {
                            glmPeriodRow.ActualForeign = ActualForeign;
                        }
                    }
                }
            }
            finally
            {
                // Nothing special to do
            }

            if (DoExecuteableCode)
            {
                GlmTDS.ThrowAwayAfterSubmitChanges = true;
                GLPostingTDSAccess.SubmitChanges(GlmTDS);
            }

            return EntryCount;
        } // RunOperation
        private void ConsolidateBudgetsInternal(Int32 ALedgerNumber, bool AConsolidateAll)
        {
            TDBTransaction Transaction  = new TDBTransaction();
            Boolean        SubmissionOK = false;
            TDataBase      db           = DBAccess.Connect("Budget");

            db.WriteTransaction(ref Transaction, ref SubmissionOK,
                                delegate
            {
                ALedgerRow LedgerRow = FBudgetTDS.ALedger[0];

                // first clear the old budget from GLMPeriods
                if (AConsolidateAll)
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        BudgetRow.BudgetStatus = false;
                    }

                    foreach (AGeneralLedgerMasterRow GeneralLedgerMasterRow in FGLPostingDS.AGeneralLedgerMaster.Rows)
                    {
                        for (int Period = 1; Period <= LedgerRow.NumberOfAccountingPeriods; Period++)
                        {
                            ClearAllBudgetValues(GeneralLedgerMasterRow.GlmSequence, Period);
                        }
                    }
                }
                else
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        if (!BudgetRow.BudgetStatus)
                        {
                            UnPostBudget(BudgetRow, ALedgerNumber);
                        }
                    }
                }

                foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                {
                    if (!BudgetRow.BudgetStatus || AConsolidateAll)
                    {
                        List <ABudgetPeriodRow> budgetPeriods = new List <ABudgetPeriodRow>();

                        FBudgetTDS.ABudgetPeriod.DefaultView.RowFilter = ABudgetPeriodTable.GetBudgetSequenceDBName() + " = " +
                                                                         BudgetRow.BudgetSequence.ToString();

                        foreach (DataRowView rv in FBudgetTDS.ABudgetPeriod.DefaultView)
                        {
                            budgetPeriods.Add((ABudgetPeriodRow)rv.Row);
                        }

                        PostBudget(ALedgerNumber, BudgetRow, budgetPeriods);
                    }
                }

                /*Consolidate_Budget*/
                foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                {
                    BudgetRow.BudgetStatus = true;
                }

                ABudgetAccess.SubmitChanges(FBudgetTDS.ABudget, Transaction);

                FGLPostingDS.ThrowAwayAfterSubmitChanges = true;
                GLPostingTDSAccess.SubmitChanges(FGLPostingDS, Transaction.DataBaseObj);
                FGLPostingDS.Clear();

                SubmissionOK = true;
            });
        }
        public static void ConsolidateBudgets(Int32 ALedgerNumber, bool AConsolidateAll)
        {
            bool           NewTransaction           = false;
            TDBTransaction SubmitChangesTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.Serializable,
                                                                                                        out NewTransaction);

            ALedgerRow LedgerRow = FBudgetTDS.ALedger[0];

            try
            {
                // first clear the old budget from GLMPeriods
                if (AConsolidateAll)
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        BudgetRow.BudgetStatus = false;
                    }

                    foreach (AGeneralLedgerMasterRow GeneralLedgerMasterRow in GLPostingDS.AGeneralLedgerMaster.Rows)
                    {
                        for (int Period = 1; Period <= LedgerRow.NumberOfAccountingPeriods; Period++)
                        {
                            ClearAllBudgetValues(GeneralLedgerMasterRow.GlmSequence, Period);
                        }
                    }
                }
                else
                {
                    foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                    {
                        if (!BudgetRow.BudgetStatus)
                        {
                            UnPostBudget(BudgetRow, ALedgerNumber);
                        }
                    }
                }

                foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
                {
                    if (!BudgetRow.BudgetStatus || AConsolidateAll)
                    {
                        List <ABudgetPeriodRow> budgetPeriods = new List <ABudgetPeriodRow>();

                        FBudgetTDS.ABudgetPeriod.DefaultView.RowFilter = ABudgetPeriodTable.GetBudgetSequenceDBName() + " = " +
                                                                         BudgetRow.BudgetSequence.ToString();

                        foreach (DataRowView rv in FBudgetTDS.ABudgetPeriod.DefaultView)
                        {
                            budgetPeriods.Add((ABudgetPeriodRow)rv.Row);
                        }

                        PostBudget(ALedgerNumber, BudgetRow, budgetPeriods);
                    }
                }

                FinishConsolidateBudget(SubmitChangesTransaction);


                GLPostingDS.ThrowAwayAfterSubmitChanges = true;
                GLPostingTDSAccess.SubmitChanges(GLPostingDS);
                GLPostingDS.Clear();

                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.CommitTransaction();
                }
            }
            catch (Exception Exc)
            {
                TLogging.Log("An Exception occured during the consolidation of Budgets:" + Environment.NewLine + Exc.ToString());

                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.RollbackTransaction();
                }

                throw;
            }
        }