diff --git a/src/Ledger.php b/src/Ledger.php index d14a4b0..b5d98ed 100644 --- a/src/Ledger.php +++ b/src/Ledger.php @@ -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; @@ -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; } /** @@ -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); @@ -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; @@ -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); } /** diff --git a/src/Traits/Ledgerable.php b/src/Traits/Ledgerable.php index d3588a5..8e7eef7 100644 --- a/src/Traits/Ledgerable.php +++ b/src/Traits/Ledgerable.php @@ -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); } @@ -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); } @@ -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); } /** @@ -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); } } \ No newline at end of file