Renamed methods in domain to the names of API

This commit is contained in:
dankito 2024-11-21 02:05:47 +01:00
parent 2d42e58d0f
commit f37ee036f0
9 changed files with 49 additions and 37 deletions

View File

@ -9,11 +9,11 @@ Tools for working with eInvoicing according to EU standard EU 16931.
```kotlin
val reader = EInvoiceReader()
// read a ZUGFeRD or Factor-X PDF that contains eInvoice XML as attachment
// extract invoice data from a ZUGFeRD or Factor-X PDF that contains eInvoice XML as attachment
val invoiceFromPDF = reader.extractFromPdf(File("ZUGFeRD.pdf"))
// read a eInvoice XML file like XRechnung:
val invoiceFromXml = reader.readFromXml(File("XRechnung.xml"))
// extract eInvoice data from a XML file like XRechnung:
val invoiceFromXml = reader.extractFromXml(File("XRechnung.xml"))
```
### Find all invoices of an IMAP email account
@ -51,10 +51,10 @@ fun create() {
val creator = EInvoiceCreator()
// create a PDF that also contains the eInvoice as XML attachment
creator.createZugferdPdf(invoice, pdfResultFile)
creator.createFacturXPdf(invoice, pdfResultFile)
// create only the XML file
val xml = creator.createZugferdXml(invoice)
val xml = creator.createFacturXXml(invoice)
// create a XRechnung
val xRechnung = creator.createXRechnungXml(invoice)
@ -78,10 +78,10 @@ val output = File("Zugferd.pdf")
val creator = EInvoiceCreator()
creator.combinePdfAndInvoiceXml(invoice, existingPdf, output)
creator.attachInvoiceXmlToPdf(invoice, existingPdf, output)
// or if you already have the invoice XML:
val invoiceXml: String = "..." // e.g. creator.createZugferdXml(invoice)
creator.combinePdfAndInvoiceXml(invoiceXml, existingPdf, output)
creator.attachInvoiceXmlToPdf(invoiceXml, existingPdf, output)
```

View File

@ -23,12 +23,12 @@ class InvoicingService {
creator.createXRechnungXml(invoice)
fun createFacturXXml(invoice: Invoice): String =
creator.createZugferdXml(invoice)
creator.createFacturXXml(invoice)
fun createFacturXPdf(invoice: Invoice): Path {
val resultFile = createTempPdfFile()
creator.createZugferdPdf(invoice, resultFile.toFile())
creator.createFacturXPdf(invoice, resultFile.toFile())
return resultFile
}
@ -36,14 +36,14 @@ class InvoicingService {
fun attachInvoiceXmlToPdf(invoice: Invoice, pdf: Path): Path {
val resultFile = createTempPdfFile()
creator.combinePdfAndInvoiceXml(invoice, pdf.toFile(), resultFile.toFile())
creator.attachInvoiceXmlToPdf(invoice, pdf.toFile(), resultFile.toFile())
return resultFile
}
fun extractInvoiceData(invoiceFile: Path) = when (invoiceFile.extension.lowercase()) {
"xml" -> reader.readFromXml(invoiceFile.toFile())
"xml" -> reader.extractFromXml(invoiceFile.toFile())
"pdf" -> reader.extractFromPdf(invoiceFile.toFile())
else -> throw IllegalArgumentException("We can only extract eInvoice data from .xml and .pdf files")
}

View File

@ -16,15 +16,26 @@ class EInvoiceCreator(
return createXml(provider, invoice)
}
fun createZugferdXml(invoice: Invoice): String {
/**
* Synonym for [createFacturXXml] (ZUGFeRD 2 is a synonym for Factur-X).
*/
fun createZugferdXml(invoice: Invoice) = createFacturXXml(invoice)
fun createFacturXXml(invoice: Invoice): String {
val exporter = ZUGFeRDExporterFromA3()
.setProfile("EN16931") // required for XML?
return createXml(exporter.provider, invoice)
}
fun createZugferdPdf(invoice: Invoice, outputFile: File) {
val xml = createZugferdXml(invoice)
/**
* Synonym for [createFacturXPdf] (ZUGFeRD 2 is a synonym for Factur-X).
*/
fun createZugferdPdf(invoice: Invoice, outputFile: File) = createFacturXPdf(invoice, outputFile)
fun createFacturXPdf(invoice: Invoice, outputFile: File) {
val xml = createFacturXXml(invoice)
val xmlFile = File.createTempFile(outputFile.nameWithoutExtension, ".xml")
.also { it.writeText(xml) }
val pdfFile = File(xmlFile.parentFile, xmlFile.nameWithoutExtension + ".pdf")
@ -32,16 +43,17 @@ class EInvoiceCreator(
val visualizer = ZUGFeRDVisualizer()
visualizer.toPDF(xmlFile.absolutePath, pdfFile.absolutePath)
combinePdfAndInvoiceXml(xml, pdfFile, outputFile)
attachInvoiceXmlToPdf(xml, pdfFile, outputFile)
xmlFile.delete()
pdfFile.delete()
}
fun combinePdfAndInvoiceXml(invoice: Invoice, pdfFile: File, outputFile: File) =
combinePdfAndInvoiceXml(createZugferdXml(invoice), pdfFile, outputFile)
fun combinePdfAndInvoiceXml(invoiceXml: String, pdfFile: File, outputFile: File) {
fun attachInvoiceXmlToPdf(invoice: Invoice, pdfFile: File, outputFile: File) =
attachInvoiceXmlToPdf(createFacturXXml(invoice), pdfFile, outputFile)
fun attachInvoiceXmlToPdf(invoiceXml: String, pdfFile: File, outputFile: File) {
val exporter = ZUGFeRDExporterFromA3()
.setZUGFeRDVersion(2)
.setProfile("EN16931") // available values: MINIMUM, BASICWL, BASIC, CIUS, EN16931, EXTENDED, XRECHNUNG

View File

@ -37,7 +37,7 @@ class FilesystemInvoiceReader(
if (extension == "pdf") {
eInvoiceReader.extractFromPdf(file.inputStream())
} else if (extension == "xml") {
eInvoiceReader.readFromXml(file.inputStream())
eInvoiceReader.extractFromXml(file.inputStream())
} else {
null
}

View File

@ -108,7 +108,7 @@ class MailReader(
if (filename.endsWith(".pdf") || contentType.startsWith("application/pdf") || contentType.startsWith("application/octet-stream")) {
eInvoiceReader.extractFromPdf(part.inputStream)
} else if (filename.endsWith(".xml") || contentType.startsWith("application/xml") || contentType.startsWith("text/xml")) {
eInvoiceReader.readFromXml(part.inputStream)
eInvoiceReader.extractFromXml(part.inputStream)
} else {
null
}

View File

@ -10,11 +10,11 @@ class EInvoiceReader(
private val mapper: MustangMapper = MustangMapper()
) {
fun readFromXml(xmlFile: File) = readFromXml(xmlFile.inputStream())
fun extractFromXml(xmlFile: File) = extractFromXml(xmlFile.inputStream())
fun readFromXml(stream: InputStream) = readFromXml(stream.reader().readText())
fun extractFromXml(stream: InputStream) = extractFromXml(stream.reader().readText())
fun readFromXml(xml: String): Invoice {
fun extractFromXml(xml: String): Invoice {
val importer = ZUGFeRDInvoiceImporter() // XRechnungImporter only reads properties but not to a Invoice object
importer.fromXML(xml)

View File

@ -17,11 +17,11 @@ class Demonstration {
fun read() {
val reader = EInvoiceReader()
// read a ZUGFeRD or Factor-X PDF that contains eInvoice XML as attachment
// extract invoice data from a ZUGFeRD or Factor-X PDF that contains eInvoice XML as attachment
val invoiceFromPDF = reader.extractFromPdf(File("ZUGFeRD.pdf"))
// read a eInvoice XML file like XRechnung:
val invoiceFromXml = reader.readFromXml(File("XRechnung.xml"))
// extract eInvoice data from a XML file like XRechnung:
val invoiceFromXml = reader.extractFromXml(File("XRechnung.xml"))
}
fun fromMail() {
@ -53,28 +53,28 @@ class Demonstration {
val creator = EInvoiceCreator()
// create a PDF that also contains the eInvoice as XML attachment
creator.createZugferdPdf(invoice, pdfResultFile)
creator.createFacturXPdf(invoice, pdfResultFile)
// create only the XML file
val xml = creator.createZugferdXml(invoice)
val xml = creator.createFacturXXml(invoice)
// create a XRechnung
val xRechnung = creator.createXRechnungXml(invoice)
}
fun combinePdfAndInvoiceXml() {
fun attachInvoiceXmlToPdf() {
val invoice: Invoice = createInvoice()
val existingPdf = File("Invoice.pdf")
val output = File("Zugferd.pdf")
val creator = EInvoiceCreator()
creator.combinePdfAndInvoiceXml(invoice, existingPdf, output)
creator.attachInvoiceXmlToPdf(invoice, existingPdf, output)
// or if you already have the invoice XML:
val invoiceXml: String = "..." // e.g. creator.createZugferdXml(invoice)
creator.combinePdfAndInvoiceXml(invoiceXml, existingPdf, output)
creator.attachInvoiceXmlToPdf(invoiceXml, existingPdf, output)
}

View File

@ -21,20 +21,20 @@ class EInvoiceCreatorTest {
}
@Test
fun createZugferdXml() {
fun createFacturXXml() {
val invoice = createInvoice()
val result = underTest.createZugferdXml(invoice)
val result = underTest.createFacturXXml(invoice)
assertInvoiceXml(result)
}
@Test
fun createZugferdPdf() {
fun createFacturXPdf() {
val invoice = createInvoice()
val testFile = File.createTempFile("Zugferd", ".pdf")
underTest.createZugferdPdf(invoice, testFile)
underTest.createFacturXPdf(invoice, testFile)
val importer = ZUGFeRDInvoiceImporter(testFile.inputStream())
val xml = String(importer.rawXML, Charsets.UTF_8)

View File

@ -11,8 +11,8 @@ class EInvoiceReaderTest {
@Test
fun readFromXml() {
val result = underTest.readFromXml(getTestFile("XRechnung.xml"))
fun extractFromXml() {
val result = underTest.extractFromXml(getTestFile("XRechnung.xml"))
assertInvoice(result)
}