Skip to content

Commit

Permalink
CSV Export für Kontensaldo (#318)
Browse files Browse the repository at this point in the history
* CSV Export für Kontensaldo View

* Großschrift
  • Loading branch information
JohannMaierhofer authored Sep 10, 2024
1 parent 745cc5c commit 8202d5d
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/de/jost_net/JVerein/gui/control/KontensaldoControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import de.jost_net.JVerein.Einstellungen;
import de.jost_net.JVerein.gui.parts.KontensaldoList;
import de.jost_net.JVerein.io.KontenSaldoCSV;
import de.jost_net.JVerein.io.KontenSaldoPDF;
import de.jost_net.JVerein.io.SaldoZeile;
import de.jost_net.JVerein.util.Dateiname;
Expand All @@ -46,6 +47,10 @@ public class KontensaldoControl extends SaldoControl
{

private KontensaldoList saldoList;

final static String AuswertungPDF = "PDF";

final static String AuswertungCSV = "CSV";

public KontensaldoControl(AbstractView view)
{
Expand All @@ -60,12 +65,26 @@ public Button getStartAuswertungButton()
@Override
public void handleAction(Object context) throws ApplicationException
{
starteAuswertung();
starteAuswertung(AuswertungPDF);
}
}, null, false, "file-pdf.png");
// button
return b;
}

public Button getStartAuswertungCSVButton()
{
Button b = new Button("CSV", new Action()
{
@Override
public void handleAction(Object context) throws ApplicationException
{
starteAuswertung(AuswertungCSV);
}
}, null, false, "xsd.png");
// button
return b;
}

public void handleStore()
{
Expand Down Expand Up @@ -107,7 +126,7 @@ public Part getSaldoList() throws ApplicationException
return saldoList.getSaldoList();
}

private void starteAuswertung() throws ApplicationException
private void starteAuswertung(String type) throws ApplicationException
{
try
{
Expand All @@ -125,7 +144,7 @@ private void starteAuswertung() throws ApplicationException
fd.setFilterPath(path);
}
fd.setFileName(new Dateiname("kontensaldo", "",
Einstellungen.getEinstellung().getDateinamenmuster(), "PDF").get());
Einstellungen.getEinstellung().getDateinamenmuster(), type).get());

final String s = fd.open();

Expand All @@ -136,8 +155,8 @@ private void starteAuswertung() throws ApplicationException

final File file = new File(s);
settings.setAttribute("lastdir", file.getParent());
auswertungSaldoPDF(zeile, file, getDatumvon().getDate(),
getDatumbis().getDate());
auswertungSaldo(zeile, file, getDatumvon().getDate(),
getDatumbis().getDate(), type);
}
catch (RemoteException e)
{
Expand All @@ -146,8 +165,9 @@ private void starteAuswertung() throws ApplicationException
}
}

private void auswertungSaldoPDF(final ArrayList<SaldoZeile> zeile,
final File file, final Date datumvon, final Date datumbis)
private void auswertungSaldo(final ArrayList<SaldoZeile> zeile,
final File file, final Date datumvon, final Date datumbis,
final String type)
{
BackgroundTask t = new BackgroundTask()
{
Expand All @@ -157,7 +177,15 @@ public void run(ProgressMonitor monitor) throws ApplicationException
{
try
{
new KontenSaldoPDF(zeile, file, datumvon, datumbis);
switch (type)
{
case AuswertungCSV:
new KontenSaldoCSV(zeile, file, datumvon, datumbis);
break;
case AuswertungPDF:
new KontenSaldoPDF(zeile, file, datumvon, datumbis);
break;
}
GUI.getCurrentView().reload();
}
catch (ApplicationException ae)
Expand Down
1 change: 1 addition & 0 deletions src/de/jost_net/JVerein/gui/view/KontensaldoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void bind() throws Exception
ButtonArea buttons = new ButtonArea();
buttons.addButton("Hilfe", new DokumentationAction(),
DokumentationUtil.JAHRESSALDO, false, "question-circle.png");
buttons.addButton(control.getStartAuswertungCSVButton());
buttons.addButton(control.getStartAuswertungButton());
buttons.paint(this.getParent());
}
Expand Down
122 changes: 122 additions & 0 deletions src/de/jost_net/JVerein/io/KontenSaldoCSV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**********************************************************************
* Copyright (c) by Thomas Laubrock
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*
* [email protected] | www.jverein.de
**********************************************************************/
package de.jost_net.JVerein.io;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.supercsv.cellprocessor.ConvertNullTo;
import org.supercsv.cellprocessor.FmtNumber;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;

import de.jost_net.JVerein.Einstellungen;
import de.jost_net.JVerein.util.JVDateFormatTTMMJJJJ;
import de.willuhn.jameica.gui.GUI;
import de.willuhn.logging.Logger;
import de.willuhn.util.ApplicationException;

public class KontenSaldoCSV
{

private static CellProcessor[] getProcessors()
{

final CellProcessor[] processors = new CellProcessor[] {
new ConvertNullTo(""), // Kontonummer
new ConvertNullTo(""), // Bezeichnung
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Anfangsbestand
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Einnahmen
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Ausgaben
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Umbuchung
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Endbestand
new ConvertNullTo(""), // Bemerkumg
};

return processors;
}

public KontenSaldoCSV(ArrayList<SaldoZeile> zeile,
final File file, Date datumvon, Date datumbis) throws ApplicationException
{
ICsvMapWriter writer = null;
try
{
writer = new CsvMapWriter(new FileWriter(file),
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
final CellProcessor[] processors = getProcessors();
Map<String, Object> csvzeile = new HashMap<>();

String[] header = { "Kontonummer", "Kontobezeichnung", "Anfangsbestand",
"Einnahmen", "Ausgaben", "Umbuchungen", "Endbestand", "Bemerkung" };
writer.writeHeader(header);

String subtitle = new JVDateFormatTTMMJJJJ().format(datumvon) + " - "
+ new JVDateFormatTTMMJJJJ().format(datumbis);
csvzeile.put(header[0], subtitle);
writer.write(csvzeile, header, processors);

for (SaldoZeile sz : zeile)
{
csvzeile = new HashMap<>();

csvzeile.put(header[0], (String) sz.getAttribute("kontonummer"));
csvzeile.put(header[1], (String) sz.getAttribute("kontobezeichnung"));
csvzeile.put(header[2], (Double) sz.getAttribute("anfangsbestand"));
csvzeile.put(header[3], (Double) sz.getAttribute("einnahmen"));
csvzeile.put(header[4], (Double) sz.getAttribute("ausgaben"));
csvzeile.put(header[5], (Double) sz.getAttribute("umbuchungen"));
csvzeile.put(header[6], (Double) sz.getAttribute("endbestand"));
csvzeile.put(header[7], (String) sz.getAttribute("bemerkung"));

writer.write(csvzeile, header, processors);
}
GUI.getStatusBar().setSuccessText("Auswertung fertig");
writer.close();

FileViewer.show(file);
}
catch (Exception e)
{
Logger.error("Error while creating report", e);
throw new ApplicationException("Fehler", e);
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch (Exception e)
{
Logger.error("Error while creating report", e);
throw new ApplicationException("Fehler", e);
}
}
}

}

}

0 comments on commit 8202d5d

Please sign in to comment.