123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using OpenExchangeRates;
- using Exception = System.Exception;
- namespace PRSDesktop;
- public class ForeignCurrencyGridSettings : BaseObject, IGlobalConfigurationSettings
- {
- [TextBoxEditor]
- [EditorSequence(1)]
- public string ApiKey { get; set; }
- [TextBoxEditor]
- [EditorSequence(1)]
- public string BaseCurrency { get; set; } = "AUD";
- }
- public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
- {
- private BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private Button update;
- private ForeignCurrencyGridSettings _settings;
-
- public ForeignCurrencyGrid()
- {
- _settings = new GlobalConfiguration<ForeignCurrencyGridSettings>().Load();
- HiddenColumns.Add(x=>x.Code);
- HiddenColumns.Add(x=>x.Description);
- HiddenColumns.Add(x=>x.Identifier);
- HiddenColumns.Add(x=>x.Active);
- ActionColumns.Add(new DynamicTickColumn<ForeignCurrency,bool>(x=>x.Active, tick, tick, null, ToggleActive));
- AddButton(null, PRSDesktop.Resources.autoupdate.AsBitmapImage(), UpdateSettings);
- update = AddButton("Update", PRSDesktop.Resources.payment.AsBitmapImage(), UpdateExchangeRates);
- update.Visibility = !string.IsNullOrEmpty(_settings.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
- }
- private bool UpdateSettings(Button button, CoreRow[] rows)
- {
- if (DynamicGridUtils.EditObject(_settings))
- {
- new GlobalConfiguration<ForeignCurrencyGridSettings>().Save(_settings);
- update.Visibility = !string.IsNullOrEmpty(_settings.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
- }
- return false;
- }
- private bool ToggleActive(CoreRow? row)
- {
- if (row == null)
- return false;
- var fc = row.ToObject<ForeignCurrency>();
- fc.Active = !fc.Active;
- new Client<ForeignCurrency>().Save(fc, string.Format("Set to {0} ", fc.Active ? "Active" : "InActive"));
- return true;
- }
- private bool UpdateExchangeRates(Button button, CoreRow[] rows)
- {
- try
- {
- Progress.ShowModal("Retrieving Rates", progress =>
- {
- var data = Data.ToObjects<ForeignCurrency>().ToList();
- using (var client = new OpenExchangeRatesClient("ff16587e173b43788a34c035a2c53edd"))
- {
- var currencies = client.GetCurrenciesAsync().Result;
- if (currencies == null)
- throw new Exception("No Currencies Returned");
- var rates = client.GetLatestRatesAsync().Result;
- if (rates == null)
- throw new Exception("No Rates Returned");
- if (!rates.Rates.TryGetValue("AUD", out decimal aud_decimal))
- throw new Exception("Cannot Find AUD rate");
- var usd_aud = Convert.ToDouble(aud_decimal);
- if (usd_aud.IsEffectivelyEqual(0.0))
- throw new Exception("USD -> AUD conversion return 0.00");
- foreach (var rate in rates.Rates)
- {
- var foreign_usd = Convert.ToDouble(rate.Value);
- var items = data.Where(x => String.Equals(x.Identifier, rate.Key)).ToList();
- if (!items.Any())
- {
- if (currencies.TryGetValue(rate.Key, out string? description))
- {
- var newitem = new ForeignCurrency()
- {
- Code = rate.Key,
- Description = description,
- Identifier = rate.Key,
- Active = false
- };
- items.Add(newitem);
- data.Add(newitem);
- }
- }
- foreach (var item in items.Where(x => x.Active))
- item.ExchangeRate = foreign_usd / usd_aud;
- }
- }
-
- var updates = data.Where(x => x.IsChanged()).ToArray();
- if (updates.Any())
- {
- progress.Report($"Updating {updates.Length} records...");
- new Client<ForeignCurrency>().Save(updates, "Updated from openexchangerates.org");
- }
-
-
- });
- }
- catch (Exception e)
- {
- MessageWindow.ShowError("Error retrieving data!", e);
- }
- return true;
- }
-
- }
|