update multi currency

This commit is contained in:
Ralph Soika 2024-11-18 08:16:13 +01:00
parent b7011a5cf8
commit 89865fcde1
2 changed files with 77 additions and 81 deletions

View file

@ -11,6 +11,7 @@ import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.util.CellReference; 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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -124,9 +125,9 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
XSSFSheet sheet = doc.getSheetAt(0); XSSFSheet sheet = doc.getSheetAt(0);
// first add the columns for the currency list // first add the columns for the currency list
addCurrencyColumns(sheet, currencyList); addCurrencyColumns(doc, sheet, currencyList);
// insertInvoiceRows(document, targetName); insertInvoiceRows(sheet, document, currencyList);
// write back the updated excel file // write back the updated excel file
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@ -138,7 +139,7 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
document.addFileData(fileDataNew); document.addFileData(fileDataNew);
logger.finest("......new document added"); logger.finest("......new document added");
} catch (IOException e) { } catch (IOException | QueryException e) {
throw new PluginException(AGLAnalyticExcelAdapter.class.getSimpleName(), throw new PluginException(AGLAnalyticExcelAdapter.class.getSimpleName(),
OPListExportService.ERROR_CONFIG, OPListExportService.ERROR_CONFIG,
"failed to update opliste: " + e.getMessage()); "failed to update opliste: " + e.getMessage());
@ -189,19 +190,33 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
* @param sheet * @param sheet
* @param currencyList * @param currencyList
*/ */
private void addCurrencyColumns(XSSFSheet sheet, List<String> currencyList) { private void addCurrencyColumns(XSSFWorkbook doc, XSSFSheet sheet, List<String> currencyList) {
if (currencyList == null || currencyList.size() <= 1) { if (currencyList == null || currencyList.size() <= 1) {
// no extra currencies defined! // no extra currencies defined!
return; return;
} }
int newColumns = (currencyList.size() - 1) * 2;
// zusaetzliche Zellen einfuegen...
int newColumns = (currencyList.size() - 1) * 2;
for (int i = 0; i < newColumns; i++) { for (int i = 0; i < newColumns; i++) {
logger.info(".. add currency column..."); logger.info(".. add currency column...");
POIUtil.insertColumn(sheet, 6, 8 + i); POIUtil.insertColumn(sheet, 6, 8 + i);
// POIUtil.insertColumn(sheet, 6, 8 + i+1);
} }
// Beschriftungen eintragen
int col = 6;
XSSFCell cell = null;
XSSFRow labelRow = sheet.getRow(9);
for (String currency : currencyList) {
cell = labelRow.getCell(col);
cell.setCellValue("Total " + currency);
col++;
cell = labelRow.getCell(col);
cell.setCellValue("Saldo " + currency);
col++;
}
} }
/** /**
@ -229,97 +244,78 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
* @throws PluginException * @throws PluginException
* @throws QueryException * @throws QueryException
*/ */
private void insertInvoiceRows(ItemCollection document, String fileName) private void insertInvoiceRows(XSSFSheet sheet, ItemCollection document, List<String> currencyList)
throws PluginException, QueryException { throws PluginException, QueryException {
// load dummy rechnungen // load dummy rechnungen
List<ItemCollection> invoices = loadInvoices(getDbtNr(document)); List<ItemCollection> invoices = loadInvoices(getDbtNr(document));
FileData fileData = document.getFileData(fileName);
// load XSSFWorkbook // 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);
CellReference invoiceRefCell = new CellReference("A11");
XSSFRow referenceRowInvoice = sheet.getRow(invoiceRefCell.getRow());
int referenceRowPos = 10;
int rowPos = referenceRowPos;
int lastRow = 2999;
logger.finest("Last rownum=" + lastRow);
// jetzt füge alle Rechnungen an. CellReference invoiceRefCell = new CellReference("A11");
int totalRowCount = invoices.size(); XSSFRow referenceRowInvoice = sheet.getRow(invoiceRefCell.getRow());
sheet.shiftRows(referenceRowPos, lastRow, totalRowCount, true, true); int referenceRowPos = 10;
for (ItemCollection invoice : invoices) { int rowPos = referenceRowPos;
logger.fine("......add invoice " + invoice.getUniqueID()); int lastRow = 2999;
// now create a new line.. logger.finest("Last rownum=" + lastRow);
XSSFRow row = sheet.createRow(rowPos);
row.copyRowFrom(referenceRowInvoice, new CellCopyPolicy());
// insert values
row.getCell(0)
.setCellValue(invoice.getItemValueDate("$created"));
row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("invoice.number"));
row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDate("invoice.date"));
row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDate("invoice.duedate"));
row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("space.name"));
row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("invoice.text"));
// Waehrung? // jetzt füge alle Rechnungen an.
if (invoice.getItemValueDouble("invoice.rate") == 0) { int totalRowCount = invoices.size();
row.getCell(6, MissingCellPolicy.CREATE_NULL_AS_BLANK) POIUtil.insertRows(sheet, "A11", totalRowCount);
.setCellValue(invoice.getItemValueDouble("invoice.total"));
row.getCell(7, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDouble("invoice.saldo"));
} else {
row.getCell(8, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDouble("invoice.total"));
row.getCell(9, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDouble("invoice.saldo"));
}
row.getCell(10, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("$workflowstatus"));
rowPos++; 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
row.getCell(0)
.setCellValue(invoice.getItemValueDate("$created"));
row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("invoice.number"));
row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDate("invoice.date"));
row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDate("invoice.duedate"));
row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("space.name"));
row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("invoice.text"));
// Waehrung?
String currency = invoice.getItemValueString("invoice.currency");
int totalCol = findTotalPos(currency, currencyList);
row.getCell(totalCol, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDouble("invoice.total"));
row.getCell(totalCol + 1, MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueDouble("invoice.saldo"));
// Status
row.getCell(6 + (currencyList.size() * 2), MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellValue(invoice.getItemValueString("$workflowstatus"));
// Leerzeile
// categoryRow = sheet.createRow(rowPos);
rowPos++; rowPos++;
}
// write back the file }
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
doc.write(byteArrayOutputStream);
byte[] newContent = byteArrayOutputStream.toByteArray(); /**
FileData fileDataNew = new FileData(fileData.getName(), newContent, fileData.getContentType(), * Findet die Total Spalte für eine Währung
null); *
// update the fileData * @param currency
document.addFileData(fileDataNew); * @return
*/
logger.finest("......new document added"); private int findTotalPos(String currency, List<String> currencyList) {
int result = 6;
} catch (IOException e) { if (currency != null) {
throw new PluginException(AGLAnalyticExcelAdapter.class.getSimpleName(), for (String cur : currencyList) {
OPListExportService.ERROR_CONFIG, if (currency.equals(cur)) {
"failed to update opliste: " + e.getMessage()); return result;
} finally {
if (doc != null) {
try {
doc.close();
} catch (IOException e) {
logger.severe("Failed to close workbook: " + e.getMessage());
e.printStackTrace();
} }
result = result + 2;
} }
} }
return result;
} }
/** /**

Binary file not shown.