2018-03-01 08:01:32 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Based on https://github.com/scottlaurent/accounting
|
|
|
|
* With modifications for phpVMS
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2018-03-01 08:01:32 +08:00
|
|
|
use App\Support\Money;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Journal
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property Money $balance
|
2018-03-01 08:01:32 +08:00
|
|
|
* @property string $currency
|
|
|
|
* @property Carbon $updated_at
|
|
|
|
* @property Carbon $post_date
|
|
|
|
* @property Carbon $created_at
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Ledger extends Model
|
2018-03-01 08:01:32 +08:00
|
|
|
{
|
2018-03-21 08:40:19 +08:00
|
|
|
public $table = 'ledgers';
|
2018-03-01 08:01:32 +08:00
|
|
|
|
|
|
|
public function journals()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Journal::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the posts for the country.
|
|
|
|
*/
|
|
|
|
public function journal_transctions()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(JournalTransaction::class, Journal::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function getCurrentBalance(): Money
|
|
|
|
{
|
|
|
|
if ($this->type === 'asset' || $this->type === 'expense') {
|
|
|
|
$balance = $this->journal_transctions->sum('debit') - $this->journal_transctions->sum('credit');
|
|
|
|
} else {
|
|
|
|
$balance = $this->journal_transctions->sum('credit') - $this->journal_transctions->sum('debit');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Money($balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function getCurrentBalanceInDollars()
|
|
|
|
{
|
|
|
|
return $this->getCurrentBalance()->getValue();
|
|
|
|
}
|
|
|
|
}
|