Add ZUGFeRD parsing and invoice storage support

Enhanced `ZugferdInvoice` model with default string values to prevent nulls. Updated `Upload.cshtml` to display parsed invoice data.

Refactored `Upload.cshtml.cs` to handle ZUGFeRD XML parsing and database storage. Introduced `ImportedInvoice` property and buffered file processing with `MemoryStream`.

Extended `ZugferdParserService` to support ZUGFeRD v1, v1.0 FeRD, and v2/Factur-X. Added version-specific parsing methods and namespaces. Improved date and decimal parsing for robustness.

Added database migration (`20260522084606_InitialCreate`) to define `ZugferdInvoices` table. Updated migration snapshot to reflect schema changes.

Fixed localization issue in `Upload.cshtml.cs` error message.
This commit is contained in:
OlgunR
2026-05-22 14:01:07 +02:00
parent d351a3d577
commit c45e837c2b
7 changed files with 361 additions and 30 deletions

View File

@@ -64,4 +64,22 @@
</tbody>
</table>
}
@if (Model.ImportedInvoice is not null)
{
<hr />
<h4>📄 Geparste Rechnungsdaten</h4>
<table class="table table-sm table-bordered w-auto">
<tr><th>Rechnungsnummer</th><td>@Model.ImportedInvoice.InvoiceNumber</td></tr>
<tr><th>Rechnungsdatum</th><td>@Model.ImportedInvoice.InvoiceDate.ToString("dd.MM.yyyy")</td></tr>
<tr><th>Verkäufer</th><td>@Model.ImportedInvoice.SellerName</td></tr>
<tr><th>USt-ID Verkäufer</th><td>@Model.ImportedInvoice.SellerTaxId</td></tr>
<tr><th>Käufer</th><td>@Model.ImportedInvoice.BuyerName</td></tr>
<tr><th>Währung</th><td>@Model.ImportedInvoice.CurrencyCode</td></tr>
<tr><th>Steuerbetrag</th><td>@Model.ImportedInvoice.TaxAmount.ToString("N2")</td></tr>
<tr><th>Gesamtbetrag</th><td><strong>@Model.ImportedInvoice.TotalAmount.ToString("N2")</strong></td></tr>
<tr><th>IBAN</th><td>@Model.ImportedInvoice.Iban</td></tr>
<tr><th>Importiert am</th><td>@Model.ImportedInvoice.ImportedAt.ToString("dd.MM.yyyy HH:mm")</td></tr>
</table>
<div class="alert alert-success mt-2">✔ Rechnung wurde in der Datenbank gespeichert (ID: @Model.ImportedInvoice.Id)</div>
}
}

View File

@@ -1,4 +1,4 @@
using DXApp.TemplateKitProject.Models;
using DXApp.TemplateKitProject.Models;
using DXApp.TemplateKitProject.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
@@ -7,6 +7,7 @@ namespace DXApp.TemplateKitProject.Pages.Invoices;
public class UploadModel(
PdfAttachmentExtractorService extractor,
ZugferdImportService zugferdImportService,
ILogger<UploadModel> logger) : PageModel
{
[BindProperty]
@@ -15,6 +16,7 @@ public class UploadModel(
public PdfExtractionResult? Result { get; private set; }
public bool ExtractionDone { get; private set; }
public string? ErrorMessage { get; private set; }
public ZugferdInvoice? ImportedInvoice { get; private set; }
public void OnGet()
{ }
@@ -23,7 +25,7 @@ public class UploadModel(
{
if (PdfFile is null || PdfFile.Length == 0)
{
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
return Page();
}
@@ -37,8 +39,20 @@ public class UploadModel(
try
{
await using var stream = PdfFile.OpenReadStream();
Result = extractor.ExtractAttachments(stream, PdfFile.FileName);
// Stream in MemoryStream puffern → kann zweimal gelesen werden
using var memStream = new MemoryStream();
await PdfFile.CopyToAsync(memStream);
// 1. Anhänge extrahieren und auf Disk speichern
memStream.Position = 0;
Result = extractor.ExtractAttachments(memStream, PdfFile.FileName);
// 2. Wenn ZUGFeRD-XML gefunden → parsen und in DB speichern
if (Result.HasZugferdXml)
{
memStream.Position = 0;
ImportedInvoice = await zugferdImportService.ImportAsync(memStream, "Upload");
}
}
catch (Exception ex)
{