2018-03-17 09:12:56 +08:00
|
|
|
<?php
|
|
|
|
|
2018-04-07 06:14:01 +08:00
|
|
|
namespace App\Cron\Nightly;
|
2018-03-17 09:12:56 +08:00
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Listener;
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Events\CronNightly;
|
2018-03-17 09:12:56 +08:00
|
|
|
use App\Models\Journal;
|
|
|
|
use App\Repositories\JournalRepository;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This recalculates the balances on all of the journals
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class RecalculateBalances extends Listener
|
2018-03-17 09:12:56 +08:00
|
|
|
{
|
|
|
|
private $journalRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nightly constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-17 09:12:56 +08:00
|
|
|
* @param JournalRepository $journalRepo
|
|
|
|
*/
|
|
|
|
public function __construct(JournalRepository $journalRepo)
|
|
|
|
{
|
|
|
|
$this->journalRepo = $journalRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recalculate all the balances for the different ledgers
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-17 09:12:56 +08:00
|
|
|
* @param CronNightly $event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-17 09:12:56 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function handle(CronNightly $event): void
|
|
|
|
{
|
2021-03-03 04:43:34 +08:00
|
|
|
Log::info('Nightly: Recalculating balances');
|
2018-03-17 09:12:56 +08:00
|
|
|
|
|
|
|
$journals = Journal::all();
|
|
|
|
foreach ($journals as $journal) {
|
2018-03-18 11:20:08 +08:00
|
|
|
$old_balance = $journal->balance;
|
2018-03-17 09:12:56 +08:00
|
|
|
|
2018-03-18 11:20:08 +08:00
|
|
|
$this->journalRepo->recalculateBalance($journal);
|
|
|
|
$journal->refresh();
|
2018-03-17 09:12:56 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('Adjusting balance on '.
|
|
|
|
$journal->morphed_type.':'.$journal->morphed_id
|
|
|
|
.' from '.$old_balance.' to '.$journal->balance);
|
2018-03-17 09:12:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Done calculating balances');
|
|
|
|
}
|
|
|
|
}
|