2016-02-05 12:08:25 +01:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* UpdateJournalConnection.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
namespace FireflyIII\Handlers\Events;
|
2015-03-02 20:05:28 +01:00
|
|
|
|
2016-01-12 21:38:05 +01:00
|
|
|
use FireflyIII\Events\TransactionJournalUpdated;
|
2015-03-02 20:05:28 +01:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2015-05-17 15:17:06 +02:00
|
|
|
use FireflyIII\Models\PiggyBankRepetition;
|
2016-03-02 20:11:28 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-03-02 20:05:28 +01:00
|
|
|
|
2015-03-29 07:51:56 +02:00
|
|
|
/**
|
|
|
|
* Class UpdateJournalConnection
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Handlers\Events
|
|
|
|
*/
|
2015-03-02 20:05:28 +01:00
|
|
|
class UpdateJournalConnection
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the event.
|
|
|
|
*
|
2016-01-12 21:38:05 +01:00
|
|
|
* @param TransactionJournalUpdated $event
|
2016-08-24 19:33:58 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
|
2015-03-02 20:05:28 +01:00
|
|
|
*
|
2016-02-18 07:21:48 +01:00
|
|
|
* @return bool
|
2015-03-02 20:05:28 +01:00
|
|
|
*/
|
2016-02-18 07:21:48 +01:00
|
|
|
public function handle(TransactionJournalUpdated $event):bool
|
2015-03-02 20:05:28 +01:00
|
|
|
{
|
|
|
|
$journal = $event->journal;
|
|
|
|
|
2016-06-06 20:25:01 +02:00
|
|
|
if (!$journal->isTransfer()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:05:28 +01:00
|
|
|
// get the event connected to this journal:
|
|
|
|
/** @var PiggyBankEvent $event */
|
2015-03-29 21:27:51 +02:00
|
|
|
$event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first();
|
|
|
|
if (is_null($event)) {
|
2016-02-18 07:21:48 +01:00
|
|
|
return false;
|
2015-03-03 08:46:14 +01:00
|
|
|
}
|
2015-03-02 20:05:28 +01:00
|
|
|
$piggyBank = $event->piggyBank()->first();
|
2015-05-17 15:17:06 +02:00
|
|
|
$repetition = null;
|
2016-08-24 19:33:58 +02:00
|
|
|
if (!is_null($piggyBank)) {
|
2015-05-17 15:17:06 +02:00
|
|
|
/** @var PiggyBankRepetition $repetition */
|
|
|
|
$repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first();
|
|
|
|
}
|
2015-03-02 20:05:28 +01:00
|
|
|
|
|
|
|
if (is_null($repetition)) {
|
2016-02-18 07:21:48 +01:00
|
|
|
return false;
|
2015-03-02 20:05:28 +01:00
|
|
|
}
|
2015-07-26 19:42:28 +02:00
|
|
|
|
2016-03-02 20:11:28 +01:00
|
|
|
$amount = TransactionJournal::amount($journal);
|
2015-07-26 19:42:28 +02:00
|
|
|
$diff = bcsub($amount, $event->amount); // update current repetition
|
2015-03-02 20:05:28 +01:00
|
|
|
|
2015-07-26 19:42:28 +02:00
|
|
|
$repetition->currentamount = bcadd($repetition->currentamount, $diff);
|
2015-03-02 20:05:28 +01:00
|
|
|
$repetition->save();
|
|
|
|
|
|
|
|
|
|
|
|
$event->amount = $amount;
|
|
|
|
$event->save();
|
2016-02-18 07:21:48 +01:00
|
|
|
|
|
|
|
return true;
|
2015-03-02 20:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|