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
//! A double entry accounting system/library.
//!
//! # Optional Features
//!
//! The doublecount package has the following optional cargo features:
//!
//! + `serde-support`
//!   + **Currently incomplete**
//!   + Disabled by default
//!   + Enables support for serialization/de-serialization via `serde`
//!   + Enables support for json serialization/de-serialization via `serde_json`
//!
//! # Usage
//!
//! ```
//! use doublecount::{
//!     AccountStatus, EditAccountStatus, Account, Program, Action,
//!     ProgramState, Transaction, TransactionElement, BalanceAssertion,
//! };
//! use commodity::{CommodityType, Commodity};
//! use chrono::NaiveDate;
//! use std::rc::Rc;
//! use std::str::FromStr;
//!
//! // create a commodity from a currency's iso4317 alphanumeric code
//! let aud = Rc::from(CommodityType::from_currency_alpha3("AUD").unwrap());
//!
//! // Create a couple of accounts
//! let account1 = Rc::from(Account::new(Some("Account 1"), aud.id, None));
//! let account2 = Rc::from(Account::new(Some("Account 2"), aud.id, None));
//!
//! // create a new program state, with accounts starting Closed
//! let mut program_state = ProgramState::new(
//!     &vec![account1.clone(), account2.clone()],
//!     AccountStatus::Closed
//! );
//!
//! // open account1
//! let open_account1 = EditAccountStatus::new(
//!     account1.id,
//!     AccountStatus::Open,
//!     NaiveDate::from_str("2020-01-01").unwrap(),
//! );
//!
//! // open account2
//! let open_account2 = EditAccountStatus::new(
//!     account2.id,
//!     AccountStatus::Open,
//!     NaiveDate::from_str("2020-01-01").unwrap(),
//! );
//!
//! // create a transaction to transfer some commodity
//! // from account1 to account2.
//! let transaction1 = Transaction::new(
//!     Some(String::from("Transaction 1")),
//!     NaiveDate::from_str("2020-01-02").unwrap(),
//!     vec![
//!         TransactionElement::new(
//!             account1.id,
//!             Some(Commodity::from_str("-2.52 AUD").unwrap()),
//!             None,
//!         ),
//!         TransactionElement::new(
//!             account2.id,
//!             Some(Commodity::from_str("2.52 AUD").unwrap()),
//!             None,
//!         ),
//!     ],
//! );
//!
//! // create a balance assertion (that will cause the program to return an error
//! // if it fails), to check that the balance of account1 matches the expected
//! // value of -1.52 AUD at the start of the date of 2020-01-03
//! let balance_assertion1 = BalanceAssertion::new(
//!     account1.id,
//!     NaiveDate::from_str("2020-01-03").unwrap(),
//!     Commodity::from_str("-2.52 AUD").unwrap()
//! );
//!
//! // create another transaction to transfer commodity from
//! // account2 to account1, using the simpler syntax.
//! let transaction2 =  Transaction::new_simple(
//!    Some("Transaction 2"),
//!    NaiveDate::from_str("2020-01-03").unwrap(),
//!    account2.id,
//!    account1.id,
//!    Commodity::from_str("1.0 AUD").unwrap(),
//!    None,
//! );
//!
//! let balance_assertion2 = BalanceAssertion::new(
//!     account1.id,
//!     NaiveDate::from_str("2020-01-04").unwrap(),
//!     Commodity::from_str("-1.52 AUD").unwrap()
//! );
//!
//! let balance_assertion3 = BalanceAssertion::new(
//!     account2.id,
//!     NaiveDate::from_str("2020-01-04").unwrap(),
//!     Commodity::from_str("1.52 AUD").unwrap()
//! );
//!
//! let actions: Vec<Rc<dyn Action>> = vec![
//!     Rc::from(open_account1),
//!     Rc::from(open_account2),
//!     Rc::from(transaction1),
//!     Rc::from(balance_assertion1),
//!     Rc::from(transaction2),
//!     Rc::from(balance_assertion2),
//!     Rc::from(balance_assertion3),
//! ];
//!
//! // create a program from the actions
//! let program = Program::new(actions);
//!
//! // run the program
//! program_state.execute_program(&program).unwrap();
//! ```

extern crate arrayvec;
extern crate chrono;
extern crate commodity;
extern crate nanoid;
extern crate rust_decimal;
extern crate thiserror;

#[cfg(feature = "serde-support")]
extern crate serde;

#[cfg(test)]
#[cfg(feature = "serde-support")]
extern crate serde_json;

mod account;
mod actions;
mod error;
mod program;

pub use account::*;
pub use actions::*;
pub use error::AccountingError;
pub use program::*;

#[cfg(doctest)]
#[macro_use]
extern crate doc_comment;

#[cfg(doctest)]
doctest!("../README.md");

#[cfg(test)]
mod tests {
    use super::{
        sum_account_states, Account, AccountState, AccountStatus, Action, BalanceAssertion,
        EditAccountStatus, Program, ProgramState, Transaction, TransactionElement,
    };
    use chrono::NaiveDate;
    use commodity::{Commodity, CommodityType, CommodityTypeID};
    use std::rc::Rc;
    use std::str::FromStr;

    #[test]
    fn execute_program() {
        let aud = Rc::from(CommodityType::new(
            CommodityTypeID::from_str("AUD").unwrap(),
            None,
        ));
        let account1 = Rc::from(Account::new(Some("Account 1"), aud.id, None));
        let account2 = Rc::from(Account::new(Some("Account 2"), aud.id, None));

        let accounts = vec![account1.clone(), account2.clone()];

        let mut program_state = ProgramState::new(&accounts, AccountStatus::Closed);

        let open_account1 = EditAccountStatus::new(
            account1.id,
            AccountStatus::Open,
            NaiveDate::from_str("2020-01-01").unwrap(),
        );

        let open_account2 = EditAccountStatus::new(
            account2.id,
            AccountStatus::Open,
            NaiveDate::from_str("2020-01-01").unwrap(),
        );

        let transaction1 = Transaction::new(
            Some(String::from("Transaction 1")),
            NaiveDate::from_str("2020-01-02").unwrap(),
            vec![
                TransactionElement::new(
                    account1.id,
                    Some(Commodity::from_str("-2.52 AUD").unwrap()),
                    None,
                ),
                TransactionElement::new(
                    account2.id,
                    Some(Commodity::from_str("2.52 AUD").unwrap()),
                    None,
                ),
            ],
        );

        let transaction2 = Transaction::new(
            Some(String::from("Transaction 2")),
            NaiveDate::from_str("2020-01-02").unwrap(),
            vec![
                TransactionElement::new(
                    account1.id,
                    Some(Commodity::from_str("-1.0 AUD").unwrap()),
                    None,
                ),
                TransactionElement::new(account2.id, None, None),
            ],
        );

        let balance_assertion = BalanceAssertion::new(
            account1.id,
            NaiveDate::from_str("2020-01-03").unwrap(),
            Commodity::from_str("-3.52 AUD").unwrap(),
        );

        let actions: Vec<Rc<dyn Action>> = vec![
            Rc::from(open_account1),
            Rc::from(open_account2),
            Rc::from(transaction1),
            Rc::from(transaction2),
            Rc::from(balance_assertion),
        ];

        let program = Program::new(actions);

        let account1_state_before: AccountState = program_state
            .get_account_state(&account1.id)
            .unwrap()
            .clone();

        assert_eq!(AccountStatus::Closed, account1_state_before.status);

        program_state.execute_program(&program).unwrap();

        let account1_state_after: AccountState = program_state
            .get_account_state(&account1.id)
            .unwrap()
            .clone();

        assert_eq!(AccountStatus::Open, account1_state_after.status);
        assert_eq!(
            Commodity::from_str("-3.52 AUD").unwrap(),
            account1_state_after.amount
        );

        assert_eq!(
            Commodity::from_str("0.0 AUD").unwrap(),
            sum_account_states(
                &program_state.account_states,
                CommodityTypeID::from_str("AUD").unwrap(),
                None
            )
            .unwrap()
        );
    }
}