ForeignCurrencyGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Configuration;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.Wpf;
  12. using InABox.WPF;
  13. using OpenExchangeRates;
  14. using Exception = System.Exception;
  15. namespace PRSDesktop;
  16. public class ForeignCurrencyGridSettings : BaseObject, IGlobalConfigurationSettings
  17. {
  18. [TextBoxEditor]
  19. [EditorSequence(1)]
  20. public string ApiKey { get; set; }
  21. [TextBoxEditor]
  22. [EditorSequence(1)]
  23. public string BaseCurrency { get; set; } = "AUD";
  24. }
  25. public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
  26. {
  27. private BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  28. private Button update;
  29. private ForeignCurrencyGridSettings _settings;
  30. public ForeignCurrencyGrid()
  31. {
  32. _settings = new GlobalConfiguration<ForeignCurrencyGridSettings>().Load();
  33. HiddenColumns.Add(x=>x.Code);
  34. HiddenColumns.Add(x=>x.Description);
  35. HiddenColumns.Add(x=>x.Identifier);
  36. HiddenColumns.Add(x=>x.Active);
  37. ActionColumns.Add(new DynamicTickColumn<ForeignCurrency,bool>(x=>x.Active, tick, tick, null, ToggleActive));
  38. AddButton(null, PRSDesktop.Resources.autoupdate.AsBitmapImage(), UpdateSettings);
  39. update = AddButton("Update", PRSDesktop.Resources.payment.AsBitmapImage(), UpdateExchangeRates);
  40. update.Visibility = !string.IsNullOrEmpty(_settings.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
  41. }
  42. private bool UpdateSettings(Button button, CoreRow[] rows)
  43. {
  44. if (DynamicGridUtils.EditObject(_settings))
  45. {
  46. new GlobalConfiguration<ForeignCurrencyGridSettings>().Save(_settings);
  47. update.Visibility = !string.IsNullOrEmpty(_settings.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
  48. }
  49. return false;
  50. }
  51. private bool ToggleActive(CoreRow? row)
  52. {
  53. if (row == null)
  54. return false;
  55. var fc = row.ToObject<ForeignCurrency>();
  56. fc.Active = !fc.Active;
  57. new Client<ForeignCurrency>().Save(fc, string.Format("Set to {0} ", fc.Active ? "Active" : "InActive"));
  58. return true;
  59. }
  60. private bool UpdateExchangeRates(Button button, CoreRow[] rows)
  61. {
  62. try
  63. {
  64. Progress.ShowModal("Retrieving Rates", progress =>
  65. {
  66. var data = Data.ToObjects<ForeignCurrency>().ToList();
  67. using (var client = new OpenExchangeRatesClient("ff16587e173b43788a34c035a2c53edd"))
  68. {
  69. var currencies = client.GetCurrenciesAsync().Result;
  70. if (currencies == null)
  71. throw new Exception("No Currencies Returned");
  72. var rates = client.GetLatestRatesAsync().Result;
  73. if (rates == null)
  74. throw new Exception("No Rates Returned");
  75. if (!rates.Rates.TryGetValue("AUD", out decimal aud_decimal))
  76. throw new Exception("Cannot Find AUD rate");
  77. var usd_aud = Convert.ToDouble(aud_decimal);
  78. if (usd_aud.IsEffectivelyEqual(0.0))
  79. throw new Exception("USD -> AUD conversion return 0.00");
  80. foreach (var rate in rates.Rates)
  81. {
  82. var foreign_usd = Convert.ToDouble(rate.Value);
  83. var items = data.Where(x => String.Equals(x.Identifier, rate.Key)).ToList();
  84. if (!items.Any())
  85. {
  86. if (currencies.TryGetValue(rate.Key, out string? description))
  87. {
  88. var newitem = new ForeignCurrency()
  89. {
  90. Code = rate.Key,
  91. Description = description,
  92. Identifier = rate.Key,
  93. Active = false
  94. };
  95. items.Add(newitem);
  96. data.Add(newitem);
  97. }
  98. }
  99. foreach (var item in items.Where(x => x.Active))
  100. item.ExchangeRate = foreign_usd / usd_aud;
  101. }
  102. }
  103. var updates = data.Where(x => x.IsChanged()).ToArray();
  104. if (updates.Any())
  105. {
  106. progress.Report($"Updating {updates.Length} records...");
  107. new Client<ForeignCurrency>().Save(updates, "Updated from openexchangerates.org");
  108. }
  109. });
  110. }
  111. catch (Exception e)
  112. {
  113. MessageWindow.ShowError("Error retrieving data!", e);
  114. }
  115. return true;
  116. }
  117. }