public static bool GetAccountingYearPeriodByDate(Int32 ALedgerNumber, DateTime ADate, out Int32 AYearNumber, out Int32 APeriodNumber) { bool newTransaction; Int32 CurrentFinancialYear; //Set the year to return AYearNumber = FindFinancialYearByDate(ALedgerNumber, ADate); if (AYearNumber == 99) { AYearNumber = 0; APeriodNumber = 0; return(false); } TDBTransaction Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.Serializable, out newTransaction); ALedgerTable LedgerTable = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, Transaction); CurrentFinancialYear = ((ALedgerRow)LedgerTable.Rows[0]).CurrentFinancialYear; AAccountingPeriodTable AccPeriodTableTmp = new AAccountingPeriodTable(); AAccountingPeriodRow TemplateRow = AccPeriodTableTmp.NewRowTyped(false); TemplateRow.LedgerNumber = ALedgerNumber; TemplateRow.PeriodStartDate = ADate.AddYears(CurrentFinancialYear - AYearNumber); TemplateRow.PeriodEndDate = ADate.AddYears(CurrentFinancialYear - AYearNumber); StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "<=", ">=" }); AAccountingPeriodTable AccountingPeriodTable = AAccountingPeriodAccess.LoadUsingTemplate(TemplateRow, operators, null, Transaction); if ((AccountingPeriodTable == null) || (AccountingPeriodTable.Count == 0)) { if (newTransaction) { DBAccess.GDBAccessObj.RollbackTransaction(); } APeriodNumber = 0; return(false); } AAccountingPeriodRow AccountingPeriodRow = (AAccountingPeriodRow)AccountingPeriodTable.Rows[0]; APeriodNumber = AccountingPeriodRow.AccountingPeriodNumber; if (newTransaction) { DBAccess.GDBAccessObj.RollbackTransaction(); } return(true); }
public static bool GetAccountingYearPeriodByDate(Int32 ALedgerNumber, DateTime ADate, out Int32 AYearNumber, out Int32 APeriodNumber) { Int32 CurrentFinancialYear; //Set the year to return Int32 YearNumber = FindFinancialYearByDate(ALedgerNumber, ADate); AYearNumber = YearNumber; if (AYearNumber == 99) { AYearNumber = 0; APeriodNumber = 0; return(false); } Int32 PeriodNumber = 0; TDBTransaction transaction = null; DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.Serializable, ref transaction, delegate { ALedgerTable LedgerTable = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, transaction); CurrentFinancialYear = ((ALedgerRow)LedgerTable.Rows[0]).CurrentFinancialYear; AAccountingPeriodTable AccPeriodTableTmp = new AAccountingPeriodTable(); AAccountingPeriodRow TemplateRow = AccPeriodTableTmp.NewRowTyped(false); TemplateRow.LedgerNumber = ALedgerNumber; TemplateRow.PeriodStartDate = ADate.AddYears(CurrentFinancialYear - YearNumber); TemplateRow.PeriodEndDate = ADate.AddYears(CurrentFinancialYear - YearNumber); StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "<=", ">=" }); AAccountingPeriodTable AccountingPeriodTable = AAccountingPeriodAccess.LoadUsingTemplate(TemplateRow, operators, null, transaction); if (AccountingPeriodTable.Count == 0) { return; } AAccountingPeriodRow AccountingPeriodRow = (AAccountingPeriodRow)AccountingPeriodTable.Rows[0]; PeriodNumber = AccountingPeriodRow.AccountingPeriodNumber; }); APeriodNumber = PeriodNumber; return(true); } // Get AccountingYear Period ByDate
public static bool GetAccountingYearPeriodByDate(Int32 ALedgerNumber, DateTime ADate, out Int32 AYearNumber, out Int32 APeriodNumber) { Int32 CurrentFinancialYear; //Set the year to return Int32 YearNumber = FindFinancialYearByDate(ALedgerNumber, ADate); AYearNumber = YearNumber; if (AYearNumber == 99) { AYearNumber = 0; APeriodNumber = 0; return false; } Int32 PeriodNumber = 0; TDBTransaction transaction = null; DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.Serializable, ref transaction, delegate { ALedgerTable LedgerTable = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, transaction); CurrentFinancialYear = ((ALedgerRow)LedgerTable.Rows[0]).CurrentFinancialYear; AAccountingPeriodTable AccPeriodTableTmp = new AAccountingPeriodTable(); AAccountingPeriodRow TemplateRow = AccPeriodTableTmp.NewRowTyped(false); TemplateRow.LedgerNumber = ALedgerNumber; TemplateRow.PeriodStartDate = ADate.AddYears(CurrentFinancialYear - YearNumber); TemplateRow.PeriodEndDate = ADate.AddYears(CurrentFinancialYear - YearNumber); StringCollection operators = StringHelper.InitStrArr(new string[] { "=", "<=", ">=" }); AAccountingPeriodTable AccountingPeriodTable = AAccountingPeriodAccess.LoadUsingTemplate(TemplateRow, operators, null, transaction); if (AccountingPeriodTable.Count == 0) { return; } AAccountingPeriodRow AccountingPeriodRow = (AAccountingPeriodRow)AccountingPeriodTable.Rows[0]; PeriodNumber = AccountingPeriodRow.AccountingPeriodNumber; }); APeriodNumber = PeriodNumber; return true; } // Get AccountingYear Period ByDate
public static TSubmitChangesResult SaveLedgerSettings( Int32 ALedgerNumber, DateTime ACalendarStartDate, ref GLSetupTDS AInspectDS) { #region Validate Arguments if (ALedgerNumber <= 0) { throw new EFinanceSystemInvalidLedgerNumberException(String.Format(Catalog.GetString( "Function:{0} - The Ledger number must be greater than 0!"), Utilities.GetMethodName(true)), ALedgerNumber); } else if (AInspectDS == null) { return TSubmitChangesResult.scrNothingToBeSaved; } #endregion Validate Arguments ALedgerTable LedgerTable; ALedgerRow LedgerRow; AAccountingPeriodTable AccountingPeriodTable; AAccountingPeriodRow AccountingPeriodRow; AAccountingPeriodTable NewAccountingPeriodTable; AAccountingPeriodRow NewAccountingPeriodRow; AGeneralLedgerMasterTable GLMTable; AGeneralLedgerMasterRow GLMRow; AGeneralLedgerMasterPeriodTable GLMPeriodTable; AGeneralLedgerMasterPeriodTable TempGLMPeriodTable; AGeneralLedgerMasterPeriodTable NewGLMPeriodTable; AGeneralLedgerMasterPeriodRow GLMPeriodRow; AGeneralLedgerMasterPeriodRow TempGLMPeriodRow; AGeneralLedgerMasterPeriodRow NewGLMPeriodRow; int CurrentNumberPeriods; int NewNumberPeriods; int CurrentNumberFwdPostingPeriods; int NewNumberFwdPostingPeriods; int CurrentLastFwdPeriod; int NewLastFwdPeriod; int Period; Boolean ExtendFwdPeriods = false; DateTime PeriodStartDate; DateTime CurrentCalendarStartDate; Boolean CreateCalendar = false; TDBTransaction Transaction = null; bool SubmissionOK = false; GLSetupTDS InspectDS = AInspectDS; try { DBAccess.GDBAccessObj.GetNewOrExistingAutoTransaction(IsolationLevel.Serializable, TEnforceIsolationLevel.eilMinimum, ref Transaction, ref SubmissionOK, delegate { // load ledger row currently saved in database so it can be used for comparison with modified data LedgerTable = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, Transaction); #region Validate Data if ((LedgerTable == null) || (LedgerTable.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 LedgerRow = (ALedgerRow)LedgerTable.Rows[0]; if (InspectDS.ALedger != null) { // initialize variables for accounting periods and forward periods CurrentNumberPeriods = LedgerRow.NumberOfAccountingPeriods; NewNumberPeriods = ((ALedgerRow)(InspectDS.ALedger.Rows[0])).NumberOfAccountingPeriods; CurrentNumberFwdPostingPeriods = LedgerRow.NumberFwdPostingPeriods; NewNumberFwdPostingPeriods = ((ALedgerRow)(InspectDS.ALedger.Rows[0])).NumberFwdPostingPeriods; // retrieve currently saved calendar start date (start date of financial year) AAccountingPeriodTable CalendarTable = AAccountingPeriodAccess.LoadByPrimaryKey(ALedgerNumber, 1, Transaction); CurrentCalendarStartDate = DateTime.MinValue; if (CalendarTable.Count > 0) { CurrentCalendarStartDate = ((AAccountingPeriodRow)CalendarTable.Rows[0]).PeriodStartDate; } // update accounting periods (calendar): // this only needs to be done if the calendar mode is changed // or if calendar mode is monthly and the start date has changed // or if not monthly and number of periods has changed if (((ALedgerRow)(InspectDS.ALedger.Rows[0])).CalendarMode != LedgerRow.CalendarMode) { CreateCalendar = true; } else if (((ALedgerRow)(InspectDS.ALedger.Rows[0])).CalendarMode && (ACalendarStartDate != CurrentCalendarStartDate)) { CreateCalendar = true; } else if (!((ALedgerRow)(InspectDS.ALedger.Rows[0])).CalendarMode && (NewNumberPeriods != CurrentNumberPeriods)) { CreateCalendar = true; } if (!CreateCalendar && (NewNumberFwdPostingPeriods < CurrentNumberFwdPostingPeriods)) { CreateCalendar = true; } if (!CreateCalendar && (NewNumberFwdPostingPeriods > CurrentNumberFwdPostingPeriods)) { // in this case only extend the periods (as there may already be existing transactions) ExtendFwdPeriods = true; } // now perform the actual update of accounting periods (calendar) if (CreateCalendar) { // first make sure all accounting period records are deleted if (AAccountingPeriodAccess.CountViaALedger(ALedgerNumber, Transaction) > 0) { AAccountingPeriodTable TemplateTable = new AAccountingPeriodTable(); AAccountingPeriodRow TemplateRow = TemplateTable.NewRowTyped(false); TemplateRow.LedgerNumber = ALedgerNumber; AAccountingPeriodAccess.DeleteUsingTemplate(TemplateRow, null, Transaction); } // now create all accounting period records according to monthly calendar mode // (at the same time create forwarding periods. If number of forwarding periods also // changes with this saving method then this will be dealt with further down in the code) NewAccountingPeriodTable = new AAccountingPeriodTable(); PeriodStartDate = ACalendarStartDate; for (Period = 1; Period <= NewNumberPeriods; Period++) { NewAccountingPeriodRow = NewAccountingPeriodTable.NewRowTyped(); NewAccountingPeriodRow.LedgerNumber = ALedgerNumber; NewAccountingPeriodRow.AccountingPeriodNumber = Period; NewAccountingPeriodRow.PeriodStartDate = PeriodStartDate; //TODO: Calendar vs Financial Date Handling - Check for current ledger number of periods if ((((ALedgerRow)(InspectDS.ALedger.Rows[0])).NumberOfAccountingPeriods == 13) && (Period == 12)) { // in case of 12 periods the second last period represents the last month except for the very last day NewAccountingPeriodRow.PeriodEndDate = PeriodStartDate.AddMonths(1).AddDays(-2); } else if ((((ALedgerRow)(InspectDS.ALedger.Rows[0])).NumberOfAccountingPeriods == 13) && (Period == 13)) { // in case of 13 periods the last period just represents the very last day of the financial year NewAccountingPeriodRow.PeriodEndDate = PeriodStartDate; } else { NewAccountingPeriodRow.PeriodEndDate = PeriodStartDate.AddMonths(1).AddDays(-1); } NewAccountingPeriodRow.AccountingPeriodDesc = PeriodStartDate.ToString("MMMM"); NewAccountingPeriodTable.Rows.Add(NewAccountingPeriodRow); PeriodStartDate = NewAccountingPeriodRow.PeriodEndDate.AddDays(1); } AAccountingPeriodAccess.SubmitChanges(NewAccountingPeriodTable, Transaction); TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing( TCacheableFinanceTablesEnum.AccountingPeriodList.ToString()); CurrentNumberPeriods = NewNumberPeriods; } // check if any new forwarding periods need to be created if (CreateCalendar || ExtendFwdPeriods) { // now create new forwarding posting periods (if at all needed) NewAccountingPeriodTable = new AAccountingPeriodTable(); // if calendar was created then there are no forward periods yet if (CreateCalendar) { Period = CurrentNumberPeriods + 1; } else { Period = CurrentNumberPeriods + CurrentNumberFwdPostingPeriods + 1; } while (Period <= NewNumberPeriods + NewNumberFwdPostingPeriods) { AccountingPeriodTable = AAccountingPeriodAccess.LoadByPrimaryKey(ALedgerNumber, Period - CurrentNumberPeriods, Transaction); AccountingPeriodRow = (AAccountingPeriodRow)AccountingPeriodTable.Rows[0]; NewAccountingPeriodRow = NewAccountingPeriodTable.NewRowTyped(); NewAccountingPeriodRow.LedgerNumber = ALedgerNumber; NewAccountingPeriodRow.AccountingPeriodNumber = Period; NewAccountingPeriodRow.AccountingPeriodDesc = AccountingPeriodRow.AccountingPeriodDesc; NewAccountingPeriodRow.PeriodStartDate = AccountingPeriodRow.PeriodStartDate.AddYears(1); NewAccountingPeriodRow.PeriodEndDate = AccountingPeriodRow.PeriodEndDate.AddYears(1); NewAccountingPeriodTable.Rows.Add(NewAccountingPeriodRow); Period++; } AAccountingPeriodAccess.SubmitChanges(NewAccountingPeriodTable, Transaction); TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing( TCacheableFinanceTablesEnum.AccountingPeriodList.ToString()); // also create new general ledger master periods with balances CurrentLastFwdPeriod = LedgerRow.NumberOfAccountingPeriods + CurrentNumberFwdPostingPeriods; NewLastFwdPeriod = LedgerRow.NumberOfAccountingPeriods + NewNumberFwdPostingPeriods; // TODO: the following 2 lines would need to replace the 2 lines above if not all possible forward periods are created initially //CurrentLastFwdPeriod = LedgerRow.CurrentPeriod + CurrentNumberFwdPostingPeriods; //NewLastFwdPeriod = LedgerRow.CurrentPeriod + NewNumberFwdPostingPeriods; GLMTable = new AGeneralLedgerMasterTable(); AGeneralLedgerMasterRow template = GLMTable.NewRowTyped(false); template.LedgerNumber = ALedgerNumber; template.Year = LedgerRow.CurrentFinancialYear; // find all general ledger master records of the current financial year for given ledger GLMTable = AGeneralLedgerMasterAccess.LoadUsingTemplate(template, Transaction); NewGLMPeriodTable = new AGeneralLedgerMasterPeriodTable(); foreach (DataRow Row in GLMTable.Rows) { // for each of the general ledger master records of the current financial year set the // new, extended forwarding glm period records (most likely they will not exist yet // but if they do then update values) GLMRow = (AGeneralLedgerMasterRow)Row; GLMPeriodTable = AGeneralLedgerMasterPeriodAccess.LoadByPrimaryKey(GLMRow.GlmSequence, CurrentLastFwdPeriod, Transaction); if (GLMPeriodTable.Count > 0) { GLMPeriodRow = (AGeneralLedgerMasterPeriodRow)GLMPeriodTable.Rows[0]; for (Period = CurrentLastFwdPeriod + 1; Period <= NewLastFwdPeriod; Period++) { if (AGeneralLedgerMasterPeriodAccess.Exists(GLMPeriodRow.GlmSequence, Period, Transaction)) { // if the record already exists then just change values TempGLMPeriodTable = AGeneralLedgerMasterPeriodAccess.LoadByPrimaryKey(GLMPeriodRow.GlmSequence, Period, Transaction); TempGLMPeriodRow = (AGeneralLedgerMasterPeriodRow)TempGLMPeriodTable.Rows[0]; TempGLMPeriodRow.ActualBase = GLMPeriodRow.ActualBase; TempGLMPeriodRow.ActualIntl = GLMPeriodRow.ActualIntl; if (!GLMPeriodRow.IsActualForeignNull()) { TempGLMPeriodRow.ActualForeign = GLMPeriodRow.ActualForeign; } else { TempGLMPeriodRow.SetActualForeignNull(); } NewGLMPeriodTable.Merge(TempGLMPeriodTable, true); } else { // add new row since it does not exist yet NewGLMPeriodRow = NewGLMPeriodTable.NewRowTyped(); NewGLMPeriodRow.GlmSequence = GLMPeriodRow.GlmSequence; NewGLMPeriodRow.PeriodNumber = Period; NewGLMPeriodRow.ActualBase = GLMPeriodRow.ActualBase; NewGLMPeriodRow.ActualIntl = GLMPeriodRow.ActualIntl; if (!GLMPeriodRow.IsActualForeignNull()) { NewGLMPeriodRow.ActualForeign = GLMPeriodRow.ActualForeign; } else { NewGLMPeriodRow.SetActualForeignNull(); } NewGLMPeriodTable.Rows.Add(NewGLMPeriodRow); } } // remove periods if the number of periods + forwarding periods has been reduced int NumberOfExistingPeriods = LedgerRow.NumberOfAccountingPeriods + LedgerRow.NumberFwdPostingPeriods; while ((NewNumberPeriods + NewNumberFwdPostingPeriods) < NumberOfExistingPeriods) { AGeneralLedgerMasterPeriodAccess.DeleteByPrimaryKey(GLMPeriodRow.GlmSequence, NumberOfExistingPeriods, Transaction); NumberOfExistingPeriods--; } } } // just one SubmitChanges for all records needed AGeneralLedgerMasterPeriodAccess.SubmitChanges(NewGLMPeriodTable, Transaction); } } // update a_ledger_init_flag records for: // suspense account flag: "SUSP-ACCT" // budget flag: "BUDGET" // base currency: "CURRENCY" // international currency: "INTL-CURRENCY" (this is a new flag for OpenPetra) // current period (start of ledger date): CURRENT-PERIOD // calendar settings: CAL AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_SUSP_ACC, LedgerRow.SuspenseAccountFlag, Transaction); AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_BUDGET, LedgerRow.BudgetControlFlag, Transaction); AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_CURRENCY, !LedgerRow.IsBaseCurrencyNull(), Transaction); AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_INTL_CURRENCY, !LedgerRow.IsIntlCurrencyNull(), Transaction); AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_CURRENT_PERIOD, !LedgerRow.IsCurrentPeriodNull(), Transaction); AddOrRemoveLedgerInitFlag(ALedgerNumber, MFinanceConstants.LEDGER_INIT_FLAG_CAL, !LedgerRow.IsNumberOfAccountingPeriodsNull(), Transaction); GLSetupTDSAccess.SubmitChanges(InspectDS); SubmissionOK = true; }); } catch (Exception ex) { TLogging.Log(String.Format("Method:{0} - Unexpected error!{1}{1}{2}", Utilities.GetMethodSignature(), Environment.NewLine, ex.Message)); throw ex; } return TSubmitChangesResult.scrOK; }