journal; $piggyBankId = $event->piggyBankId; /** @var PiggyBank $piggyBank */ $piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); if (is_null($piggyBank)) { return true; } // update piggy bank rep for date of transaction journal. $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); if (is_null($repetition)) { return true; } $amount = TransactionJournal::amountPositive($journal); // if piggy account matches source account, the amount is positive $sources = TransactionJournal::sourceAccountList($journal)->pluck('id')->toArray(); if (in_array($piggyBank->account_id, $sources)) { $amount = bcmul($amount, '-1'); } $repetition->currentamount = bcadd($repetition->currentamount, $amount); $repetition->save(); PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'transaction_journal_id' => $journal->id, 'date' => $journal->date, 'amount' => $amount]); return true; } }