forked from Bios-Marcel/baka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ed9792
commit 4d522ea
Showing
5 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/link/biosmarcel/baka/bankimport/DKB2CSV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package link.biosmarcel.baka.bankimport; | ||
|
||
import link.biosmarcel.baka.data.Account; | ||
import link.biosmarcel.baka.data.Payment; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.QuoteMode; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.math.BigDecimal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.text.DecimalFormat; | ||
import java.text.ParseException; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Format for new DKB UI. | ||
*/ | ||
public class DKB2CSV { | ||
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd.MM.yy"); | ||
private static final DecimalFormat CURRENCY_FORMAT; | ||
|
||
static { | ||
CURRENCY_FORMAT = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.GERMAN); | ||
CURRENCY_FORMAT.setParseBigDecimal(true); | ||
} | ||
|
||
public static List<Payment> parse(final Account account, final File file) { | ||
try (Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.ISO_8859_1)) { | ||
final var format = CSVFormat.Builder | ||
.create() | ||
.setDelimiter(';') | ||
.setQuote('"') | ||
// Haven't figured out the escape character yet. | ||
.setQuoteMode(QuoteMode.MINIMAL) | ||
.setIgnoreSurroundingSpaces(true) | ||
.setIgnoreEmptyLines(true) | ||
.setAllowMissingColumnNames(true) | ||
.build(); | ||
final var records = format.parse(reader).iterator(); | ||
// Skip header row; Format.setSkipHeaderRecord doesn't seem to work. | ||
// Also skip rows that just contain meta data. Empty rows don't count, so we skip 5. | ||
for (int i = 0; i < 5; i++) { | ||
records.next(); | ||
} | ||
|
||
final List<Payment> newPayments = new ArrayList<>(); | ||
records.forEachRemaining(record -> { | ||
final var reference = record.get(5); | ||
final BigDecimal amount; | ||
try { | ||
amount = (BigDecimal) CURRENCY_FORMAT.parse(record.get(8)); | ||
} catch (final ParseException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
|
||
final var bookingDate = LocalDate.parse(record.get(0), DATE_FORMAT).atStartOfDay(); | ||
final var effectiveDate = switch (record.get(1)) { | ||
// EffectiveDate is optional, so to avoid confusion, we just set it to the same as bookingDate. | ||
case null -> bookingDate; | ||
case "" -> bookingDate; | ||
default -> LocalDate.parse(record.get(1), DATE_FORMAT).atStartOfDay(); | ||
}; | ||
final String name = record.get(3); | ||
|
||
final Payment payment = new Payment( | ||
account, | ||
amount, | ||
reference, | ||
name, | ||
bookingDate, | ||
effectiveDate | ||
); | ||
|
||
payment.participant = Import.prepareIBAN(record.get(7)); | ||
|
||
newPayments.add(payment); | ||
}); | ||
|
||
return newPayments; | ||
} catch (final IOException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package link.biosmarcel.baka.bankimport; | ||
|
||
import link.biosmarcel.baka.data.Account; | ||
import link.biosmarcel.baka.data.Payment; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.QuoteMode; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.math.BigDecimal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.text.DecimalFormat; | ||
import java.text.ParseException; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
public class INGCSV { | ||
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd.MM.yyyy"); | ||
private static final DecimalFormat CURRENCY_FORMAT; | ||
|
||
static { | ||
CURRENCY_FORMAT = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.GERMAN); | ||
CURRENCY_FORMAT.setParseBigDecimal(true); | ||
} | ||
|
||
public static List<Payment> parse(final Account account, final File file) { | ||
try (Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.ISO_8859_1)) { | ||
final var format = CSVFormat.Builder | ||
.create() | ||
.setDelimiter(';') | ||
.setQuote('"') | ||
// Haven't figured out the escape character yet. | ||
.setQuoteMode(QuoteMode.MINIMAL) | ||
.setIgnoreSurroundingSpaces(true) | ||
.setIgnoreEmptyLines(true) | ||
.setAllowMissingColumnNames(true) | ||
.build(); | ||
final var records = format.parse(reader).iterator(); | ||
// Skip header row; Format.setSkipHeaderRecord doesn't seem to work. | ||
// Also skip rows that just contain meta data. Empty rows don't count, so we skip 10. | ||
for (int i = 0; i < 10; i++) { | ||
records.next(); | ||
} | ||
|
||
final List<Payment> newPayments = new ArrayList<>(); | ||
records.forEachRemaining(record -> { | ||
final var reference = record.get(4); | ||
final BigDecimal amount; | ||
try { | ||
amount = (BigDecimal) CURRENCY_FORMAT.parse(record.get(7)); | ||
} catch (final ParseException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
|
||
final var bookingDate = LocalDate.parse(record.get(0), DATE_FORMAT).atStartOfDay(); | ||
final var effectiveDate = switch (record.get(1)) { | ||
// EffectiveDate is optional, so to avoid confusion, we just set it to the same as bookingDate. | ||
case null -> bookingDate; | ||
case "" -> bookingDate; | ||
default -> LocalDate.parse(record.get(1), DATE_FORMAT).atStartOfDay(); | ||
}; | ||
final String name = record.get(2); | ||
|
||
final Payment payment = new Payment( | ||
account, | ||
amount, | ||
reference, | ||
name, | ||
bookingDate, | ||
effectiveDate | ||
); | ||
|
||
payment.participant = ""; // Not included in ING format | ||
|
||
newPayments.add(payment); | ||
}); | ||
|
||
return newPayments; | ||
} catch (final IOException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package link.biosmarcel.baka.data; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
|
||
public enum ImportFormat { | ||
SparkasseCSV, | ||
DKBCSV, | ||
RevolutCSV, | ||
SparkasseCSV(link.biosmarcel.baka.bankimport.SparkasseCSV::parse), | ||
DKBCSV(link.biosmarcel.baka.bankimport.DKBCSV::parse), | ||
RevolutCSV(link.biosmarcel.baka.bankimport.RevolutCSV::parse), | ||
DKB2CSV(link.biosmarcel.baka.bankimport.DKB2CSV::parse), | ||
INGCSV(link.biosmarcel.baka.bankimport.INGCSV::parse), | ||
; | ||
|
||
public final transient BiFunction<Account, File, List<Payment>> func; | ||
|
||
ImportFormat(final BiFunction<Account, File, List<Payment>> func) { | ||
this.func = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters