diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt index 2edb32a..db5f3a5 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt @@ -3,14 +3,36 @@ package net.codinux.invoicing.converter import net.codinux.invoicing.creation.EInvoiceCreator import net.codinux.invoicing.model.Invoice import org.mustangproject.CII.CIIToUBL +import org.mustangproject.ZUGFeRD.ZUGFeRDVisualizer import java.io.File class 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 + } + + /** * Converts a CII (Cross Industry Invoice) invoice, e.g. a Zugferd or Factur-X invoice, to UBL (Universal Business Language). */ - fun convertCiiToUbl(invoice: Invoice) = convertCiiToUbl(EInvoiceCreator().createXRechnungXml(invoice)) + fun convertCiiToUbl(invoice: Invoice) = convertCiiToUbl(createXRechnungXml(invoice)) /** * Converts a CII (Cross Industry Invoice) invoice, e.g. a Zugferd or Factur-X invoice, to UBL (Universal Business Language). @@ -38,4 +60,14 @@ class EInvoiceConverter { val cii2Ubl = CIIToUBL() cii2Ubl.convert(xmlFile, outputFile) } + + + private fun createXRechnungXml(invoice: Invoice): String = EInvoiceCreator().createXRechnungXml(invoice) + + private fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) { + javaClass.classLoader.getResourceAsStream(resourceName).use { + it?.copyTo(File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream()) + } + } + } \ No newline at end of file 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 4d109d1..4d9e117 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,33 +52,6 @@ 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/converter/EInvoiceConverterTest.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/converter/EInvoiceConverterTest.kt index a722ffe..2c31356 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/converter/EInvoiceConverterTest.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/converter/EInvoiceConverterTest.kt @@ -3,6 +3,7 @@ package net.codinux.invoicing.converter import assertk.assertThat import assertk.assertions.isNotEmpty import net.codinux.invoicing.test.DataGenerator +import java.io.File import kotlin.test.Test class EInvoiceConverterTest { @@ -10,6 +11,16 @@ class EInvoiceConverterTest { private val underTest = EInvoiceConverter() + @Test + fun convertInvoiceToHtml() { + val invoice = createInvoice() + val testFile = File.createTempFile("Zugferd", ".html") + + val result = underTest.convertInvoiceToHtml(invoice, testFile) + + assertThat(result).isNotEmpty() + } + @Test fun convertCiiToUbl() { val invoice = createInvoice() 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 5ceae93..1beb989 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,7 +1,5 @@ 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 @@ -44,16 +42,6 @@ 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()