1.1.11 • Published 1 year ago

balancebookjs v1.1.11

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

BalanceBookJS

BalanceBookJS is a JavaScript library that provides an object-oriented approach based on fundamental accounting principles.

Installation

npm install balancebookjs

Usage

Account Class

The Account class is the foundational class in BalanceBookJS that represents a general accounting account.

Constructor

new Account(name, initialBalance, isDebitPositive)

  • name: String - the name of the account.
  • initialBalance: Number - the starting balance of the account.
  • isDebitPositive: Boolean - determines if debits increase (true) or decrease (false) the account balance.

Debit Method

account.debit(amount)

  • amount: Number - the amount to be debited from the account. The effect on the balance is determined by the isDebitPositive flag.

Credit Method

account.credit(amount)

  • amount: Number - the amount to be credited to the account. The effect on the balance is determined by the isDebitPositive flag.

GetBalance Method

account.getBalance()

  • Returns the current balance of the account as a number.

Examples

Creating and using an account:

const checkingAccount = new Account('Checking Account', 1000, true);

// Debiting an amount:
checkingAccount.debit(200);
console.log(checkingAccount.getBalance()); // 1200 if isDebitPositive is true

// Crediting an amount:
checkingAccount.credit(100);
console.log(checkingAccount.getBalance()); // 1100 if isDebitPositive is true

Asset Class

The Asset class extends the Account class and is tailored specifically for managing asset accounts. In accounting terms, asset accounts increase with debits and decrease with credits, which is reflected in the behavior of this class.

Constructor

new Asset(name, initialBalance)

  • name: String - The name of the asset account.
  • initialBalance: Number - The starting balance of the asset account, typically represented in the smallest unit of your currency (e.g., cents, pence).

Methods

Inherits all methods from the Account class, with specific functionality adjusted for asset accounts:

  • debit(amount): Increases the balance of the asset account by the specified amount.
  • credit(amount): Decreases the balance of the asset account by the specified amount.
  • getBalance(): Returns the current balance of the asset account.

Usage Example

Here is a simple example demonstrating the creation of an asset account and how to perform transactions like debits and credits:

const Asset = require('./path/to/Asset');

// Creating an asset account with an initial balance
const officeEquipment = new Asset('Office Equipment', 5000);

// Increasing the balance by debiting the account
officeEquipment.debit(1500);
console.log('Post-debit Balance:', officeEquipment.getBalance()); // Outputs: 6500

// Decreasing the balance by crediting the account
officeEquipment.credit(2000);
console.log('Post-credit Balance:', officeEquipment.getBalance()); // Outputs: 4500

Liability Class

The Liability class extends the Account class and is specifically designed for managing liability accounts. In accounting, liabilities increase with credits and decrease with debits.

Constructor

new Liability(name, initialBalance)

  • name: String - The name of the liability account.
  • initialBalance: Number - The initial balance of the liability account, typically represented in the smallest unit of your currency (e.g., cents, pence).

Methods

Inherits all methods from the Account class, but adapts them to the specific behavior of liability accounts:

  • debit(amount): Decreases the balance of the liability account by the specified amount.
  • credit(amount): Increases the balance of the liability account by the specified amount.
  • getBalance(): Returns the current balance of the liability account.

Usage Example

Here is an example of how to use the Liability class to manage liability accounts:

const Liability = require('./path/to/Liability');

// Creating a liability account with an initial balance
const loansPayable = new Liability('Loans Payable', 10000);

// Decreasing the balance by debiting the account
loansPayable.debit(500);
console.log('Post-debit Balance:', loansPayable.getBalance()); // Outputs: 9500

// Increasing the balance by crediting the account
loansPayable.credit(1500);
console.log('Post-credit Balance:', loansPayable.getBalance()); // Outputs: 11000

Equity Class

The Equity class extends the Account class and is designed for managing equity accounts. Equity accounts are affected by owner's contributions, profits, losses, and withdrawals. In accounting, equity increases with credits and decreases with debits.

Constructor

new Equity(name, initialBalance)

  • name: String - The name of the equity account.
  • initialBalance: Number - The initial balance of the equity account, typically represented in the smallest unit of your currency (e.g., cents, pence).

Methods

Inherits all methods from the Account class, with specific functionality adjusted for equity accounts:

  • debit(amount): Decreases the balance of the equity account by the specified amount.
  • credit(amount): Increases the balance of the equity account by the specified amount.
  • getBalance(): Returns the current balance of the equity account.

Usage Example

Here is an example of how to use the Equity class to manage equity accounts:

const Equity = require('./path/to/Equity');

// Creating an equity account with an initial balance
const retainedEarnings = new Equity('Retained Earnings', 20000);

// Decreasing the balance by debiting the account
retainedEarnings.debit(5000);
console.log('Post-debit Balance:', retainedEarnings.getBalance()); // Outputs: 15000

// Increasing the balance by crediting the account
retainedEarnings.credit(8000);
console.log('Post-credit Balance:', retainedEarnings.getBalance()); // Outputs: 23000

Income Class

The Income class extends the Account class and is designed for managing income accounts. In accounting, income accounts increase with credits and decrease with debits.

Constructor

new Income(name, initialBalance)

  • name: String - The name of the income account.
  • initialBalance: Number - The initial balance of the income account, often starts at 0 since income is typically not carried over year to year.

Methods

Inherits all methods from the Account class, with specific functionality adjusted for income accounts:

  • debit(amount): Decreases the balance of the income account by the specified amount.
  • credit(amount): Increases the balance of the income account by the specified amount.
  • getBalance(): Returns the current balance of the income account.

Usage Example

Here is an example of how to use the Income class to manage income transactions:

const Income = require('./path/to/Income');

// Creating an income account
const salesRevenue = new Income('Sales Revenue');

// Increasing the balance by crediting the account
salesRevenue.credit(2000);
console.log('Post-credit Balance:', salesRevenue.getBalance()); // Outputs: 2000

// Decreasing the balance by debiting the account
salesRevenue.debit(500);
console.log('Post-debit Balance:', salesRevenue.getBalance()); // Outputs: 1500

Expense Class

The Expense class extends the Account class and is designed for managing expense accounts. In accounting, expense accounts increase with debits and decrease with credits.

Constructor

new Expense(name, initialBalance)

  • name: String - The name of the expense account.
  • initialBalance: Number - The initial balance of the expense account, often starts at 0 since expenses are typically recorded as they occur.

Methods

Inherits all methods from the Account class, with specific functionality adjusted for expense accounts:

  • debit(amount): Increases the balance of the expense account by the specified amount.
  • credit(amount): Decreases the balance of the expense account by the specified amount.
  • getBalance(): Returns the current balance of the expense account.

Usage Example

Here is an example of how to use the Expense class to manage expense transactions:

const Expense = require('./path/to/Expense');

// Creating an expense account
const travelExpenses = new Expense('Travel Expenses');

// Increasing the balance by debiting the account
travelExpenses.debit(300);
console.log('Post-debit Balance:', travelExpenses.getBalance()); // Outputs: 300

// Decreasing the balance by crediting the account
travelExpenses.credit(100);
console.log('Post-credit Balance:', travelExpenses.getBalance()); // Outputs: 200

JournalEntry Class

The JournalEntry class is designed to handle the recording of accounting transactions in the form of journal entries. Each journal entry involves multiple transactions that affect different accounts in specific ways, either as debits or credits.

Constructor

new JournalEntry(description, date = new Date())

  • description: String - Description of the journal entry.
  • date: Date - Date of the journal entry, defaults to the current date if not provided.

Methods

addEntry

addEntry(account, amount, type)

  • account: Account - The account affected by the transaction. This should be an instance of an Account subclass.
  • amount: Number - The monetary amount of the transaction.
  • type: String - The type of the transaction, either 'debit' or 'credit'. It ensures that the operation corresponds to the correct method on the account.

This method records a planned transaction as part of the journal entry. It does not perform the transaction immediately but queues it for execution upon the commit of the entire entry.

commit

commit()

This method processes all queued transactions in the journal entry, applying them to their respective accounts only if the total debits equal the total credits. This ensures the accounting balance is maintained. If the totals do not match, it throws an error, preventing the entry from being committed.

getDetails

getDetails()

  • Returns: Array of Objects - A list of all transactions recorded in this journal entry with their details including account name, amount, type, and the entry's date and description.

Usage Example

Here is an example of how to create a journal entry, record transactions, and commit them:

const Account = require('./path/to/Account');
const Asset = require('./path/to/Asset');
const Expense = require('./path/to/Expense');

// Create accounts
let cash = new Asset('Cash', 1000);
let rentExpense = new Expense('Rent', 0);

// Create a journal entry
let entry = new JournalEntry("Payment of rent");
entry.addEntry(cash, 500, 'credit');
entry.addEntry(rentExpense, 500, 'debit');

// Commit the entry
try {
    entry.commit();
    console.log('Journal entry committed successfully.');
} catch (error) {
    console.error('Failed to commit journal entry:', error.message);
}
1.1.1

1 year ago

1.1.0

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.0.0

1 year ago