Giter VIP home page Giter VIP logo

laravel-ledger's Introduction

Laravel Ledger

Simple package to keep a track of monetary transactions.

Usage

First off you'll need to add the Accountable trait to the Eloquent models you want to start tracking transactions for.

use Ledger\Accountable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, Accountable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Creating an account

An account is simply an entity with a balance, All mutations will be performed on these accounts. A single accountable can have multiple accounts. You can create an account for a singular user like this:

User::find(1)->createAccount('groceries', 'Day-to-day groceries');

The first parameter singifies the name of the account, this must be unique in the scope of the Accountable as you'll be using it as an identifier. The second parameter is simply a description.

Creating a transaction

There are several types of transactions, simple withdrawals to transfers. Transactions are simply a way of grouping mutations. This way you can set up a transaction that operates on multiple accounts easily.

Simple credit/debit

We just want to take/add some money from/to an account.

use Ledger\TransactionFactory;

// 5 euro
$amount  = new Money::EUR(500);
$account = User::find(1)->account('groceries');
TransactionFactory::debit($account, $amount, 'bought some vegetables');

Transfers

In some cases, we want to take money from one account, and put it in a different account (be it paying for something or allocating savings).

use Ledger\TransactionFactory;

$amount = new Money::EUR(500);
$from   = User::find(1)->account('salary');
$to     = User::find(1)->account('car');
TransactionFactory::transfer(
    $from,
    $to,
    $amount,
    'monthly savings'
);

Fetching account balance

An account is not very useful without being able to query its balance. The balance itself is simply the sum of all credit mutations with the sum of the debit transactions subtracted.

User::find(1)->account('groceries')->balance();

laravel-ledger's People

Contributors

ikidnapmyself avatar vswarte avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.