From 1a967efcbdc555b8350e2b557e2e65bb87d8fa90 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 15 Nov 2024 01:24:38 +0100 Subject: [PATCH] Implemented convertInvoiceToHtml() --- .../invoicing/creation/EInvoiceCreator.kt | 27 +++++++++++++++++++ .../invoicing/creation/EInvoiceCreatorTest.kt | 14 +++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt index 4d9e117..4d109d1 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt @@ -52,6 +52,33 @@ class EInvoiceCreator( exporter.export(outputFile.outputStream()) } + // extract to EInvoiceConverter? + fun convertInvoiceToHtml(invoice: Invoice, outputFile: File, language: ZUGFeRDVisualizer.Language = ZUGFeRDVisualizer.Language.DE) = + convertInvoiceToHtml(createXRechnungXml(invoice), outputFile, language) + + fun convertInvoiceToHtml(invoiceXml: String, outputFile: File, language: ZUGFeRDVisualizer.Language = ZUGFeRDVisualizer.Language.DE): String { + val xmlFile = File.createTempFile("Zugferd", ".xml") + .also { it.writeText(invoiceXml) } + + val visualizer = ZUGFeRDVisualizer() + + val html = visualizer.visualize(xmlFile.absolutePath, language) + + outputFile.writeText(html) + copyResource("xrechnung-viewer.css", outputFile, ".css") + copyResource("xrechnung-viewer.js", outputFile, ".js") + + xmlFile.delete() + + return html + } + + private fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) { + javaClass.classLoader.getResourceAsStream(resourceName).use { + it?.copyTo(File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream()) + } + } + private fun createXml(provider: IXMLProvider, invoice: Invoice): String { val transaction = mapper.mapToTransaction(invoice) diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt index 87cf26d..5ceae93 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt @@ -1,5 +1,7 @@ package net.codinux.invoicing.creation +import assertk.assertThat +import assertk.assertions.isNotEmpty import net.codinux.invoicing.test.DataGenerator import net.codinux.invoicing.test.InvoiceAsserter import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter @@ -31,8 +33,8 @@ class EInvoiceCreatorTest { @Test fun createZugferdPdf() { - val testFile = File.createTempFile("Zugferd", ".pdf") val invoice = createInvoice() + val testFile = File.createTempFile("Zugferd", ".pdf") underTest.createZugferdPdf(invoice, testFile) @@ -42,6 +44,16 @@ class EInvoiceCreatorTest { assertInvoiceXml(xml) } + @Test + fun convertInvoiceToHtml() { + val invoice = createInvoice() + val testFile = File.createTempFile("Zugferd", ".html") + + val result = underTest.convertInvoiceToHtml(invoice, testFile) + + assertThat(result).isNotEmpty() + } + private fun createInvoice() = DataGenerator.createInvoice()