diff --git a/README.md b/README.md index 0176b25..5760c6c 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ fun create() { val creator = EInvoiceCreator() // create a PDF that also contains the eInvoice as XML attachment - creator.createFacturXPdf(invoice, pdfResultFile) + creator.createPdfWithAttachedXml(invoice, pdfResultFile) // create only the XML file val xml = creator.createFacturXXml(invoice) 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 86a8f5b..b66ed6f 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 @@ -27,7 +27,7 @@ class InvoicingService { fun createFacturXPdf(invoice: Invoice): Path { val resultFile = createTempPdfFile() - creator.createFacturXPdf(invoice, resultFile.toFile()) + creator.createPdfWithAttachedXml(invoice, resultFile.toFile()) return resultFile } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt index 9acfb1d..536b2ea 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt @@ -10,46 +10,39 @@ open class EInvoiceCreator( protected open val mapper: MustangMapper = MustangMapper() ) { - open fun createXRechnungXml(invoice: Invoice): String { - val provider = ZUGFeRD2PullProvider() - provider.profile = Profiles.getByName("XRechnung") - - return createXml(provider, invoice) - } - + open fun createXRechnungXml(invoice: Invoice) = createXml(invoice, EInvoiceXmlFormat.XRechnung) /** * Synonym for [createFacturXXml] (ZUGFeRD 2 is a synonym for Factur-X). */ open fun createZugferdXml(invoice: Invoice) = createFacturXXml(invoice) - open fun createFacturXXml(invoice: Invoice): String { + open fun createFacturXXml(invoice: Invoice) = createXml(invoice, EInvoiceXmlFormat.FacturX) + + protected open fun createXml(invoice: Invoice, format: EInvoiceXmlFormat): String { val exporter = ZUGFeRDExporterFromA3() - .setProfile("EN16931") // required for XML? + .setProfile(getProfileNameForFormat(format)) return createXml(exporter.provider, invoice) } - /** - * Synonym for [createFacturXPdf] (ZUGFeRD 2 is a synonym for Factur-X). - */ - open fun createZugferdPdf(invoice: Invoice, outputFile: File) = createFacturXPdf(invoice, outputFile) + protected open fun createXml(provider: IXMLProvider, invoice: Invoice): String { + val transaction = mapper.mapToTransaction(invoice) - open fun createFacturXPdf(invoice: Invoice, outputFile: File) { - val xml = createFacturXXml(invoice) + provider.generateXML(transaction) - createFacturXPdf(xml, EInvoiceXmlFormat.FacturX, outputFile) + return String(provider.xml, Charsets.UTF_8) } + /** - * Synonym for [createFacturXPdfWithXRechnungXML] (ZUGFeRD 2 is a synonym for Factur-X). + * Creates a hybrid PDF that also contains the Factur-X / ZUGFeRD or XRechnung XML as attachment. */ - open fun createZugferdPdfWithXRechnungXML(invoice: Invoice, outputFile: File) = createFacturXPdfWithXRechnungXML(invoice, outputFile) + @JvmOverloads + open fun createPdfWithAttachedXml(invoice: Invoice, outputFile: File, format: EInvoiceXmlFormat = EInvoiceXmlFormat.FacturX) { + val xml = createXml(invoice, format) - open fun createFacturXPdfWithXRechnungXML(invoice: Invoice, outputFile: File) { - val xml = createXRechnungXml(invoice) - - createFacturXPdf(xml, EInvoiceXmlFormat.XRechnung, outputFile) + createFacturXPdf(xml, format, outputFile) } protected open fun createFacturXPdf(invoiceXml: String, format: EInvoiceXmlFormat, outputFile: File) { @@ -67,8 +60,9 @@ open class EInvoiceCreator( } - open fun attachInvoiceXmlToPdf(invoice: Invoice, pdfFile: File, outputFile: File) = - attachInvoiceXmlToPdf(createFacturXXml(invoice), EInvoiceXmlFormat.FacturX, pdfFile, outputFile) + @JvmOverloads + open fun attachInvoiceXmlToPdf(invoice: Invoice, pdfFile: File, outputFile: File, format: EInvoiceXmlFormat = EInvoiceXmlFormat.FacturX) = + attachInvoiceXmlToPdf(createXml(invoice, format), format, pdfFile, outputFile) open fun attachInvoiceXmlToPdf(invoiceXml: String, format: EInvoiceXmlFormat, pdfFile: File, outputFile: File) { val exporter = ZUGFeRDExporterFromA3() @@ -87,15 +81,6 @@ open class EInvoiceCreator( } - protected open fun createXml(provider: IXMLProvider, invoice: Invoice): String { - val transaction = mapper.mapToTransaction(invoice) - - provider.generateXML(transaction) - - return String(provider.xml, Charsets.UTF_8) - } - - protected open fun getProfileNameForFormat(format: EInvoiceXmlFormat) = when (format) { EInvoiceXmlFormat.FacturX -> "EN16931" // available values: MINIMUM, BASICWL, BASIC, CIUS, EN16931, EXTENDED, XRECHNUNG EInvoiceXmlFormat.XRechnung -> "XRECHNUNG" diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt index d6fdb66..038d912 100644 --- a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt @@ -58,7 +58,7 @@ class Demonstration { val creator = EInvoiceCreator() // create a PDF that also contains the eInvoice as XML attachment - creator.createFacturXPdf(invoice, pdfResultFile) + creator.createPdfWithAttachedXml(invoice, pdfResultFile) // create only the XML file val xml = creator.createFacturXXml(invoice) diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt index 767db89..47f6b5c 100644 --- a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt @@ -1,5 +1,6 @@ package net.codinux.invoicing.creation +import net.codinux.invoicing.model.EInvoiceXmlFormat import net.codinux.invoicing.test.DataGenerator import net.codinux.invoicing.test.InvoiceAsserter import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter @@ -30,11 +31,11 @@ class EInvoiceCreatorTest { } @Test - fun createFacturXPdf() { + fun createPdfWithAttachedXml_FacturX() { val invoice = createInvoice() val testFile = File.createTempFile("Zugferd", ".pdf") - underTest.createFacturXPdf(invoice, testFile) + underTest.createPdfWithAttachedXml(invoice, testFile, EInvoiceXmlFormat.FacturX) val importer = testFile.inputStream().use { ZUGFeRDInvoiceImporter(it) } val xml = String(importer.rawXML, Charsets.UTF_8) @@ -43,11 +44,11 @@ class EInvoiceCreatorTest { } @Test - fun createFacturXPdfWithXRechnungXML() { + fun createPdfWithAttachedXml_XRechnung() { val invoice = createInvoice() val testFile = File.createTempFile("Zugferd", ".pdf") - underTest.createFacturXPdfWithXRechnungXML(invoice, testFile) + underTest.createPdfWithAttachedXml(invoice, testFile, EInvoiceXmlFormat.XRechnung) val importer = testFile.inputStream().use { ZUGFeRDInvoiceImporter(it) } val xml = String(importer.rawXML, Charsets.UTF_8)