Controlling now eInvoice XML format via EInvoiceXmlFormat

This commit is contained in:
dankito 2024-11-26 16:39:19 +01:00
parent 6ec302e50f
commit f24b2004bb
5 changed files with 26 additions and 40 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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"

View File

@ -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)

View File

@ -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)