Skip to content

Commit

Permalink
implemented ZUGFerd XML creation and embedding upon PDF creation. issue
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Sep 11, 2024
1 parent 2769d8e commit 8bec54c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST
byte[] content=CachingDocumentLoader.getInstance().getDocument(doc.getId());
if (this.convertToPdf && conv.supportsInputFormat(doc.getName())) {
String tempPath = FileUtils.createTempFile(doc.getName(), content);
String tempPdfPath = conv.convertToPDF(tempPath);
String tempPdfPath = conv.convertToPDF(tempPath, doc);
byte[] fileContent = FileUtils.readFile(new File(tempPdfPath));
FileUtils.cleanupTempFile(tempPdfPath);
FileUtils.cleanupTempFile(tempPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5598,7 +5598,7 @@ public void convertDocumentToPdf(ArchiveFileDocumentsBean doc) throws Exception

FileConverter conv = FileConverter.getInstance();
String tempPath = FileUtils.createTempFile(doc.getName(), content);
String tempPdfPath = conv.convertToPDF(tempPath);
String tempPdfPath = conv.convertToPDF(tempPath, doc);
byte[] pdfContent = FileUtils.readFile(new File(tempPdfPath));
FileUtils.cleanupTempFile(tempPath);
FileUtils.cleanupTempFile(tempPdfPath);
Expand Down Expand Up @@ -6029,7 +6029,7 @@ private void mnuSaveDocumentEncryptedActionPerformed(java.awt.event.ActionEvent

if (!(tmpUrl.toLowerCase().endsWith(".pdf"))) {
FileConverter conv = FileConverter.getInstance();
tmpUrl = conv.convertToPDF(tmpUrl);
tmpUrl = conv.convertToPDF(tmpUrl, doc);
}

PDFEncryptionDialog encDlg = new PDFEncryptionDialog(EditorsRegistry.getInstance().getMainWindow(), true, tmpUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public boolean execute() throws Exception {
if (doc.getName().toLowerCase().endsWith(".pdf") || !(conv.supportsInputFormat(doc.getName().toLowerCase()))) {
dlg.addAttachment(tmpUrl, doc.getDictateSign());
} else {
String pdfUrl = conv.convertToPDF(tmpUrl);
String pdfUrl = conv.convertToPDF(tmpUrl, doc);
try {
// give some more time to LibreOffice to shut down
Thread.sleep(2500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@

import com.jdimension.jlawyer.client.bea.BeaAccess;
import com.jdimension.jlawyer.client.settings.ClientSettings;
import com.jdimension.jlawyer.client.settings.UserSettings;
import com.jdimension.jlawyer.client.utils.einvoice.EInvoiceUtils;
import com.jdimension.jlawyer.persistence.AppUserBean;
import com.jdimension.jlawyer.persistence.ArchiveFileDocumentsBean;
import com.jdimension.jlawyer.persistence.Invoice;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -675,6 +681,8 @@
import org.jlawyer.bea.model.Attachment;
import org.jlawyer.bea.model.Message;
import org.jlawyer.bea.model.MessageExport;
import org.mustangproject.ZUGFeRD.IZUGFeRDExporter;
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromPDFA;

/**
*
Expand Down Expand Up @@ -708,6 +716,49 @@ public String convertToPDF(String file) throws Exception {
return null;
}

/**
* This method will check if the document is related to an invoice and
* generate a ZUGFerd electronic invoice.
*
* @param file
* @param doc
* @return
* @throws Exception
*/
public String convertToPDF(String file, ArchiveFileDocumentsBean doc) throws Exception {

String pdfTempFile = convertToPDF(file);

ClientSettings settings = ClientSettings.getInstance();
FileConverter conv = FileConverter.getInstance();
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
List<Invoice> invoices = locator.lookupArchiveFileServiceRemote().getInvoicesForDocument(doc.getId());
if (invoices.size() > 1) {
throw new Exception("Diesem Dokument ist mit mehreren Rechnungen verknüpft, elektronische Rechnung kann nicht erstellt werden.");
}
if (invoices.size() == 1) {
// generate electronic invoice
Invoice sourceInvoice = invoices.get(0);
if (StringUtils.isEmpty(sourceInvoice.getSender())) {
throw new Exception("Die dem Dokument zugeordnete Rechnung hat keinen Absender, elektronische Rechnung kann nicht erstellt werden.");
}

AppUserBean sender = locator.lookupSystemManagementRemote().getUser(sourceInvoice.getSender());
if (sender == null) {
throw new Exception("Die dem Dokument zugeordnete Rechnung hat den unbekannten Absender '" + sourceInvoice.getSender() + "', elektronische Rechnung kann nicht erstellt werden.");
}

org.mustangproject.Invoice i = EInvoiceUtils.getEInvoice(sourceInvoice, sender);


IZUGFeRDExporter ze = new ZUGFeRDExporterFromPDFA().load(pdfTempFile).setProducer("j-lawyer.org " + VersionUtils.getFullClientVersion()).setCreator(UserSettings.getInstance().getCurrentUser().getDisplayName());
ze.setTransaction(i);
ze.export(pdfTempFile);
}

return pdfTempFile;
}

public String convertTo(String file, String targetFileExtension) throws Exception {
return null;
}
Expand Down Expand Up @@ -738,7 +789,7 @@ protected static String pdf2pdf(String url) throws Exception {
}
return url;
}

protected String bea2pdf(String url) throws Exception {
if (url.toLowerCase().endsWith(".bea")) {

Expand All @@ -747,24 +798,24 @@ protected String bea2pdf(String url) throws Exception {
MessageExport export = new MessageExport();
export.setContent(data);
Message msg = BeaAccess.getMessageFromExport(export);
byte[] pdf=msg.toPdf("j-lawyer.org " + VersionUtils.getFullClientVersion());
String beaPdf=FileUtils.createTempFile(inputFile.getName() + ".pdf", pdf);
ArrayList<Attachment> attachments=msg.getAttachments();

byte[] pdf = msg.toPdf("j-lawyer.org " + VersionUtils.getFullClientVersion());
String beaPdf = FileUtils.createTempFile(inputFile.getName() + ".pdf", pdf);

ArrayList<Attachment> attachments = msg.getAttachments();
if (!attachments.isEmpty()) {
PDFMergerUtility merger = new PDFMergerUtility();

merger.addSource(new File(beaPdf));

for (Attachment att : attachments) {

try {
String attFile=FileUtils.createTempFile(att.getFileName(), att.getContent());
String attFilePdf=convertToPDF(attFile);
String attFile = FileUtils.createTempFile(att.getFileName(), att.getContent());
String attFilePdf = convertToPDF(attFile);

merger.addSource(new File(attFilePdf));

} catch (Throwable t) {
log.error("unable to convert attachment " + att.getFileName() + " of beA message " + url + " to PDF - skipping!", t);
}
Expand Down Expand Up @@ -879,19 +930,19 @@ public String convertTo(String url, String targetFileExtension) throws Exception

@Override
public String convertToPDF(String url) throws Exception {

if (url.toLowerCase().endsWith(".pdf")) {
return pdf2pdf(url);
}

if (url.toLowerCase().endsWith(".eml")) {
return eml2pdf(url);
}

if (url.toLowerCase().endsWith(".bea")) {
return bea2pdf(url);
}

ClientSettings set = ClientSettings.getInstance();
String wordProcessor = set.getConfiguration(ClientSettings.CONF_APPS_WORDPROCESSOR_KEY, ClientSettings.CONF_APPS_WORDPROCESSOR_VALUE_LO);
boolean wordProcessorMicrosoft = ClientSettings.CONF_APPS_WORDPROCESSOR_VALUE_MSO.equalsIgnoreCase(wordProcessor);
Expand Down Expand Up @@ -1045,7 +1096,7 @@ public String convertToPDF(String url) throws Exception {
if (url.toLowerCase().endsWith(".eml")) {
return eml2pdf(url);
}

if (url.toLowerCase().endsWith(".bea")) {
return bea2pdf(url);
}
Expand Down Expand Up @@ -1131,11 +1182,11 @@ public String convertToPDF(String url) throws Exception {
if (url.toLowerCase().endsWith(".eml")) {
return eml2pdf(url);
}

if (url.toLowerCase().endsWith(".bea")) {
return bea2pdf(url);
}

if (!this.supportsInputFormat(url)) {
throw new Exception("Format nicht unterstützt: " + new File(url).getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ public interface ArchiveFileServiceRemote {
List<InvoicePosition> getInvoicePositions(String invoiceId) throws Exception;

ArchiveFileDocumentsBean getInvoiceDocument(String invoiceId) throws Exception;

List<Invoice> getInvoicesForDocument(String docId) throws Exception;

InvoicePosition updateInvoicePosition(String invoiceId, InvoicePosition position) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5106,6 +5106,16 @@ public List<InvoicePosition> getInvoicePositions(String invoiceId) throws Except
}
}

@Override
@RolesAllowed({"readArchiveFileRole"})
public List<Invoice> getInvoicesForDocument(String docId) throws Exception {
// this call will also check if caller is allowed for this case
ArchiveFileDocumentsBean doc=this.getDocument(docId);

return this.invoicesFacade.findByInvoiceDocument(doc);

}

@Override
@RolesAllowed({"readArchiveFileRole"})
public ArchiveFileDocumentsBean getInvoiceDocument(String invoiceId) throws Exception {
Expand Down

0 comments on commit 8bec54c

Please sign in to comment.