From d879ba74d889ecf3ae7800c34fcae08c9d9642fc Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 21 Nov 2024 01:23:47 +0100 Subject: [PATCH] Implemented extracting eInvoice data via API --- .../net/codinux/invoicing/api/InvoicingResource.kt | 13 +++++++++++-- .../codinux/invoicing/service/InvoicingService.kt | 12 ++++++++++++ .../src/main/resources/application.properties | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/e-invoice-api/src/main/kotlin/net/codinux/invoicing/api/InvoicingResource.kt b/e-invoice-api/src/main/kotlin/net/codinux/invoicing/api/InvoicingResource.kt index eb311e6..929eabc 100644 --- a/e-invoice-api/src/main/kotlin/net/codinux/invoicing/api/InvoicingResource.kt +++ b/e-invoice-api/src/main/kotlin/net/codinux/invoicing/api/InvoicingResource.kt @@ -9,7 +9,7 @@ import org.eclipse.microprofile.openapi.annotations.Operation @Path("") @Consumes(MediaType.APPLICATION_JSON) -@Produces(MediaType.APPLICATION_JSON) +@Produces(MediaType.APPLICATION_OCTET_STREAM) class InvoicingResource( private val service: InvoicingService ) { @@ -30,7 +30,6 @@ class InvoicingResource( @Path("facturx/pdf") @POST - @Produces(MediaType.APPLICATION_OCTET_STREAM) @Operation(summary = "Create a Factur-X / ZUGFeRD XML, transforms it to PDF and attaches before created XML to it") fun createFacturXPdf(invoice: Invoice): Response { val pdfFile = service.createFacturXPdf(invoice) @@ -40,4 +39,14 @@ class InvoicingResource( .build() } + + + @Path("extract") + @POST + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + @Produces(MediaType.APPLICATION_JSON) + @Operation(summary = "Extract invoice data from a Factur-x / ZUGFeRD or XRechnung file") + fun extractInvoiceData(invoice: java.nio.file.Path) = + service.extractInvoiceData(invoice) + } \ No newline at end of file diff --git a/e-invoice-api/src/main/kotlin/net/codinux/invoicing/service/InvoicingService.kt b/e-invoice-api/src/main/kotlin/net/codinux/invoicing/service/InvoicingService.kt index 7bc6340..c5e709c 100644 --- a/e-invoice-api/src/main/kotlin/net/codinux/invoicing/service/InvoicingService.kt +++ b/e-invoice-api/src/main/kotlin/net/codinux/invoicing/service/InvoicingService.kt @@ -3,13 +3,18 @@ package net.codinux.invoicing.service import jakarta.inject.Singleton import net.codinux.invoicing.creation.EInvoiceCreator import net.codinux.invoicing.model.Invoice +import net.codinux.invoicing.reader.EInvoiceReader import java.io.File +import java.nio.file.Path +import kotlin.io.path.extension @Singleton class InvoicingService { private val creator = EInvoiceCreator() + private val reader = EInvoiceReader() + fun createXRechnung(invoice: Invoice): String = creator.createXRechnungXml(invoice) @@ -27,4 +32,11 @@ class InvoicingService { return resultFile } + + fun extractInvoiceData(invoiceFile: Path) = when (invoiceFile.extension.lowercase()) { + "xml" -> reader.readFromXml(invoiceFile.toFile()) + "pdf" -> reader.extractFromPdf(invoiceFile.toFile()) + else -> throw IllegalArgumentException("We can only extract eInvoice data from .xml and .pdf files") + } + } \ No newline at end of file diff --git a/e-invoice-api/src/main/resources/application.properties b/e-invoice-api/src/main/resources/application.properties index fc78ca3..9c59bb2 100644 --- a/e-invoice-api/src/main/resources/application.properties +++ b/e-invoice-api/src/main/resources/application.properties @@ -6,6 +6,8 @@ quarkus.http.enable-compression=true +quarkus.http.body.delete-uploaded-files-on-end=true + %dev.quarkus.http.cors=true quarkus.http.cors.origins=* quarkus.http.cors.headers=accept, authorization, content-type, x-requested-with