[][src]Crate doublecount

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::{Currency, Commodity};
use chrono::NaiveDate;
use std::rc::Rc;
use std::str::FromStr;
 
// create a currency from its iso4317 alphanumeric code
let aud = Rc::from(Currency::from_alpha3("AUD").unwrap());
 
// Create a couple of accounts
let account1 = Rc::from(Account::new(Some("Account 1"), aud.clone(), None));
let account2 = Rc::from(Account::new(Some("Account 2"), aud.clone(), 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.clone(),
    AccountStatus::Open,
    NaiveDate::from_str("2020-01-01").unwrap(),
);
 
// open account2
let open_account2 = EditAccountStatus::new(
    account2.clone(),
    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.clone(),
            Some(Commodity::from_str("-2.52 AUD").unwrap()),
            None,
        ),
        TransactionElement::new(
            account2.clone(),
            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.clone(),
    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.clone(),
   account1.clone(),
   Commodity::from_str("1.0 AUD").unwrap(),
   None,
);
 
let balance_assertion2 = BalanceAssertion::new(
    account1.clone(),
    NaiveDate::from_str("2020-01-04").unwrap(),
    Commodity::from_str("-1.52 AUD").unwrap()
);
 
let balance_assertion3 = BalanceAssertion::new(
    account2.clone(),
    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();

Structs

Account

Details for an account, which holds a Commodity with a type of Currency.

AccountCategory

A way to categorize Accounts.

AccountState

Mutable state associated with an Account.

ActionOrder

A way to sort Actions by their date, and then by the priority of their ActionType.

BalanceAssertion

A type of Action to check and assert the balance of a given Account in its AccountStatus at the beginning of the given date.

EditAccountStatus

A type of Action to edit the AccountStatus of a given Account's AccountState.

FailedBalanceAssertion

Records the failure of a BalanceAssertion when it is evaluated using its implementation of the Action::perform() method.

Program

A collection of Actions to be executed in order to mutate some ProgramState.

ProgramState

The state of a Program being executed.

Transaction

A movement of Commodity between two or more accounts on a given date. Implements Action so it can be applied to change AccountStates.

TransactionElement

An element of a Transaction.

Enums

AccountStatus

The status of an Account stored within an AccountState.

AccountingError

An error associated with functionality in the accounting module.

ActionType

A representation of what type of Action is being performed.

Traits

Action

Represents an action which can modify ProgramState.

Functions

sum_account_states

Sum the values in all the accounts into a single Commodity, and use the supplied exchange rate if required to convert a currency in an account to the sum_currency.

Type Definitions

AccountID

The type to use for the id of Accounts.