Skip to content

Commit

Permalink
Merge pull request #7 from adriangoris/master
Browse files Browse the repository at this point in the history
Adds fields currency fields, convert currency fields from strings to floats, modified the balance calculator
  • Loading branch information
mpaannddreew authored Mar 20, 2019
2 parents 1f1c417 + a3c8e1e commit d841404
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLedgerEntriesTable extends Migration
{
/**
Expand All @@ -21,12 +19,13 @@ public function up()
$table->text('reason');
$table->boolean('credit')->default(0);
$table->boolean('debit')->default(0);
$table->string('amount');
$table->string('current_balance');
$table->float('amount', 8, 2);
$table->string('amount_currency')->nullable();
$table->float('current_balance', 8, 2);
$table->string('current_balance_currency')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
Expand All @@ -36,4 +35,4 @@ public function down()
{
Schema::dropIfExists('ledger_entries');
}
}
}
90 changes: 35 additions & 55 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,137 +8,117 @@ This is a simple/basic implementation of a ledger in laravel 5
- BALANCE COMPUTATION

## Installation
git clone https://github.com/mpaannddreew/laravel-ledger.git

#### Composer
Above installation can also be simplify by using the following command:

```php
composer require fannypack/ledger
```

#### Service Provider and Facade

Add `FannyPack\Ledger\LedgerServiceProvider::class` to your application service providers in `config/app.php` file:

Register service provider
```php
FannyPack\Ledger\LedgerServiceProvider::class,
```

You can use the facade for shorter code. Add this to your aliases:

Register Facade
Register service provider
```php
'Ledger' => FannyPack\Ledger\Facades\Ledger::class,
```
#### Migrations

Finally, you'll also need to run migration on the package:
```
php artisan vendor:publish
```

#### Vue
Optionally, this command will copy the library's vue components into your codebase. You can publish with this command:
After the service provider is registered run this command
```
php artisan vendor:publish --tag=ledger
```

Using the provided `Ledger.vue` component in your blade templates
```php
<ledger></ledger>
Run migrations
```
php artisan migrate
```
This command will copy the library's vue components into your codebase

Register package routes in your app's `RouteServiceProvider` boot method
Register package routes in your app's RouteServiceProvider
```
public function boot() {
Ledger::routes();
}
Ledger::routes();
```

## Usage

Add `Ledgerable` trait to your model. See the following example:

Using it with your models, add
```php
namespace App;

use FannyPack\Ledger\Traits\Ledgerable;
use Illuminate\Database\Eloquent\Model;

class Account extends Model {
class Account extends Model
{
use Ledgerable;
}
```

#### Show available balance
Show available balance
```php
$account = Account::find(1);
$balance = Ledger::balance($account);

// or

```
or
```php
$account = Account::find(1);
$balance = $account->balance();
```
#### Record a credit entry
Record a credit entry
```php
$account = Account::find(1);
Ledger::credit($account, $to, $amount, $reason);

// or

```
or
```php
$account = Account::find(1);
$account->credit($to, $amount, $reason);
```
#### Record a debit entry
Record a debit entry
```php
$account = Account::find(1);
Ledger::debit($account, $from, $amount, $reason);

// or

```
or
```php
$account = Account::find(1);
$account->debit($from, $amount, $reason);
```

#### Recording debits and credits in one transaction
Recording debits and credits in one transaction
```php
$account = Account::find(1);
$account2 = Account::find(2);
$account3 = Account::find(3);
Ledger::transfer($account, [$account2, $account3], $amount, $reason);

// or

Ledger::transfer($account, $account2, $amount, $reason);
Ledger::transfer($account, $account3, $amount, $reason);
```

or
```php
$account = Account::find(1);
$account2 = Account::find(2);
$account3 = Account::find(3);
$account->transfer([$account2, $account3], $amount, $reason);

// or

$account->transfer($account2, $amount, $reason);
$account->transfer($account3, $amount, $reason);
```
#### Retrieving all entries of a ledgerable
Retrieving all entries of a ledgerable
```php
$account = Account::find(1);
$entries = $account->entries();
```
#### Retrieving all debits of a ledgerable
Retrieving all debits of a ledgerable
```php
$account = Account::find(1);
debits = $account->debits();
```
#### Retrieving all credits of a ledgerable
Retrieving all credits of a ledgerable
```php
$account = Account::find(1);
debits = $account->credits();
```
Using the provided Ledger.vue component in your blade templates
```php
<ledger></ledger>
```

## Bugs
For any bugs found, please email me at [email protected] or register an issue at [issues](https://github.com/mpaannddreew/laravel-ledger/issues)
30 changes: 23 additions & 7 deletions src/Ledger.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ public function __construct(Router $router)
* @param $reason
* @return mixed
*/
public function debit($to, $from, $amount, $reason)
public function debit($to, $from, $amount, $amount_currency, $reason)
{
$balance = $to->balance();
$current_balance_currency = isset($to->current_balance_currency) ? $to->current_balance_currency : Null;

$data = [
'money_from' => $from,'debit' => 1, 'reason' => $reason, 'amount' => $amount, 'current_balance' => (int)$balance + (int)$amount
'money_from' => $from,
'debit' => 1,
'reason' => $reason,
'amount' => $amount,
'amount_currency' => $amount_currency,
'current_balance' => (float)$balance + (float)$amount,
'current_balance_currency' => $current_balance_currency
];

return $this->log($to, $data);
Expand All @@ -58,14 +66,22 @@ public function debit($to, $from, $amount, $reason)
* @return mixed
* @throws InsufficientBalanceException
*/
public function credit($from, $to, $amount, $reason)
public function credit($from, $to, $amount, $amount_currency, $reason)
{
$balance = $from->balance();
if ((int)$balance == 0 || (int)$amount > (int)$balance )
$current_balance_currency = isset($from->current_balance_currency) ? $from->current_balance_currency : Null;

if ((float)$balance == 0 || (float)$amount > (float)$balance )
throw new InsufficientBalanceException("Insufficient balance");

$data = [
'money_to' => $to,'credit' => 1, 'reason' => $reason, 'amount' => $amount, 'current_balance' => (int)$balance - (int)$amount
'money_to' => $to,
'credit' => 1,
'reason' => $reason,
'amount' => $amount,
'amount_currency' => $amount_currency,
'current_balance' => (float)$balance - (float)$amount,
'current_balance_currency' => $current_balance_currency
];

return $this->log($from, $data);
Expand All @@ -87,7 +103,7 @@ protected function log($ledgerable, array $data)
* balance of a ledgerable instance
*
* @param $ledgerable
* @return int
* @return float
*/
public function balance($ledgerable)
{
Expand All @@ -111,7 +127,7 @@ public function transfer($from, $to, $amount, $reason = "funds transfer")
if (!is_array($to))
return $this->transferOnce($from, $to, $amount, $reason);

$total_amount = (int)$amount * count($to);
$total_amount = (float)$amount * count($to);
if ($total_amount > $from->balance())
throw new InsufficientBalanceException("Insufficient balance");

Expand Down
2 changes: 1 addition & 1 deletion src/LedgerEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LedgerEntry extends Model
/**
* @var array
*/
protected $fillable = ['reason', 'debit', 'credit', 'amount', 'current_balance', 'money_to', 'money_from'];
protected $fillable = ['reason', 'debit', 'credit', 'amount_currency', 'amount', 'current_balance_currency', 'current_balance', 'money_to', 'money_from'];

protected $hidden = ['ledgerable_id', 'ledgerable_type', 'current_balance', 'updated_at'];

Expand Down
13 changes: 8 additions & 5 deletions src/Traits/Ledgerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function credits()
* @param $reason
* @return mixed
*/
public function debit($from, $amount, $reason)
public function debit($from, $amount, $amount_currency, $reason)
{
return Ledger::debit($this, $from, $amount, $reason);
return Ledger::debit($this, $from, $amount, $amount_currency, $reason);
}

/**
Expand All @@ -65,9 +65,9 @@ public function debit($from, $amount, $reason)
* @param $reason
* @return mixed
*/
public function credit($to, $amount, $reason)
public function credit($to, $amount, $amount_currency, $reason)
{
return Ledger::credit($this, $to, $amount, $reason);
return Ledger::credit($this, $to, $amount, $amount_currency, $reason);
}

/**
Expand All @@ -77,7 +77,10 @@ public function credit($to, $amount, $reason)
*/
public function balance()
{
return Ledger::balance($this);
$credits = $this->credits()->sum('amount');
$debits = $this->debits()->sum('amount');
$balance = $debits - $credits;
return $balance;
}

/**
Expand Down

0 comments on commit d841404

Please sign in to comment.