phpvms/app/Cron/Nightly/RecalculateBalances.php

53 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2018-04-07 06:14:01 +08:00
namespace App\Cron\Nightly;
use App\Events\CronNightly;
use App\Interfaces\Listener;
use App\Models\Journal;
use App\Repositories\JournalRepository;
use Log;
/**
* This recalculates the balances on all of the journals
* @package App\Listeners\Cron
*/
class RecalculateBalances extends Listener
{
private $journalRepo;
/**
* Nightly constructor.
* @param JournalRepository $journalRepo
*/
public function __construct(JournalRepository $journalRepo)
{
$this->journalRepo = $journalRepo;
}
/**
* Recalculate all the balances for the different ledgers
* @param CronNightly $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function handle(CronNightly $event): void
{
Log::info('Recalculating balances');
$journals = Journal::all();
foreach ($journals as $journal) {
$old_balance = $journal->balance;
$this->journalRepo->recalculateBalance($journal);
$journal->refresh();
Log::info('Adjusting balance on '.
$journal->morphed_type.':'.$journal->morphed_id
.' from '.$old_balance.' to '.$journal->balance);
}
Log::info('Done calculating balances');
}
}