diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java index 17a9eed..ff67a83 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/OPListExportAdapter.java @@ -94,7 +94,7 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { "missing Currency configuration - please check parameters"); } - // read the zahlungsavis options + // read the template options ItemCollection evalItemCollection = workflowService.evalWorkflowResult(event, "opliste", document, false); @@ -105,25 +105,10 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { List excelDefList = evalItemCollection.getItemValue("excel-export"); ItemCollection excelDefCollection = XMLParser.parseItemStructure(excelDefList.get(0)); - // // read the zahlungsavis options - // ItemCollection evalItemCollection = workflowService.evalWorkflowResult(event, - // "opliste", document, false); - // if (evalItemCollection == null || - // !evalItemCollection.hasItem("excel-export")) { - // throw new - // PluginException(AGLAnalyticExcelAdapterDebitor.class.getSimpleName(), - // CONFIG_ERROR, - // "missing opliste configuration in model event - please check model - // configuration"); - // } - - // List excelDefList = evalItemCollection.getItemValue("excel-export"); - // ItemCollection excelDefCollection = - // XMLParser.parseItemStructure(excelDefList.get(0)); - String textblock = excelDefCollection.getItemValueString("textblock"); String template = excelDefCollection.getItemValueString("template"); String targetName = excelDefCollection.getItemValueString("target-name"); + boolean filter = evalItemCollection.getItemValueBoolean("filter"); // adapt text.... targetName = workflowService.adaptText(targetName, document); @@ -140,6 +125,8 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { // first add the columns for the currency list addCurrencyColumns(doc, sheet, currencyList); + insertInvoiceRows(sheet, document, targetName, filter, currencyList); + // write back the updated excel file ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); doc.write(byteArrayOutputStream); @@ -149,7 +136,7 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { null); document.addFileData(fileDataNew); logger.finest("......new document added"); - } catch (IOException e) { + } catch (IOException | QueryException e) { throw new PluginException(OPListExportAdapter.class.getSimpleName(), InvoiceService.ERROR_CONFIG, "failed to update opliste: " + e.getMessage()); @@ -167,7 +154,9 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { // Finally process POI instructions processPOIUpdate(document, event, targetName); - } catch (PluginException e) { + } catch ( + + PluginException e) { throw new PluginException(OPListExportAdapter.class.getSimpleName(), InvoiceService.ERROR_CONFIG, "failed to update op-liste: " + e.getMessage()); @@ -252,8 +241,8 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { * @throws PluginException * @throws QueryException */ - private void insertInvoiceRows(ItemCollection document, String fileName, boolean filter, String currency1, - String currency2) + private void insertInvoiceRows(XSSFSheet sheet, ItemCollection document, String fileName, boolean filter, + List currencyList) throws PluginException, QueryException { double saldo1 = 0; @@ -264,179 +253,102 @@ public class OPListExportAdapter extends POIFindReplaceAdapter { logger.info("... space.ref=" + spaceID + " ...grouping invoices by spaceid...."); Map> invoiceMap = groupInvoicesBySpaceID(spaceID, filter); - FileData fileData = document.getFileData(fileName); + CellReference categoryRefCell = new CellReference("A11"); + XSSFRow referenceRowCategory = sheet.getRow(categoryRefCell.getRow()); + CellReference invoiceRefCell = new CellReference("A12"); + XSSFRow referenceRowInvoice = sheet.getRow(invoiceRefCell.getRow()); - // load XSSFWorkbook - XSSFWorkbook doc = null; - try (InputStream imputStream = new ByteArrayInputStream(fileData.getContent())) { - doc = new XSSFWorkbook(imputStream); - // NOTE: we only take the first sheet ! - XSSFSheet sheet = doc.getSheetAt(0); - // Währungsüberschriften setzten - sheet.getRow(10).getCell(6).setCellValue(currency1); - sheet.getRow(10).getCell(8).setCellValue(currency2); + int referenceRowPos = 11; + int rowPos = 11; + int lastRow = 4999; + logger.finest("Last rownum=" + lastRow); - CellReference categoryRefCell = new CellReference("A12"); - XSSFRow referenceRowCategory = sheet.getRow(categoryRefCell.getRow()); - CellReference invoiceRefCell = new CellReference("A13"); - XSSFRow referenceRowInvoice = sheet.getRow(invoiceRefCell.getRow()); - - int referenceRowPos = 12; - int rowPos = 12; - int lastRow = 2999; - logger.finest("Last rownum=" + lastRow); - - int totalRowCount = invoiceMap.size() * 2; - for (List list : invoiceMap.values()) { - totalRowCount = totalRowCount + list.size(); - } - if (totalRowCount == 0) { - logger.warning("No Invoices found - skip update workbook!"); - return; - } - sheet.shiftRows(rowPos, lastRow, totalRowCount, true, true); - for (Map.Entry> entry : invoiceMap.entrySet()) { - String dbtrNumber = entry.getKey(); - List invoices = entry.getValue(); - - // erzeuge eine Zwischenüberschrift für den Debitor.... - XSSFRow categoryRow = sheet.createRow(rowPos); - categoryRow.copyRowFrom(referenceRowCategory, new CellCopyPolicy()); - // insert values - categoryRow.getCell(0).setCellValue(dbtrNumber); - String debitorName = invoices.get(0).getItemValueString("dbtr.name"); - categoryRow.getCell(1).setCellValue(debitorName); - - // calculate summ of all invoices - double catSumEUR = 0; - double catSumUSD = 0; - for (ItemCollection invoice : invoices) { - if (currency1.equals(invoice.getItemValueString("invoice.currency"))) { - catSumEUR = catSumEUR + invoice.getItemValueDouble("invoice.saldo"); - } else { - catSumUSD = catSumUSD + invoice.getItemValueDouble("invoice.saldo"); - } - } - categoryRow.getCell(6).setCellValue(catSumEUR); - categoryRow.getCell(8).setCellValue(catSumUSD); - - rowPos++; - - // jetzt füge alle Rechnungen an. - for (ItemCollection invoice : invoices) { - logger.fine("......add invoice " + invoice.getUniqueID()); - - // now create a new line.. - XSSFRow row = sheet.createRow(rowPos); - row.copyRowFrom(referenceRowInvoice, new CellCopyPolicy()); - // insert values - if (invoice.getItemValueBoolean("invoice.protest")) { - row.getCell(2).setCellValue(invoice.getItemValueString("invoice.number") + " (R)"); - } else { - row.getCell(2).setCellValue(invoice.getItemValueString("invoice.number")); - } - row.getCell(3).setCellValue(invoice.getItemValueString("invoice.text")); - row.getCell(4).setCellValue(invoice.getItemValueDate("invoice.date")); - row.getCell(5).setCellValue(invoice.getItemValueDate("invoice.duedate")); - double total = invoice.getItemValueDouble("invoice.saldo"); - String currency = invoice.getItemValueString("invoice.currency"); - if (currency1.equals(currency)) { - // EUR - row.getCell(6).setCellValue(total); - row.getCell(7).setCellValue(currency); - saldo1 = InvoiceUtil.round(saldo1 + total); - } else { - // USD - row.getCell(8).setCellValue(total); - row.getCell(9).setCellValue(currency); - saldo2 = InvoiceUtil.round(saldo2 + total); - } - row.getCell(10).setCellValue(invoice.getItemValueString("$workflowstatus")); - - rowPos++; - } - - // Leerzeile - categoryRow = sheet.createRow(rowPos); - rowPos++; - } - // delete reference row 12 - sheet.shiftRows(referenceRowPos, lastRow + totalRowCount, -1, true, true); - - // finally update the total formula... - XSSFCell cTotal1 = getCellByRef(doc, sheet, "TOTAL1"); - XSSFCell cTotal2 = getCellByRef(doc, sheet, "TOTAL2"); - cTotal1.setCellValue(saldo1); - cTotal2.setCellValue(saldo2); - // Set währung - XSSFCell cTotalCurrency1 = getCellByRef(doc, sheet, "TOTAL_CURRENCY1"); - XSSFCell cTotalCurrency2 = getCellByRef(doc, sheet, "TOTAL_CURRENCY2"); - cTotalCurrency1.setCellValue(currency1); - cTotalCurrency2.setCellValue(currency2); - - // write back the file - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - doc.write(byteArrayOutputStream); - - byte[] newContent = byteArrayOutputStream.toByteArray(); - FileData fileDataNew = new FileData(fileData.getName(), newContent, fileData.getContentType(), null); - // update the fileData - document.addFileData(fileDataNew); - document.setItemValue("space.saldo.currency1", saldo1); - document.setItemValue("space.saldo.currency2", saldo2); - logger.finest("......new document added"); - - } catch (IOException e) { - throw new PluginException(OPListExportAdapter.class.getSimpleName(), InvoiceService.ERROR_CONFIG, - "failed to update opliste: " + e.getMessage()); - } finally { - if (doc != null) { - try { - doc.close(); - } catch (IOException e) { - logger.severe("Failed to close workbook: " + e.getMessage()); - e.printStackTrace(); - } - } + int totalRowCount = invoiceMap.size() * 2; + for (List list : invoiceMap.values()) { + totalRowCount = totalRowCount + list.size(); } + if (totalRowCount == 0) { + logger.warning("No Invoices found - skip update workbook!"); + return; + } + sheet.shiftRows(rowPos, lastRow, totalRowCount, true, true); + // Build a category for each debitor... + for (Map.Entry> entry : invoiceMap.entrySet()) { + String dbtrNumber = entry.getKey(); + List invoices = entry.getValue(); + + // erzeuge eine Zwischenüberschrift für den Debitor.... + XSSFRow categoryRow = sheet.createRow(rowPos); + categoryRow.copyRowFrom(referenceRowCategory, new CellCopyPolicy()); + // insert values + categoryRow.getCell(0).setCellValue(dbtrNumber); + String debitorName = invoices.get(0).getItemValueString("dbtr.name"); + categoryRow.getCell(1).setCellValue(debitorName); + + categoryRow.getCell(6).setCellValue(42); + categoryRow.getCell(8).setCellValue(42); + + rowPos++; + + // Init Saldo Map + Map currencySum = new HashMap(); + for (String cur : currencyList) { + Double saldo = Double.valueOf(0); + currencySum.put(cur, saldo); + } + + // jetzt füge alle Rechnungen an. + for (ItemCollection invoice : invoices) { + logger.fine("......add invoice " + invoice.getUniqueID()); + + // now create a new line.. + XSSFRow row = sheet.createRow(rowPos); + row.copyRowFrom(referenceRowInvoice, new CellCopyPolicy()); + // insert values + if (invoice.getItemValueBoolean("invoice.protest")) { + row.getCell(1).setCellValue(invoice.getItemValueString("invoice.number") + " (R)"); + } else { + row.getCell(1).setCellValue(invoice.getItemValueString("invoice.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")); + double total = invoice.getItemValueDouble("invoice.saldo"); + + // calculate summary + String currency = invoice.getItemValueString("invoice.currency"); + Double saldo = currencySum.get(currency); + saldo = InvoiceUtil.round(saldo + total); + currencySum.put(currency, saldo); + + // Saldo in richtige Spalte setzen... + int currencyColIndex = currencyList.indexOf(currency); + row.getCell(5 + currencyColIndex).setCellValue(total); + // if (currency1.equals(currency)) { + // // EUR + // row.getCell(6).setCellValue(total); + // row.getCell(7).setCellValue(currency); + // saldo1 = InvoiceUtil.round(saldo1 + total); + // } else { + // // USD + // row.getCell(8).setCellValue(total); + // row.getCell(9).setCellValue(currency); + // saldo2 = InvoiceUtil.round(saldo2 + total); + // } + row.getCell(5 + currencyList.size()).setCellValue(invoice.getItemValueString("$workflowstatus")); + + rowPos++; + } + + // Leerzeile + categoryRow = sheet.createRow(rowPos); + rowPos++; + } + // delete reference row 11 + sheet.shiftRows(referenceRowPos, lastRow + totalRowCount, -1, true, true); + } - /** - * This method loads a text-block for a specified ref and appends the named - * fileData object of this document. - * - * @param document - * @throws PluginException - */ - // private void appendExcelTemplate(ItemCollection document, String - // textblockRef, String template, String targetName) - // throws PluginException { - - // if ((template == null || template.isEmpty()) || (textblockRef == null || - // textblockRef.isEmpty())) { - // throw new PluginException(OPListExportAdapter.class.getSimpleName(), - // InvoiceService.ERROR_CONFIG, - // "invalid op-list configuration in model event - textblock/template reference - // not defined!"); - // } - - // // load the text block - // FileData fileData = invoiceService.loadTextBlockFileData(textblockRef, - // template); - - // // do we found the document? - // if (fileData == null) { - // throw new PluginException(OPListExportAdapter.class.getSimpleName(), - // InvoiceService.ERROR_CONFIG, - // "invalid op-list configuration in model event - template=" + template + " not - // found!"); - // } - // fileData.setName(targetName); - // // append document - // logger.info("...append new op-list Template: " + targetName); - - // document.addFileData(fileData); - // } /** * This method loads a text-block for a specified ref and appends the named diff --git a/templates/op-liste_template.xlsx b/templates/op-liste_template.xlsx index 20eafdf..cc83a1b 100644 Binary files a/templates/op-liste_template.xlsx and b/templates/op-liste_template.xlsx differ