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
use std::{fmt,fmt::{Display, Formatter}};
use std::collections::BTreeMap;
use std::ops::Neg;

use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc, NaiveDate};

use crate::currency::{Currency, CurrencyConverter, CurrencyError};

/// Container for an amount of money in some currency
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq)]
pub struct CashAmount {
    pub amount: f64,
    pub currency: Currency,
}

pub fn round2digits(x: f64, digits: i32) -> f64 {
    (x * 10.0_f64.powi(digits)).round() / 10.0_f64.powi(digits)
}

impl CashAmount {
    pub async fn add(
        &mut self,
        cash_amount: CashAmount,
        time: DateTime<Utc>,
        currency_converter: &mut dyn CurrencyConverter,
        with_rounding: bool,
    ) -> Result<&mut Self, CurrencyError> {
        if self.currency == cash_amount.currency {
            self.amount += cash_amount.amount;
            Ok(self)
        } else {
            let fx_rate = currency_converter.fx_rate(cash_amount.currency, self.currency, time).await?;
            self.amount += fx_rate * cash_amount.amount;
            if with_rounding {
                let digits = self.currency.rounding_digits();
                self.amount = round2digits(self.amount, digits);
            }
            Ok(self)
        }
    }

    pub async fn add_opt(
        &mut self,
        cash_amount: Option<CashAmount>,
        time: DateTime<Utc>,
        currency_converter: &mut dyn CurrencyConverter,
        with_rounding: bool,
    ) -> Result<&mut Self, CurrencyError> {
        match cash_amount {
            None => Ok(self),
            Some(cash_amount) => self.add(cash_amount, time, currency_converter, with_rounding).await,
        }
    }

    pub async fn sub(
        &mut self,
        cash_amount: CashAmount,
        time: DateTime<Utc>,
        currency_converter: &mut dyn CurrencyConverter,
        with_rounding: bool,
    ) -> Result<&mut Self, CurrencyError> {
        if self.currency == cash_amount.currency {
            self.amount -= cash_amount.amount;
            Ok(self)
        } else {
            let fx_rate = currency_converter.fx_rate(cash_amount.currency, self.currency, time).await?;
            self.amount -= fx_rate * cash_amount.amount;
            if with_rounding {
                let digits = self.currency.rounding_digits();
                self.amount = round2digits(self.amount, digits);
            }
            Ok(self)
        }
    }

    pub async fn sub_opt(
        &mut self,
        cash_amount: Option<CashAmount>,
        time: DateTime<Utc>,
        currency_converter: &mut dyn CurrencyConverter,
        with_rounding: bool,
    ) -> Result<&mut Self, CurrencyError> {
        match cash_amount {
            None => Ok(self),
            Some(cash_amount) => self.sub(cash_amount, time, currency_converter, with_rounding).await,
        }
    }

    /// Round a cash amount to that number of decimals
    pub fn round(&self, digits: i32) -> CashAmount {
        CashAmount {
            amount: round2digits(self.amount, digits),
            currency: self.currency,
        }
    }

    /// Round Cash amount according to rounding conventions
    /// Lookup currency in rounding_conventions. If found, use the number of digits found for
    /// rounding to that number of decimals, otherwise round to two decimals.
    pub fn round_by_convention(&self, rounding_conventions: &BTreeMap<String, i32>) -> CashAmount {
        match rounding_conventions.get_key_value(&self.currency.to_string()) {
            Some((_, digits)) => self.round(*digits),
            None => self.round(2),
        }
    }
}

impl Display for CashAmount {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{:16.4} {}", self.amount, self.currency)
    }
}

impl Neg for CashAmount {
    type Output = CashAmount;

    fn neg(self) -> Self::Output {
        CashAmount {
            amount: -self.amount,
            currency: self.currency,
        }
    }
}

/// Container for a single cash flow
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub struct CashFlow {
    pub amount: CashAmount,
    pub date: NaiveDate,
}

impl CashFlow {
    /// Construct new cash flow
    pub fn new(amount: f64, currency: Currency, date: NaiveDate) -> CashFlow {
        CashFlow {
            amount: CashAmount { amount, currency },
            date,
        }
    }
    /// Check, whether cash flows could be aggregated
    pub fn aggregatable(&self, cf: &CashFlow) -> bool {
        self.amount.currency == cf.amount.currency && self.date == cf.date
    }

    /// Compare to cash flows for equality within a given absolute tolerance
    pub fn fuzzy_cash_flows_cmp_eq(&self, cf: &CashFlow, tol: f64) -> bool {
        self.aggregatable(cf) 
            && !self.amount.amount.is_nan()
            && !cf.amount.amount.is_nan()
            && (self.amount.amount - cf.amount.amount).abs() <= tol
    }
}

impl Display for CashFlow {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.date, self.amount)
    }
}

impl Neg for CashFlow {
    type Output = CashFlow;

    fn neg(self) -> Self::Output {
        CashFlow {
            amount: -self.amount,
            date: self.date,
        }
    }
}