Update balance in callbacks
This commit is contained in:
@@ -7,14 +7,14 @@
|
||||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* @property string ref_class
|
||||
* @property string ref_class_id
|
||||
* @property string currency
|
||||
* @property string memo
|
||||
* @property string transaction_group
|
||||
* @property static post_date
|
||||
* @property string currency
|
||||
* @property string memo
|
||||
* @property string transaction_group
|
||||
* @property string post_date
|
||||
* @property integer credit
|
||||
* @property integer debit
|
||||
* @property string ref_class
|
||||
* @property integer ref_class_id
|
||||
*/
|
||||
class JournalTransaction extends BaseModel
|
||||
{
|
||||
@@ -42,20 +42,53 @@ class JournalTransaction extends BaseModel
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* Callbacks
|
||||
* @throws \UnexpectedValueException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
static::creating(function ($transaction) {
|
||||
$transaction->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
if(!$transaction->id) {
|
||||
$transaction->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Adjust the balance according to credits and debits
|
||||
*/
|
||||
static::saved(function ($transaction) {
|
||||
$transaction->journal->resetCurrentBalances();
|
||||
//$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();
|
||||
});
|
||||
|
||||
/**
|
||||
* Deleting a transaction reverses the credits and debits
|
||||
*/
|
||||
static::deleted(function ($transaction) {
|
||||
$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();
|
||||
});
|
||||
|
||||
parent::boot();
|
||||
@@ -70,7 +103,7 @@ class JournalTransaction extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model $object
|
||||
* @param BaseModel $object
|
||||
* @return JournalTransaction
|
||||
*/
|
||||
public function referencesObject($object)
|
||||
@@ -87,8 +120,8 @@ class JournalTransaction extends BaseModel
|
||||
public function getReferencedObject()
|
||||
{
|
||||
if ($classname = $this->ref_class) {
|
||||
$_class = new $this->ref_class;
|
||||
return $_class->find($this->ref_class_id);
|
||||
$klass = new $this->ref_class;
|
||||
return $klass->find($this->ref_class_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user