LicenseTrackingItemGrid.cs 4.6 KB

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