upd dyn report for 13

This commit is contained in:
Kukks
2024-03-25 10:38:26 +01:00
parent 8e8ae0b24b
commit 28a87c6338
6 changed files with 2 additions and 179 deletions

View File

@@ -80,17 +80,4 @@ public class DynamicReportService:IHostedService
_reportService.ReportProviders.TryAdd(name, reportProvider);
}
}
public async Task<bool> IsLegacyEnabled()
{
var result = await _settingsRepository.GetSettingAsync<DynamicReportsSettings>();
return result?.EnableLegacyInvoiceExport is true;
}
public async Task<bool> ToggleLegacy()
{
var result = await _settingsRepository.GetSettingAsync<DynamicReportsSettings>() ?? new DynamicReportsSettings();
result.EnableLegacyInvoiceExport = !result.EnableLegacyInvoiceExport;
await _settingsRepository.UpdateSetting(result);
return result.EnableLegacyInvoiceExport;
}
}

View File

@@ -34,14 +34,6 @@ public class DynamicReportsController : Controller
_scopeProvider = scopeProvider;
}
[HttpGet("toggle-legacy")]
public async Task<IActionResult> ToggleLegacy()
{
var result = await _dynamicReportService.ToggleLegacy();
TempData[WellKnownTempData.SuccessMessage] = $"Legacy report {(result? "enabled" : "disabled")}";
return RedirectToAction(nameof(Update));
}
[HttpGet("update")]
[Authorize(Policy = Policies.CanModifyServerSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public IActionResult Update(

View File

@@ -10,12 +10,11 @@ public class DynamicReportsPlugin : BaseBTCPayServerPlugin
{
public override IBTCPayServerPlugin.PluginDependency[] Dependencies { get; } =
{
new() { Identifier = nameof(BTCPayServer), Condition = ">=1.12.0" }
new() { Identifier = nameof(BTCPayServer), Condition = ">=1.13.0" }
};
public override void Execute(IServiceCollection applicationBuilder)
{
applicationBuilder.AddSingleton<DynamicReportService>();
applicationBuilder.AddReportProvider<LegacyInvoiceExportReportProvider>();
applicationBuilder.AddSingleton<IHostedService>(provider => provider.GetRequiredService<DynamicReportService>());
applicationBuilder.AddSingleton<IUIExtension>(new UIExtension("DynamicReportsPlugin/Nav",
"server-nav"));

View File

@@ -6,7 +6,6 @@ namespace BTCPayServer.Plugins.DynamicReports;
public class DynamicReportsSettings
{
public Dictionary<string, DynamicReportSetting> Reports { get; set; } = new();
public bool EnableLegacyInvoiceExport { get; set; }
public class DynamicReportSetting
{

View File

@@ -1,150 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Reporting;
namespace BTCPayServer.Plugins.DynamicReports;
public class LegacyInvoiceExportReportProvider : ReportProvider
{
private readonly CurrencyNameTable _currencyNameTable;
private readonly InvoiceRepository _invoiceRepository;
private readonly SettingsRepository _settingsRepository;
public override bool IsAvailable() => _settingsRepository.GetSettingAsync<DynamicReportsSettings>().GetAwaiter()
.GetResult()?.EnableLegacyInvoiceExport is true;
public override string Name { get; } = "Invoice Export (legacy)";
public override async Task Query(QueryContext queryContext, CancellationToken cancellation)
{
var invoices = await _invoiceRepository.GetInvoices(new InvoiceQuery()
{
EndDate = queryContext.To,
StartDate = queryContext.From,
StoreId = new[] {queryContext.StoreId},
}, cancellation);
queryContext.ViewDefinition = new ViewDefinition()
{
Fields = new List<StoreReportResponse.Field>()
{
new("ReceivedDate", "datetime"),
new("StoreId", "text"),
new("OrderId", "text"),
new("InvoiceId", "text"),
new("InvoiceCreatedDate", "datetime"),
new("InvoiceExpirationDate", "datetime"),
new("InvoiceMonitoringDate", "datetime"),
new("PaymentId", "text"),
new("Destination", "text"),
new("PaymentType", "text"),
new("CryptoCode", "text"),
new("Paid", "text"),
new("NetworkFee", "text"),
new("ConversionRate", "number"),
new("PaidCurrency", "text"),
new("InvoiceCurrency", "text"),
new("InvoiceDue", "number"),
new("InvoicePrice", "number"),
new("InvoiceItemCode", "text"),
new("InvoiceItemDesc", "text"),
new("InvoiceFullStatus", "text"),
new("InvoiceStatus", "text"),
new("InvoiceExceptionStatus", "text"),
new("BuyerEmail", "text"),
new("Accounted", "boolean")
}
};
foreach (var invoiceEntity in invoices)
{
var currency = _currencyNameTable.GetNumberFormatInfo(invoiceEntity.Currency, true);
var invoiceDue = invoiceEntity.Price;
var payments = invoiceEntity.GetPayments(false);
if (payments.Count > 0)
{
foreach (var payment in payments)
{
var pdata = payment.GetCryptoPaymentData();
invoiceDue -= payment.InvoicePaidAmount.Net;
var data = queryContext.AddData();
// Add each field in the order defined in ViewDefinition
data.Add(payment.ReceivedTime);
data.Add(invoiceEntity.StoreId);
data.Add(invoiceEntity.Metadata.OrderId ?? string.Empty);
data.Add(invoiceEntity.Id);
data.Add(invoiceEntity.InvoiceTime);
data.Add(invoiceEntity.ExpirationTime);
data.Add(invoiceEntity.MonitoringExpiration);
data.Add(pdata.GetPaymentId());
data.Add(pdata.GetDestination());
data.Add(payment.GetPaymentMethodId().PaymentType.ToPrettyString());
data.Add(payment.Currency);
data.Add(payment.PaidAmount.Gross.ToString(CultureInfo.InvariantCulture));
data.Add(payment.NetworkFee.ToString(CultureInfo.InvariantCulture));
data.Add(payment.Rate);
data.Add(Math.Round(payment.InvoicePaidAmount.Gross, currency.NumberDecimalDigits)
.ToString(CultureInfo.InvariantCulture));
data.Add(invoiceEntity.Currency);
data.Add(Math.Round(invoiceDue, currency.NumberDecimalDigits));
data.Add(invoiceEntity.Price);
data.Add(invoiceEntity.Metadata.ItemCode);
data.Add(invoiceEntity.Metadata.ItemDesc);
data.Add(invoiceEntity.GetInvoiceState().ToString());
data.Add(invoiceEntity.StatusString);
data.Add(invoiceEntity.ExceptionStatusString);
data.Add(invoiceEntity.Metadata.BuyerEmail);
data.Add(payment.Accounted);
}
}
else
{
var data = queryContext.AddData();
// Add fields for invoices without payments
data.Add(null); // ReceivedDate
data.Add(invoiceEntity.StoreId);
data.Add(invoiceEntity.Metadata.OrderId ?? string.Empty);
data.Add(invoiceEntity.Id);
data.Add(invoiceEntity.InvoiceTime);
data.Add(invoiceEntity.ExpirationTime);
data.Add(invoiceEntity.MonitoringExpiration);
data.Add(null); // PaymentId
data.Add(null); // Destination
data.Add(null); // PaymentType
data.Add(null); // CryptoCode
data.Add(null); // Paid
data.Add(null); // NetworkFee
data.Add(null); // ConversionRate
data.Add(null); // PaidCurrency
data.Add(invoiceEntity.Currency);
data.Add(Math.Round(invoiceDue, currency.NumberDecimalDigits)); // InvoiceDue
data.Add(invoiceEntity.Price);
data.Add(invoiceEntity.Metadata.ItemCode);
data.Add(invoiceEntity.Metadata.ItemDesc);
data.Add(invoiceEntity.GetInvoiceState().ToString());
data.Add(invoiceEntity.StatusString);
data.Add(invoiceEntity.ExceptionStatusString);
data.Add(invoiceEntity.Metadata.BuyerEmail);
data.Add(null); // Accounted
}
}
}
public LegacyInvoiceExportReportProvider(CurrencyNameTable currencyNameTable, InvoiceRepository invoiceRepository,
SettingsRepository settingsRepository)
{
_currencyNameTable = currencyNameTable;
_invoiceRepository = invoiceRepository;
_settingsRepository = settingsRepository;
}
}

View File

@@ -17,7 +17,6 @@
reportName = string.IsNullOrEmpty(reportName) ? null : reportName;
var existingReports = ReportService.ReportProviders.Where(pair => pair.Value is PostgresReportProvider).Select(pair => pair.Key).ToList();
ViewData.SetActivePage("DynamicReports", reportName is null ? "Create dynamic report" : $"Edit {reportName} dynamic report", reportName);
var legacyEnabled = await DynamicReportService.IsLegacyEnabled();
}
@@ -55,9 +54,6 @@
}
</select>
}
<a class="btn btn-outline-secondary text-nowrap" asp-controller="DynamicReports" asp-action="ToggleLegacy" >
@(legacyEnabled ? "Disable legacy report" : "Enable legacy report")
</a>
</div>
</div>
<div class="row">
@@ -98,7 +94,7 @@
{
var queryContext = JsonConvert.DeserializeObject<QueryContext>(dataS);
<div class="row">
.
<div class="col-12 col-xxl-constrain">
<div class="table-responsive" style=" transform: rotateX(180deg);">