From f37ee036f0febfbd3fe09f466e85111ee0f1a85a Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 21 Nov 2024 02:05:47 +0100 Subject: [PATCH] Renamed methods in domain to the names of API --- README.md | 14 +++++----- .../invoicing/service/InvoicingService.kt | 8 +++--- .../invoicing/creation/EInvoiceCreator.kt | 26 ++++++++++++++----- .../filesystem/FilesystemInvoiceReader.kt | 2 +- .../net/codinux/invoicing/mail/MailReader.kt | 2 +- .../invoicing/reader/EInvoiceReader.kt | 6 ++--- .../net/codinux/invoicing/Demonstration.kt | 16 ++++++------ .../invoicing/creation/EInvoiceCreatorTest.kt | 8 +++--- .../invoicing/reader/EInvoiceReaderTest.kt | 4 +-- 9 files changed, 49 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e3dea1e..f7d68d6 100644 --- a/README.md +++ b/README.md @@ -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) ``` \ No newline at end of file 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 36d502d..2142b30 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 @@ -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") } 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 0cafae8..8b4d700 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 @@ -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 diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt index 99b0080..0834642 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FilesystemInvoiceReader.kt @@ -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 } diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt index 4d49a87..95cdf43 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt @@ -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 } diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt index dda5e44..0f0ce6a 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt @@ -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) diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt index d0c4aed..659ec84 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt @@ -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) } 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 1beb989..c0ca1df 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 @@ -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) diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/reader/EInvoiceReaderTest.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/reader/EInvoiceReaderTest.kt index 5c34df0..19f7c2a 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/reader/EInvoiceReaderTest.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/reader/EInvoiceReaderTest.kt @@ -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) }