|
@@ -11,11 +11,10 @@ using System.Windows.Controls;
|
|
|
using System.Drawing;
|
|
|
using Image = System.Windows.Controls.Image;
|
|
|
using System.Diagnostics;
|
|
|
-using System.Diagnostics.CodeAnalysis;
|
|
|
-using System.Threading.Tasks;
|
|
|
using FluentResults;
|
|
|
using InABox.Client.Remote.Json;
|
|
|
using InABox.Configuration;
|
|
|
+using Microsoft.Win32;
|
|
|
using PRS.Shared;
|
|
|
using Stripe;
|
|
|
|
|
@@ -37,25 +36,7 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
public LicenseData? CurrentLicense
|
|
|
{
|
|
|
get => _currentLicense;
|
|
|
- set
|
|
|
- {
|
|
|
- if(value == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- PayWithStripe.IsEnabled = false;
|
|
|
- PayTooltip.Visibility = Visibility.Visible;
|
|
|
- PayTooltip.Content = new Label { Content = "Loading..." };
|
|
|
-
|
|
|
- _currentLicense = value;
|
|
|
- LastRenewal.Value = value.LastRenewal;
|
|
|
- CurrentExpiry.Value = value.Expiry;
|
|
|
- //Modules.Renewed = value.LastRenewal;
|
|
|
- RenewalAvailableFrom = value.RenewalAvailable;
|
|
|
-
|
|
|
- Modules.Refresh(false, true);
|
|
|
- }
|
|
|
+ set => _currentLicense = value;
|
|
|
}
|
|
|
|
|
|
public int RenewalPeriod
|
|
@@ -126,6 +107,8 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
|
|
|
+ Modules.OnCustomiseColumns += Modules_OnOnCustomiseColumns;
|
|
|
+
|
|
|
// This should get us the RegistrationID, which we need in order
|
|
|
// to get the License Pricing from the Server
|
|
|
_licenseRegistrationDetails = _config.Load(false);
|
|
@@ -162,8 +145,6 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
|
|
|
Help.Content = new Image { Source = Properties.Resources.help.AsBitmapImage(Color.White) };
|
|
|
Help.Click += Help_Click;
|
|
|
-
|
|
|
- Modules.AfterRefresh += ModulesRefreshed;
|
|
|
|
|
|
}
|
|
|
|
|
@@ -172,41 +153,93 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/License_Renewal") { UseShellExecute = true });
|
|
|
}
|
|
|
|
|
|
- private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
+ private void LoadData()
|
|
|
{
|
|
|
- if (!LicenseClient.Ping("ping"))
|
|
|
+
|
|
|
+ Result<bool> result = Progress.ShowModal<bool>("Getting License", progress =>
|
|
|
{
|
|
|
- MessageBox.Show("The PRS server is not available right now. Please try again later.");
|
|
|
- Close();
|
|
|
- return;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ progress.Report("Loading License");
|
|
|
+ CurrentLicense = LoadCurrentLicense();
|
|
|
+
|
|
|
+ progress.Report("Checking Server");
|
|
|
+ if (!LicenseClient.Ping("ping"))
|
|
|
+ return Result.Fail<bool>("Server Unavailable");
|
|
|
+
|
|
|
+ progress.Report("Retrieving Data");
|
|
|
+ RetrieveFees();
|
|
|
+
|
|
|
+ progress.Report("Scanning Data");
|
|
|
+ LoadUserTracking();
|
|
|
+
|
|
|
+ foreach (var item in LicenseItems)
|
|
|
+ item.Rate = LicenseUtils.GetLicenseFee(item.Type);
|
|
|
+
|
|
|
+ return Result.Ok<bool>(true);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ return Result.Fail<bool>(e.Message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (result.IsSuccess && result.Value)
|
|
|
+ UpdateWindow();
|
|
|
+ else
|
|
|
+ {
|
|
|
+ MessageWindow.ShowMessage(String.Join("\n",result.Errors.Select(x=>x.Message)), "Error");
|
|
|
+ UpdateWindow();
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateWindow()
|
|
|
+ {
|
|
|
|
|
|
- Task feetask = Task.Run(RetrieveFees);
|
|
|
- Task lictask = Task.Run(LoadUserTracking);
|
|
|
- Task.WaitAll(feetask, lictask);
|
|
|
-
|
|
|
- foreach (var item in LicenseItems)
|
|
|
- item.Rate = LicenseUtils.GetLicenseFee(item.Type);
|
|
|
- Licenses = LicenseItems.OrderByDescending(x => x.Users).FirstOrDefault()?.Users ?? 0;
|
|
|
+ // Always a minimum of one license required!
|
|
|
+ Licenses = Math.Max(1, LicenseItems.OrderByDescending(x => x.Users).FirstOrDefault()?.Users ?? 0);
|
|
|
|
|
|
Modules.Items = LicenseItems;
|
|
|
- Modules.Refresh(true, false);
|
|
|
-
|
|
|
- var license = LoadCurrentLicense();
|
|
|
- if(license != null)
|
|
|
- CurrentLicense = license;
|
|
|
-
|
|
|
+ Modules.Refresh(true, true);
|
|
|
+
|
|
|
+ var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
|
|
|
+ RenewalPeriodEditor.LoadLookups(lookups);
|
|
|
+ RenewalPeriodEditor.Loaded = true;
|
|
|
+
|
|
|
+ RenewalPeriod = LicenseUtils.TimeDiscountLevels().OrderBy(x => x).FirstOrDefault();
|
|
|
+ PayWithStripe.IsEnabled = CanRenew;
|
|
|
+ if (!PayWithStripe.IsEnabled)
|
|
|
+ {
|
|
|
+ PayTooltip.Visibility = Visibility.Visible;
|
|
|
+ PayTooltip.Content = new Label { Content = $"Renewal available from {RenewalAvailableFrom:dd MMM yyyy}" };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ PayTooltip.Visibility = Visibility.Collapsed;
|
|
|
+
|
|
|
LastRenewal.Loaded = true;
|
|
|
CurrentExpiry.Loaded = true;
|
|
|
NewExpiry.Loaded = true;
|
|
|
- RenewalPeriodEditor.Loaded = true;
|
|
|
+
|
|
|
+ //PayWithStripe.IsEnabled = false;
|
|
|
+
|
|
|
+ PayTooltip.Visibility = Visibility.Visible;
|
|
|
+ PayTooltip.Content = new Label { Content = "Loading..." };
|
|
|
+
|
|
|
+ LastRenewal.Value = CurrentLicense?.LastRenewal ?? DateTime.MinValue;
|
|
|
+ CurrentExpiry.Value = CurrentLicense?.Expiry ?? DateTime.MinValue;
|
|
|
+ RenewalAvailableFrom = CurrentLicense?.RenewalAvailable ?? DateTime.MinValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ LoadData();
|
|
|
}
|
|
|
|
|
|
private void RetrieveFees()
|
|
|
{
|
|
|
- var summary = LicenseClient.PostRequest<LicenseSummary>(
|
|
|
- new LicenseSummaryRequest() { RegistrationID = CurrentLicense?.CustomerID ?? Guid.Empty },
|
|
|
- nameof(LicenseSummary)
|
|
|
+ var summary = LicenseClient.PostRequest<LicenseFeeResponse>(
|
|
|
+ new LicenseFeeRequest() { RegistrationID = CurrentLicense?.CustomerID ?? Guid.Empty },
|
|
|
+ nameof(LicenseFeeRequest)
|
|
|
);
|
|
|
LicenseUtils.LoadSummary(summary);
|
|
|
}
|
|
@@ -251,30 +284,10 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
item.UserIDs.AddRange(users);
|
|
|
}
|
|
|
LicenseItems = result
|
|
|
- .Where(x=>x.Users > 0)
|
|
|
.OrderBy(x=>x.Caption)
|
|
|
.ToList();
|
|
|
}
|
|
|
|
|
|
- private void ModulesRefreshed(object sender, AfterRefreshEventArgs args)
|
|
|
- {
|
|
|
- if (CurrentLicense == null) return;
|
|
|
-
|
|
|
- var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
|
|
|
- RenewalPeriodEditor.LoadLookups(lookups);
|
|
|
-
|
|
|
- RenewalPeriod = LicenseUtils.TimeDiscountLevels().OrderBy(x => x).First();
|
|
|
- PayWithStripe.IsEnabled = CanRenew;
|
|
|
- if (!PayWithStripe.IsEnabled)
|
|
|
- {
|
|
|
- PayTooltip.Visibility = Visibility.Visible;
|
|
|
- PayTooltip.Content = new Label { Content = $"Renewal available from {RenewalAvailableFrom:dd MMM yyyy}" };
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- PayTooltip.Visibility = Visibility.Collapsed;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
private static LicenseData? LoadCurrentLicense()
|
|
|
{
|
|
@@ -301,16 +314,34 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
|
|
|
private void CalculateDiscounts()
|
|
|
{
|
|
|
+ double CalcDiscount(double amount, int months)
|
|
|
+ {
|
|
|
+ return (amount * (LicenseUtils.GetUserDiscount(Licenses) / 100.0F)) +
|
|
|
+ (amount * (LicenseUtils.GetTimeDiscount(months) / 100.0F));
|
|
|
+ }
|
|
|
+
|
|
|
var periodInMonths = RenewalPeriod;
|
|
|
|
|
|
NewExpiry.Value = NewExpiration;
|
|
|
double total = 0.0F;
|
|
|
- foreach (var row in Modules.Data.Rows)
|
|
|
- total += row.Get<LicenseTrackingItem, double>(x => x.ExGST) * periodInMonths;
|
|
|
- Gross = total;
|
|
|
- Discount = total * (LicenseUtils.GetUserDiscount(Licenses) / 100.0F) +
|
|
|
- total * (LicenseUtils.GetTimeDiscount(periodInMonths) / 100.0F);
|
|
|
+ if (CurrentLicense?.IsDynamic == true)
|
|
|
+ {
|
|
|
+ foreach (var row in Modules.Data.Rows)
|
|
|
+ total += row.Get<LicenseTrackingItem, double>(x => x.ExGST) * periodInMonths;
|
|
|
+ Gross = total;
|
|
|
+ Discount = CalcDiscount(total, periodInMonths);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ foreach (var row in Modules.Data.Rows)
|
|
|
+ total += Licenses * LicenseUtils.GetLicenseFee(row.Get<LicenseTrackingItem, String>(x => x.Type)) * periodInMonths;
|
|
|
+ Gross = Math.Round(total) - 0.05;
|
|
|
+ Gross = total;
|
|
|
+ Discount = Math.Round(CalcDiscount(total, periodInMonths) * 20F) / 20F;
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+
|
|
|
GrossLicenseFee.Value = Gross;
|
|
|
DiscountEditor.Value = Discount;
|
|
|
NettLicenseFee.Value = Net;
|
|
@@ -343,17 +374,22 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
{
|
|
|
_config.Save(_licenseRegistrationDetails);
|
|
|
Result<String> result = Result.Fail("Incomplete");
|
|
|
- var renewal = CreateRenewal();
|
|
|
+ var renewalRequest = CreateRenewal();
|
|
|
Progress.ShowModal("Processing", progress =>
|
|
|
{
|
|
|
- // Process the Stripe Payment
|
|
|
- progress.Report("Processing Payment");
|
|
|
- result = ProcessStripePayment();
|
|
|
- if (result.IsFailed)
|
|
|
- return;
|
|
|
-
|
|
|
+ if (renewalRequest.Net > 0.0F)
|
|
|
+ {
|
|
|
+ // Process the Stripe Payment
|
|
|
+ progress.Report("Processing Payment");
|
|
|
+ result = ProcessStripePayment();
|
|
|
+ if (result.IsFailed)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ result = Result.Ok("no payment required");
|
|
|
+
|
|
|
progress.Report("Creating Renewal");
|
|
|
- result = ProcessRenewal(renewal, result.Value);
|
|
|
+ result = RenewLicense(renewalRequest, result.Value);
|
|
|
|
|
|
if (result.IsFailed)
|
|
|
return;
|
|
@@ -420,9 +456,9 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
|
|
|
}
|
|
|
|
|
|
- private LicenseRenewal CreateRenewal()
|
|
|
+ private LicenseRenewalRequest CreateRenewal()
|
|
|
{
|
|
|
- return new LicenseRenewal
|
|
|
+ return new LicenseRenewalRequest
|
|
|
{
|
|
|
Company = _licenseRegistrationDetails.Company,
|
|
|
DateRenewed = RenewalDate,
|
|
@@ -431,16 +467,17 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
LicenseTracking = LicenseItems.ToArray(),
|
|
|
Gross = Gross,
|
|
|
Discount = Discount,
|
|
|
- Net = Net
|
|
|
+ Net = Net,
|
|
|
+ Addresses = LicenseUtils.GetMacAddresses(),
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private Result<string> ProcessRenewal(LicenseRenewal renewal, String transactionID)
|
|
|
+ private Result<string> RenewLicense(LicenseRenewalRequest renewalRequest, String transactionID)
|
|
|
{
|
|
|
- renewal.TransactionID = transactionID;
|
|
|
+ renewalRequest.TransactionID = transactionID;
|
|
|
try
|
|
|
{
|
|
|
- var result = LicenseClient.PostRequest<LicenseRenewalResult>(renewal, nameof(LicenseRenewal));
|
|
|
+ var result = LicenseClient.PostRequest<LicenseRenewalResult>(renewalRequest, nameof(LicenseRenewalRequest));
|
|
|
return Result.Ok(result.License);
|
|
|
}
|
|
|
catch (Exception e)
|
|
@@ -495,9 +532,73 @@ namespace PRSServer.Forms.DatabaseLicense
|
|
|
|
|
|
private void EnterKey_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- // popup a text editor
|
|
|
- // is it a valid licence key?
|
|
|
- // Update the Database
|
|
|
+ if (EnterKey?.ContextMenu != null)
|
|
|
+ EnterKey.ContextMenu.IsOpen = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CreateManualRequest(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ var request = new LicenseRequest()
|
|
|
+ {
|
|
|
+ CustomerID = CurrentLicense?.CustomerID ?? Guid.Empty,
|
|
|
+ Addresses = LicenseUtils.GetMacAddresses(),
|
|
|
+ IsDynamic = CurrentLicense?.IsDynamic ?? false,
|
|
|
+ };
|
|
|
+ SaveFileDialog sfd = new SaveFileDialog()
|
|
|
+ {
|
|
|
+ FileName = "license.request"
|
|
|
+ };
|
|
|
+ if (sfd.ShowDialog() == true)
|
|
|
+ {
|
|
|
+ System.IO.File.WriteAllText(sfd.FileName, LicenseUtils.EncryptLicenseRequest(request));
|
|
|
+ MessageWindow.ShowMessage("Please email this file to support@prsdigital.com.au!","Request Created");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LoadManualResponse(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ var ofd = new OpenFileDialog()
|
|
|
+ {
|
|
|
+ FileName = "license.key",
|
|
|
+ Filter = "License Files (*.key)|*.key"
|
|
|
+ };
|
|
|
+ if (ofd.ShowDialog() == true && System.IO.File.Exists(ofd.FileName))
|
|
|
+ {
|
|
|
+ var text = System.IO.File.ReadAllText(ofd.FileName);
|
|
|
+ if (LicenseUtils.TryDecryptLicense(text, out var data, out var _))
|
|
|
+ {
|
|
|
+ if (LicenseUtils.ValidateMacAddresses(data.Addresses))
|
|
|
+ {
|
|
|
+ SaveLicense(text);
|
|
|
+ MessageWindow.ShowMessage("License Updated", "Success");
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ MessageWindow.ShowMessage("License Key is not valid!","Invalid Key");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ MessageWindow.ShowMessage("License Key is not valid!","Bad Key");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Modules_OnOnCustomiseColumns(object sender, DynamicGridColumns columns)
|
|
|
+ {
|
|
|
+ var userscol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.Users)));
|
|
|
+ if (userscol != null)
|
|
|
+ userscol.Alignment = Alignment.MiddleCenter;
|
|
|
+
|
|
|
+ if (_currentLicense?.IsDynamic != true)
|
|
|
+ {
|
|
|
+ var ratecol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.Rate)));
|
|
|
+ if (ratecol != null)
|
|
|
+ columns.Remove(ratecol);
|
|
|
+
|
|
|
+ var exgstcol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.ExGST)));
|
|
|
+ if (exgstcol != null)
|
|
|
+ columns.Remove(exgstcol);
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|