Handle foreign currency purchases in DKB importer

korrat
Dec 17, 2023, 9:37 AM
DWUVE75BTYH56NS2I4J2XSPCU3XTJBJQB567CC3BFMEMFUIE4KLQC

Dependencies

  • [2] 7URE4HPL Add an importer for DKB account statements

Change contents

  • edit in importers/dkb/src/lib.rs at line 350
    [2.8343]
    [2.8343]
    if let Some(info) = record.exchange_information {
    posting.set_price(info.exchange_rate);
    }
  • replacement in importers/dkb/src/lib.rs at line 355
    [2.8366][2.8366:8435]()
    .build_posting(opposite_account, |_posting| {});
    [2.8366]
    [2.8435]
    .build_posting(opposite_account, |posting| {
  • edit in importers/dkb/src/lib.rs at line 360
    [2.8436]
    [2.8436]
    if let Some(info) = record.exchange_information {
    posting.set_amount(info.foreign_total);
    }
    });
  • edit in importers/dkb/src/lib.rs at line 443
    [2.10165]
    [2.10165]
    exchange_information: Option<CurrencyExchangeInformation>,
  • edit in importers/dkb/src/lib.rs at line 470
    [2.10593]
    [2.10593]
    #[derive(Clone, Copy, Debug)]
    struct CurrencyExchangeInformation {
    exchange_rate: Amount,
    foreign_total: Amount,
    }
    impl CurrencyExchangeInformation {
    fn extract_from_description(description: &str) -> Result<Option<Self>, Error> {
    description
    .find("Original ")
    .map(|index| {
    let (foreign_total, rest) = split_off_amount(&description[index + 9..])?;
  • edit in importers/dkb/src/lib.rs at line 484
    [2.10594]
    [2.10594]
    let exchange_rate = if let Some(rest) = rest.strip_prefix("1 Euro=") {
    split_off_amount(rest)?.0
    } else if let Some(rest) = rest.strip_prefix("Kurs ") {
    let (amount, _rest) = rest.split_once(' ').unwrap_or((rest, ""));
    Amount::new(
    Decimal::from_str_exact(amount).map_err(|_| todo!())?,
    foreign_total.commodity,
    )
    } else {
    todo!("handle {rest:?}")
    };
    Ok(Self {
    exchange_rate,
    foreign_total,
    })
    })
    .transpose()
    }
    }
  • edit in importers/dkb/src/lib.rs at line 624
    [2.13595]
    [2.13595]
    let exchange_information =
    CurrencyExchangeInformation::extract_from_description(description)
    .map_err(Self::Error::custom)?;
  • edit in importers/dkb/src/lib.rs at line 634
    [2.13739]
    [2.13739]
    exchange_information,
  • edit in importers/dkb/src/lib.rs at line 711
    [2.15652]
    [2.15652]
  • edit in importers/dkb/src/lib.rs at line 714
    [2.15692]
    fn split_off_amount(v: &str) -> Result<(Amount, &str), Error> {
    let index = v.find(' ').ok_or_else(|| todo!())?;
    let index = v[index + 1..]
    .find(' ')
    .map_or(v.len(), |new| new + index + 1);
    let (amount, rest) = v.split_at(index);
    let amount = parse_german_amount(amount)?;
    // Remove whitespace from the beginning
    let rest = rest.trim_start();
    Ok((amount, rest))
    }