Files
DXApp/DXApp.TemplateKitProject/Services/ZugferdImportService.cs
OlgunR 245f7a8268 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.
2026-05-28 08:35:01 +02:00

51 lines
1.6 KiB
C#

using DXApp.TemplateKitProject.Data;
using DXApp.TemplateKitProject.Models;
using Microsoft.EntityFrameworkCore;
namespace DXApp.TemplateKitProject.Services;
public class ZugferdImportService(
ZugferdExtractorService extractor,
ZugferdParserService parser,
AppDbContext db,
ILogger<ZugferdImportService> logger)
{
public async Task<ZugferdInvoice?> ImportAsync(Stream pdfStream, string sourceType, string guidelineId = "")
{
var xml = extractor.ExtractXml(pdfStream);
if (xml is null)
{
logger.LogWarning("Kein ZUGFeRD-XML in der PDF-Datei gefunden.");
return null;
}
var invoice = parser.Parse(xml);
// Duplikatprüfung
var duplicate = await db.ZugferdInvoices.FirstOrDefaultAsync(i =>
i.InvoiceNumber == invoice.InvoiceNumber &&
i.SellerTaxId == invoice.SellerTaxId);
if (duplicate is not null)
{
logger.LogWarning(
"Duplikat erkannt: Rechnung '{Number}' von '{Seller}' existiert bereits (ID: {Id}).",
invoice.InvoiceNumber, invoice.SellerName, duplicate.Id);
return duplicate;
}
invoice.SourceType = sourceType;
invoice.GuidelineId = guidelineId;
invoice.ImportedAt = DateTime.UtcNow;
db.ZugferdInvoices.Add(invoice);
await db.SaveChangesAsync();
logger.LogInformation(
"Rechnung '{Number}' von '{Seller}' importiert (ID: {Id}).",
invoice.InvoiceNumber, invoice.SellerName, invoice.Id);
return invoice;
}
}