2016-02-05 05:08:25 -06:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
namespace FireflyIII\Handlers\Events;
|
2015-03-02 13:05:28 -06:00
|
|
|
|
2016-01-12 14:38:05 -06:00
|
|
|
use FireflyIII\Events\TransactionJournalUpdated;
|
2015-03-02 13:05:28 -06:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2015-05-17 08:17:06 -05:00
|
|
|
use FireflyIII\Models\PiggyBankRepetition;
|
2015-03-02 13:05:28 -06:00
|
|
|
|
2015-03-29 00:51:56 -05:00
|
|
|
/**
|
|
|
|
* Class UpdateJournalConnection
|
|
|
|
*
|
2015-05-12 13:51:03 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-03-29 00:51:56 -05:00
|
|
|
* @package FireflyIII\Handlers\Events
|
|
|
|
*/
|
2015-03-02 13:05:28 -06:00
|
|
|
class UpdateJournalConnection
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the event handler.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the event.
|
|
|
|
*
|
2016-01-12 14:38:05 -06:00
|
|
|
* @param TransactionJournalUpdated $event
|
2015-03-02 13:05:28 -06:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-01-12 14:38:05 -06:00
|
|
|
public function handle(TransactionJournalUpdated $event)
|
2015-03-02 13:05:28 -06:00
|
|
|
{
|
|
|
|
$journal = $event->journal;
|
|
|
|
|
|
|
|
// get the event connected to this journal:
|
|
|
|
/** @var PiggyBankEvent $event */
|
2015-03-29 14:27:51 -05:00
|
|
|
$event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first();
|
|
|
|
if (is_null($event)) {
|
2015-03-03 01:46:14 -06:00
|
|
|
return;
|
|
|
|
}
|
2015-03-02 13:05:28 -06:00
|
|
|
$piggyBank = $event->piggyBank()->first();
|
2015-05-17 08:17:06 -05:00
|
|
|
$repetition = null;
|
|
|
|
if ($piggyBank) {
|
|
|
|
/** @var PiggyBankRepetition $repetition */
|
|
|
|
$repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first();
|
|
|
|
}
|
2015-03-02 13:05:28 -06:00
|
|
|
|
|
|
|
if (is_null($repetition)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-26 12:42:28 -05:00
|
|
|
bcscale(2);
|
|
|
|
|
2015-04-09 13:58:57 -05:00
|
|
|
$amount = $journal->amount;
|
2015-07-26 12:42:28 -05:00
|
|
|
$diff = bcsub($amount, $event->amount); // update current repetition
|
2015-03-02 13:05:28 -06:00
|
|
|
|
2015-07-26 12:42:28 -05:00
|
|
|
$repetition->currentamount = bcadd($repetition->currentamount, $diff);
|
2015-03-02 13:05:28 -06:00
|
|
|
$repetition->save();
|
|
|
|
|
|
|
|
|
|
|
|
$event->amount = $amount;
|
|
|
|
$event->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|