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.
65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using DXApp.TemplateKitProject.Models;
|
|
using DXApp.TemplateKitProject.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
namespace DXApp.TemplateKitProject.Pages.Invoices;
|
|
|
|
public class UploadModel(
|
|
PdfAttachmentExtractorService extractor,
|
|
ZugferdImportService zugferdImportService,
|
|
ILogger<UploadModel> logger) : PageModel
|
|
{
|
|
[BindProperty]
|
|
public IFormFile? PdfFile { get; set; }
|
|
|
|
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()
|
|
{ }
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (PdfFile is null || PdfFile.Length == 0)
|
|
{
|
|
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
|
|
return Page();
|
|
}
|
|
|
|
if (!PdfFile.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
ModelState.AddModelError(nameof(PdfFile), "Nur PDF-Dateien sind erlaubt.");
|
|
return Page();
|
|
}
|
|
|
|
ExtractionDone = true;
|
|
|
|
try
|
|
{
|
|
// 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)
|
|
{
|
|
logger.LogError(ex, "Fehler beim Verarbeiten der Datei '{FileName}'.", PdfFile.FileName);
|
|
ErrorMessage = $"Fehler beim Verarbeiten der Datei: {ex.Message}";
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
} |