phpvms/app/Models/JournalTransaction.php

137 lines
3.3 KiB
PHP
Raw Normal View History

2018-03-01 08:01:32 +08:00
<?php
/**
* Based on https://github.com/scottlaurent/accounting
* With modifications for phpVMS
*/
namespace App\Models;
/**
2018-03-02 12:00:11 +08:00
* @property string currency
* @property string memo
* @property string transaction_group
* @property string post_date
2018-03-01 08:01:32 +08:00
* @property integer credit
* @property integer debit
2018-03-02 12:00:11 +08:00
* @property string ref_class
* @property integer ref_class_id
2018-03-01 08:01:32 +08:00
*/
class JournalTransaction extends BaseModel
{
protected $table = 'journal_transactions';
public $incrementing = false;
public $fillable = [
'transaction_group',
'journal_id',
'credit',
'debit',
2018-03-01 08:01:32 +08:00
'currency',
'memo',
'tags',
'ref_class',
'ref_class_id',
'post_date'
];
protected $casts = [
'credits' => 'integer',
'debit' => 'integer',
2018-03-01 08:01:32 +08:00
'post_date' => 'datetime',
'tags' => 'array',
];
/**
2018-03-02 12:00:11 +08:00
* Callbacks
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-03-01 08:01:32 +08:00
*/
protected static function boot()
{
static::creating(function ($transaction) {
2018-03-02 12:00:11 +08:00
if(!$transaction->id) {
$transaction->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
}
2018-03-01 08:01:32 +08:00
});
2018-03-02 12:00:11 +08:00
/**
* Adjust the balance according to credits and debits
*/
2018-03-01 08:01:32 +08:00
static::saved(function ($transaction) {
2018-03-02 12:00:11 +08:00
//$transaction->journal->resetCurrentBalances();
$journal = $transaction->journal;
if($transaction['credit']) {
$balance = $journal->balance->toAmount();
$journal->balance = $balance + $transaction['credit'];
}
if($transaction['debit']) {
$balance = $journal->balance->toAmount();
$journal->balance = $balance - $transaction['debit'];
}
$journal->save();
2018-03-01 08:01:32 +08:00
});
2018-03-02 12:00:11 +08:00
/**
* Deleting a transaction reverses the credits and debits
*/
2018-03-01 08:01:32 +08:00
static::deleted(function ($transaction) {
2018-03-02 12:00:11 +08:00
$journal = $transaction->journal;
if ($transaction['credit']) {
$balance = $journal->balance->toAmount();
$journal->balance = $balance - $transaction['credit'];
}
if ($transaction['debit']) {
$balance = $journal->balance->toAmount();
$journal->balance = $balance + $transaction['debit'];
}
$journal->save();
2018-03-01 08:01:32 +08:00
});
parent::boot();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function journal()
{
return $this->belongsTo(Journal::class);
}
/**
2018-03-02 12:00:11 +08:00
* @param BaseModel $object
2018-03-01 08:01:32 +08:00
* @return JournalTransaction
*/
public function referencesObject($object)
{
$this->ref_class = \get_class($object);
$this->ref_class_id = $object->id;
$this->save();
return $this;
}
/**
*
*/
public function getReferencedObject()
{
if ($classname = $this->ref_class) {
2018-03-02 12:00:11 +08:00
$klass = new $this->ref_class;
return $klass->find($this->ref_class_id);
2018-03-01 08:01:32 +08:00
}
return false;
}
/**
* @param string $currency
*/
public function setCurrency($currency)
{
$this->currency = $currency;
}
}