Skip to content

Commit

Permalink
fixed ledger bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaannddreew committed Sep 5, 2019
1 parent eeb1fea commit 5bc25bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/Ledger.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function debit($to, $from, $amount, $amount_currency, $reason)
* @return mixed
* @throws InsufficientBalanceException
*/
public function credit($from, $to, $amount, $amount_currency, $reason)
public function credit($from, $to, $amount, $amount_currency="UGX", $reason)
{
$balance = $from->balance();
$current_balance_currency = isset($from->current_balance_currency) ? $from->current_balance_currency : Null;
Expand Down Expand Up @@ -107,8 +107,10 @@ protected function log($ledgerable, array $data)
*/
public function balance($ledgerable)
{
$entry = $ledgerable->entries()->first();
return $entry ? $entry->current_balance : 0;
$credits = $ledgerable->credits()->sum('amount');
$debits = $ledgerable->debits()->sum('amount');
$balance = $debits - $credits;
return $balance;
}

/**
Expand All @@ -122,7 +124,7 @@ public function balance($ledgerable)
* @throws InvalidRecipientException
* @throws InsufficientBalanceException
*/
public function transfer($from, $to, $amount, $reason = "funds transfer")
public function transfer($from, $to, $amount, $amount_currency="UGX", $reason = "funds transfer")
{
if (!is_array($to))
return $this->transferOnce($from, $to, $amount, $reason);
Expand All @@ -134,7 +136,7 @@ public function transfer($from, $to, $amount, $reason = "funds transfer")
$recipients = [];
foreach ($to as $recipient)
{
array_push($recipients, $this->transferOnce($from, $recipient, $amount, $reason));
array_push($recipients, $this->transferOnce($from, $recipient, $amount, $amount_currency, $reason));
}

return $recipients;
Expand All @@ -151,13 +153,13 @@ public function transfer($from, $to, $amount, $reason = "funds transfer")
* @throws InsufficientBalanceException
* @throws InvalidRecipientException
*/
protected function transferOnce($from, $to, $amount, $reason)
protected function transferOnce($from, $to, $amount, $amount_currency="UGX", $reason)
{
if (get_class($from) == get_class($to) && $from->id == $to->id)
throw new InvalidRecipientException("Source and recipient cannot be the same object");

$this->credit($from, $to->name, $amount, $reason);
return $this->debit($to, $from->name, $amount, $reason);
$this->credit($from, $to->name, $amount, $amount_currency ,$reason);
return $this->debit($to, $from->name, $amount, $amount_currency, $reason);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Traits/Ledgerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function credits()
* @param $reason
* @return mixed
*/
public function debit($from, $amount, $amount_currency, $reason)
public function debit($from, $amount, $amount_currency="UGX", $reason)
{
return Ledger::debit($this, $from, $amount, $amount_currency, $reason);
}
Expand All @@ -65,7 +65,7 @@ public function debit($from, $amount, $amount_currency, $reason)
* @param $reason
* @return mixed
*/
public function credit($to, $amount, $amount_currency, $reason)
public function credit($to, $amount, $amount_currency="UGX", $reason)
{
return Ledger::credit($this, $to, $amount, $amount_currency, $reason);
}
Expand All @@ -77,10 +77,7 @@ public function credit($to, $amount, $amount_currency, $reason)
*/
public function balance()
{
$credits = $this->credits()->sum('amount');
$debits = $this->debits()->sum('amount');
$balance = $debits - $credits;
return $balance;
return Ledger::balance($this);
}

/**
Expand All @@ -91,8 +88,8 @@ public function balance()
* @param $reason
* @return mixed
*/
public function transfer($to, $amount, $reason)
public function transfer($to, $amount, $amount_currency="UGX", $reason)
{
return Ledger::transfer($this, $to, $amount, $reason);
return Ledger::transfer($this, $to, $amount, $amount_currency, $reason);
}
}

0 comments on commit 5bc25bb

Please sign in to comment.