/** * Handle the event. * * @param JournalSaved $event * * @return void */ public function handle(JournalSaved $event) { $journal = $event->journal; // get the event connected to this journal: /** @var PiggyBankEvent $event */ $event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first(); if (is_null($event)) { return; } $piggyBank = $event->piggyBank()->first(); $repetition = null; if ($piggyBank) { /** @var PiggyBankRepetition $repetition */ $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); } if (is_null($repetition)) { return; } $amount = $journal->amount; $diff = $amount - $event->amount; // update current repetition $repetition->currentamount += $diff; $repetition->save(); $event->amount = $amount; $event->save(); }
/** * Handle the event. * * @param TransactionJournalUpdated $event * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5. * * @return bool */ public function handle(TransactionJournalUpdated $event) : bool { $journal = $event->journal; if (!$journal->isTransfer()) { return true; } // get the event connected to this journal: /** @var PiggyBankEvent $event */ $event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first(); if (is_null($event)) { return false; } $piggyBank = $event->piggyBank()->first(); $repetition = null; if (!is_null($piggyBank)) { /** @var PiggyBankRepetition $repetition */ $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); } if (is_null($repetition)) { return false; } $amount = TransactionJournal::amount($journal); $diff = bcsub($amount, $event->amount); // update current repetition $repetition->currentamount = bcadd($repetition->currentamount, $diff); $repetition->save(); $event->amount = $amount; $event->save(); return true; }