Skip to content
Snippets Groups Projects
Commit 977768ff authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#60

-implemented feature to split report in two tables (income/payments)
-implemented totals
-FileChooser remembers last choosen save path
parent af5145b4
No related branches found
No related tags found
2 merge requests!142merge v1_3_0 into master,!121Merge Reports into v1_3_0
package de.deadlocker8.budgetmaster.logic.report;
public enum AmountType
{
INCOME, PAYMENT, BOTH
}
...@@ -46,30 +46,28 @@ public class ReportGenerator ...@@ -46,30 +46,28 @@ public class ReportGenerator
this.date = date; this.date = date;
} }
public void generate() throws FileNotFoundException, DocumentException private Chapter generateHeader()
{ {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(savePath));
writer.setPageEvent(new HeaderFooterPageEvent());
document.open();
//header
Font chapterFont = new Font(FontFamily.HELVETICA, 16, Font.BOLDITALIC); Font chapterFont = new Font(FontFamily.HELVETICA, 16, Font.BOLDITALIC);
Font paragraphFont = new Font(FontFamily.HELVETICA, 12, Font.NORMAL); Font paragraphFont = new Font(FontFamily.HELVETICA, 12, Font.NORMAL);
Chunk chunk = new Chunk("Monatsbericht - " + date.toString("MMMM yyyy"), chapterFont); Chunk chunk = new Chunk("Monatsbericht - " + date.toString("MMMM yyyy"), chapterFont);
Chapter chapter = new Chapter(new Paragraph(chunk), 1); Chapter chapter = new Chapter(new Paragraph(chunk), 1);
chapter.setNumberDepth(0); chapter.setNumberDepth(0);
chapter.add(Chunk.NEWLINE); chapter.add(Chunk.NEWLINE);
chapter.add(new Paragraph("Buchungen", paragraphFont)); chapter.add(new Paragraph("Buchungsübersicht", paragraphFont));
document.add(chapter); return chapter;
document.add(Chunk.NEWLINE); }
//table private PdfPTable generateTable(int tableWidth, AmountType amountType)
{
int numberOfColumns = columnOrder.getColumns().size(); int numberOfColumns = columnOrder.getColumns().size();
int totalIncome = 0;
int totalPayment = 0;
if(numberOfColumns > 0) if(numberOfColumns > 0)
{ {
PdfPTable table = new PdfPTable(numberOfColumns); PdfPTable table = new PdfPTable(numberOfColumns);
table.setWidthPercentage(100); table.setWidthPercentage(tableWidth);
Font font = new Font(FontFamily.HELVETICA, 8, Font.NORMAL, GrayColor.BLACK); Font font = new Font(FontFamily.HELVETICA, 8, Font.NORMAL, GrayColor.BLACK);
for(ColumnType column : columnOrder.getColumns()) for(ColumnType column : columnOrder.getColumns())
...@@ -81,9 +79,25 @@ public class ReportGenerator ...@@ -81,9 +79,25 @@ public class ReportGenerator
table.addCell(cell); table.addCell(cell);
} }
for(ReportItem currentItem : reportItems) for(ReportItem currentItem : reportItems)
{ {
if(currentItem.getAmount() > 0)
{
totalIncome += currentItem.getAmount();
if(amountType == AmountType.PAYMENT)
{
continue;
}
}
else
{
totalPayment += currentItem.getAmount();
if(amountType == AmountType.INCOME)
{
continue;
}
}
for(ColumnType column : columnOrder.getColumns()) for(ColumnType column : columnOrder.getColumns())
{ {
PdfPCell cell = new PdfPCell(new Phrase(getProperty(currentItem, column), font)); PdfPCell cell = new PdfPCell(new Phrase(getProperty(currentItem, column), font));
...@@ -93,9 +107,76 @@ public class ReportGenerator ...@@ -93,9 +107,76 @@ public class ReportGenerator
} }
} }
PdfPCell cellTotal;
String total = "";
switch(amountType)
{
case BOTH:
String totalIncomeString = String.valueOf(Helpers.NUMBER_FORMAT.format(totalIncome / 100.0).replace(".", ",")) + " " + currency;
String totalPaymentString = String.valueOf(Helpers.NUMBER_FORMAT.format(totalPayment / 100.0).replace(".", ",")) + " " + currency;
total = "Einnahmen: " + totalIncomeString + " / Ausgaben: " + totalPaymentString;
break;
case INCOME:
total = "Summe: " + String.valueOf(Helpers.NUMBER_FORMAT.format(totalIncome / 100.0).replace(".", ",")) + " " + currency;
break;
case PAYMENT:
total = "Summe: " + String.valueOf(Helpers.NUMBER_FORMAT.format(totalPayment / 100.0).replace(".", ",")) + " " + currency;
break;
default:
break;
}
cellTotal = new PdfPCell(new Phrase(total, font));
cellTotal.setBackgroundColor(new BaseColor(Color.WHITE));
cellTotal.setColspan(numberOfColumns);
cellTotal.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cellTotal);
return table;
}
return null;
}
public void generate() throws FileNotFoundException, DocumentException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(savePath));
writer.setPageEvent(new HeaderFooterPageEvent());
document.open();
document.setMargins(50, 45, 50, 70);
document.add(generateHeader());
document.add(Chunk.NEWLINE);
if(splitTable)
{
Font paragraphFont = new Font(FontFamily.HELVETICA, 12, Font.NORMAL);
document.add(new Paragraph("Einnahmen", paragraphFont));
document.add(Chunk.NEWLINE);
PdfPTable table = generateTable(100, AmountType.INCOME);
if(table != null)
{
document.add(table);
}
document.add(Chunk.NEWLINE);
document.add(new Paragraph("Ausgaben", paragraphFont));
document.add(Chunk.NEWLINE);
table = generateTable(100, AmountType.PAYMENT);
if(table != null)
{
document.add(table); document.add(table);
} }
}
else
{
PdfPTable table = generateTable(100, AmountType.BOTH);
if(table != null)
{
document.add(table);
}
}
document.close(); document.close();
} }
......
...@@ -67,6 +67,7 @@ public class ReportController implements Refreshable ...@@ -67,6 +67,7 @@ public class ReportController implements Refreshable
private Controller controller; private Controller controller;
private ColumnFilter columnFilter; private ColumnFilter columnFilter;
private File reportPath;
public void init(Controller controller) public void init(Controller controller)
{ {
...@@ -421,10 +422,16 @@ public class ReportController implements Refreshable ...@@ -421,10 +422,16 @@ public class ReportController implements Refreshable
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Bericht speichern"); fileChooser.setTitle("Bericht speichern");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF (*.pdf)", "*.pdf"); FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF (*.pdf)", "*.pdf");
if(reportPath != null)
{
fileChooser.setInitialDirectory(reportPath.getParentFile());
fileChooser.setInitialFileName(reportPath.getName());
}
fileChooser.getExtensionFilters().add(extFilter); fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(controller.getStage()); File file = fileChooser.showSaveDialog(controller.getStage());
if(file != null) if(file != null)
{ {
reportPath = file;
ReportGenerator reportGenerator = new ReportGenerator(new ArrayList<ReportItem>(tableView.getItems()), ReportGenerator reportGenerator = new ReportGenerator(new ArrayList<ReportItem>(tableView.getItems()),
columnOrder, columnOrder,
checkBoxSplitTable.isSelected(), checkBoxSplitTable.isSelected(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment