update multi currency
This commit is contained in:
parent
b7011a5cf8
commit
89865fcde1
2 changed files with 77 additions and 81 deletions
|
|
@ -11,6 +11,7 @@ import java.util.logging.Logger;
|
|||
import org.apache.poi.ss.usermodel.CellCopyPolicy;
|
||||
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
||||
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;
|
||||
|
|
@ -124,9 +125,9 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
|
|||
XSSFSheet sheet = doc.getSheetAt(0);
|
||||
|
||||
// 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
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
|
|
@ -138,7 +139,7 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
|
|||
document.addFileData(fileDataNew);
|
||||
logger.finest("......new document added");
|
||||
|
||||
} catch (IOException e) {
|
||||
} catch (IOException | QueryException e) {
|
||||
throw new PluginException(AGLAnalyticExcelAdapter.class.getSimpleName(),
|
||||
OPListExportService.ERROR_CONFIG,
|
||||
"failed to update opliste: " + e.getMessage());
|
||||
|
|
@ -189,19 +190,33 @@ public class AGLAnalyticExcelAdapterDebitor extends POIFindReplaceAdapter {
|
|||
* @param sheet
|
||||
* @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) {
|
||||
// no extra currencies defined!
|
||||
return;
|
||||
}
|
||||
int newColumns = (currencyList.size() - 1) * 2;
|
||||
|
||||
// zusaetzliche Zellen einfuegen...
|
||||
int newColumns = (currencyList.size() - 1) * 2;
|
||||
for (int i = 0; i < newColumns; i++) {
|
||||
logger.info(".. add currency column...");
|
||||
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 QueryException
|
||||
*/
|
||||
private void insertInvoiceRows(ItemCollection document, String fileName)
|
||||
private void insertInvoiceRows(XSSFSheet sheet, ItemCollection document, List<String> currencyList)
|
||||
throws PluginException, QueryException {
|
||||
|
||||
// load dummy rechnungen
|
||||
List<ItemCollection> invoices = loadInvoices(getDbtNr(document));
|
||||
|
||||
FileData fileData = document.getFileData(fileName);
|
||||
|
||||
// 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.
|
||||
int totalRowCount = invoices.size();
|
||||
sheet.shiftRows(referenceRowPos, lastRow, totalRowCount, true, true);
|
||||
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"));
|
||||
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);
|
||||
|
||||
// Waehrung?
|
||||
if (invoice.getItemValueDouble("invoice.rate") == 0) {
|
||||
row.getCell(6, MissingCellPolicy.CREATE_NULL_AS_BLANK)
|
||||
.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"));
|
||||
// jetzt füge alle Rechnungen an.
|
||||
int totalRowCount = invoices.size();
|
||||
POIUtil.insertRows(sheet, "A11", totalRowCount);
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
logger.finest("......new document added");
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new PluginException(AGLAnalyticExcelAdapter.class.getSimpleName(),
|
||||
OPListExportService.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();
|
||||
/**
|
||||
* Findet die Total Spalte für eine Währung
|
||||
*
|
||||
* @param currency
|
||||
* @return
|
||||
*/
|
||||
private int findTotalPos(String currency, List<String> currencyList) {
|
||||
int result = 6;
|
||||
if (currency != null) {
|
||||
for (String cur : currencyList) {
|
||||
if (currency.equals(cur)) {
|
||||
return result;
|
||||
}
|
||||
result = result + 2;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in a new issue