LicenseTrackingItemGrid.cs 4.5 KB

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