123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System;
- using System.Linq;
- using System.Threading.Tasks;
- 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(2)]
- public string BaseCurrency { get; set; } = "AUD";
- }
- public class ForeignCurrencyGridPreferences : BaseObject, IUserConfigurationSettings
- {
- public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
- }
- public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
- {
- private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private readonly Button update;
- private ForeignCurrencyGridSettings _settings;
-
- private ForeignCurrencyGridPreferences _preferences;
-
- public ForeignCurrencyGrid()
- {
- Task[] tasks = {
- Task.Run(() =>
- {
- _settings = new GlobalConfiguration<ForeignCurrencyGridSettings>().Load();
- }),
-
- Task.Run(() =>
- {
- _preferences = new UserConfiguration<ForeignCurrencyGridPreferences>().Load();
- }),
- };
- Task.WaitAll(tasks);
-
- FilterComponent.SetSettings(_preferences!.Filters, false);
- FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
-
- 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 void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
- {
- _preferences.Filters = settings;
- new UserConfiguration<ForeignCurrencyGridPreferences>().Save(_preferences);
- }
- 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, $"Set to {(fc.Active ? "Active" : "InActive")} ");
- return true;
- }
-
- protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
- {
- BaseEditor? editor;
- if (column.ColumnName.Equals(nameof(ForeignCurrency.Identifier)) && string.IsNullOrWhiteSpace(_settings.ApiKey))
- {
- editor = base.GetEditor(item, column)?.CloneEditor() ?? new NullEditor();
- editor.Editable = Editable.Hidden;
- editor.Visible = Visible.Hidden;
- }
- else
- editor = base.GetEditor(item, column);
- return editor;
- }
- private bool UpdateExchangeRates(Button button, CoreRow[] rows)
- {
- try
- {
- Progress.ShowModal("Retrieving Currencies", progress =>
- {
- var data = new Client<ForeignCurrency>().Query().ToObjects<ForeignCurrency>().ToList();
- progress.Report("Updating Data");
- 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)
- 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;
- }
-
- }
|