diff --git a/src/de/jost_net/JVerein/gui/control/KontensaldoControl.java b/src/de/jost_net/JVerein/gui/control/KontensaldoControl.java index e305fc6e4..c10606765 100644 --- a/src/de/jost_net/JVerein/gui/control/KontensaldoControl.java +++ b/src/de/jost_net/JVerein/gui/control/KontensaldoControl.java @@ -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; @@ -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) { @@ -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() { @@ -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 { @@ -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(); @@ -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) { @@ -146,8 +165,9 @@ private void starteAuswertung() throws ApplicationException } } - private void auswertungSaldoPDF(final ArrayList zeile, - final File file, final Date datumvon, final Date datumbis) + private void auswertungSaldo(final ArrayList zeile, + final File file, final Date datumvon, final Date datumbis, + final String type) { BackgroundTask t = new BackgroundTask() { @@ -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) diff --git a/src/de/jost_net/JVerein/gui/view/KontensaldoView.java b/src/de/jost_net/JVerein/gui/view/KontensaldoView.java index 84c8d5cbf..2ca190aef 100644 --- a/src/de/jost_net/JVerein/gui/view/KontensaldoView.java +++ b/src/de/jost_net/JVerein/gui/view/KontensaldoView.java @@ -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()); } diff --git a/src/de/jost_net/JVerein/io/KontenSaldoCSV.java b/src/de/jost_net/JVerein/io/KontenSaldoCSV.java new file mode 100644 index 000000000..6c3f9961b --- /dev/null +++ b/src/de/jost_net/JVerein/io/KontenSaldoCSV.java @@ -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 . + * + * heiner@jverein.de | 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 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 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); + } + } + } + + } + +}