private bool GetSuggestedRateForCurrencyAndDateInternal(string FromCurrency,
                                                                string ToCurrency,
                                                                DateTime EffectiveDate,
                                                                out decimal SuggestedRate)
        {
            // If we cannot come up with a rate, it will be 0.0 (which is not allowed so it will force the user to enter a better number)
            SuggestedRate = 0.0m;

            // Rate of exchange will be the latest value used, if there is one
            // Get the most recent value for this currency pair
            string rowFilter = String.Format(CultureInfo.InvariantCulture, "{0}='{1}' AND {2}='{3}' AND {4} <= #{5}#",
                                             ACorporateExchangeRateTable.GetFromCurrencyCodeDBName(),
                                             FromCurrency,
                                             ACorporateExchangeRateTable.GetToCurrencyCodeDBName(),
                                             ToCurrency,
                                             ACorporateExchangeRateTable.GetDateEffectiveFromDBName(),
                                             EffectiveDate.ToString("d", CultureInfo.InvariantCulture));
            string   sortBy = String.Format("{0} DESC", ACorporateExchangeRateTable.GetDateEffectiveFromDBName());
            DataView dv     = new DataView(FMainDS.ACorporateExchangeRate, rowFilter, sortBy, DataViewRowState.CurrentRows);

            if (dv.Count > 0)
            {
                // Use this rate
                SuggestedRate = ((ACorporateExchangeRateRow)dv[0].Row).RateOfExchange;
                return(true);
            }

            return(false);
        }
예제 #2
0
        private void RunOnceOnActivationManual()
        {
            // Set the Tag for the checkbox since we don't want changes to the checkbox to look like we have to save the data
            this.chkHideOthers.Tag = MCommon.MCommonResourcestrings.StrCtrlSuppressChangeDetection;

            // Activate events we will use in manual code
            this.txtDetailRateOfExchange.TextChanged +=
                new System.EventHandler(this.txtDetailRateOfExchange_TextChanged);

            // These Leave events are all fired before validation updates the row
            // This is important because we need to suggest a new date/rate depending on the selection made
            //  and so we need to be able to check on the existence of specific rows in the data table before they get updated on validation
            this.cmbDetailFromCurrencyCode.Leave +=
                new System.EventHandler(this.CurrencyCodeComboBox_Leave);
            this.cmbDetailToCurrencyCode.Leave +=
                new System.EventHandler(this.CurrencyCodeComboBox_Leave);
            this.dtpDetailDateEffectiveFrom.Leave +=
                new EventHandler(dtpDetailDateEffectiveFrom_Leave);

            this.btnInvertExchangeRate.Click +=
                new System.EventHandler(this.InvertExchangeRate);
            this.chkHideOthers.CheckedChanged +=
                new EventHandler(chkHideOthers_CheckedChanged);

            FPetraUtilsObject.DataSavingStarted += new TDataSavingStartHandler(FPetraUtilsObject_DataSavingStarted);

            if (baseCurrencyOfLedger == null)
            {
                // Have a last attempt at deciding what the base currency is...
                // What ledgers does the user have access to??
                ALedgerTable ledgers    = TRemote.MFinance.Setup.WebConnectors.GetAvailableLedgers();
                DataView     ledgerView = ledgers.DefaultView;
                ledgerView.RowFilter = "a_ledger_status_l = 1";     // Only view 'in use' ledgers

                if (ledgerView.Count > 0)
                {
                    // There is at least one - so default to the currency of the first one
                    baseCurrencyOfLedger = ((ALedgerRow)ledgerView.Table.Rows[0]).BaseCurrency;
                }
            }

            DataView myView = FMainDS.ACorporateExchangeRate.DefaultView;

            myView.Sort = ACorporateExchangeRateTable.GetToCurrencyCodeDBName() + ", " +
                          ACorporateExchangeRateTable.GetFromCurrencyCodeDBName() + ", " +
                          ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + " DESC";
            myView.RowFilter = "";

            if (myView.Count > 0)
            {
                // We have to use this construct because simple ShoWDetails requires two cursor down keypresses to move the cursor
                // because we have changed the row filter.
                grdDetails.Selection.Focus(new SourceGrid.Position(1, 0), false);
                ShowDetails();
            }
            else
            {
                ShowDetails(null);
            }
        }
        /// <summary>
        /// get corporate exchange rate for the given currencies and date;
        /// </summary>
        /// <param name="ACurrencyFrom"></param>
        /// <param name="ACurrencyTo"></param>
        /// <param name="AStartDate"></param>
        /// <param name="AEndDate"></param>
        /// <param name="AExchangeRateToFind"></param>
        /// <returns>true if a exchange rate was found for the date. Otherwise false</returns>
        public static bool GetCorporateExchangeRate(string ACurrencyFrom,
                                                    string ACurrencyTo,
                                                    DateTime AStartDate,
                                                    DateTime AEndDate,
                                                    out decimal AExchangeRateToFind)
        {
            AExchangeRateToFind = decimal.MinValue;
            decimal ExchangeRateToFind = AExchangeRateToFind;

            TDBTransaction Transaction = null;

            ACorporateExchangeRateTable tempTable   = new ACorporateExchangeRateTable();
            ACorporateExchangeRateRow   templateRow = tempTable.NewRowTyped(false);

            templateRow.FromCurrencyCode = ACurrencyFrom;
            templateRow.ToCurrencyCode   = ACurrencyTo;

            DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum,
                                                                      ref Transaction,
                                                                      delegate
            {
                try
                {
                    ACorporateExchangeRateTable ExchangeRates = ACorporateExchangeRateAccess.LoadUsingTemplate(templateRow, Transaction);

                    if (ExchangeRates.Count > 0)
                    {
                        // sort rates by date, look for rate just before the date we are looking for
                        ExchangeRates.DefaultView.Sort      = ACorporateExchangeRateTable.GetDateEffectiveFromDBName();
                        ExchangeRates.DefaultView.RowFilter = ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + ">= #" +
                                                              AStartDate.ToString("yyyy-MM-dd") + "# AND " +
                                                              ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + "<= #" +
                                                              AEndDate.ToString("yyyy-MM-dd") + "#";

                        if (ExchangeRates.DefaultView.Count > 0)
                        {
                            ExchangeRateToFind = ((ACorporateExchangeRateRow)ExchangeRates.DefaultView[0].Row).RateOfExchange;
                        }
                    }

                    if (ExchangeRateToFind == decimal.MinValue)
                    {
                        // try other way round
                        templateRow.FromCurrencyCode = ACurrencyTo;
                        templateRow.ToCurrencyCode   = ACurrencyFrom;

                        ExchangeRates = ACorporateExchangeRateAccess.LoadUsingTemplate(templateRow, Transaction);

                        if (ExchangeRates.Count > 0)
                        {
                            // sort rates by date, look for rate just before the date we are looking for
                            ExchangeRates.DefaultView.Sort      = ACorporateExchangeRateTable.GetDateEffectiveFromDBName();
                            ExchangeRates.DefaultView.RowFilter = ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + ">= #" +
                                                                  AStartDate.ToString("yyyy-MM-dd") + "# AND " +
                                                                  ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + "<= #" +
                                                                  AEndDate.ToString("yyyy-MM-dd") + "#";

                            if (ExchangeRates.DefaultView.Count > 0)
                            {
                                ExchangeRateToFind = 1 / ((ACorporateExchangeRateRow)ExchangeRates.DefaultView[0].Row).RateOfExchange;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    TLogging.Log("Error in GetCorporateExchangeRate: " + e.Message);
                }
            });

            AExchangeRateToFind = ExchangeRateToFind;

            return(AExchangeRateToFind != decimal.MinValue);
        }
        void FPetraUtilsObject_DataSavingStarted(object Sender, EventArgs e)
        {
            // The user has clicked Save.  We need to consider if we need to make any Inverse currency additions...
            // We need to update the details and validate them first
            // When we return from this method the standard code will do the validation again and might not allow the save to go ahead
            FPetraUtilsObject.VerificationResultCollection.Clear();
            ValidateAllData(false, TErrorProcessingMode.Epm_None);

            if (!TVerificationHelper.IsNullOrOnlyNonCritical(FPetraUtilsObject.VerificationResultCollection))
            {
                return;
            }

            // Now go through all the grid rows (view) checking all the added rows.  Keep a list of inverses
            List <tInverseItem> lstInverses = new List <tInverseItem>();
            DataView            gridView    = ((DevAge.ComponentModel.BoundDataView)grdDetails.DataSource).DataView;

            for (int i = 0; i < gridView.Count; i++)
            {
                ACorporateExchangeRateRow ARow = (ACorporateExchangeRateRow)gridView[i].Row;

                if (ARow.RowState == DataRowState.Added)
                {
                    tInverseItem item = new tInverseItem();
                    item.FromCurrencyCode = ARow.ToCurrencyCode;
                    item.ToCurrencyCode   = ARow.FromCurrencyCode;
                    item.RateOfExchange   = Math.Round(1 / ARow.RateOfExchange, 10);
                    item.DateEffective    = ARow.DateEffectiveFrom;
                    lstInverses.Add(item);
                }
            }

            if (lstInverses.Count == 0)
            {
                return;
            }

            // Now go through our list and check if any items need adding to the data Table
            // The user may already have put an inverse currency in by hand
            DataView dv = new DataView(FMainDS.ACorporateExchangeRate);

            for (int i = 0; i < lstInverses.Count; i++)
            {
                tInverseItem item = lstInverses[i];

                // Does the item exist already?
                dv.RowFilter = String.Format(CultureInfo.InvariantCulture, "{0}='{1}' AND {2}='{3}' AND {4}=#{5}#",
                                             ACorporateExchangeRateTable.GetFromCurrencyCodeDBName(),
                                             item.FromCurrencyCode,
                                             ACorporateExchangeRateTable.GetToCurrencyCodeDBName(),
                                             item.ToCurrencyCode,
                                             ACorporateExchangeRateTable.GetDateEffectiveFromDBName(),
                                             item.DateEffective.ToString("d", CultureInfo.InvariantCulture));

                if (dv.Count == 0)
                {
                    ACorporateExchangeRateRow NewRow = FMainDS.ACorporateExchangeRate.NewRowTyped();
                    NewRow.FromCurrencyCode  = item.FromCurrencyCode;
                    NewRow.ToCurrencyCode    = item.ToCurrencyCode;
                    NewRow.DateEffectiveFrom = DateTime.Parse(item.DateEffective.ToLongDateString());
                    NewRow.RateOfExchange    = item.RateOfExchange;

                    FMainDS.ACorporateExchangeRate.Rows.Add(NewRow);
                }
            }

            // Now make sure to select the row that was currently selected when we started the Save operation
            SelectRowInGrid(grdDetails.DataSourceRowToIndex2(FPreviouslySelectedDetailRow) + 1);
        }
        private void RunOnceOnActivationManual()
        {
            // Set the Tag for the checkbox since we don't want changes to the checkbox to look like we have to save the data
            this.chkHideOthers.Tag = MCommon.MCommonResourcestrings.StrCtrlSuppressChangeDetection;

            // Activate events we will use in manual code
            this.txtDetailRateOfExchange.TextChanged +=
                new System.EventHandler(this.txtDetailRateOfExchange_TextChanged);

            // These Leave events are all fired before validation updates the row
            // This is important because we need to suggest a new date/rate depending on the selection made
            //  and so we need to be able to check on the existence of specific rows in the data table before they get updated on validation
            this.cmbDetailFromCurrencyCode.Leave +=
                new System.EventHandler(this.CurrencyCodeComboBox_Leave);
            this.cmbDetailToCurrencyCode.Leave +=
                new System.EventHandler(this.CurrencyCodeComboBox_Leave);
            this.dtpDetailDateEffectiveFrom.Leave +=
                new EventHandler(dtpDetailDateEffectiveFrom_Leave);

            this.btnInvertExchangeRate.Click +=
                new System.EventHandler(this.InvertExchangeRate);
            this.chkHideOthers.CheckedChanged +=
                new EventHandler(chkHideOthers_CheckedChanged);

            FPetraUtilsObject.DataSavingStarted += new TDataSavingStartHandler(FPetraUtilsObject_DataSavingStarted);

            // What ledgers does the user have access to??
            FAvailableLedgers = TRemote.MFinance.Setup.WebConnectors.GetAvailableLedgers();
            DataView ledgerView = FAvailableLedgers.DefaultView;

            ledgerView.RowFilter = "a_ledger_status_l = 1";     // Only view 'in use' ledgers

            for (int i = 0; i < ledgerView.Count; i++)
            {
                // Have a last attempt at deciding what the base currency is...
                if (baseCurrencyOfLedger == null)
                {
                    // we default to the first one we find
                    baseCurrencyOfLedger = ((ALedgerRow)ledgerView[i].Row).BaseCurrency;
                }

                if (intlCurrencyOfLedger == null)
                {
                    // we default to the first one we find
                    intlCurrencyOfLedger = ((ALedgerRow)ledgerView[i].Row).IntlCurrency;
                }

                // Get the accounting periods for this ledger
                AAccountingPeriodTable periods = (AAccountingPeriodTable)TDataCache.TMFinance.GetCacheableFinanceTable(
                    TCacheableFinanceTablesEnum.AccountingPeriodList,
                    ((ALedgerRow)ledgerView[i].Row).LedgerNumber);

                if ((periods != null) && (periods.Rows.Count > 0))
                {
                    int firstDay = ((AAccountingPeriodRow)periods.Rows[0]).PeriodStartDate.Day;

                    if ((FAlternativeFirstDayInMonth == 1) && (firstDay != 1))
                    {
                        // Now we have an alternative first day of month
                        FAlternativeFirstDayInMonth = firstDay;
                    }
                    else if ((FAlternativeFirstDayInMonth != 1) && (firstDay != 1) && (firstDay != FAlternativeFirstDayInMonth))
                    {
                        // Ooops.  Now we seem to have more than one alternative first day of month.
                        // We can't cope with that level of complexity!
                        FAlternativeFirstDayInMonth = 0;
                    }
                }
            }

            DataView myView = FMainDS.ACorporateExchangeRate.DefaultView;

            myView.Sort = ACorporateExchangeRateTable.GetToCurrencyCodeDBName() + ", " +
                          ACorporateExchangeRateTable.GetFromCurrencyCodeDBName() + ", " +
                          ACorporateExchangeRateTable.GetDateEffectiveFromDBName() + " DESC";
            myView.RowFilter = "";

            if (myView.Count > 0)
            {
                // We have to use this construct because simple ShoWDetails requires two cursor down keypresses to move the cursor
                // because we have changed the row filter.
                grdDetails.Selection.Focus(new SourceGrid.Position(1, 0), false);
                ShowDetails();
            }
            else
            {
                ShowDetails(null);
            }
        }