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
///! # Read pdf files and transform into plain text
///! This requires the extern tool `pdftotext`
///! which is part of [XpdfReader](https://www.xpdfreader.com/pdftotext-man.html).
use super::accounts::{Account, AccountHandler};
use crate::PdfParseParams;
use chrono::NaiveDate;
use chrono::{Datelike, TimeZone, Utc};
use finql::asset::Asset;
use finql::currency;
use finql::data_handler::DataError;
use finql::fx_rates::insert_fx_quote;
use finql::sqlite_handler::SqliteDB;
use finql::transaction::{Transaction, TransactionType};
use finql::{CashAmount, CashFlow};
use rusqlite::Connection;
use pdf_store::store_pdf;
use std::error::Error;
use std::process::Command;
use std::{fmt, io, num, string};

pub mod pdf_store;
mod read_account_info;
mod read_transactions;
pub use pdf_store::sha256_hash;
use read_account_info::parse_account_info;
use read_transactions::parse_transactions;

#[derive(Debug)]
pub enum ReadPDFError {
    IoError(io::Error),
    ParseError(string::FromUtf8Error),
    ParseFloat(num::ParseFloatError),
    ParseCurrency(currency::CurrencyError),
    DBError(DataError),
    CurrencyMismatch,
    ParseDate,
    ConsistencyCheckFailed(String),
    AlreadyParsed,
    NotFound(&'static str),
    UnknownDocumentType,
}

impl fmt::Display for ReadPDFError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Conversion of pdf to text failed.")
    }
}

impl Error for ReadPDFError {
    fn cause(&self) -> Option<&dyn Error> {
        match self {
            Self::IoError(err) => Some(err),
            Self::ParseError(err) => Some(err),
            Self::ParseFloat(err) => Some(err),
            Self::ParseCurrency(err) => Some(err),
            Self::DBError(err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::string::FromUtf8Error> for ReadPDFError {
    fn from(error: string::FromUtf8Error) -> Self {
        Self::ParseError(error)
    }
}

impl From<io::Error> for ReadPDFError {
    fn from(error: io::Error) -> Self {
        Self::IoError(error)
    }
}

impl From<DataError> for ReadPDFError {
    fn from(error: DataError) -> Self {
        Self::DBError(error)
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum DocumentType {
    Buy,
    Sell,
    Dividend,
    Tax,
    Interest,
    BondPayBack,
}

// Collect all parsed data that is required to construct by category distinct cash flow transactions
pub struct ParsedTransactionInfo {
    doc_type: DocumentType,
    asset: Asset,
    position: f64,
    valuta: NaiveDate,
    fx_rate: Option<f64>,
    main_amount: CashAmount,
    total_amount: CashAmount,
    extra_fees: Vec<CashAmount>,
    extra_taxes: Vec<CashAmount>,
    accruals: Vec<CashAmount>,
    note: Option<String>,
}

impl ParsedTransactionInfo {
    fn new(
        doc_type: DocumentType,
        asset: Asset,
        main_amount: CashAmount,
        total_amount: CashAmount,
        fx_rate: Option<f64>,
        valuta: NaiveDate,
    ) -> ParsedTransactionInfo {
        ParsedTransactionInfo {
            doc_type,
            asset,
            position: 0.0,
            valuta,
            fx_rate,
            main_amount,
            total_amount,
            extra_fees: Vec::new(),
            extra_taxes: Vec::new(),
            accruals: Vec::new(),
            note: None,
        }
    }
}

pub fn rounded_equal(x: f64, y: f64, precision: i32) -> bool {
    let factor = 10.0_f64.powi(precision);
    return (x * factor).round() == (y * factor).round();
}

pub fn text_from_pdf(file: &str) -> Result<String, ReadPDFError> {
    let output = Command::new("pdftotext")
        .arg("-layout")
        .arg("-q")
        .arg(&file)
        .arg("-")
        .output()?;
    Ok(String::from_utf8(output.stdout)?)
}

/// Convert a string with German number convention
/// (e.g. '.' as thousands separator and ',' as decimal separator)
pub fn german_string_to_float(num_string: &str) -> Result<f64, ReadPDFError> {
    let sign_less_string = num_string.replace("-", "");
    let positive = if sign_less_string != num_string {
        false
    } else {
        true
    };
    let result = sign_less_string
        .trim()
        .replace(".", "")
        .replace(",", ".")
        .parse()
        .map_err(|err| ReadPDFError::ParseFloat(err));
    match result {
        Ok(num) => {
            if positive {
                Ok(num)
            } else {
                Ok(-num)
            }
        }
        Err(err) => Err(err),
    }
}

/// Converts strings in German data convention to NaiveDate
pub fn german_string_to_date(date_string: &str) -> Result<NaiveDate, ReadPDFError> {
    NaiveDate::parse_from_str(date_string, "%d.%m.%Y").map_err(|_| ReadPDFError::ParseDate)
}

pub fn parse_and_store<DB: AccountHandler>(
    pdf_file: &str,
    db: &mut DB,
    config: &PdfParseParams,
) -> Result<i32, ReadPDFError> {
    let hash = sha256_hash(pdf_file)?;
    match db.lookup_hash(&hash) {
        Ok((ids, _path)) => {
            if ids.len() > 0 {
                if config.warn_old {
                    return Err(ReadPDFError::AlreadyParsed);
                }
                return Ok(0);
            }
        }
        Err(_) => {}
    }
    //println!("Start parsing document {}", pdf_file);
    let text = text_from_pdf(pdf_file);
    match text {
        Ok(text) => {
            let account_info = parse_account_info(&text);
            let (broker, account_name) = if account_info.is_err() && config.default_account {
                ("nobroker".to_string(), "unassigned".to_string())
            } else {
                account_info?
            };
            let mut account = Account {
                id: None,
                broker,
                account_name,
            };
            let acc_id = db
                .insert_account_if_new(&account)
                .map_err(|err| ReadPDFError::DBError(err))?;
            account.id = Some(acc_id);

            // Retrieve all transaction relevant data from pdf
            let tri = parse_transactions(&text)?;
            // If not disable, perform consistency check
            if config.consistency_check {
                check_consistency(&tri)?;
            }
            // Generate list of transactions
            let transactions = make_transactions(&tri);
            let trans_ids = match transactions {
                Ok((transactions, asset)) => {
                    let asset_id = if asset.name == "" {
                        db.get_asset_by_isin(&asset.isin.unwrap())
                            .map_err(|_| ReadPDFError::NotFound("could not find ISIN in db"))?
                            .id
                            .unwrap()
                    } else {
                        db.insert_asset_if_new(&asset, config.rename_asset)
                            .map_err(|err| ReadPDFError::DBError(err))?
                    };
                    let mut trans_ids = Vec::new();
                    for trans in transactions {
                        let mut trans = trans.clone();
                        trans.set_asset_id(asset_id);
                        if trans_ids.len() > 0 {
                            trans.set_transaction_ref(trans_ids[0]);
                        }
                        let trans_id = db
                            .insert_transaction(&trans)
                            .map_err(|err| ReadPDFError::DBError(err))?;
                        trans_ids.push(trans_id);
                        let _ = db
                            .add_transaction_to_account(acc_id, trans_id)
                            .map_err(|err| ReadPDFError::DBError(err))?;
                    }
                    Ok(trans_ids)
                }
                Err(err) => Err(err),
            }?;
            let name = store_pdf(pdf_file, &hash, &config)?;
            db.insert_doc(&trans_ids, &hash, &name)?;
            Ok(trans_ids.len() as i32)
        }
        Err(err) => Err(err),
    }
}

// Check if main payment plus all fees and taxes add up to total payment
// Add up all payments separate by currencies, convert into total currency, and check if the add up to zero.
pub fn check_consistency(tri: &ParsedTransactionInfo) -> Result<(), ReadPDFError> {
    let time = Utc
        .ymd(tri.valuta.year(), tri.valuta.month(), tri.valuta.day())
        .and_hms_milli(18, 0, 0, 0);

    // temporary storage for fx rates
    // total payment is always in base currency, but main_amount (and maybe fees or taxes) could be in foreign currency.
    let mut conn = Connection::open(":memory:").unwrap();
    let mut fx_db = SqliteDB{ conn: &mut conn };
    fx_db.init().unwrap();

    if tri.fx_rate.is_some() {
        insert_fx_quote(
            tri.fx_rate.unwrap(),
            tri.total_amount.currency,
            tri.main_amount.currency,
            time,
            &mut fx_db,
        )?;
    }

    // Add up all payment components and check whether they equal the final payment
    let mut check_sum = -tri.total_amount;
    let mut foreign_check_sum = tri.main_amount;
    for fee in &tri.extra_fees {
        add_by_currency(fee, &mut check_sum, &mut foreign_check_sum);
    }
    for tax in &tri.extra_taxes {
        add_by_currency(tax, &mut check_sum, &mut foreign_check_sum);
    }
    for accrued in &tri.accruals {
        add_by_currency(accrued, &mut check_sum, &mut foreign_check_sum);
    }
    check_sum.add(foreign_check_sum, time, &mut fx_db, true)?;

    // Final sum should be nearly zero
    if !rounded_equal(check_sum.amount, 0.0, 4) {
        let warning = format!(
            "Sum of payments does not equal total payments, difference is {}.",
            check_sum.amount
        );
        return Err(ReadPDFError::ConsistencyCheckFailed(warning));
    } else {
        Ok(())
    }
}

// Transaction in foreign currency will be converted to currency of total payment amount
pub fn make_transactions(
    tri: &ParsedTransactionInfo,
) -> Result<(Vec<Transaction>, Asset), ReadPDFError> {
    let mut transactions = Vec::new();
    let time = Utc
        .ymd(tri.valuta.year(), tri.valuta.month(), tri.valuta.day())
        .and_hms_milli(18, 0, 0, 0);

    // temporary storage for fx rates
    // total payment is always in base currency, but main_amount (and maybe fees or taxes) could be in foreign currency.
    let mut conn = Connection::open(":memory:").unwrap();
    let mut fx_db = SqliteDB{ conn: &mut conn };
    fx_db.init().unwrap();
    if tri.fx_rate.is_some() {
        insert_fx_quote(
            tri.fx_rate.unwrap(),
            tri.total_amount.currency,
            tri.main_amount.currency,
            time,
            &mut fx_db,
        )?;
    }

    // Construct main transaction
    if tri.main_amount.amount != 0.0 {
        transactions.push(Transaction {
            id: None,
            transaction_type: match tri.doc_type {
                DocumentType::Buy | DocumentType::Sell | DocumentType::BondPayBack => {
                    TransactionType::Asset {
                        asset_id: 0,
                        position: tri.position,
                    }
                }
                DocumentType::Dividend => TransactionType::Dividend { asset_id: 0 },
                DocumentType::Interest => TransactionType::Interest { asset_id: 0 },
                DocumentType::Tax => TransactionType::Tax {
                    transaction_ref: None,
                },
            },
            cash_flow: CashFlow {
                amount: tri.main_amount,
                date: tri.valuta,
            },
            note: tri.note.clone(),
        });
    } else {
        // No main transaction, nothing todo
        return Ok((transactions, tri.asset.clone()));
    }

    let mut total_fee = CashAmount {
        amount: 0.0,
        currency: tri.total_amount.currency,
    };
    for fee in &tri.extra_fees {
        total_fee.add(*fee, time, &mut fx_db, true)?;
    }
    if total_fee.amount != 0.0 {
        transactions.push(Transaction {
            id: None,
            transaction_type: TransactionType::Fee {
                transaction_ref: None,
            },
            cash_flow: CashFlow {
                amount: total_fee,
                date: tri.valuta,
            },
            note: None,
        });
    }

    let mut total_tax = CashAmount {
        amount: 0.0,
        currency: tri.total_amount.currency,
    };
    for tax in &tri.extra_taxes {
        total_tax.add(*tax, time, &mut fx_db, true)?;
    }
    if total_tax.amount != 0.0 {
        transactions.push(Transaction {
            id: None,
            transaction_type: TransactionType::Tax {
                transaction_ref: None,
            },
            cash_flow: CashFlow {
                amount: total_tax,
                date: tri.valuta,
            },
            note: None,
        });
    }

    let mut total_accrued = CashAmount {
        amount: 0.0,
        currency: tri.total_amount.currency,
    };
    for accrued in &tri.accruals {
        total_accrued.add(*accrued, time, &mut fx_db, true)?;
    }
    if total_accrued.amount != 0.0 {
        transactions.push(Transaction {
            id: None,
            transaction_type: TransactionType::Interest { asset_id: 0 },
            cash_flow: CashFlow {
                amount: total_accrued,
                date: tri.valuta,
            },
            note: None,
        });
    }

    // Ensure that sum of payments equal total payments in spite of rounding errors
    transactions[0].cash_flow.amount.amount =
        tri.total_amount.amount - total_accrued.amount - total_tax.amount - total_fee.amount;
    transactions[0].cash_flow.amount.currency = tri.total_amount.currency;

    Ok((transactions, tri.asset.clone()))
}

fn add_by_currency(
    new_amount: &CashAmount,
    base_amount: &mut CashAmount,
    foreign_amount: &mut CashAmount,
) {
    if new_amount.currency == base_amount.currency {
        base_amount.amount += new_amount.amount;
    } else {
        foreign_amount.amount += new_amount.amount;
    }
}