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)
        {
            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
        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;
            }
        }