Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following people (in alphabetical order) contributed (commits on GitHub) to
* Falk Brockmann
* Felipe Morato
* Geert Janssens
* Gleb Semyannikov
* Jörg Möller
* Israel Buitron
* Jesse Shieh
Expand Down Expand Up @@ -54,6 +55,8 @@ The following people (in alphabetical order) contributed (commits on GitHub) to
* Stephan Windmüller
* Terry Chung
* thesebas thesebas@thesebas.net
* Timur Badretdinov
* Timur Khuzin
* Vladimir Rutsky
* Weslly Oliveira
* windwarrior lennartbuit@gmail.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import org.gnucash.android.db.adapter.DatabaseAdapter;
import org.gnucash.android.db.adapter.SplitsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.export.csv.CsvAccountExporter;
import org.gnucash.android.export.csv.CsvTransactionsExporter;
import org.gnucash.android.export.ofx.OfxExporter;
import org.gnucash.android.export.qif.QifExporter;
import org.gnucash.android.export.xml.GncXmlExporter;
Expand Down Expand Up @@ -217,7 +219,7 @@ private void dismissProgressDialog() {

/**
* Returns an exporter corresponding to the user settings.
* @return Object of one of {@link QifExporter}, {@link OfxExporter} or {@link GncXmlExporter}
* @return Object of one of {@link QifExporter}, {@link OfxExporter} or {@link GncXmlExporter}, {@Link CsvAccountExporter} or {@Link CsvTransactionsExporter}
*/
private Exporter getExporter() {
switch (mExportParams.getExportFormat()) {
Expand All @@ -228,8 +230,11 @@ private Exporter getExporter() {
return new OfxExporter(mExportParams, mDb);

case XML:
default:
return new GncXmlExporter(mExportParams, mDb);
case CSVA:
return new CsvAccountExporter(mExportParams, mDb);
default:
return new CsvTransactionsExporter(mExportParams, mDb);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
public enum ExportFormat {
QIF("Quicken Interchange Format"),
OFX("Open Financial eXchange"),
XML("GnuCash XML");
XML("GnuCash XML"),
CSVA("GnuCash accounts CSV"),
CSVT("GnuCash transactions CSV");

/**
* Full name of the export format acronym
Expand All @@ -45,6 +47,10 @@ public String getExtension(){
return ".ofx";
case XML:
return ".gnca";
case CSVA:
return ".csv";
case CSVT:
return ".csv";
default:
return ".txt";
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/gnucash/android/export/ExportParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public String getDescription(){
*/
private String mExportLocation;

/**
* CSV-separator char
*/
private char mCsvSeparator = ',';

/**
* Creates a new set of paramters and specifies the export format
* @param format Format to use when exporting the transactions
Expand Down Expand Up @@ -169,6 +174,22 @@ public void setExportLocation(String exportLocation){
mExportLocation = exportLocation;
}

/**
* Get the CSV-separator char
* @return CSV-separator char
*/
public char getCsvSeparator(){
return mCsvSeparator;
}

/**
* Set the CSV-separator char
* @param separator CSV-separator char
*/
public void setCsvSeparator(char separator) {
mCsvSeparator = separator;
}

@Override
public String toString() {
return "Export all transactions created since " + TimestampHelper.getUtcStringFromTimestamp(mExportStartTime) + " UTC"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/org/gnucash/android/export/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ public static String sanitizeFilename(String inputName) {
*/
public static String buildExportFilename(ExportFormat format, String bookName) {
return EXPORT_FILENAME_DATE_FORMAT.format(new Date(System.currentTimeMillis()))
+ "_gnucash_export_" + sanitizeFilename(bookName) + format.getExtension();
+ "_gnucash_export_" + sanitizeFilename(bookName) +
(format==ExportFormat.CSVA?"_accounts_":"") +
(format==ExportFormat.CSVT?"_transactions_":"") +
format.getExtension();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2018 Semyannikov Gleb <nightdevgame@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gnucash.android.export.csv;

import android.database.sqlite.SQLiteDatabase;
import com.crashlytics.android.Crashlytics;
import org.gnucash.android.export.ExportParams;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.model.Account;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
* Creates a GnuCash CSV account representation of the accounts and transactions
*
* @author Semyannikov Gleb <nightdevgame@gmail.com>
*/
public class CsvAccountExporter extends Exporter{
private char mCsvSeparator;

/**
* Construct a new exporter with export parameters
* @param params Parameters for the export
*/
public CsvAccountExporter(ExportParams params) {
super(params, null);
mCsvSeparator = params.getCsvSeparator();
LOG_TAG = "GncXmlExporter";
}

/**
* Overloaded constructor.
* Creates an exporter with an already open database instance.
* @param params Parameters for the export
* @param db SQLite database
*/
public CsvAccountExporter(ExportParams params, SQLiteDatabase db) {
super(params, db);
mCsvSeparator = params.getCsvSeparator();
LOG_TAG = "GncXmlExporter";
}

@Override
public List<String> generateExport() throws ExporterException {
OutputStreamWriter writerStream = null;
CsvWriter writer = null;
String outputFile = getExportCacheFilePath();
try {
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
writerStream = new OutputStreamWriter(bufferedOutputStream);
writer = new CsvWriter(writerStream);
generateExport(writer);
} catch (IOException ex){
Crashlytics.log("Error exporting CSV");
Crashlytics.logException(ex);
} finally {
if (writerStream != null) {
try {
writerStream.close();
} catch (IOException e) {
throw new ExporterException(mExportParams, e);
}
}
}

List<String> exportedFiles = new ArrayList<>();
exportedFiles.add(outputFile);

return exportedFiles;
}

public void generateExport(final CsvWriter writer) throws ExporterException {
try {
String separator = mCsvSeparator + "";
List<String> names = new ArrayList<String>();
names.add("type");
names.add("full_name");
names.add("name");
names.add("code");
names.add("description");
names.add("color");
names.add("notes");
names.add("commoditym");
names.add("commodityn");
names.add("hidden");
names.add("tax");
names.add("place_holder");

List<Account> accounts = mAccountsDbAdapter.getAllRecords();

for(int i = 0; i < names.size(); i++) {
writer.write(names.get(i) + separator);
}
writer.write("\n");
for(int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);

writer.write(account.getAccountType().toString() + separator);
writer.write(account.getFullName() + separator);
writer.write(account.getName() + separator);

//Code
writer.write(separator);

writer.write(account.getDescription() + separator);
writer.write(account.getColor() + separator);

//Notes
writer.write(separator);

writer.write(account.getCommodity().getCurrencyCode() + separator);
writer.write("CURRENCY" + separator);
writer.write(account.isHidden()?"T":"F" + separator);

writer.write("F" + separator);

writer.write(account.isPlaceholderAccount()?"T":"F" + separator);

writer.write("\n");
}
} catch (Exception e) {
Crashlytics.logException(e);
throw new ExporterException(mExportParams, e);
}
}
}
Loading