123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using InABox.Client.Remote.Json;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using RestSharp;
- namespace PRSServer.Forms.DatabaseLicense
- {
- public class LicenseTrackingItemGrid : DynamicGrid<LicenseTrackingItem>
- {
- public static HttpJsonClient LicenseClient = new("https", "remote.prsdigital.com.au", 5000);
- public LicenseTrackingItemGrid()
- {
- Options.Clear();
- }
- public DateTime Renewed { get; set; }
- public int Licenses { get; private set; }
- private static void RetrieveFees()
- {
- var summary = LicenseClient.GetRequest<LicenseSummary>("LicenseSummary");
- LicenseUtils.LoadSummary(summary);
- }
- protected override void DeleteItems(params CoreRow[] rows)
- {
- }
- protected override LicenseTrackingItem LoadItem(CoreRow row)
- {
- return row.ToObject<LicenseTrackingItem>();
- }
- protected override void Reload(Filters<LicenseTrackingItem> criteria, Columns<LicenseTrackingItem> columns, ref SortOrder<LicenseTrackingItem>? sort,
- Action<CoreTable?, Exception?> action)
- {
- Task.Run(() =>
- {
- RetrieveFees();
- var data = new Client<UserTracking>()
- .Query(
- new Filter<UserTracking>(x => x.Date).IsGreaterThanOrEqualTo(Renewed.Date)
- .And(x => x.TotalWrite).IsGreaterThan(0),
- new Columns<UserTracking>(x => x.User.ID)
- .Add(x => x.Type));
- Licenses = data.ExtractValues<UserTracking, Guid>(x => x.User.ID).Distinct().Count();
- var licensemap = LicenseUtils.LicenseMap();
- var summaries = LicenseUtils.LicenseTypes()
- .Select(x => new LicenseSummary0(x, licensemap.Where(e => e.Value == x).Select(a => a.Key))
- ).ToArray();
- var missing = new List<string>();
- foreach (var row in data.Rows)
- {
- var type = row.Get<UserTracking, string>(x => x.Type);
- var summary = summaries.FirstOrDefault(x => x.Entities.Contains(type));
- if (summary != null)
- {
- summary.AddGuid(row.Get<UserTracking, Guid>(x => x.User.ID));
- }
- else
- {
- if (!missing.Contains(type))
- {
- Logger.Send(LogType.Error, "", "Unknown License Type: " + type);
- missing.Add(type);
- }
- }
- }
- var result = new CoreTable();
- result.LoadColumns(typeof(LicenseTrackingItem));
- foreach (var summary in summaries.Where(x => x.Users > 0).OrderBy(x => x.Caption))
- {
- var renewal = new LicenseTrackingItem
- {
- License = summary.Caption,
- Users = summary.Users,
- Rate = summary.Fee,
- ExGST = summary.Users * summary.Fee
- };
- var row = result.NewRow();
- result.LoadRow(row, renewal);
- result.Rows.Add(row);
- }
- action?.Invoke(result, null);
- });
- }
- protected override void SaveItem(LicenseTrackingItem item)
- {
- }
- private class LicenseSummary0
- {
- private readonly List<Guid> _guids = new();
- public LicenseSummary0(Type license, IEnumerable<Type> entities)
- {
- License = license;
- Entities = entities.Select(x => x.EntityName().Split('.').Last()).ToArray();
- _guids = new List<Guid>();
- }
- public Type License { get; }
- public string Caption => License != null ? License.GetCaption() : "";
- public double Fee => License != null ? LicenseUtils.GetLicenseFee(License) : 0.00F;
- public string[] Entities { get; }
- public int Users => _guids.Count;
- public void AddGuid(Guid id)
- {
- if (!_guids.Contains(id))
- _guids.Add(id);
- }
- }
- }
- }
|