Update balance in callbacks
This commit is contained in:
@@ -128,22 +128,6 @@ class Journal extends BaseModel
|
|||||||
$this->attributes['balance'] = $value ? (int)$value->getAmount() : null;
|
$this->attributes['balance'] = $value ? (int)$value->getAmount() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the debit only balance of the journal based on a given date.
|
|
||||||
* @param Carbon $date
|
|
||||||
* @return Money
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getDebitBalanceOn(Carbon $date): Money
|
|
||||||
{
|
|
||||||
$balance = $this->transactions()
|
|
||||||
->where('post_date', '<=', $date)
|
|
||||||
->sum('debit') ?: 0;
|
|
||||||
|
|
||||||
return new Money($balance);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Journal $object
|
* @param Journal $object
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
@@ -210,180 +194,4 @@ class Journal extends BaseModel
|
|||||||
|
|
||||||
return new Money($balance);
|
return new Money($balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the balance of the journal in dollars. This "could" include future dates.
|
|
||||||
* @return float|int
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getCurrentBalanceInDollars()
|
|
||||||
{
|
|
||||||
return $this->getCurrentBalance()->getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get balance
|
|
||||||
* @return float|int
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getBalanceInDollars()
|
|
||||||
{
|
|
||||||
return $this->getBalance()->getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @param null $memo
|
|
||||||
* @param null $post_date
|
|
||||||
* @return JournalTransaction
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function credit($value, $memo = null, $post_date = null, $transaction_group = null)
|
|
||||||
{
|
|
||||||
$value = ($value instanceof Money)
|
|
||||||
? $value
|
|
||||||
: new Money($value);
|
|
||||||
|
|
||||||
return $this->post($value, null, $memo, $post_date, $transaction_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @param null $memo
|
|
||||||
* @param null $post_date
|
|
||||||
* @return JournalTransaction
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function debit($value, $memo = null, $post_date = null, $transaction_group = null)
|
|
||||||
{
|
|
||||||
$value = ($value instanceof Money)
|
|
||||||
? $value
|
|
||||||
: new Money($value);
|
|
||||||
|
|
||||||
return $this->post(null, $value, $memo, $post_date, $transaction_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Money $credit
|
|
||||||
* @param Money $debit
|
|
||||||
* @param $memo
|
|
||||||
* @param Carbon $post_date
|
|
||||||
* @param $transaction_group
|
|
||||||
* @return JournalTransaction
|
|
||||||
*/
|
|
||||||
private function post(Money $credit = null, Money $debit = null, $memo = null, $post_date = null, $transaction_group)
|
|
||||||
{
|
|
||||||
$transaction = new JournalTransaction();
|
|
||||||
$transaction->credit = $credit ? $credit->getAmount() : null;
|
|
||||||
$transaction->debit = $debit ? $debit->getAmount() : null;
|
|
||||||
$currency_code = $credit
|
|
||||||
? $credit->getCurrency()->getCode()
|
|
||||||
: $debit->getCurrency()->getCode();
|
|
||||||
|
|
||||||
$transaction->memo = $memo;
|
|
||||||
$transaction->currency = $currency_code;
|
|
||||||
$transaction->post_date = $post_date ?: Carbon::now();
|
|
||||||
$transaction->transaction_group = $transaction_group;
|
|
||||||
|
|
||||||
$this->transactions()->save($transaction);
|
|
||||||
|
|
||||||
return $transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Credit a journal by a given dollar amount
|
|
||||||
* @param $value
|
|
||||||
* @param null $memo
|
|
||||||
* @param null $post_date
|
|
||||||
* @return JournalTransaction
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function creditDollars($value, $memo = null, $post_date = null)
|
|
||||||
{
|
|
||||||
$value = Money::convertToSubunit($value);
|
|
||||||
return $this->credit($value, $memo, $post_date);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Debit a journal by a given dollar amount
|
|
||||||
* @param $value
|
|
||||||
* @param null $memo
|
|
||||||
* @param null $post_date
|
|
||||||
* @return JournalTransaction
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function debitDollars($value, $memo = null, $post_date = null)
|
|
||||||
{
|
|
||||||
$value = Money::convertToSubunit($value);
|
|
||||||
return $this->debit($value, $memo, $post_date);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the dollar amount debited to a journal today
|
|
||||||
* @return float|int
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getDollarsDebitedToday()
|
|
||||||
{
|
|
||||||
$today = Carbon::now();
|
|
||||||
return $this->getDollarsDebitedOn($today);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the dollar amount credited to a journal today
|
|
||||||
* @return Money
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getDollarsCreditedToday()
|
|
||||||
{
|
|
||||||
$today = Carbon::now();
|
|
||||||
return $this->getDollarsCreditedOn($today);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the dollar amount debited to a journal on a given day
|
|
||||||
* @param Carbon $date
|
|
||||||
* @return Money|float|int
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getDollarsDebitedOn(Carbon $date)
|
|
||||||
{
|
|
||||||
$amount = $this->transactions()
|
|
||||||
->whereBetween('post_date', [
|
|
||||||
$date->copy()->startOfDay(),
|
|
||||||
$date->copy()->endOfDay()
|
|
||||||
])
|
|
||||||
->sum('debit');
|
|
||||||
|
|
||||||
return new Money($amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the dollar amount credited to a journal on a given day
|
|
||||||
* @param Carbon $date
|
|
||||||
* @return Money
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function getDollarsCreditedOn(Carbon $date)
|
|
||||||
{
|
|
||||||
$amount = $this
|
|
||||||
->transactions()
|
|
||||||
->whereBetween('post_date', [
|
|
||||||
$date->copy()->startOfDay(),
|
|
||||||
$date->copy()->endOfDay()
|
|
||||||
])
|
|
||||||
->sum('credit');
|
|
||||||
|
|
||||||
return new Money($amount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,14 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property string ref_class
|
* @property string currency
|
||||||
* @property string ref_class_id
|
* @property string memo
|
||||||
* @property string currency
|
* @property string transaction_group
|
||||||
* @property string memo
|
* @property string post_date
|
||||||
* @property string transaction_group
|
|
||||||
* @property static post_date
|
|
||||||
* @property integer credit
|
* @property integer credit
|
||||||
* @property integer debit
|
* @property integer debit
|
||||||
|
* @property string ref_class
|
||||||
|
* @property integer ref_class_id
|
||||||
*/
|
*/
|
||||||
class JournalTransaction extends BaseModel
|
class JournalTransaction extends BaseModel
|
||||||
{
|
{
|
||||||
@@ -42,20 +42,53 @@ class JournalTransaction extends BaseModel
|
|||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Callbacks
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected static function boot()
|
protected static function boot()
|
||||||
{
|
{
|
||||||
static::creating(function ($transaction) {
|
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) {
|
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) {
|
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();
|
parent::boot();
|
||||||
@@ -70,7 +103,7 @@ class JournalTransaction extends BaseModel
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Model $object
|
* @param BaseModel $object
|
||||||
* @return JournalTransaction
|
* @return JournalTransaction
|
||||||
*/
|
*/
|
||||||
public function referencesObject($object)
|
public function referencesObject($object)
|
||||||
@@ -87,8 +120,8 @@ class JournalTransaction extends BaseModel
|
|||||||
public function getReferencedObject()
|
public function getReferencedObject()
|
||||||
{
|
{
|
||||||
if ($classname = $this->ref_class) {
|
if ($classname = $this->ref_class) {
|
||||||
$_class = new $this->ref_class;
|
$klass = new $this->ref_class;
|
||||||
return $_class->find($this->ref_class_id);
|
return $klass->find($this->ref_class_id);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,16 +79,6 @@ class JournalRepository extends BaseRepository implements CacheableInterface
|
|||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Adjust the balance on the journal
|
|
||||||
if($credit) {
|
|
||||||
$journal->balance->add($credit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($debit) {
|
|
||||||
$journal->balance->subtract($debit);
|
|
||||||
}
|
|
||||||
|
|
||||||
$journal->save();
|
|
||||||
$journal->refresh();
|
$journal->refresh();
|
||||||
|
|
||||||
return $transaction;
|
return $transaction;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use App\Repositories\ExpenseRepository;
|
|||||||
use App\Repositories\JournalRepository;
|
use App\Repositories\JournalRepository;
|
||||||
use App\Support\Math;
|
use App\Support\Math;
|
||||||
use App\Support\Money;
|
use App\Support\Money;
|
||||||
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FinanceService
|
* Class FinanceService
|
||||||
@@ -89,6 +90,8 @@ class FinanceService extends BaseService
|
|||||||
$fares = $this->getReconciledFaresForPirep($pirep);
|
$fares = $this->getReconciledFaresForPirep($pirep);
|
||||||
foreach($fares as $fare) {
|
foreach($fares as $fare) {
|
||||||
|
|
||||||
|
Log::info('Finance: PIREP: ' . $pirep->id . ', fare:', $fare->toArray());
|
||||||
|
|
||||||
$credit = Money::createFromAmount($fare->count * $fare->price);
|
$credit = Money::createFromAmount($fare->count * $fare->price);
|
||||||
$debit = Money::createFromAmount($fare->count * $fare->cost);
|
$debit = Money::createFromAmount($fare->count * $fare->cost);
|
||||||
|
|
||||||
@@ -109,6 +112,9 @@ class FinanceService extends BaseService
|
|||||||
*/
|
*/
|
||||||
$expenses = $this->getExpenses($pirep);
|
$expenses = $this->getExpenses($pirep);
|
||||||
foreach($expenses as $expense) {
|
foreach($expenses as $expense) {
|
||||||
|
|
||||||
|
Log::info('Finance: PIREP: '.$pirep->id.', expense:', $expense->toArray());
|
||||||
|
|
||||||
$debit = Money::createFromAmount($expense->amount);
|
$debit = Money::createFromAmount($expense->amount);
|
||||||
$this->journalRepo->post(
|
$this->journalRepo->post(
|
||||||
$journal,
|
$journal,
|
||||||
@@ -222,6 +228,7 @@ class FinanceService extends BaseService
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO Apply the multiplier from the subfleet
|
// TODO Apply the multiplier from the subfleet
|
||||||
|
return $expense;
|
||||||
});
|
});
|
||||||
|
|
||||||
$gathered_expenses = event(new ExpensesEvent($pirep));
|
$gathered_expenses = event(new ExpensesEvent($pirep));
|
||||||
|
|||||||
Reference in New Issue
Block a user