} // End of the UpsertSupplier method

        /// <summary>
        /// Upsert currencies
        /// </summary>
        public async Task UpsertCurrencies(FixerRates fixer_rates)
        {
            // Loop currency rates
            foreach (KeyValuePair<string, decimal> entry in fixer_rates.rates)
            {
                // A boolean that indicates if the currency exists
                bool currency_exists = false;

                // Get the currency root
                FortnoxResponse<CurrencyRoot> fr = await this.nox_client.Get<CurrencyRoot>($"currencies/{entry.Key.ToUpper()}");

                // Log errors
                if (string.IsNullOrEmpty(fr.error) == false)
                {
                    this.logger.LogError(fr.error);
                }

                // Check if the currency exists
                if (fr.model != null)
                {
                    currency_exists = true;
                }

                // Calculate the currency rate
                decimal currency_rate = Math.Round(1 / entry.Value, 6, MidpointRounding.AwayFromZero);

                // Create a new currency
                fr.model = new CurrencyRoot
                {
                    Currency = new Currency
                    {
                        Code = entry.Key.ToUpper(),
                        Description = currency_exists == false ? entry.Key.ToUpper() : null,
                        Unit = 1M,
                        BuyRate = currency_rate,
                        SellRate = currency_rate
                    }
                };

                // Update or add the currency
                if (currency_exists == true)
                {
                    // Update the post
                    fr = await this.nox_client.Update<CurrencyRoot>(fr.model, $"currencies/{entry.Key.ToUpper()}");
                }
                else
                {
                    // Add the post
                    fr = await this.nox_client.Add<CurrencyRoot>(fr.model, $"currencies");
                }

                // Log errors
                if (string.IsNullOrEmpty(fr.error) == false)
                {
                    this.logger.LogError(fr.error);
                }
            }

        } // End of the UpsertCurrencies method
Example #2
0
        } // End of the constructor

        #endregion

        #region Update methods

        /// <summary>
        /// Update currency rates
        /// </summary>
        public async Task<DoxservrResponse<FixerRates>> UpdateCurrencyRates(string directory)
        {
            // Create the response to return
            DoxservrResponse<FixerRates> dr = new DoxservrResponse<FixerRates>();

            // Set the file path
            string file_path = directory + "\\currency_rates.json";

            try
            {
                // Get currency rates
                FixerRates file = JsonConvert.DeserializeObject<FixerRates>(System.IO.File.ReadAllText(file_path, Encoding.UTF8));

                // Check if currency rates are up to date
                if (DateTime.Now.Date <= file.date.Date.AddDays(4))
                {
                    return dr;
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                // File not found, it will be created
            }
            catch (Exception ex)
            {
                // Add error data
                dr.error = $"UpdateCurrencyRates: {ex.ToString()}";
            }

            // Get the base currency
            string base_currency = string.IsNullOrEmpty(this.default_values.BaseCurrency) == false ? this.default_values.BaseCurrency : "SEK";

            try
            {
                // Get the response
                HttpResponseMessage response = await this.client.GetAsync($"/latest?base={base_currency}");

                // Get the data
                if (response.IsSuccessStatusCode == true)
                {
                    // Get string data
                    string data = await response.Content.ReadAsStringAsync();

                    // Save currency rates to a file
                    System.IO.File.WriteAllText(file_path, data);

                    // Get fixer rates
                    dr.model = JsonConvert.DeserializeObject<FixerRates>(data);
                }
                else
                {
                    // Get string data
                    string data = await response.Content.ReadAsStringAsync();

                    // Add error data
                    dr.error = $"UpdateCurrencyRates: {data}";
                }
            }
            catch (Exception ex)
            {
                // Add exception data
                dr.error = $"UpdateCurrencyRates: {ex.ToString()}";
            }

            // Return the response
            return dr;

        } // End of the UpdateCurrencyRates