kontochronik 0.4.0

Long-Term archive for account transactions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Import adapter for the CSV export of GLS Bank online banking.

use anyhow::{Context as _, bail};
use csv::ReaderBuilder;
use rust_decimal::Decimal;
use serde::Deserialize;
use time::Date;

use crate::{
    archive::ArchiveRecord,
    util::{canonical_text, clean_optional, read_to_utf8},
};

const CSV_DELIMITER: u8 = b';';

/// One row of the GLS CSV export.
/// The "Bezeichnung Auftragskonto" column (a portal label, not ledger
/// data) has no archive counterpart and no field here;
/// [`KNOWN_HEADERS`] still names it.
#[derive(Debug, Deserialize)]
struct GlsRecord {
    #[serde(rename = "IBAN Auftragskonto")]
    account_iban: String,

    #[serde(rename = "BIC Auftragskonto")]
    account_bic: String,

    #[serde(rename = "Bankname Auftragskonto")]
    account_bank_name: String,

    #[serde(rename = "Buchungstag", with = "german_date")]
    booking_date: Date,

    #[serde(rename = "Valutadatum", with = "german_date")]
    value_date: Date,

    #[serde(rename = "Name Zahlungsbeteiligter")]
    participant_name: Option<String>,

    #[serde(rename = "IBAN Zahlungsbeteiligter")]
    participant_iban: Option<String>,

    #[serde(rename = "BIC (SWIFT-Code) Zahlungsbeteiligter")]
    participant_bic: Option<String>,

    #[serde(rename = "Buchungstext")]
    booking_text: String,

    #[serde(rename = "Verwendungszweck")]
    purpose: Option<String>,

    #[serde(rename = "Betrag", with = "crate::util::german_number")]
    amount: Decimal,

    #[serde(rename = "Waehrung")]
    currency: String,

    #[serde(rename = "Saldo nach Buchung", with = "crate::util::german_number")]
    balance_after_booking: Decimal,

    #[serde(rename = "Glaeubiger ID")]
    creditor_id: Option<String>,

    #[serde(rename = "Mandatsreferenz")]
    mandate_reference: Option<String>,

    #[serde(rename = "Bemerkung", default)]
    note: Option<String>,

    #[serde(rename = "Gekennzeichneter Umsatz", default)]
    flagged: Option<String>,
}

impl TryFrom<GlsRecord> for ArchiveRecord {
    type Error = anyhow::Error;

    fn try_from(gls: GlsRecord) -> Result<Self, Self::Error> {
        let GlsRecord {
            account_iban,
            account_bic,
            account_bank_name,
            booking_date,
            value_date,
            participant_name,
            participant_iban,
            participant_bic,
            booking_text,
            purpose,
            amount,
            currency,
            balance_after_booking,
            creditor_id,
            mandate_reference,
            note,
            flagged,
        } = gls;
        // Portal annotations are out of scope for the archive;
        if let Some(note) = clean_optional(note) {
            bail!("\"Bemerkung\" contains data ({note:?}) - portal annotations are not archived");
        }
        if let Some(flagged) = clean_optional(flagged) {
            bail!(
                "\"Gekennzeichneter Umsatz\" contains data ({flagged:?}) - portal annotations are not archived"
            );
        }
        Ok(Self {
            booking_date,
            value_date,
            amount,
            currency: canonical_text(&currency),
            participant_name: clean_optional(participant_name),
            purpose: clean_optional(purpose),
            booking_text: canonical_text(&booking_text),
            participant_iban: clean_optional(participant_iban),
            participant_bic: clean_optional(participant_bic),
            creditor_id: clean_optional(creditor_id),
            mandate_reference: clean_optional(mandate_reference),
            balance_after_booking: Some(balance_after_booking),
            account_iban: account_iban
                .parse()
                .with_context(|| format!("Invalid \"IBAN Auftragskonto\" {account_iban:?}"))?,
            account_bic: clean_optional(Some(account_bic)),
            account_bank_name: clean_optional(Some(account_bank_name)),
            fingerprint: None,
        })
    }
}

/// Every column the GLS export is known to contain.
/// Serves as a tripwire:
/// an unknown column means the bank changed its format, and silently
/// ignoring it could silently ignore data.
const KNOWN_HEADERS: [&str; 18] = [
    "Bezeichnung Auftragskonto",
    "IBAN Auftragskonto",
    "BIC Auftragskonto",
    "Bankname Auftragskonto",
    "Buchungstag",
    "Valutadatum",
    "Name Zahlungsbeteiligter",
    "IBAN Zahlungsbeteiligter",
    "BIC (SWIFT-Code) Zahlungsbeteiligter",
    "Buchungstext",
    "Verwendungszweck",
    "Betrag",
    "Waehrung",
    "Saldo nach Buchung",
    "Glaeubiger ID",
    "Mandatsreferenz",
    "Bemerkung",
    "Gekennzeichneter Umsatz",
];

/// Parses the CSV export of GLS Bank online banking into archive records.
pub fn parse_gls(bytes: &[u8]) -> anyhow::Result<Vec<ArchiveRecord>> {
    parse_gls_csv(bytes)?
        .into_iter()
        .map(ArchiveRecord::try_from)
        .collect()
}

/// Rejects files with columns this adapter doesn't know.
fn parse_gls_csv(bytes: &[u8]) -> anyhow::Result<Vec<GlsRecord>> {
    let text = read_to_utf8(bytes)?;
    let mut rdr = ReaderBuilder::new()
        .delimiter(CSV_DELIMITER)
        .from_reader(text.as_bytes());
    let headers = rdr.headers()?.clone();
    for header in &headers {
        let header = header.trim();
        if !KNOWN_HEADERS.contains(&header) {
            bail!(
                "GLS export contains unknown column {header:?} - \
                 the import adapter must be updated before this data can be archived"
            );
        }
    }
    let mut records = vec![];
    for result in rdr.deserialize() {
        records.push(result?);
    }
    Ok(records)
}

mod german_date {
    use serde::{Deserialize, Deserializer};
    use time::{Date, format_description::FormatItem, macros::format_description};

    const DATE_FMT: &[FormatItem<'static>] = format_description!("[day].[month].[year]");

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Date, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Date::parse(&s, DATE_FMT).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use std::{
        fs,
        path::{Path, PathBuf},
    };

    use super::*;

    use crate::{GapPolicy, ImportSummary, import, read_archive};

    const GLS_HEADER: &str = "Bezeichnung Auftragskonto;IBAN Auftragskonto;\
        BIC Auftragskonto;Bankname Auftragskonto;Buchungstag;Valutadatum;\
        Name Zahlungsbeteiligter;IBAN Zahlungsbeteiligter;\
        BIC (SWIFT-Code) Zahlungsbeteiligter;Buchungstext;Verwendungszweck;\
        Betrag;Waehrung;Saldo nach Buchung;Glaeubiger ID;Mandatsreferenz;\
        Bemerkung;Gekennzeichneter Umsatz";

    const SALARY: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
        GLS Gemeinschaftsbank eG;01.01.2025;01.01.2025;ACME GmbH;\
        DE02300209000106531065;CMCIDEDD;Gutschrift;Gehalt Januar;\
        1.000,00;EUR;1000,00;;;;";

    const BAKERY: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
        GLS Gemeinschaftsbank eG;02.01.2025;02.01.2025;Bäckerei Müller;\
        DE02120300000000202051;BYLADEM1001;Kartenzahlung;Brötchen;\
        -3,50;EUR;996,50;;;;";

    const POWER: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
        GLS Gemeinschaftsbank eG;03.01.2025;03.01.2025;Stadtwerke;\
        DE02500105170137075030;INGDDEFF;Lastschrift;Strom Abschlag;\
        -45,00;EUR;951,50;DE98ZZZ09999999999;M-001;;";

    const COFFEE_1: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
        GLS Gemeinschaftsbank eG;05.01.2025;05.01.2025;Café Blau;\
        DE02100100100006820101;PBNKDEFF;Kartenzahlung;Kaffee;\
        -2,50;EUR;997,50;;;;";

    const COFFEE_2: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
        GLS Gemeinschaftsbank eG;05.01.2025;05.01.2025;Café Blau;\
        DE02100100100006820101;PBNKDEFF;Kartenzahlung;Kaffee;\
        -2,50;EUR;995,00;;;;";

    fn gls_content(rows: &[&str]) -> String {
        let mut content = GLS_HEADER.to_owned();
        for row in rows {
            content.push('\n');
            content.push_str(row);
        }
        content.push('\n');
        content
    }

    fn gls_file(dir: &Path, name: &str, rows: &[&str]) -> PathBuf {
        let path = dir.join(name);
        fs::write(&path, gls_content(rows)).unwrap();
        path
    }

    #[test]
    fn merges_overlapping_exports() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // Rows newest first, like the online banking export.
        let first = gls_file(dir.path(), "export1.csv", &[BAKERY, SALARY]);
        let second = gls_file(dir.path(), "export2.csv", &[POWER, BAKERY]);

        let summary = import(parse_gls, &first, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 2,
                duplicates: 0,
                total: 2
            }
        );

        let summary = import(parse_gls, &second, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 1,
                duplicates: 1,
                total: 3
            }
        );

        let records = read_archive(&archive_path).unwrap();
        let dates: Vec<String> = records
            .iter()
            .map(|record| record.booking_date.to_string())
            .collect();
        assert_eq!(dates, ["2025-01-03", "2025-01-02", "2025-01-01"]);
        assert!(
            records
                .iter()
                .all(|record| record.fingerprint.as_ref().is_some_and(|fp| fp.len() == 32))
        );
        assert_eq!(
            records[1].participant_name.as_deref(),
            Some("Bäckerei Müller")
        );
        assert_eq!(records[2].amount.to_string(), "1000.00");
    }

    #[test]
    fn import_is_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[BAKERY, SALARY]);

        import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap();
        let before = fs::read(&archive_path).unwrap();

        let summary = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 0,
                duplicates: 2,
                total: 2
            }
        );
        assert_eq!(fs::read(&archive_path).unwrap(), before);
    }

    #[test]
    fn identical_bookings_are_distinguished_by_occurrence() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // Two card payments identical in everything the fingerprint hashes;
        // only the balance differs. The narrower export contains just one.
        let narrow = gls_file(dir.path(), "narrow.csv", &[COFFEE_1]);
        let full = gls_file(dir.path(), "full.csv", &[COFFEE_1, COFFEE_2]);

        import(parse_gls, &narrow, &archive_path, GapPolicy::Reject).unwrap();
        let summary = import(parse_gls, &full, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 1,
                duplicates: 1,
                total: 2
            }
        );

        let records = read_archive(&archive_path).unwrap();
        assert_eq!(records.len(), 2);
        assert_ne!(records[0].fingerprint, records[1].fingerprint);
        assert_ne!(
            records[0].balance_after_booking,
            records[1].balance_after_booking
        );
    }

    #[test]
    fn empty_purpose_is_allowed() {
        const NO_PURPOSE: &str = "Girokonto;DE44500105175407324931;GENODEM1GLS;\
            GLS Gemeinschaftsbank eG;04.01.2025;04.01.2025;Automat;\
            DE02100100100006820101;PBNKDEFF;Bargeldauszahlung;;\
            -50,00;EUR;901,50;;;;";
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[NO_PURPOSE]);

        let summary = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(summary.imported, 1);

        let records = read_archive(&archive_path).unwrap();
        assert_eq!(records[0].purpose, None);
        assert!(
            records[0]
                .fingerprint
                .as_ref()
                .is_some_and(|fp| fp.len() == 32)
        );
    }

    #[test]
    fn rejects_filled_portal_annotations() {
        let annotated: &str = &SALARY.replace("1000,00;;;;", "1000,00;;;Notiz;");
        assert_ne!(annotated, SALARY);
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[annotated]);

        let error = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("Bemerkung"));
    }

    #[test]
    fn rejects_unknown_export_columns() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let path = dir.path().join("export.csv");
        fs::write(&path, format!("{GLS_HEADER};Neue Spalte\n{SALARY};x\n")).unwrap();

        let error = import(parse_gls, &path, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("Neue Spalte"));
    }

    #[test]
    fn accepts_exports_without_annotation_columns() {
        let legacy_header = GLS_HEADER.replace(";Bemerkung;Gekennzeichneter Umsatz", "");
        assert_ne!(legacy_header, GLS_HEADER);
        let legacy_row = SALARY.strip_suffix(";;").unwrap();
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let path = dir.path().join("export.csv");
        fs::write(&path, format!("{legacy_header}\n{legacy_row}\n")).unwrap();

        let summary = import(parse_gls, &path, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(summary.imported, 1);
    }

    #[test]
    fn rejects_mixed_accounts() {
        let other_account: &str =
            &SALARY.replace("DE44500105175407324931", "DE02120300000000202051");
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[BAKERY, other_account]);

        assert!(import(parse_gls, &export, &archive_path, GapPolicy::Reject).is_err());
        assert!(!archive_path.exists());
    }

    #[test]
    fn rejects_mixed_currencies() {
        let dollars: &str = &SALARY.replace(";EUR;", ";USD;");
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[BAKERY, dollars]);

        assert!(import(parse_gls, &export, &archive_path, GapPolicy::Reject).is_err());
        assert!(!archive_path.exists());
    }

    #[test]
    fn archives_a_foreign_currency_account() {
        let dollars: &str = &SALARY.replace(";EUR;", ";USD;");
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[dollars]);

        let summary = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(summary.imported, 1);

        let records = read_archive(&archive_path).unwrap();
        assert_eq!(records[0].currency, "USD");
    }

    #[test]
    fn rejects_amounts_with_more_than_two_decimals() {
        let sub_cent: &str = &SALARY.replace(";1.000,00;", ";1.000,001;");
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[sub_cent]);

        let error = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("two decimal places"));
    }

    #[test]
    fn windows_1252_export_is_imported() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let path = dir.path().join("export.csv");
        let bytes: Vec<u8> = gls_content(&[BAKERY])
            .chars()
            .map(|c| u8::try_from(u32::from(c)).expect("Latin-1 test data"))
            .collect();
        fs::write(&path, bytes).unwrap();

        let summary = import(parse_gls, &path, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(summary.imported, 1);

        let records = read_archive(&archive_path).unwrap();
        assert_eq!(
            records[0].participant_name.as_deref(),
            Some("Bäckerei Müller")
        );
        assert_eq!(records[0].purpose.as_deref(), Some("Brötchen"));
    }

    #[test]
    fn verifies_stored_fingerprints() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = gls_file(dir.path(), "export.csv", &[SALARY]);
        import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap();

        // Tamper with the archived amount; the stored fingerprint no longer
        // matches the entry's content.
        let content = fs::read_to_string(&archive_path)
            .unwrap()
            .replace("01.01.2025;1000,00;EUR;ACME", "01.01.2025;999,00;EUR;ACME");
        assert!(content.contains("999,00"));
        fs::write(&archive_path, content).unwrap();

        let error = import(parse_gls, &export, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("does not match"));
    }
}