Add duplicate invoice detection and warning message

Added a warning message in `Upload.cshtml` to notify users when a duplicate invoice is detected. Introduced the `IsDuplicate` property in `UploadModel` to track duplicates and updated the `OnPostAsync` method to set this property based on the `ImportedAt` timestamp.

Enhanced the `ImportAsync` method in `ZugferdImportService` to include duplicate detection by checking the database for invoices with the same `InvoiceNumber` and `SellerTaxId`. If a duplicate is found, it logs a warning and returns the existing invoice.

Updated `ImportAsync` to accept an optional `guidelineId` parameter and added logging for duplicate detection and successful imports.
This commit is contained in:
OlgunR
2026-05-28 08:35:01 +02:00
parent 6582370c08
commit 245f7a8268
3 changed files with 37 additions and 4 deletions

View File

@@ -104,5 +104,12 @@
<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>
@if (Model.IsDuplicate)
{
<div class="alert alert-warning mt-2">
⚠️ <strong>Duplikat:</strong> Diese Rechnung wurde bereits importiert (ID: @Model.ImportedInvoice!.Id).
Es wurde kein neuer Eintrag angelegt.
</div>
}
}
}

View File

@@ -21,6 +21,7 @@ public class UploadModel(
public string? ErrorMessage { get; private set; }
public ZugferdInvoice? ImportedInvoice { get; private set; }
public string? ResultFilePath { get; private set; }
public bool IsDuplicate { get; private set; }
public void OnGet()
{ }
@@ -56,10 +57,14 @@ public class UploadModel(
if (Result.HasZugferdXml)
{
memStream.Position = 0;
ImportedInvoice = await zugferdImportService.ImportAsync(memStream, "Upload");
ImportedInvoice = await zugferdImportService.ImportAsync(memStream, "Upload", Result.ZugferdGuidelineId);
// 3. Result-Package erstellen (nur wenn Import erfolgreich)
if (ImportedInvoice is not null)
// Duplikat erkennen: vorhandener Eintrag hat ImportedAt von früher
if (ImportedInvoice is not null && ImportedInvoice.ImportedAt < DateTime.UtcNow.AddSeconds(-5))
IsDuplicate = true;
// 3. Result-Package erstellen (nur wenn Import erfolgreich UND kein Duplikat)
if (ImportedInvoice is not null && !IsDuplicate)
{
ResultFilePath = await resultPackageService.CreateResultPackageAsync(
originalBytes, PdfFile.FileName, ImportedInvoice);