/// <summary>
    /// Changes the selected prices and other object fields.
    /// </summary>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        string err = "";

        if ((mainCurrency != null) && RecalculationRequested())
        {
            err = new Validator().NotEmpty(txtEchangeRate.Text.Trim(), GetString("StoreSettings_ChangeCurrency.EmptyExchangeRate"))
                .NotEmpty(txtRound.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidRound"))
                .IsInteger(txtRound.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidRound"))
                .IsDouble(txtEchangeRate.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidExchangeRate")).Result;
        }

        // Check exchange rate value
        double newRate = ValidationHelper.GetDouble(txtEchangeRate.Text.Trim(), 1);
        if (string.IsNullOrEmpty(err) && (newRate <= 0))
        {
            err = GetString("StoreSettings_ChangeCurrency.NegativeExchangeRate");
        }

        // Check new currency ID
        int newCurrencyId = ValidationHelper.GetInteger(currencyElem.Value, 0);
        if (string.IsNullOrEmpty(err) && (newCurrencyId <= 0))
        {
            err = GetString("StoreSettings_ChangeCurrency.NoNewMainCurrency");
        }

        // Show error message if any
        if (err != "")
        {
            ShowError(err);
            return;
        }

        // Get the new main currency
        CurrencyInfo newCurrency = CurrencyInfoProvider.GetCurrencyInfo(newCurrencyId);
        if (newCurrency != null)
        {
            // Only select new main currency when no old main currency specified
            if (mainCurrency == null)
            {
                newCurrency.CurrencyIsMain = true;
                CurrencyInfoProvider.SetCurrencyInfo(newCurrency);

                // Refresh the page
                URLHelper.Redirect(URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "saved", "1"));

                return;
            }

            // Set new main currency and recalculate requested objects
            double rate = 1 / newRate;
            int round = ValidationHelper.GetInteger(txtRound.Text.Trim(), 2);

            try
            {
                RecalculationSettings settings = new RecalculationSettings();
                settings.ExchangeRates = chkExchangeRates.Checked;
                settings.FromGlobalCurrencyRates = (plcRecalculateFromGlobal.Visible && chkRecalculateFromGlobal.Checked);
                settings.Products = chkProductPrices.Checked;
                settings.Discounts = chkDiscounts.Checked;
                settings.CreditEvents = chkCredit.Checked;
                settings.ShippingOptions = chkShipping.Checked;
                // Do not recalculate orders after main currency changed
                settings.Orders = false;

                // Recalculates the values
                CurrencyInfoProvider.ChangeMainCurrency(editedSiteId, newCurrencyId, rate, round, settings);

                // Refresh the page
                URLHelper.Redirect(URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "saved", "1"));
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
        }
        else
        {
            // Show error - No currency selected
            ShowError(GetString("StoreSettings_ChangeCurrency.NoNewMainCurrency"));
        }
    }
Пример #2
0
    /// <summary>
    /// Changes the selected prices and other object fields.
    /// </summary>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        string err = "";

        if ((mainCurrency != null) && RecalculationRequested())
        {
            err = new Validator().NotEmpty(txtEchangeRate.Text.Trim(), GetString("StoreSettings_ChangeCurrency.EmptyExchangeRate"))
                  .NotEmpty(txtRound.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidRound"))
                  .IsInteger(txtRound.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidRound"))
                  .IsDouble(txtEchangeRate.Text.Trim(), GetString("StoreSettings_ChangeCurrency.InvalidExchangeRate")).Result;
        }

        // Check exchange rate value
        double newRate = ValidationHelper.GetDouble(txtEchangeRate.Text.Trim(), 1);

        if (string.IsNullOrEmpty(err) && (newRate <= 0))
        {
            err = GetString("StoreSettings_ChangeCurrency.NegativeExchangeRate");
        }

        // Check new currency ID
        int newCurrencyId = ValidationHelper.GetInteger(currencyElem.Value, 0);

        if (string.IsNullOrEmpty(err) && (newCurrencyId <= 0))
        {
            err = GetString("StoreSettings_ChangeCurrency.NoNewMainCurrency");
        }

        // Show error message if any
        if (err != "")
        {
            ShowError(err);
            return;
        }

        // Get the new main currency
        CurrencyInfo newCurrency = CurrencyInfoProvider.GetCurrencyInfo(newCurrencyId);

        if (newCurrency != null)
        {
            // Only select new main currency when no old main currency specified
            if (mainCurrency == null)
            {
                newCurrency.CurrencyIsMain = true;
                CurrencyInfoProvider.SetCurrencyInfo(newCurrency);

                // Refresh the page
                URLHelper.Redirect(URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "saved", "1"));

                return;
            }

            // Set new main currency and recalculate requested objects
            double rate  = 1 / newRate;
            int    round = ValidationHelper.GetInteger(txtRound.Text.Trim(), 2);

            try
            {
                RecalculationSettings settings = new RecalculationSettings();
                settings.ExchangeRates           = chkExchangeRates.Checked;
                settings.FromGlobalCurrencyRates = (plcRecalculateFromGlobal.Visible && chkRecalculateFromGlobal.Checked);
                settings.Products        = chkProductPrices.Checked;
                settings.Taxes           = chkFlatTaxes.Checked;
                settings.Discounts       = chkDiscounts.Checked;
                settings.CreditEvents    = chkCredit.Checked;
                settings.ShippingOptions = chkShipping.Checked;
                settings.Documents       = (plcRecountDocuments.Visible && chkDocuments.Checked);
                // Do not recalculate orders after main currency changed
                settings.Orders = false;

                // Recalculates the values
                CurrencyInfoProvider.ChangeMainCurrency(editedSiteId, newCurrencyId, rate, round, settings);

                // Refresh the page
                URLHelper.Redirect(URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "saved", "1"));
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
        }
        else
        {
            // Show error - No currency selected
            ShowError(GetString("StoreSettings_ChangeCurrency.NoNewMainCurrency"));
        }
    }