From 3f84e7994ae884fc3256f7446e25226d11a08c36 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 15 Nov 2024 02:11:28 +0100 Subject: [PATCH] Implemented converting CII to UBL --- .../invoicing/converter/EInvoiceConverter.kt | 41 +++++++++++++++++++ .../converter/EInvoiceConverterTest.kt | 25 +++++++++++ 2 files changed, 66 insertions(+) create mode 100644 e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt create mode 100644 e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/converter/EInvoiceConverterTest.kt 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 new file mode 100644 index 0000000..2edb32a --- /dev/null +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/converter/EInvoiceConverter.kt @@ -0,0 +1,41 @@ +package net.codinux.invoicing.converter + +import net.codinux.invoicing.creation.EInvoiceCreator +import net.codinux.invoicing.model.Invoice +import org.mustangproject.CII.CIIToUBL +import java.io.File + +class EInvoiceConverter { + + /** + * 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)) + + /** + * Converts a CII (Cross Industry Invoice) invoice, e.g. a Zugferd or Factur-X invoice, to UBL (Universal Business Language). + */ + fun convertCiiToUbl(invoiceXml: String): String { + // TODO: extract a common method for this + val xmlFile = File.createTempFile("Zugferd", ".xml") + .also { it.writeText(invoiceXml) } + val ublFile = File(xmlFile.parentFile, xmlFile.nameWithoutExtension + "-ubl.xml") + + convertCiiToUbl(xmlFile, ublFile) + + val ubl = ublFile.readText() + + xmlFile.delete() + ublFile.delete() + + return ubl + } + + /** + * Converts a CII (Cross Industry Invoice) invoice, e.g. a Zugferd or Factur-X invoice, to UBL (Universal Business Language). + */ + fun convertCiiToUbl(xmlFile: File, outputFile: File) { + val cii2Ubl = CIIToUBL() + cii2Ubl.convert(xmlFile, outputFile) + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..a722ffe --- /dev/null +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/converter/EInvoiceConverterTest.kt @@ -0,0 +1,25 @@ +package net.codinux.invoicing.converter + +import assertk.assertThat +import assertk.assertions.isNotEmpty +import net.codinux.invoicing.test.DataGenerator +import kotlin.test.Test + +class EInvoiceConverterTest { + + private val underTest = EInvoiceConverter() + + + @Test + fun convertCiiToUbl() { + val invoice = createInvoice() + + val result = underTest.convertCiiToUbl(invoice) + + assertThat(result).isNotEmpty() + } + + + private fun createInvoice() = DataGenerator.createInvoice() + +} \ No newline at end of file