Skip to content

Commit

Permalink
CSV Export für Projektsaldo (#317)
Browse files Browse the repository at this point in the history
* CSV Export für Projektsaldo

* Großschrift
  • Loading branch information
JohannMaierhofer authored Sep 10, 2024
1 parent 8202d5d commit 19be249
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 12 deletions.
44 changes: 36 additions & 8 deletions src/de/jost_net/JVerein/gui/control/ProjektSaldoControl.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.ProjektSaldoList;
import de.jost_net.JVerein.io.ProjektSaldoCSV;
import de.jost_net.JVerein.io.ProjektSaldoPDF;
import de.jost_net.JVerein.io.ProjektSaldoZeile;
import de.jost_net.JVerein.util.Dateiname;
Expand All @@ -45,6 +46,10 @@ public class ProjektSaldoControl extends SaldoControl
{

private ProjektSaldoList saldoList;

final static String AuswertungPDF = "PDF";

final static String AuswertungCSV = "CSV";

public ProjektSaldoControl(AbstractView view)
{
Expand All @@ -58,12 +63,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 @@ -110,7 +129,7 @@ public Part getSaldoList() throws ApplicationException
return saldoList.getSaldoList();
}

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

final String s = fd.open();

Expand All @@ -139,8 +158,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 @@ -149,8 +168,9 @@ private void starteAuswertung() throws ApplicationException
}
}

private void auswertungSaldoPDF(final ArrayList<ProjektSaldoZeile> zeile,
final File file, final Date datumvon, final Date datumbis)
private void auswertungSaldo(final ArrayList<ProjektSaldoZeile> zeile,
final File file, final Date datumvon, final Date datumbis,
final String type)
{
BackgroundTask t = new BackgroundTask()
{
Expand All @@ -159,7 +179,15 @@ public void run(ProgressMonitor monitor) throws ApplicationException
{
try
{
new ProjektSaldoPDF(zeile, file, datumvon, datumbis);
switch (type)
{
case AuswertungCSV:
new ProjektSaldoCSV(zeile, file, datumvon, datumbis);
break;
case AuswertungPDF:
new ProjektSaldoPDF(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/ProjektSaldoView.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.PROJEKTSALDO, false, "question-circle.png");
buttons.addButton(control.getStartAuswertungCSVButton());
buttons.addButton(control.getStartAuswertungButton());
buttons.paint(this.getParent());
}
Expand Down
8 changes: 4 additions & 4 deletions src/de/jost_net/JVerein/io/BuchungsklassesaldoCSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ public BuchungsklassesaldoCSV(ArrayList<BuchungsklasseSaldoZeile> zeile,
}
default:
{
csvzeile.put(header[0], "leer - warum auch immer");
csvzeile.put(header[0], "");
}

}

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

FileViewer.show(file);
}
catch (Exception e)
{
Logger.error("error while creating report", e);
Logger.error("Error while creating report", e);
throw new ApplicationException("Fehler", e);
}
finally
Expand All @@ -174,7 +174,7 @@ public BuchungsklassesaldoCSV(ArrayList<BuchungsklasseSaldoZeile> zeile,
}
catch (Exception e)
{
Logger.error("error while creating report", e);
Logger.error("Error while creating report", e);
throw new ApplicationException("Fehler", e);
}
}
Expand Down
166 changes: 166 additions & 0 deletions src/de/jost_net/JVerein/io/ProjektSaldoCSV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**********************************************************************
* 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.constraint.NotNull;
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 ProjektSaldoCSV
{

private static CellProcessor[] getProcessors()
{

final CellProcessor[] processors = new CellProcessor[] { new NotNull(), // BuchungsArt/Klasse,
// Summe
// new Optional(new FmtNumber(Einstellungen.DECIMALFORMAT)), //
// Einnahmen
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Einnahmen
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)), // Ausgaben
new ConvertNullTo("", new FmtNumber(Einstellungen.DECIMALFORMAT)) // Umbuchung
};

return processors;
}

public ProjektSaldoCSV(ArrayList<ProjektSaldoZeile> 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 = { "Buchungsart", "Einnahmen", "Ausgaben", "Umbuchung" };
writer.writeHeader(header);

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

for (ProjektSaldoZeile pz : zeile)
{
csvzeile = new HashMap<>();
switch (pz.getStatus())
{
case ProjektSaldoZeile.HEADER:
{
csvzeile.put(header[0],
(String) pz.getAttribute("projektbezeichnung"));
break;
}
case ProjektSaldoZeile.DETAIL:
{
csvzeile.put(header[0],
(String) pz.getAttribute("buchungsartbezeichnung"));
csvzeile.put(header[1], (Double) pz.getAttribute("einnahmen"));
csvzeile.put(header[2], (Double) pz.getAttribute("ausgaben"));
csvzeile.put(header[3], (Double) pz.getAttribute("umbuchungen"));
break;
}
case ProjektSaldoZeile.SALDOFOOTER:
{
csvzeile.put(header[0],
(String) pz.getAttribute("projektbezeichnung"));
csvzeile.put(header[1], (Double) pz.getAttribute("einnahmen"));
csvzeile.put(header[2], (Double) pz.getAttribute("ausgaben"));
csvzeile.put(header[3], (Double) pz.getAttribute("umbuchungen"));
break;
}
case ProjektSaldoZeile.GESAMTSALDOFOOTER:
{
csvzeile.put(header[0],
(String) pz.getAttribute("projektbezeichnung"));
csvzeile.put(header[1], (Double) pz.getAttribute("einnahmen"));
csvzeile.put(header[2], (Double) pz.getAttribute("ausgaben"));
csvzeile.put(header[3], (Double) pz.getAttribute("umbuchungen"));
break;
}
case ProjektSaldoZeile.SALDOGEWINNVERLUST:
case ProjektSaldoZeile.GESAMTSALDOGEWINNVERLUST:
{
csvzeile.put(header[0],
(String) pz.getAttribute("projektbezeichnung"));
csvzeile.put(header[1], (Double) pz.getAttribute("einnahmen"));
break;
}
case ProjektSaldoZeile.NICHTZUGEORDNETEBUCHUNGEN:
{
csvzeile.put(header[0],
(String) pz.getAttribute("projektbezeichnung"));
break;
}
default:
{
csvzeile.put(header[0], "");
}

}

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 19be249

Please sign in to comment.