Skip to content

imetaxas/double-entry-bookkeeping-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov Codacy Badge Maven Central PRs Welcome License: MIT

Double Entry Bookkeeping API

A library of the Double-entry bookkeeping concept which is downloadable from the Central Repository.

It uses Spring 5 and the Java Transaction API internally.

It supports H2, HSQL and Derby databases in embedded mode.

And H2, MySQL and Postgres using JDBC.

Description

Double-entry bookkeeping involves making at least two entries or legs for every transaction. A debit in one account and a corresponding credit in another account. The sum of all debits should always equal the sum of all credits, providing a simple way to check for errors. The following rules MUST apply:

  • An account MUST NOT be overdrawn, i.e. have a negative balance.
  • A monetary transaction MAY support multiple currencies as long as the total balance for the transaction legs with the same currency is zero.
  • The concepts of debit and credit are simplified by specifying that monetary transactions towards an account can have either a positive or negative value.

API

Using embedded H2

ChartOfAccounts chartOfAccounts = new ChartOfAccountsBuilder()
        .create("CASH_ACCOUNT_1", "1000.00", "EUR")
        .create("REVENUE_ACCOUNT_1", "0.00", "EUR")
        .build();

Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("My Embedded H2 ledger")
        .build()
        .init();

Using Embedded HSQL

ChartOfAccounts chartOfAccounts = new ChartOfAccountsBuilder()
        .create("CASH_ACCOUNT_1", "1000.00", "EUR")
        .create("REVENUE_ACCOUNT_1", "0.00", "EUR")
        .build();

Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("My Embedded HSQL ledger")
        .options(ConnectionOptions.EMBEDDED_HSQL_CONNECTION)
        .build()
        .init();

Using Embedded Derby

ChartOfAccounts chartOfAccounts = new ChartOfAccountsBuilder()
        .create("CASH_ACCOUNT_1", "1000.00", "EUR")
        .create("REVENUE_ACCOUNT_1", "0.00", "EUR")
        .build();

Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("My Embedded Derby ledger")
        .options(ConnectionOptions.EMBEDDED_DERBY_CONNECTION)
        .build()
        .init();

Using JDBC H2

ConnectionOptions options = new ConnectionOptions(DataSourceDriver.JDBC_H2)
        .url("URL")
        .username("USERNAME")
        .password("PASSWORD");
        
Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("My JDBC H2 ledger")
        .options(options)
        .build()
        .init();

Using JDBC MySQL

ConnectionOptions options = new ConnectionOptions(DataSourceDriver.JDBC_MYSQL)
          .url("URL")
          .username("USERNAME")
          .password("PASSWORD");

Ledger ledger = new LedgerBuilder(chartOfAccounts)
          .name("My JDBC MYSQL ledger")
          .options(options)
          .build()
          .init();

Using JDBC Postgres

ConnectionOptions options = new ConnectionOptions(DataSourceDriver.JDBC_POSTGRES)
        .url("URL")
        .username("USERNAME")
        .password("PASSWORD");

Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("My JDBC POSTGRES ledger")
        .options(options)
        .build()
        .init();

Create new accounts and include already created ones in the ledger

ChartOfAccounts chartOfAccounts = new ChartOfAccountsBuilder()
          .create("CASH_ACCOUNT_1", "1000.00", "EUR")
          .create("REVENUE_ACCOUNT_1", "0.00", "EUR")
          .includeExisted("ACCOUNT_1")
          .includeExisted("ACCOUNT_2")
          .build();
          
Ledger ledger = new LedgerBuilder(chartOfAccounts)
        .name("Ledger with both new and already created accounts")
        .options(options)
        .build()
        .init();

Commit Transfer Requests

TransferRequest transferRequest1 = ledger.createTransferRequest()
    .reference("T1")
    .type("testing1")
    .account("CASH_ACCOUNT_1").debit("5.00", "EUR")
    .account("REVENUE_ACCOUNT_1").credit("5.00", "EUR")
    .build();
    
ledger.commit(transferRequest1);
  
TransferRequest transferRequest2 = ledger.createTransferRequest()
    .reference("T2")
    .type("testing2")
    .account("CASH_ACCOUNT_1").debit("10.50", "EUR")
    .account("REVENUE_ACCOUNT_1").credit("10.50", "EUR")
    .build();
  
ledger.commit(transferRequest2);

Search the Ledger for committed Transactions

List<Transaction> cashAccountTransactionList = ledger.findTransactions("CASH_ACCOUNT_1");
List<Transaction> revenueAccountTransactionList = ledger.findTransactions("REVENUE_ACCOUNT_1");

Transaction transaction1 = ledger.getTransactionByRef("T1");
Transaction transaction2 = ledger.getTransactionByRef("T2");

Print the Ledger's history of operations

ledger.printHistoryLog();

Build

mvn package

Contributing

If you would like to help making this project better, see the CONTRIBUTING.md.

Maintainers

Send any other comments and suggestions to Yani Metaxas.

License

This project is distributed under the MIT License.