Ejemplo n.º 1
0
        public static decimal SetBudgetPeriodBaseAmount(int ABudgetSequence, int APeriodNumber, decimal ABudgetAmount)
        {
            decimal retVal = 0;

            ABudgetPeriodTable BudgetPeriodTable = FMainDS.ABudgetPeriod;
            ABudgetPeriodRow   BudgetPeriodRow   = (ABudgetPeriodRow)BudgetPeriodTable.Rows.Find(new object[] { ABudgetSequence, APeriodNumber });

            if (BudgetPeriodRow != null)
            {
                BudgetPeriodRow.BeginEdit();
                BudgetPeriodRow.BudgetBase = ABudgetAmount;
                BudgetPeriodRow.EndEdit();

                retVal = ABudgetAmount;
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        /// <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);
        }