Store full datetime in post_date; recalculate finances on every pirep

This commit is contained in:
Nabeel Shahzad
2018-03-17 22:20:08 -05:00
parent 6fa724d7b7
commit 6b002f24a8
10 changed files with 52 additions and 17 deletions

View File

@@ -41,6 +41,29 @@ class JournalRepository extends BaseRepository implements CacheableInterface
return $date->setTimezone('UTC')->toDateString();
}
/**
* Recalculate the balance of the given journal
* @param Journal $journal
* @return Journal
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function recalculateBalance(Journal $journal)
{
$where = [
'journal_id' => $journal->id
];
$credits = Money::create($this->findWhere($where)->sum('credit') ?: 0);
$debits = Money::create($this->findWhere($where)->sum('debit') ?: 0);
$balance = $credits->subtract($debits);
$journal->balance = $balance->getAmount();
$journal->save();
return $journal;
}
/**
* Post a new transaction to a journal, and also adjust the balance
* on the transaction itself. A cron will run to reconcile the journal
@@ -67,7 +90,6 @@ class JournalRepository extends BaseRepository implements CacheableInterface
$transaction_group = null,
$tags = null
) {
# tags can be passed in a list
if ($tags && \is_array($tags)) {
$tags = implode(',', $tags);