LicenseTrackingItemGrid.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using InABox.Client.Remote.Json;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using RestSharp;
  11. namespace PRSServer.Forms.DatabaseLicense
  12. {
  13. public class LicenseTrackingItemGrid : DynamicGrid<LicenseTrackingItem>
  14. {
  15. public static HttpJsonClient LicenseClient = new("https", "remote.prsdigital.com.au", 5000);
  16. public LicenseTrackingItemGrid()
  17. {
  18. Options.Clear();
  19. }
  20. public DateTime Renewed { get; set; }
  21. public int Licenses { get; private set; }
  22. private static void RetrieveFees()
  23. {
  24. var summary = LicenseClient.GetRequest<LicenseSummary>("LicenseSummary");
  25. LicenseUtils.LoadSummary(summary);
  26. }
  27. protected override void DeleteItems(params CoreRow[] rows)
  28. {
  29. }
  30. protected override LicenseTrackingItem LoadItem(CoreRow row)
  31. {
  32. return row.ToObject<LicenseTrackingItem>();
  33. }
  34. protected override void Reload(Filters<LicenseTrackingItem> criteria, Columns<LicenseTrackingItem> columns, ref SortOrder<LicenseTrackingItem>? sort,
  35. Action<CoreTable?, Exception?> action)
  36. {
  37. Task.Run(() =>
  38. {
  39. RetrieveFees();
  40. var data = new Client<UserTracking>()
  41. .Query(
  42. new Filter<UserTracking>(x => x.Date).IsGreaterThanOrEqualTo(Renewed.Date)
  43. .And(x => x.TotalWrite).IsGreaterThan(0),
  44. new Columns<UserTracking>(x => x.User.ID)
  45. .Add(x => x.Type));
  46. Licenses = data.ExtractValues<UserTracking, Guid>(x => x.User.ID).Distinct().Count();
  47. var licensemap = LicenseUtils.LicenseMap();
  48. var summaries = LicenseUtils.LicenseTypes()
  49. .Select(x => new LicenseSummary0(x, licensemap.Where(e => e.Value == x).Select(a => a.Key))
  50. ).ToArray();
  51. var missing = new List<string>();
  52. foreach (var row in data.Rows)
  53. {
  54. var type = row.Get<UserTracking, string>(x => x.Type);
  55. var summary = summaries.FirstOrDefault(x => x.Entities.Contains(type));
  56. if (summary != null)
  57. {
  58. summary.AddGuid(row.Get<UserTracking, Guid>(x => x.User.ID));
  59. }
  60. else
  61. {
  62. if (!missing.Contains(type))
  63. {
  64. Logger.Send(LogType.Error, "", "Unknown License Type: " + type);
  65. missing.Add(type);
  66. }
  67. }
  68. }
  69. var result = new CoreTable();
  70. result.LoadColumns(typeof(LicenseTrackingItem));
  71. foreach (var summary in summaries.Where(x => x.Users > 0).OrderBy(x => x.Caption))
  72. {
  73. var renewal = new LicenseTrackingItem
  74. {
  75. License = summary.Caption,
  76. Users = summary.Users,
  77. Rate = summary.Fee,
  78. ExGST = summary.Users * summary.Fee
  79. };
  80. var row = result.NewRow();
  81. result.LoadRow(row, renewal);
  82. result.Rows.Add(row);
  83. }
  84. action?.Invoke(result, null);
  85. });
  86. }
  87. protected override void SaveItem(LicenseTrackingItem item)
  88. {
  89. }
  90. private class LicenseSummary0
  91. {
  92. private readonly List<Guid> _guids = new();
  93. public LicenseSummary0(Type license, IEnumerable<Type> entities)
  94. {
  95. License = license;
  96. Entities = entities.Select(x => x.EntityName().Split('.').Last()).ToArray();
  97. _guids = new List<Guid>();
  98. }
  99. public Type License { get; }
  100. public string Caption => License != null ? License.GetCaption() : "";
  101. public double Fee => License != null ? LicenseUtils.GetLicenseFee(License) : 0.00F;
  102. public string[] Entities { get; }
  103. public int Users => _guids.Count;
  104. public void AddGuid(Guid id)
  105. {
  106. if (!_guids.Contains(id))
  107. _guids.Add(id);
  108. }
  109. }
  110. }
  111. }