2018-03-02 06:20:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Traits;
|
|
|
|
|
|
|
|
use App\Models\Journal;
|
|
|
|
|
|
|
|
trait JournalTrait
|
|
|
|
{
|
2018-03-03 03:12:39 +08:00
|
|
|
/**
|
|
|
|
* Initialize a new journal when a new record is created
|
|
|
|
*/
|
|
|
|
public static function bootJournalTrait()
|
|
|
|
{
|
|
|
|
static::created(function ($model) {
|
|
|
|
$model->initJournal(config('phpvms.currency'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
/**
|
|
|
|
* Morph to Journal.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function journal()
|
|
|
|
{
|
|
|
|
return $this->morphOne(Journal::class, 'morphed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a journal for a given model object
|
|
|
|
*
|
|
|
|
* @param string $currency_code
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return Journal
|
2018-03-02 06:20:13 +08:00
|
|
|
*/
|
|
|
|
public function initJournal($currency_code = 'USD')
|
|
|
|
{
|
|
|
|
if (!$this->journal) {
|
|
|
|
$journal = new Journal();
|
2018-03-06 09:55:48 +08:00
|
|
|
$journal->type = $this->journal_type;
|
2018-03-02 06:20:13 +08:00
|
|
|
$journal->currency = $currency_code;
|
|
|
|
$journal->balance = 0;
|
|
|
|
$this->journal()->save($journal);
|
|
|
|
|
|
|
|
$journal->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|