LicenseTrackingItemGrid.cs 4.6 KB

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