/** * Handle the event when journal is saved. * * @param JournalCreated $event * * @return boolean */ public function handle(JournalCreated $event) { /** @var TransactionJournal $journal */ $journal = $event->journal; $piggyBankId = $event->piggyBankId; /** @var PiggyBank $piggyBank */ $piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); if (is_null($piggyBank)) { return false; } // update piggy bank rep for date of transaction journal. $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); if (is_null($repetition)) { return false; } bcscale(2); $amount = $journal->amount_positive; // if piggy account matches source account, the amount is positive if ($piggyBank->account_id == $journal->source_account->id) { $amount = $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; }
/** * Connect a new transaction journal to any related piggy banks. * * @param TransactionStored $event * * @return bool */ public function handle(TransactionStored $event) : bool { /** @var PiggyBankRepositoryInterface $repository */ $repository = app(PiggyBankRepositoryInterface::class); $transaction = $event->transaction; $piggyBank = $repository->find($transaction['piggy_bank_id']); // valid piggy: if (is_null($piggyBank->id)) { return true; } $amount = strval($transaction['amount']); // piggy bank account something with amount: if ($transaction['source_account_id'] == $piggyBank->account_id) { // if the source of this transaction is the same as the piggy bank, // the money is being removed from the piggy bank. So the // amount must be negative: $amount = bcmul($amount, '-1'); } $repetition = $piggyBank->currentRelevantRep(); // add or remove the money from the piggy bank: $newAmount = bcadd(strval($repetition->currentamount), $amount); $repetition->currentamount = $newAmount; $repetition->save(); // now generate a piggy bank event: PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'date' => $transaction['date'], 'amount' => $newAmount]); return true; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @param User $user */ public static function createPiggybanks(User $user) { $account = self::findAccount($user, 'TestData Savings'); $camera = PiggyBank::create(['account_id' => $account->id, 'name' => 'New camera', 'targetamount' => 1000, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 1]); $repetition = $camera->piggyBankRepetitions()->first(); $repetition->currentamount = 735; $repetition->save(); // events: PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-05-01', 'amount' => '245']); PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-06-01', 'amount' => '245']); PiggyBankEvent::create(['piggy_bank_id' => $camera->id, 'date' => '2015-07-01', 'amount' => '245']); $phone = PiggyBank::create(['account_id' => $account->id, 'name' => 'New phone', 'targetamount' => 600, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 2]); $repetition = $phone->piggyBankRepetitions()->first(); $repetition->currentamount = 333; $repetition->save(); // events: PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-05-01', 'amount' => '111']); PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-06-01', 'amount' => '111']); PiggyBankEvent::create(['piggy_bank_id' => $phone->id, 'date' => '2015-07-01', 'amount' => '111']); $couch = PiggyBank::create(['account_id' => $account->id, 'name' => 'New couch', 'targetamount' => 500, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 3]); $repetition = $couch->piggyBankRepetitions()->first(); $repetition->currentamount = 120; $repetition->save(); // events: PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-05-01', 'amount' => '40']); PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-06-01', 'amount' => '40']); PiggyBankEvent::create(['piggy_bank_id' => $couch->id, 'date' => '2015-07-01', 'amount' => '40']); // empty one. PiggyBank::create(['account_id' => $account->id, 'name' => 'New head set', 'targetamount' => 500, 'startdate' => '2015-04-01', 'reminder_skip' => 0, 'remind_me' => 0, 'order' => 4]); }
/** * 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(); }
/** * Connect a new transaction journal to any related piggy banks. * * @param TransactionJournalStored $event * * @return bool */ public function handle(TransactionJournalStored $event) : bool { /** @var TransactionJournal $journal */ $journal = $event->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; }
/** * 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; }
/** * */ protected function createPiggyBankEvent() { // piggy bank event // add money to this piggy bank // create a piggy bank event to match: $checking = $this->findAccount('Checking account'); $savings = $this->findAccount('Savings account'); $transfer = TransactionType::whereType('Transfer')->first(); $euro = TransactionCurrency::whereCode('EUR')->first(); $groceries = $this->findBudget('Groceries'); $house = $this->findCategory('House'); $piggyBank = $this->findPiggyBank('New camera'); $intoPiggy = $this->createJournal(['from' => $checking, 'to' => $savings, 'amount' => 100, 'transactionType' => $transfer, 'description' => 'Money for piggy', 'date' => $this->yaeom, 'transactionCurrency' => $euro, 'category' => $house, 'budget' => $groceries]); PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'transaction_journal_id' => $intoPiggy->id, 'date' => $this->yaeom, 'amount' => 100]); }
/** * @param PiggyBank $piggyBank * @param string $amount * * @return PiggyBankEvent */ public function createEvent(PiggyBank $piggyBank, string $amount) : PiggyBankEvent { $event = PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]); return $event; }
/** * @param PiggyBank $piggyBank * @param $amount * * @return bool */ public function createEvent(PiggyBank $piggyBank, $amount) { PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]); return true; }