Exemple #1
0
        /// <summary>
        /// init the exchange rate, to avoid messages "Cannot find exchange rate for EUR USD"
        /// </summary>
        public static void InitExchangeRate()
        {
            TAccountPeriodInfo AccountingPeriodInfo =
                new TAccountPeriodInfo(FLedgerNumber, 1);
            ADailyExchangeRateTable dailyrates = new ADailyExchangeRateTable();
            ADailyExchangeRateRow   row        = dailyrates.NewRowTyped(true);

            row.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
            row.TimeEffectiveFrom = 100;
            row.FromCurrencyCode  = "USD";
            row.ToCurrencyCode    = "EUR";
            row.RateOfExchange    = 1.34m;
            dailyrates.Rows.Add(row);
            row = dailyrates.NewRowTyped(true);
            row.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
            row.TimeEffectiveFrom = 100;
            row.FromCurrencyCode  = "USD";
            row.ToCurrencyCode    = "GBP";
            row.RateOfExchange    = 1.57m;
            dailyrates.Rows.Add(row);

            if (!ADailyExchangeRateAccess.Exists(row.FromCurrencyCode, row.ToCurrencyCode, row.DateEffectiveFrom, row.TimeEffectiveFrom, null))
            {
                ADailyExchangeRateAccess.SubmitChanges(dailyrates, null);
            }

            ALedgerTable Ledger = ALedgerAccess.LoadByPrimaryKey(FLedgerNumber, null);

            for (int periodCounter = 1; periodCounter <= Ledger[0].NumberOfAccountingPeriods + Ledger[0].NumberFwdPostingPeriods; periodCounter++)
            {
                AccountingPeriodInfo = new TAccountPeriodInfo(FLedgerNumber, periodCounter);

                ACorporateExchangeRateTable corprates = new ACorporateExchangeRateTable();
                ACorporateExchangeRateRow   corprow   = corprates.NewRowTyped(true);
                corprow.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
                corprow.TimeEffectiveFrom = 100;
                corprow.FromCurrencyCode  = "USD";
                corprow.ToCurrencyCode    = "EUR";
                corprow.RateOfExchange    = 1.34m;
                corprates.Rows.Add(corprow);
                corprow = corprates.NewRowTyped(true);
                corprow.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
                corprow.TimeEffectiveFrom = 100;
                corprow.FromCurrencyCode  = "USD";
                corprow.ToCurrencyCode    = "GBP";
                corprow.RateOfExchange    = 1.57m;
                corprates.Rows.Add(corprow);

                if (!ACorporateExchangeRateAccess.Exists(corprow.FromCurrencyCode, corprow.ToCurrencyCode, corprow.DateEffectiveFrom, null))
                {
                    ACorporateExchangeRateAccess.SubmitChanges(corprates, null);
                }
            }
        }
        public static TSubmitChangesResult SaveData(string ATablename,
                                                    ref TTypedDataTable ASubmitTable, out TVerificationResultCollection AVerificationResult,
                                                    TDBTransaction AWriteTransaction)
        {
            AVerificationResult = null;

            // TODO: check write permissions
            string context = string.Format("SaveData {0}", SharedConstants.MODULE_ACCESS_MANAGER);

            if (ASubmitTable != null)
            {
                AVerificationResult = new TVerificationResultCollection();

                try
                {
                    if (ATablename == AAccountingPeriodTable.GetTableDBName())
                    {
                        AAccountingPeriodAccess.SubmitChanges((AAccountingPeriodTable)ASubmitTable, AWriteTransaction);

                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheableFinanceTablesEnum.AccountingPeriodList.ToString());
                    }
                    else if (ATablename == ACurrencyTable.GetTableDBName())
                    {
                        ACurrencyAccess.SubmitChanges((ACurrencyTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ADailyExchangeRateTable.GetTableDBName())
                    {
                        TSecurityChecks.CheckUserModulePermissions(
                            string.Format("AND({0},{1})", SharedConstants.PETRAGROUP_FINANCE1, SharedConstants.PETRAMODULE_FINEXRATE),
                            context);
                        ADailyExchangeRateAccess.SubmitChanges((ADailyExchangeRateTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ACorporateExchangeRateTable.GetTableDBName())
                    {
                        // AlanP:  I don't think this is used any more.  There is a TDS Save method instead
                        ACorporateExchangeRateAccess.SubmitChanges((ACorporateExchangeRateTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ACurrencyLanguageTable.GetTableDBName())
                    {
                        ACurrencyLanguageAccess.SubmitChanges((ACurrencyLanguageTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == AFeesPayableTable.GetTableDBName())
                    {
                        AFeesPayableAccess.SubmitChanges((AFeesPayableTable)ASubmitTable, AWriteTransaction);

                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheableFinanceTablesEnum.FeesPayableList.ToString());
                    }
                    else if (ATablename == AFeesReceivableTable.GetTableDBName())
                    {
                        AFeesReceivableAccess.SubmitChanges((AFeesReceivableTable)ASubmitTable, AWriteTransaction);

                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheableFinanceTablesEnum.FeesReceivableList.ToString());
                    }
                    else if (ATablename == AGiftBatchTable.GetTableDBName())
                    {
                        // This method is called from ADailyExchangeRate Setup - please do not remove
                        // The method is not required for changes made to the gift batch screens, which use a TDS
                        AGiftBatchAccess.SubmitChanges((AGiftBatchTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == AJournalTable.GetTableDBName())
                    {
                        // This method is called from ADailyExchangeRate Setup - please do not remove
                        // The method is not required for changes made to the journal screens, which use a TDS
                        AJournalAccess.SubmitChanges((AJournalTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ARecurringJournalTable.GetTableDBName())
                    {
                        // This method is called from Submit Recurring GL Batch form - please do not remove
                        // The method is not required for changes made to the journal screens, which use a TDS
                        ARecurringJournalAccess.SubmitChanges((ARecurringJournalTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ALedgerTable.GetTableDBName())
                    {
                        // This method is called from ADailyExchangeRate Testing - please do not remove
                        ALedgerAccess.SubmitChanges((ALedgerTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == AAnalysisTypeTable.GetTableDBName())
                    {
                        AAnalysisTypeAccess.SubmitChanges((AAnalysisTypeTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == ASuspenseAccountTable.GetTableDBName())
                    {
                        ASuspenseAccountAccess.SubmitChanges((ASuspenseAccountTable)ASubmitTable, AWriteTransaction);

                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheableFinanceTablesEnum.SuspenseAccountList.ToString());
                    }
                    else if (ATablename == PcAttendeeTable.GetTableDBName())
                    {
                        PcAttendeeAccess.SubmitChanges((PcAttendeeTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PcConferenceTable.GetTableDBName())
                    {
                        PcConferenceAccess.SubmitChanges((PcConferenceTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PcConferenceCostTable.GetTableDBName())
                    {
                        PcConferenceCostAccess.SubmitChanges((PcConferenceCostTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PcEarlyLateTable.GetTableDBName())
                    {
                        PcEarlyLateAccess.SubmitChanges((PcEarlyLateTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PcSupplementTable.GetTableDBName())
                    {
                        PcSupplementAccess.SubmitChanges((PcSupplementTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PcDiscountTable.GetTableDBName())
                    {
                        PcDiscountAccess.SubmitChanges((PcDiscountTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PInternationalPostalTypeTable.GetTableDBName())
                    {
                        ValidateInternationalPostalType(ref AVerificationResult, ASubmitTable);
                        ValidateInternationalPostalTypeManual(ref AVerificationResult, ASubmitTable);

                        if (TVerificationHelper.IsNullOrOnlyNonCritical(AVerificationResult))
                        {
                            PInternationalPostalTypeAccess.SubmitChanges((PInternationalPostalTypeTable)ASubmitTable, AWriteTransaction);
                        }
                    }
                    else if (ATablename == PtApplicationTypeTable.GetTableDBName())
                    {
                        PtApplicationTypeAccess.SubmitChanges((PtApplicationTypeTable)ASubmitTable, AWriteTransaction);

                        // mark dependent lists for needing to be refreshed since there was a change in base list
                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheablePersonTablesEnum.EventApplicationTypeList.ToString());
                        TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                            TCacheablePersonTablesEnum.FieldApplicationTypeList.ToString());
                    }
                    else if (ATablename == PFormTable.GetTableDBName())
                    {
                        PFormAccess.SubmitChanges((PFormTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PFormalityTable.GetTableDBName())
                    {
                        PFormalityAccess.SubmitChanges((PFormalityTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PMailingTable.GetTableDBName())
                    {
                        PMailingAccess.SubmitChanges((PMailingTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PPartnerGiftDestinationTable.GetTableDBName())
                    {
                        PPartnerGiftDestinationAccess.SubmitChanges((PPartnerGiftDestinationTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == PmDocumentTypeTable.GetTableDBName())
                    {
                        PmDocumentTypeAccess.SubmitChanges((PmDocumentTypeTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == SGroupTable.GetTableDBName())
                    {
                        SGroupAccess.SubmitChanges((SGroupTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == SSystemDefaultsTable.GetTableDBName())
                    {
                        SSystemDefaultsAccess.SubmitChanges((SSystemDefaultsTable)ASubmitTable, AWriteTransaction);
                    }
                    else if (ATablename == SSystemDefaultsGuiTable.GetTableDBName())
                    {
                        SSystemDefaultsGuiAccess.SubmitChanges((SSystemDefaultsGuiTable)ASubmitTable, AWriteTransaction);
                    }
                    else
                    {
                        throw new EOPAppException("TCommonDataReader.SaveData: unknown table '" + ATablename + "'");
                    }
                }
                catch (Exception Exc)
                {
                    AVerificationResult.Add(
                        new TVerificationResult(null, "Cannot SubmitChanges:" + Environment.NewLine +
                                                Exc.Message, "UNDEFINED", TResultSeverity.Resv_Critical));
                }
            }

            if ((AVerificationResult != null) &&
                (AVerificationResult.Count > 0))
            {
                // Downgrade TScreenVerificationResults to TVerificationResults in order to allow
                // Serialisation (needed for .NET Remoting).
                TVerificationResultCollection.DowngradeScreenVerificationResults(AVerificationResult);

                return(AVerificationResult.HasCriticalErrors ? TSubmitChangesResult.scrError : TSubmitChangesResult.scrOK);
            }

            return(TSubmitChangesResult.scrOK);
        }
Exemple #3
0
        public static TSubmitChangesResult SaveCorporateExchangeSetupTDS(ref CorporateExchangeSetupTDS AInspectDS,
                                                                         out int ATransactionsChanged, out TVerificationResultCollection AVerificationResult)
        {
            AVerificationResult  = new TVerificationResultCollection();
            ATransactionsChanged = -1;
            int TransactionsChanged = -1;

            AInspectDS = AInspectDS.GetChangesTyped(true);

            if (AInspectDS == null)
            {
                AVerificationResult.Add(new TVerificationResult(
                                            Catalog.GetString("Save Corportate Exchange Rates"),
                                            Catalog.GetString("No changes - nothing to do"),
                                            TResultSeverity.Resv_Info));
                return(TSubmitChangesResult.scrNothingToBeSaved);
            }

            TDBTransaction            Transaction  = null;
            bool                      SubmissionOK = false;
            CorporateExchangeSetupTDS InspectDS    = AInspectDS;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoTransaction(IsolationLevel.Serializable,
                                                                  ref Transaction, ref SubmissionOK,
                                                                  delegate
            {
                foreach (ACorporateExchangeRateRow Row in InspectDS.ACorporateExchangeRate.Rows)
                {
                    if ((Row.RowState == DataRowState.Modified) || (Row.RowState == DataRowState.Added))
                    {
                        // should only be -1 if no exchange rates were modified or created
                        if (TransactionsChanged == -1)
                        {
                            TransactionsChanged = 0;
                        }

                        // update international amounts for all gl transaction using modified or new exchange rate
                        string Query = "UPDATE a_transaction SET a_amount_in_intl_currency_n = " +
                                       "(a_amount_in_base_currency_n / " + Row.RateOfExchange.ToString(CultureInfo.InvariantCulture) + ")" +
                                       " FROM a_ledger" +
                                       " WHERE EXTRACT(MONTH FROM a_transaction.a_transaction_date_d) = " + Row.DateEffectiveFrom.Month +
                                       " AND EXTRACT(YEAR FROM a_transaction.a_transaction_date_d) = " + Row.DateEffectiveFrom.Year +
                                       " AND a_ledger.a_ledger_number_i = a_transaction.a_ledger_number_i" +
                                       " AND a_ledger.a_base_currency_c = '" + Row.FromCurrencyCode + "'" +
                                       " AND a_ledger.a_intl_currency_c = '" + Row.ToCurrencyCode + "'";

                        TransactionsChanged += DBAccess.GDBAccessObj.ExecuteNonQuery(Query, Transaction);
                    }
                }

                // save changes to exchange rates
                ACorporateExchangeRateAccess.SubmitChanges(InspectDS.ACorporateExchangeRate, Transaction);

                SubmissionOK = true;
            });

            TSubmitChangesResult SubmissionResult;

            if (SubmissionOK)
            {
                SubmissionResult = TSubmitChangesResult.scrOK;
            }
            else
            {
                SubmissionResult = TSubmitChangesResult.scrError;
            }

            AInspectDS           = InspectDS;
            ATransactionsChanged = TransactionsChanged;

            return(SubmissionResult);
        }
Exemple #4
0
        public static TSubmitChangesResult SaveCorporateExchangeSetupTDS(ref CorporateExchangeSetupTDS AInspectDS,
                                                                         out int ATransactionsChanged, out TVerificationResultCollection AVerificationResult)
        {
            AVerificationResult  = new TVerificationResultCollection();
            ATransactionsChanged = -1;
            int TransactionsChanged = -1;

            AInspectDS = AInspectDS.GetChangesTyped(true);

            if (AInspectDS == null)
            {
                AVerificationResult.Add(new TVerificationResult(
                                            Catalog.GetString("Save Corportate Exchange Rates"),
                                            Catalog.GetString("No changes - nothing to do"),
                                            TResultSeverity.Resv_Info));
                return(TSubmitChangesResult.scrNothingToBeSaved);
            }

            TDBTransaction            Transaction  = new TDBTransaction();
            TDataBase                 db           = DBAccess.Connect("SaveCorporateExchangeSetupTDS");
            bool                      SubmissionOK = true;
            CorporateExchangeSetupTDS InspectDS    = AInspectDS;

            db.WriteTransaction(
                ref Transaction, ref SubmissionOK,
                delegate
            {
                foreach (ACorporateExchangeRateRow xchangeRateRow in InspectDS.ACorporateExchangeRate.Rows)
                {
                    if ((xchangeRateRow.RowState == DataRowState.Modified) || (xchangeRateRow.RowState == DataRowState.Added))
                    {
                        // should only be -1 if no exchange rates were modified or created
                        if (TransactionsChanged == -1)
                        {
                            TransactionsChanged = 0;
                        }

                        String rateOfExchangeStr = xchangeRateRow.RateOfExchange.ToString(
                            CultureInfo.InvariantCulture);
                        // update international amounts for all gl transaction using modified or new exchange rate
                        string Query = "UPDATE a_transaction SET a_amount_in_intl_currency_n = " +
                                       "ROUND (a_amount_in_base_currency_n / " + rateOfExchangeStr + ", 2)" +
                                       " FROM a_ledger" +
                                       " WHERE EXTRACT(MONTH FROM a_transaction.a_transaction_date_d) = " +
                                       xchangeRateRow.DateEffectiveFrom.Month +
                                       " AND EXTRACT(YEAR FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Year +
                                       " AND a_ledger.a_ledger_number_i = a_transaction.a_ledger_number_i" +
                                       " AND a_ledger.a_base_currency_c = '" + xchangeRateRow.FromCurrencyCode + "'" +
                                       " AND a_ledger.a_intl_currency_c = '" + xchangeRateRow.ToCurrencyCode + "'";

                        TransactionsChanged += db.ExecuteNonQuery(Query, Transaction);
                    }

                    if (TransactionsChanged > 0)
                    {
                        //
                        // I also need to correct entries in GLM and GLMP after modifying these transactions:
                        DataTable ledgerTbl = db.SelectDT(
                            "SELECT * FROM a_ledger WHERE " +
                            " a_ledger.a_base_currency_c = '" + xchangeRateRow.FromCurrencyCode + "'" +
                            " AND a_ledger.a_intl_currency_c = '" + xchangeRateRow.ToCurrencyCode + "'",
                            "a_ledger", Transaction);

                        foreach (DataRow ledgerRow in ledgerTbl.Rows)
                        {
                            Int32 ledgerNumber = Convert.ToInt32(ledgerRow["a_ledger_number_i"]);
                            DataTable tempTbl  = db.SelectDT(
                                "SELECT DISTINCT a_batch.a_batch_period_i, a_batch.a_batch_year_i FROM" +
                                " a_batch, a_transaction, a_ledger WHERE" +
                                " EXTRACT(MONTH FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Month +
                                " AND EXTRACT(YEAR FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Year +
                                " AND a_transaction.a_ledger_number_i =" + ledgerNumber +
                                " AND a_batch.a_batch_number_i = a_transaction.a_batch_number_i " +
                                " ORDER BY a_batch_period_i",
                                "temp", Transaction);

                            if ((tempTbl == null) || (tempTbl.Rows.Count == 0))
                            {
                                continue;
                            }

                            Int32 transactionPeriod = Convert.ToInt32(tempTbl.Rows[0]["a_batch_period_i"]);
                            Int32 transactionYear   = Convert.ToInt32(tempTbl.Rows[0]["a_batch_year_i"]);

                            DataTable glmTbl = db.SelectDT(
                                "SELECT * from a_general_ledger_master" +
                                " WHERE a_ledger_number_i = " + ledgerNumber +
                                " AND a_year_i = " + transactionYear,
                                "temp", Transaction);
                            Boolean seemsToWorkOk = true;

//                              TLogging.Log("GLM correction: ");
                            Int32 glmSequence     = 0;
                            String accountCode    = "";
                            String costCentreCode = "";
                            String problem        = "";

                            foreach (DataRow glmRow in glmTbl.Rows)
                            {
                                glmSequence    = Convert.ToInt32(glmRow["a_glm_sequence_i"]);
                                accountCode    = glmRow["a_account_code_c"].ToString();
                                costCentreCode = glmRow["a_cost_centre_code_c"].ToString();

                                tempTbl = db.SelectDT(
                                    "SELECT sum(a_amount_in_intl_currency_n) AS debit_total FROM a_transaction WHERE " +
                                    " EXTRACT(MONTH FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Month +
                                    " AND EXTRACT(YEAR FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Year +
                                    " AND a_account_code_c = '" + accountCode + "'" +
                                    " AND a_cost_centre_code_c = '" + costCentreCode + "'" +
                                    " AND a_debit_credit_indicator_l", "temp", Transaction);

                                Boolean hasDebitTransactions = (
                                    tempTbl != null &&
                                    tempTbl.Rows[0]["debit_total"].GetType() != typeof(System.DBNull)
                                    );
                                Decimal debitTotal = 0;

                                if (hasDebitTransactions)
                                {
                                    seemsToWorkOk = (tempTbl.Rows.Count == 1);

                                    if (!seemsToWorkOk)
                                    {
                                        problem = "DebitTotal";
                                        break;
                                    }

                                    debitTotal = Convert.ToDecimal(tempTbl.Rows[0]["debit_total"]);
                                }

                                tempTbl = db.SelectDT(
                                    "SELECT sum(a_amount_in_intl_currency_n) AS credit_total FROM a_transaction WHERE " +
                                    " EXTRACT(MONTH FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Month +
                                    " AND EXTRACT(YEAR FROM a_transaction.a_transaction_date_d) = " + xchangeRateRow.DateEffectiveFrom.Year +
                                    " AND a_account_code_c = '" + accountCode + "'" +
                                    " AND a_cost_centre_code_c = '" + costCentreCode + "'" +
                                    " AND NOT a_debit_credit_indicator_l", "temp", Transaction);

                                Boolean hasCreditTransactions = (
                                    tempTbl != null &&
                                    tempTbl.Rows[0]["credit_total"].GetType() != typeof(System.DBNull)
                                    );
                                Decimal creditTotal = 0;

                                if (hasCreditTransactions)
                                {
                                    seemsToWorkOk = (tempTbl.Rows.Count == 1);

                                    if (!seemsToWorkOk)
                                    {
                                        problem = "CreditTotal";
                                        break;
                                    }

                                    creditTotal = Convert.ToDecimal(tempTbl.Rows[0]["credit_total"]);
                                }

                                if (!hasDebitTransactions && !hasCreditTransactions)
                                {
//                                      TLogging.Log("CostCentre " + costCentreCode + " Account " + accountCode + " - no transactions.");
                                    continue;
                                }

                                Decimal lastMonthBalance = 0;

                                if (transactionPeriod > 1)
                                {
                                    tempTbl = db.SelectDT(
                                        "SELECT a_actual_intl_n as last_month_balance " +
                                        " FROM a_general_ledger_master_period WHERE " +
                                        " a_glm_sequence_i = " + glmSequence + " AND a_period_number_i = " + (transactionPeriod - 1),
                                        "temp", Transaction);
                                    seemsToWorkOk = (tempTbl.Rows.Count == 1);

                                    if (!seemsToWorkOk)
                                    {
                                        problem = "lastMonthBalance";
                                        break;
                                    }

                                    lastMonthBalance = Convert.ToDecimal(tempTbl.Rows[0]["last_month_balance"]);
                                }
                                else
                                {
                                    lastMonthBalance = Convert.ToInt32(glmRow["a_start_balance_intl_n"]);
                                }

                                tempTbl = db.SelectDT(
                                    "SELECT a_actual_intl_n as this_month_balance " +
                                    " FROM  a_general_ledger_master_period WHERE " +
                                    " a_glm_sequence_i = " + glmSequence + " AND a_period_number_i = " + transactionPeriod,
                                    "temp", Transaction);

                                seemsToWorkOk = (tempTbl.Rows.Count == 1);

                                if (!seemsToWorkOk)
                                {
                                    problem = "thisMonthBalance";
                                    break;
                                }

                                Decimal thisMonthBalance = Convert.ToDecimal(tempTbl.Rows[0]["this_month_balance"]);

                                tempTbl = db.SelectDT(
                                    "SELECT a_debit_credit_indicator_l AS debit_indicator FROM " +
                                    " a_account WHERE a_account_code_c = '" + accountCode + "'" +
                                    " AND a_ledger_number_i = " + ledgerNumber,
                                    "temp", Transaction);
                                seemsToWorkOk = (tempTbl.Rows.Count == 1);

                                if (!seemsToWorkOk)
                                {
                                    problem = "debitCreditIndicator";
                                    break;
                                }

                                Boolean debitCreditIndicator = Convert.ToBoolean(tempTbl.Rows[0]["debit_indicator"]);

                                Decimal newPeriodBalance = (debitCreditIndicator) ?
                                                           lastMonthBalance + debitTotal - creditTotal
                                                               :
                                                           lastMonthBalance - debitTotal + creditTotal;

                                Decimal discrepency = newPeriodBalance - thisMonthBalance;

                                db.ExecuteNonQuery(
                                    "UPDATE a_general_ledger_master_period SET " +
                                    " a_actual_intl_n = a_actual_intl_n + " + discrepency.ToString(CultureInfo.InvariantCulture) +
                                    " WHERE a_glm_sequence_i = " + glmSequence +
                                    " AND a_period_number_i >= " + transactionPeriod, Transaction);

                                db.ExecuteNonQuery(
                                    "UPDATE a_general_ledger_master SET " +
                                    " a_ytd_actual_intl_n = a_ytd_actual_intl_n + " + discrepency.ToString(CultureInfo.InvariantCulture) +
                                    " WHERE a_glm_sequence_i = " + glmSequence, Transaction);

//                                  TLogging.Log("Discrepency for CostCentre " + costCentreCode + " Account " + accountCode + " is " + discrepency);
                            }     // foreach glmRow

                            if (!seemsToWorkOk)
                            {
                                TLogging.Log("SaveCorporateExchangeSetupTDS: unable to read " + problem + " for CostCentre " + costCentreCode +
                                             " Account " + accountCode);
                                SubmissionOK = false;
                            }
                        } // foreach ledgerRow
                    }     // if TransactionsChanged
                }

                // save changes to exchange rates
                ACorporateExchangeRateAccess.SubmitChanges(InspectDS.ACorporateExchangeRate, Transaction);
            });     // WriteTransaction

            TSubmitChangesResult SubmissionResult;

            if (SubmissionOK)
            {
                SubmissionResult = TSubmitChangesResult.scrOK;
            }
            else
            {
                SubmissionResult = TSubmitChangesResult.scrError;
            }

            AInspectDS           = InspectDS;
            ATransactionsChanged = TransactionsChanged;

            return(SubmissionResult);
        }
        public static TSubmitChangesResult SaveData(string ATablename,
                                                    ref TTypedDataTable ASubmitTable,
                                                    out TVerificationResultCollection AVerificationResult)
        {
            TDBTransaction  SubmitChangesTransaction = null;
            bool            SubmissionOK             = false;
            TTypedDataTable SubmitTable = ASubmitTable;

            TVerificationResultCollection VerificationResult = null;

            // TODO: check write permissions

            if (ASubmitTable != null)
            {
                VerificationResult = new TVerificationResultCollection();

                DBAccess.GDBAccessObj.BeginAutoTransaction(IsolationLevel.Serializable, ref SubmitChangesTransaction, ref SubmissionOK,
                                                           delegate
                {
                    try
                    {
                        if (ATablename == AAccountingPeriodTable.GetTableDBName())
                        {
                            AAccountingPeriodAccess.SubmitChanges((AAccountingPeriodTable)SubmitTable, SubmitChangesTransaction);

                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheableFinanceTablesEnum.AccountingPeriodList.ToString());
                        }
                        else if (ATablename == ACurrencyTable.GetTableDBName())
                        {
                            ACurrencyAccess.SubmitChanges((ACurrencyTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ADailyExchangeRateTable.GetTableDBName())
                        {
                            ADailyExchangeRateAccess.SubmitChanges((ADailyExchangeRateTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ACorporateExchangeRateTable.GetTableDBName())
                        {
                            ACorporateExchangeRateAccess.SubmitChanges((ACorporateExchangeRateTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ACurrencyLanguageTable.GetTableDBName())
                        {
                            ACurrencyLanguageAccess.SubmitChanges((ACurrencyLanguageTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == AFeesPayableTable.GetTableDBName())
                        {
                            AFeesPayableAccess.SubmitChanges((AFeesPayableTable)SubmitTable, SubmitChangesTransaction);

                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheableFinanceTablesEnum.FeesPayableList.ToString());
                        }
                        else if (ATablename == AFeesReceivableTable.GetTableDBName())
                        {
                            AFeesReceivableAccess.SubmitChanges((AFeesReceivableTable)SubmitTable, SubmitChangesTransaction);

                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheableFinanceTablesEnum.FeesReceivableList.ToString());
                        }
                        else if (ATablename == AGiftBatchTable.GetTableDBName())
                        {
                            // This method is called from ADailyExchangeRate Setup - please do not remove
                            // The method is not required for changes made to the gift batch screens, which use a TDS
                            AGiftBatchAccess.SubmitChanges((AGiftBatchTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == AJournalTable.GetTableDBName())
                        {
                            // This method is called from ADailyExchangeRate Setup - please do not remove
                            // The method is not required for changes made to the journal screens, which use a TDS
                            AJournalAccess.SubmitChanges((AJournalTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ARecurringJournalTable.GetTableDBName())
                        {
                            // This method is called from Submit Recurring GL Batch form - please do not remove
                            // The method is not required for changes made to the journal screens, which use a TDS
                            ARecurringJournalAccess.SubmitChanges((ARecurringJournalTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ALedgerTable.GetTableDBName())
                        {
                            // This method is called from ADailyExchangeRate Testing - please do not remove
                            ALedgerAccess.SubmitChanges((ALedgerTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == AAnalysisTypeTable.GetTableDBName())
                        {
                            AAnalysisTypeAccess.SubmitChanges((AAnalysisTypeTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == ASuspenseAccountTable.GetTableDBName())
                        {
                            ASuspenseAccountAccess.SubmitChanges((ASuspenseAccountTable)SubmitTable, SubmitChangesTransaction);

                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheableFinanceTablesEnum.SuspenseAccountList.ToString());
                        }
                        else if (ATablename == PcAttendeeTable.GetTableDBName())
                        {
                            PcAttendeeAccess.SubmitChanges((PcAttendeeTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PcConferenceTable.GetTableDBName())
                        {
                            PcConferenceAccess.SubmitChanges((PcConferenceTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PcConferenceCostTable.GetTableDBName())
                        {
                            PcConferenceCostAccess.SubmitChanges((PcConferenceCostTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PcEarlyLateTable.GetTableDBName())
                        {
                            PcEarlyLateAccess.SubmitChanges((PcEarlyLateTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PcSupplementTable.GetTableDBName())
                        {
                            PcSupplementAccess.SubmitChanges((PcSupplementTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PcDiscountTable.GetTableDBName())
                        {
                            PcDiscountAccess.SubmitChanges((PcDiscountTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PInternationalPostalTypeTable.GetTableDBName())
                        {
                            ValidateInternationalPostalType(ref VerificationResult, SubmitTable);
                            ValidateInternationalPostalTypeManual(ref VerificationResult, SubmitTable);

                            if (TVerificationHelper.IsNullOrOnlyNonCritical(VerificationResult))
                            {
                                PInternationalPostalTypeAccess.SubmitChanges((PInternationalPostalTypeTable)SubmitTable, SubmitChangesTransaction);
                            }
                        }
                        else if (ATablename == PtApplicationTypeTable.GetTableDBName())
                        {
                            PtApplicationTypeAccess.SubmitChanges((PtApplicationTypeTable)SubmitTable, SubmitChangesTransaction);

                            // mark dependent lists for needing to be refreshed since there was a change in base list
                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheablePersonTablesEnum.EventApplicationTypeList.ToString());
                            TCacheableTablesManager.GCacheableTablesManager.MarkCachedTableNeedsRefreshing(
                                TCacheablePersonTablesEnum.FieldApplicationTypeList.ToString());
                        }
                        else if (ATablename == PFormTable.GetTableDBName())
                        {
                            PFormAccess.SubmitChanges((PFormTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PFormalityTable.GetTableDBName())
                        {
                            PFormalityAccess.SubmitChanges((PFormalityTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PMailingTable.GetTableDBName())
                        {
                            PMailingAccess.SubmitChanges((PMailingTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PPartnerGiftDestinationTable.GetTableDBName())
                        {
                            PPartnerGiftDestinationAccess.SubmitChanges((PPartnerGiftDestinationTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == PmDocumentTypeTable.GetTableDBName())
                        {
                            PmDocumentTypeAccess.SubmitChanges((PmDocumentTypeTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else if (ATablename == SGroupTable.GetTableDBName())
                        {
                            SGroupAccess.SubmitChanges((SGroupTable)SubmitTable, SubmitChangesTransaction);
                        }
                        else
                        {
                            throw new EOPAppException("TCommonDataReader.SaveData: unknown table '" + ATablename + "'");
                        }

                        SubmissionOK = true;
                    }
                    catch (Exception Exc)
                    {
                        VerificationResult.Add(
                            new TVerificationResult(null, "Cannot SubmitChanges:" + Environment.NewLine +
                                                    Exc.Message, "UNDEFINED", TResultSeverity.Resv_Critical));
                    }
                });
            }

            ASubmitTable        = SubmitTable;
            AVerificationResult = VerificationResult;

            if ((AVerificationResult != null) &&
                (AVerificationResult.Count > 0))
            {
                // Downgrade TScreenVerificationResults to TVerificationResults in order to allow
                // Serialisation (needed for .NET Remoting).
                TVerificationResultCollection.DowngradeScreenVerificationResults(AVerificationResult);

                return(AVerificationResult.HasCriticalErrors ? TSubmitChangesResult.scrError : TSubmitChangesResult.scrOK);
            }

            return(TSubmitChangesResult.scrOK);
        }
        /// <summary>
        /// init the exchange rate, to avoid messages "Cannot find exchange rate for EUR USD"
        /// </summary>
        public static void InitExchangeRate(TDataBase ADataBase = null)
        {
            TDataBase      db          = DBAccess.Connect("InitExchangeRate", ADataBase);
            TDBTransaction Transaction = new TDBTransaction();
            bool           SubmitOK    = false;

            db.WriteTransaction(ref Transaction,
                                ref SubmitOK,
                                delegate
            {
                TAccountPeriodInfo AccountingPeriodInfo =
                    new TAccountPeriodInfo(FLedgerNumber, 1);
                ADailyExchangeRateTable dailyrates = new ADailyExchangeRateTable();
                ADailyExchangeRateRow row          = dailyrates.NewRowTyped(true);

                row.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
                row.TimeEffectiveFrom = 100;
                row.FromCurrencyCode  = "USD";
                row.ToCurrencyCode    = "EUR";
                row.RateOfExchange    = 1.34m;
                dailyrates.Rows.Add(row);
                row = dailyrates.NewRowTyped(true);
                row.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
                row.TimeEffectiveFrom = 100;
                row.FromCurrencyCode  = "USD";
                row.ToCurrencyCode    = "GBP";
                row.RateOfExchange    = 1.57m;
                dailyrates.Rows.Add(row);

                if (!ADailyExchangeRateAccess.Exists(row.FromCurrencyCode, row.ToCurrencyCode, row.DateEffectiveFrom, row.TimeEffectiveFrom, Transaction))
                {
                    ADailyExchangeRateAccess.SubmitChanges(dailyrates, Transaction);
                }

                ALedgerTable Ledger = ALedgerAccess.LoadByPrimaryKey(FLedgerNumber, Transaction);

                for (int periodCounter = 1; periodCounter <= Ledger[0].NumberOfAccountingPeriods + Ledger[0].NumberFwdPostingPeriods; periodCounter++)
                {
                    AccountingPeriodInfo = new TAccountPeriodInfo(FLedgerNumber, periodCounter);

                    ACorporateExchangeRateTable corprates = new ACorporateExchangeRateTable();
                    ACorporateExchangeRateRow corprow     = corprates.NewRowTyped(true);
                    corprow.DateEffectiveFrom             = AccountingPeriodInfo.PeriodStartDate;
                    corprow.TimeEffectiveFrom             = 100;
                    corprow.FromCurrencyCode = "USD";
                    corprow.ToCurrencyCode   = "EUR";
                    corprow.RateOfExchange   = 1.34m;
                    corprates.Rows.Add(corprow);
                    corprow = corprates.NewRowTyped(true);
                    corprow.DateEffectiveFrom = AccountingPeriodInfo.PeriodStartDate;
                    corprow.TimeEffectiveFrom = 100;
                    corprow.FromCurrencyCode  = "USD";
                    corprow.ToCurrencyCode    = "GBP";
                    corprow.RateOfExchange    = 1.57m;
                    corprates.Rows.Add(corprow);

                    if (!ACorporateExchangeRateAccess.Exists(corprow.FromCurrencyCode, corprow.ToCurrencyCode, corprow.DateEffectiveFrom, Transaction))
                    {
                        ACorporateExchangeRateAccess.SubmitChanges(corprates, Transaction);
                    }
                }
                SubmitOK = true;
            });

            if (ADataBase == null)
            {
                db.CloseDBConnection();
            }
        }