diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/mahnlauf/ExcelExportController.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/mahnlauf/ExcelExportController.java index 4ebd982..c4d887e 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/mahnlauf/ExcelExportController.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/mahnlauf/ExcelExportController.java @@ -1,6 +1,9 @@ package com.alexanderlogistics.mahnlauf; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.text.SimpleDateFormat; @@ -9,8 +12,16 @@ import java.time.ZoneId; import java.util.Calendar; import java.util.Collection; import java.util.Date; +import java.util.List; import java.util.logging.Logger; +import org.apache.poi.ss.usermodel.CellCopyPolicy; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.imixs.archive.core.SnapshotService; import org.imixs.workflow.FileData; import org.imixs.workflow.ItemCollection; @@ -21,6 +32,8 @@ import org.imixs.workflow.faces.data.DocumentController; import org.imixs.workflow.faces.data.WorkflowController; import org.imixs.workflow.office.forms.CustomFormItem; +import com.alexanderlogistics.ZahlungsavisExportAdapter; + import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ConversationScoped; import jakarta.faces.context.ExternalContext; @@ -47,6 +60,7 @@ public class ExcelExportController implements Serializable { public static final String ERROR_CONFIG = "CONFIG_ERROR"; final String TYPE_TEXTBLOCK = "textblock"; + public static final int MAX_ROWS = 3000; @Inject protected DocumentService documentService; @@ -86,10 +100,8 @@ public class ExcelExportController implements Serializable { public String export() throws PluginException { logger.info("start export...."); - Date start = filter.getItemValueDate("start"); Date stop = filter.getItemValueDate("stop"); - logger.info("...daterange=" + start + " - " + stop); // serach date range? @@ -123,11 +135,13 @@ public class ExcelExportController implements Serializable { if (!filter.getItemValueString("space.name").isEmpty()) { query += " AND (space.name:" + filter.getItemValueString("space.name") + ")"; } - if (workflowController.getWorkitem() != null && !workflowController.getWorkitem().getItemValueString("dbtr.number").isEmpty()) { query += " AND (dbtr.number:" + workflowController.getWorkitem().getItemValueString("dbtr.number") + ")"; } + if (!filter.getItemValueString("invoice.text").isEmpty()) { + query += " AND (invoice.positions:" + filter.getItemValueString("invoice.text") + ")"; + } // load the text block String template = "ausgangsrechnungen-export_template.xlsx"; @@ -139,13 +153,17 @@ public class ExcelExportController implements Serializable { "Missing Excel Export Template - template=" + template + " not found!"); } String targetName = "ausgangsrechnungen-export_" + dateformat.format(new Date()) + ".xlsx"; - fileData.setName(targetName); - try { + List invoices = documentService.find(query, MAX_ROWS, 0); + if (invoices.size() > 0) { + insertInvoiceRows(invoices, fileData); + } + fileData.setName(targetName); + // See: // https://stackoverflow.com/questions/9391838/how-to-provide-a-file-download-from-a-jsf-backing-bean download(fileData); - } catch (IOException e) { + } catch (IOException | QueryException e) { throw new PluginException(InkassoExportAdapter.class.getSimpleName(), ERROR_CONFIG, "Failed to generate Excel Export: " + e.getMessage()); } @@ -229,10 +247,6 @@ public class ExcelExportController implements Serializable { // debtr item dbtrItem = new CustomFormItem("dbtr.number", "text", "", false, false, false, "", null, false, 0); - // public CustomFormItem(String name, String type, String label, boolean - // required, boolean readonly, boolean disabled, - // String options, - // String path, boolean hide, int span) { } public CustomFormItem getItem() { @@ -240,4 +254,91 @@ public class ExcelExportController implements Serializable { return dbtrItem; } + /** + * This helper method inserts a row for each invoice + * + * @throws PluginException + */ + private void insertInvoiceRows(List invoices, FileData fileData) throws PluginException { + // load XSSFWorkbook + try (InputStream imputStream = new ByteArrayInputStream(fileData.getContent())) { + XSSFWorkbook doc = new XSSFWorkbook(imputStream); + // NOTE: we only take the first sheet ! + XSSFSheet sheet = doc.getSheetAt(0); + + XSSFCell standCell = getCellByRef(doc, sheet, "F6"); + standCell.setCellValue(new Date()); + + CellReference cr = new CellReference("A11"); + XSSFRow referenceRow = sheet.getRow(cr.getRow()); + int referenceRowPos = referenceRow.getRowNum() + 1; + int rowPos = referenceRowPos; + // int lastRow = sheet.getLastRowNum(); + int lastRow = 999; + logger.finest("Last rownum=" + lastRow); + sheet.shiftRows(rowPos, lastRow, invoices.size(), true, true); + + for (ItemCollection invoice : invoices) { + logger.finest("......copy row..."); + + // now create a new line.. + XSSFRow row = sheet.createRow(rowPos); + row.copyRowFrom(referenceRow, new CellCopyPolicy()); + // insert values + row.getCell(0).setCellValue(invoice.getItemValueString("invoice.number")); + row.getCell(1).setCellValue(invoice.getItemValueString("dbtr.number")); + row.getCell(2).setCellValue(invoice.getItemValueString("invoice.text")); + + row.getCell(3).setCellValue(invoice.getItemValueDate("invoice.date")); + row.getCell(4).setCellValue(invoice.getItemValueDate("invoice.duedate")); + + row.getCell(5).setCellValue(invoice.getItemValueDouble("invoice.total")); + + rowPos++; + } + // delete reference row + sheet.shiftRows(referenceRowPos, lastRow + invoices.size(), -1, true, true); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + // write back the file + doc.write(byteArrayOutputStream); + doc.close(); + byte[] newContent = byteArrayOutputStream.toByteArray(); + fileData.setContent(newContent); + + } catch (IOException e) { + throw new PluginException(ZahlungsavisExportAdapter.class.getSimpleName(), ERROR_CONFIG, + "failed to update excel export: " + e.getMessage()); + } + } + + /** + * Returns a Cell by name or an optional absolute cell postion + *

+ * Examples for refs are 'A1', 'AB3', 'MyCell' where 'MyCell' is a named cell. + *

+ * + */ + public static XSSFCell getCellByRef(XSSFWorkbook doc, XSSFSheet sheet, String cellReference) { + XSSFCell cell = null; + + // first we test if the cellName is a named cell + Name aNamedCell = doc.getName(cellReference); + if (aNamedCell != null) { + // yes its a named cell so we need to get the referrer Formula + logger.finest("...resolving named cell = " + aNamedCell.getNameName()); + cellReference = aNamedCell.getRefersToFormula(); + // now we can find the cell by its ref + } + + CellReference cr = new CellReference(cellReference); + XSSFRow row = sheet.getRow(cr.getRow()); + if (row == null) { + logger.severe("Unable to resolve cell ref '" + cellReference + "'!"); + return null; + } + cell = row.getCell(cr.getCol()); + return cell; + } + } diff --git a/office-alexander-logistics-app/src/main/webapp/pages/admin/excel_export_rechnungsausgang.xhtml b/office-alexander-logistics-app/src/main/webapp/pages/admin/excel_export_rechnungsausgang.xhtml index 7814577..64f9bc8 100644 --- a/office-alexander-logistics-app/src/main/webapp/pages/admin/excel_export_rechnungsausgang.xhtml +++ b/office-alexander-logistics-app/src/main/webapp/pages/admin/excel_export_rechnungsausgang.xhtml @@ -7,40 +7,30 @@ - - - - - + -

Excle Export Ausgangsrechnungen

- - -
- -

Der folgende Report exportiert Ausgangsrechnungen, in eine Excel Datei. Der Export ist auf maximal 3000 Einträge pro Tabelle beschränkt

@@ -56,12 +46,8 @@ - - - -
Bis:* @@ -74,9 +60,6 @@
- - -
Abteilung: @@ -88,7 +71,6 @@
-
Debitor: @@ -101,48 +83,27 @@
-
Positionsnummer:
- + value="#{excelExportController.filter.item['invoice.text']}">
- -
- x - -
- - - - - -
-
- - - - - - - \ No newline at end of file diff --git a/templates/ausgangsrechnungen-export_template.xlsx b/templates/ausgangsrechnungen-export_template.xlsx index 1d2475b..82a50e0 100644 Binary files a/templates/ausgangsrechnungen-export_template.xlsx and b/templates/ausgangsrechnungen-export_template.xlsx differ