public static BudgetTDS LoadBudget(Int32 ALedgerNumber)
        {
            BudgetTDS MainDS = new BudgetTDS();

            TDBTransaction Transaction = null;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted,
                                                                      TEnforceIsolationLevel.eilMinimum,
                                                                      ref Transaction,
                                                                      delegate
            {
                //TODO: need to filter on Year
                ABudgetAccess.LoadViaALedger(MainDS, ALedgerNumber, Transaction);
                ABudgetRevisionAccess.LoadViaALedger(MainDS, ALedgerNumber, Transaction);
                //TODO: need to filter on ABudgetPeriod using LoadViaBudget or LoadViaUniqueKey
                ABudgetPeriodAccess.LoadAll(MainDS, Transaction);
                ALedgerAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, Transaction);

                //            ABudgetPeriodTable BudgetPeriodTable = new ABudgetPeriodTable();
                //            ABudgetPeriodRow TemplateRow = (ABudgetPeriodRow)BudgetPeriodTable.NewRow(false);
                //
                //            TemplateRow.BudgetSequence;
                //            ABudgetPeriodAccess.LoadViaABudgetTemplate(MainDS, TemplateRow, Transaction);
            });

            // Accept row changes here so that the Client gets 'unmodified' rows
            MainDS.AcceptChanges();

            // Remove all Tables that were not filled with data before remoting them.
            MainDS.RemoveEmptyTables();

            return(MainDS);
        }
        public static BudgetTDS LoadBudgetForAutoGenerate(Int32 ALedgerNumber)
        {
            TDBTransaction Transaction = null;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted,
                                                                      TEnforceIsolationLevel.eilMinimum,
                                                                      ref Transaction,
                                                                      delegate
            {
                //TODO: need to filter on Year
                ABudgetAccess.LoadViaALedger(FMainDS, ALedgerNumber, Transaction);
                ABudgetRevisionAccess.LoadViaALedger(FMainDS, ALedgerNumber, Transaction);
                //TODO: need to filter on ABudgetPeriod using LoadViaBudget or LoadViaUniqueKey
                ABudgetPeriodAccess.LoadAll(FMainDS, Transaction);
                ALedgerAccess.LoadByPrimaryKey(FMainDS, ALedgerNumber, Transaction);
                ABudgetTypeAccess.LoadAll(FMainDS, Transaction);
            });

            // Accept row changes here so that the Client gets 'unmodified' rows
            FMainDS.AcceptChanges();

            // Remove all Tables that were not filled with data before remoting them.
            FMainDS.RemoveEmptyTables();

            return(FMainDS);
        }
Beispiel #3
0
        public static BudgetTDS LoadBudgetForAutoGenerate(Int32 ALedgerNumber)
        {
            BudgetTDS FMainDS = new BudgetTDS();

            TDBTransaction Transaction = new TDBTransaction();
            TDataBase      db          = DBAccess.Connect("LoadBudgetForAutoGenerate");

            db.ReadTransaction(
                ref Transaction,
                delegate
            {
                ALedgerAccess.LoadByPrimaryKey(FMainDS, ALedgerNumber, Transaction);

                #region Validate Data

                if ((FMainDS.ALedger == null) || (FMainDS.ALedger.Count == 0))
                {
                    throw new EFinanceSystemDataTableReturnedNoDataException(String.Format(Catalog.GetString(
                                                                                               "Function:{0} - Ledger data for Ledger number {1} does not exist or could not be accessed!"),
                                                                                           Utilities.GetMethodName(true),
                                                                                           ALedgerNumber));
                }

                #endregion Validate Data

                int CurrentFinancialYear = FMainDS.ALedger[0].CurrentFinancialYear;

                //Load all by Ledger/Year but none may exist
                ABudgetTable BudgetTable = new ABudgetTable();
                ABudgetRow TemplateRow   = (ABudgetRow)BudgetTable.NewRow();

                TemplateRow.LedgerNumber = ALedgerNumber;
                TemplateRow.Year         = CurrentFinancialYear;

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

                OrderList.Add("ORDER BY");
                OrderList.Add(ABudgetTable.GetCostCentreCodeDBName() + " ASC");
                OrderList.Add(ABudgetTable.GetAccountCodeDBName() + " ASC");

                ABudgetAccess.LoadUsingTemplate(FMainDS, TemplateRow, Operators, null, Transaction, OrderList, 0, 0);
                ABudgetPeriodAccess.LoadViaABudgetTemplate(FMainDS, TemplateRow, Operators, null, Transaction, OrderList, 0, 0);

                ABudgetRevisionAccess.LoadByPrimaryKey(FMainDS, ALedgerNumber, CurrentFinancialYear, 0, Transaction);

                ABudgetTypeAccess.LoadAll(FMainDS, Transaction);
            });

            // Accept row changes here so that the Client gets 'unmodified' rows
            FMainDS.AcceptChanges();

            // Remove all Tables that were not filled with data before remoting them.
            FMainDS.RemoveEmptyTables();

            return(FMainDS);
        }
        } // Consolidate Budgets

        /// <summary>
        /// Complete the Budget consolidation process
        /// </summary>
        /// <param name="ATransaction"></param>
        private static void FinishConsolidateBudget(
            TDBTransaction ATransaction)
        {
            /*Consolidate_Budget*/
            foreach (ABudgetRow BudgetRow in FBudgetTDS.ABudget.Rows)
            {
                BudgetRow.BudgetStatus = true;
            }

            ABudgetAccess.SubmitChanges(FBudgetTDS.ABudget, ATransaction);
        }
        /// <summary>
        /// Import the budget from a CSV file
        /// </summary>
        /// <param name="ALedgerNumber"></param>
        /// <param name="ACurrentBudgetYear"></param>
        /// <param name="ACSVFileName"></param>
        /// <param name="AFdlgSeparator"></param>
        /// <param name="AImportDS"></param>
        /// <param name="AVerificationResult"></param>
        /// <returns>Total number of records imported and number of which updated as the fractional part</returns>
        private static decimal ImportBudgetFromCSV(Int32 ALedgerNumber,
                                                   Int32 ACurrentBudgetYear,
                                                   string ACSVFileName,
                                                   string[] AFdlgSeparator,
                                                   ref BudgetTDS AImportDS,
                                                   ref TVerificationResultCollection AVerificationResult)
        {
            StreamReader DataFile = new StreamReader(ACSVFileName, System.Text.Encoding.Default);

            string Separator    = AFdlgSeparator[0];
            string DateFormat   = AFdlgSeparator[1];
            string NumberFormat = AFdlgSeparator[2];

            FCultureInfoNumberFormat = new CultureInfo(NumberFormat.Equals("American") ? "en-US" : "de-DE");
            CultureInfo MyCultureInfoDate = new CultureInfo("en-GB");

            MyCultureInfoDate.DateTimeFormat.ShortDatePattern = DateFormat;

            // To store the From and To currencies
            // Use an array to store these to make for easy
            //   inverting of the two currencies when calculating
            //   the inverse value.

            //string currentBudgetVal = string.Empty;
            //string mess = string.Empty;
            string CostCentre       = string.Empty;
            string Account          = string.Empty;
            string budgetType       = string.Empty;
            string budgetYearString = string.Empty;
            int    budgetYear       = 0;

            Int32 numPeriods = TAccountingPeriodsWebConnector.GetNumberOfPeriods(ALedgerNumber);

            decimal[] BudgetPeriods         = new decimal[numPeriods];
            int       YearForBudgetRevision = 0;
            int       BdgRevision           = 0; //not currently implementing versioning so always zero

            decimal rowNumber          = 0;
            decimal duplicateRowNumber = 0;

            while (!DataFile.EndOfStream)
            {
                decimal totalBudgetRowAmount = 0;

                try
                {
                    string Line = DataFile.ReadLine();

                    CostCentre = StringHelper.GetNextCSV(ref Line, Separator, false).ToString();

                    if (CostCentre == "Cost Centre")
                    {
                        //Read the next line
                        Line       = DataFile.ReadLine();
                        CostCentre = StringHelper.GetNextCSV(ref Line, Separator, false).ToString();
                    }

                    //Increment row number
                    rowNumber++;

                    //Convert separator to a char
                    // char Sep = Separator[0];
                    //Turn current line into string array of column values
                    // string[] CsvColumns = Line.Split(Sep);

                    //int NumCols = CsvColumns.Length;

                    //If number of columns is not 4 then import csv file is wrongly formed.
//                if (NumCols != 24)
//                {
//                    AVerificationResult. MessageBox.Show(Catalog.GetString("Failed to import the CSV budget file:\r\n\r\n" +
//                            "   " + ADataFilename + "\r\n\r\n" +
//                            "It contains " + NumCols.ToString() + " columns. " +
//                            ), AImportMode + " Exchange Rates Import Error");
//                    return;
//                }

                    //Read the values for the current line
                    Account    = StringHelper.GetNextCSV(ref Line, Separator, false).ToString();
                    budgetType = StringHelper.GetNextCSV(ref Line, Separator, false).ToString().ToUpper();
                    budgetType = budgetType.Replace(" ", ""); //Ad hoc will become ADHOC

                    //Allow for variations on Inf.Base and Inf.N
                    if (budgetType.Contains("INF"))
                    {
                        if (budgetType.Contains("BASE"))
                        {
                            if (budgetType != MFinanceConstants.BUDGET_INFLATE_BASE)
                            {
                                budgetType = MFinanceConstants.BUDGET_INFLATE_BASE;
                            }
                        }
                        else if (budgetType != MFinanceConstants.BUDGET_INFLATE_N)
                        {
                            budgetType = MFinanceConstants.BUDGET_INFLATE_N;
                        }
                    }

                    if ((budgetType != MFinanceConstants.BUDGET_ADHOC) &&
                        (budgetType != MFinanceConstants.BUDGET_SAME) &&
                        (budgetType != MFinanceConstants.BUDGET_INFLATE_N) &&
                        (budgetType != MFinanceConstants.BUDGET_SPLIT) &&
                        (budgetType != MFinanceConstants.BUDGET_INFLATE_BASE)
                        )
                    {
                        throw new InvalidOperationException("Budget Type: " + budgetType + " in row: " + rowNumber.ToString() + " does not exist.");
                    }

                    //Calculate the budget Year
                    budgetYearString = StringHelper.GetNextCSV(ref Line, Separator, false);

                    YearForBudgetRevision = BudgetRevisionYearNumber(ALedgerNumber, budgetYearString);

                    //Add budget revision record if there's not one already.
                    if (AImportDS.ABudgetRevision.Rows.Find(new object[] { ALedgerNumber, YearForBudgetRevision, BdgRevision }) == null)
                    {
                        ABudgetRevisionRow BudgetRevisionRow = (ABudgetRevisionRow)AImportDS.ABudgetRevision.NewRowTyped();
                        BudgetRevisionRow.LedgerNumber = ALedgerNumber;
                        BudgetRevisionRow.Year         = YearForBudgetRevision;
                        BudgetRevisionRow.Revision     = BdgRevision;
                        BudgetRevisionRow.Description  = "Budget Import from: " + ACSVFileName;
                        AImportDS.ABudgetRevision.Rows.Add(BudgetRevisionRow);
                    }

                    //Read the budgetperiod values to check if valid according to type
                    Array.Clear(BudgetPeriods, 0, numPeriods);

                    bool successfulBudgetRowProcessing = ProcessBudgetTypeImportDetails(ref Line, Separator, budgetType, ref BudgetPeriods);

                    for (int i = 0; i < numPeriods; i++)
                    {
                        totalBudgetRowAmount += BudgetPeriods[i];
                    }

                    if (!successfulBudgetRowProcessing)
                    {
                        throw new InvalidOperationException(String.Format(
                                                                "The budget in row {0} for Ledger: {1}, Year: {2}, Cost Centre: {3} and Account: {4}, does not have values consistent with Budget Type: {5}.",
                                                                rowNumber,
                                                                ALedgerNumber,
                                                                budgetYear,
                                                                CostCentre,
                                                                Account,
                                                                budgetType));
                    }

                    BudgetTDS MainDS = new BudgetTDS();

                    TDBTransaction transaction = null;
                    DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted,
                                                                              TEnforceIsolationLevel.eilMinimum,
                                                                              ref transaction,
                                                                              delegate
                    {
                        ABudgetAccess.LoadByUniqueKey(MainDS, ALedgerNumber, YearForBudgetRevision, BdgRevision, CostCentre, Account, transaction);
                        //TODO: need to filter on ABudgetPeriod using LoadViaBudget or LoadViaUniqueKey
                    });

                    //Check to see if the budget combination already exists:
                    if (MainDS.ABudget.Count > 0)
                    {
                        ABudgetRow BR2 = (ABudgetRow)MainDS.ABudget.Rows[0];

                        int BTSeq = BR2.BudgetSequence;

                        ABudgetRow BdgTRow = (ABudgetRow)AImportDS.ABudget.Rows.Find(new object[] { BTSeq });

                        if (BdgTRow != null)
                        {
                            duplicateRowNumber++;

                            BdgTRow.BeginEdit();
                            //Edit the new budget row
                            BdgTRow.BudgetTypeCode = budgetType;
                            BdgTRow.EndEdit();

                            ABudgetPeriodRow BPRow = null;

                            for (int i = 0; i < numPeriods; i++)
                            {
                                BPRow = (ABudgetPeriodRow)AImportDS.ABudgetPeriod.Rows.Find(new object[] { BTSeq, i + 1 });

                                if (BPRow != null)
                                {
                                    BPRow.BeginEdit();
                                    BPRow.BudgetBase = BudgetPeriods[i];
                                    BPRow.EndEdit();
                                }

                                BPRow = null;
                            }
                        }
                    }
                    else
                    {
                        //Add the new budget row
                        ABudgetRow BudgetRow   = (ABudgetRow)AImportDS.ABudget.NewRowTyped();
                        int        newSequence = -1 * (AImportDS.ABudget.Rows.Count + 1);

                        BudgetRow.BudgetSequence = newSequence;
                        BudgetRow.LedgerNumber   = ALedgerNumber;
                        BudgetRow.Year           = YearForBudgetRevision;
                        BudgetRow.Revision       = BdgRevision;
                        BudgetRow.CostCentreCode = CostCentre;
                        BudgetRow.AccountCode    = Account;
                        BudgetRow.BudgetTypeCode = budgetType;
                        AImportDS.ABudget.Rows.Add(BudgetRow);

                        //Add the budget periods
                        for (int i = 0; i < numPeriods; i++)
                        {
                            ABudgetPeriodRow BudgetPeriodRow = (ABudgetPeriodRow)AImportDS.ABudgetPeriod.NewRowTyped();
                            BudgetPeriodRow.BudgetSequence = newSequence;
                            BudgetPeriodRow.PeriodNumber   = i + 1;
                            BudgetPeriodRow.BudgetBase     = BudgetPeriods[i];
                            AImportDS.ABudgetPeriod.Rows.Add(BudgetPeriodRow);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }

            DataFile.Close();

            if (duplicateRowNumber > 0)
            {
                //fractional part is the number of updates divided by 10000
                if (duplicateRowNumber < 10000)
                {
                    rowNumber += (duplicateRowNumber / 10000);
                }
            }

            return(rowNumber);
        }
        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;
            });
        }