2024-11-15 17:02:50 +00:00
|
|
|
# eInvoicing
|
|
|
|
|
|
|
|
Tools for working with eInvoicing according to EU standard EU 16931.
|
|
|
|
|
2024-11-21 01:29:15 +00:00
|
|
|
As ZUGFeRD 2 and Factur-X unified their specification, these two names are used synonymously and interchangeably
|
|
|
|
throughout the documentation and code.
|
|
|
|
|
2024-11-15 17:02:50 +00:00
|
|
|
## Reading
|
|
|
|
|
|
|
|
### Extract eInvoice from a PDF or XML file:
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val reader = EInvoiceReader()
|
|
|
|
|
2024-11-21 01:05:47 +00:00
|
|
|
// extract invoice data from a ZUGFeRD or Factor-X PDF that contains eInvoice XML as attachment
|
2024-11-15 17:02:50 +00:00
|
|
|
val invoiceFromPDF = reader.extractFromPdf(File("ZUGFeRD.pdf"))
|
|
|
|
|
2024-11-21 01:05:47 +00:00
|
|
|
// extract eInvoice data from a XML file like XRechnung:
|
|
|
|
val invoiceFromXml = reader.extractFromXml(File("XRechnung.xml"))
|
2024-11-15 17:02:50 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Find all invoices of an IMAP email account
|
|
|
|
|
|
|
|
```kotlin
|
2024-11-15 17:15:41 +00:00
|
|
|
val mailReader = MailReader()
|
|
|
|
|
|
|
|
val mailsWithEInvoices = mailReader.listAllMessagesWithEInvoice(MailAccount(
|
|
|
|
username = "", // your mail account username
|
|
|
|
password = "", // your mail account username
|
|
|
|
serverAddress = "", // IMAP server address
|
2024-11-26 00:34:51 +00:00
|
|
|
port = null // IMAP server port, can be left null for default port 993
|
2024-11-15 17:15:41 +00:00
|
|
|
))
|
|
|
|
```
|
|
|
|
|
2024-11-18 15:24:20 +00:00
|
|
|
### Validate eInvoice
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val validator = EInvoiceValidator()
|
|
|
|
val invoiceFile = File("ZUGFeRD.pdf") // or XRechnung,xml, ...
|
|
|
|
|
|
|
|
val result = validator.validate(invoiceFile)
|
|
|
|
|
|
|
|
println("Is valid? ${result.isValid}")
|
|
|
|
println(result.report)
|
|
|
|
```
|
|
|
|
|
2024-11-15 17:15:41 +00:00
|
|
|
## Create eInvoice
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
fun create() {
|
|
|
|
val invoice = createInvoice()
|
|
|
|
val pdfResultFile = File.createTempFile("Zugferd", ".pdf")
|
|
|
|
|
|
|
|
val creator = EInvoiceCreator()
|
|
|
|
|
|
|
|
// create a PDF that also contains the eInvoice as XML attachment
|
2024-11-21 01:05:47 +00:00
|
|
|
creator.createFacturXPdf(invoice, pdfResultFile)
|
2024-11-15 17:15:41 +00:00
|
|
|
|
|
|
|
// create only the XML file
|
2024-11-21 01:05:47 +00:00
|
|
|
val xml = creator.createFacturXXml(invoice)
|
2024-11-15 17:15:41 +00:00
|
|
|
|
|
|
|
// create a XRechnung
|
|
|
|
val xRechnung = creator.createXRechnungXml(invoice)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createInvoice() = Invoice(
|
|
|
|
invoiceNumber = "RE-00001",
|
|
|
|
invoicingDate = LocalDate.now(),
|
|
|
|
sender = Party("codinux GmbH & Co. KG", "Fun Street 1", "12345", "Glückstadt"),
|
|
|
|
recipient = Party("Abzock GmbH", "Ausbeutstr.", "12345", "Abzockhausen"),
|
2024-11-24 15:06:02 +00:00
|
|
|
items = listOf(InvoiceItem("Erbrachte Dienstleistungen", BigDecimal(170), "HUR", BigDecimal(105), BigDecimal(19))) // HUR = EN code for hour
|
2024-11-15 17:15:41 +00:00
|
|
|
)
|
2024-11-19 22:49:39 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Attach invoice XML to existing PDF
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
val invoice: Invoice = createInvoice()
|
|
|
|
val existingPdf = File("Invoice.pdf")
|
|
|
|
val output = File("Zugferd.pdf")
|
|
|
|
|
|
|
|
val creator = EInvoiceCreator()
|
|
|
|
|
2024-11-21 01:05:47 +00:00
|
|
|
creator.attachInvoiceXmlToPdf(invoice, existingPdf, output)
|
2024-11-19 22:52:18 +00:00
|
|
|
|
|
|
|
// or if you already have the invoice XML:
|
|
|
|
val invoiceXml: String = "..." // e.g. creator.createZugferdXml(invoice)
|
|
|
|
|
2024-11-21 01:05:47 +00:00
|
|
|
creator.attachInvoiceXmlToPdf(invoiceXml, existingPdf, output)
|
2024-11-15 17:02:50 +00:00
|
|
|
```
|