diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index f7395fd13f..e8460051a2 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -276,7 +276,13 @@ class PiggybankController extends BaseController return Redirect::route('piggybanks.create')->withInput()->withErrors($messages['errors']); } // store! - $repos->store($data); + $piggyBank = $repos->store($data); + + /* + * Create the relevant repetition per Event. + */ + Event::fire('piggybank.storePiggybank',[$piggyBank]); + Session::flash('success', 'New piggy bank stored!'); if ($data['post_submit_action'] == 'create_another') { diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 77b47374ae..abc61a94aa 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -320,7 +320,7 @@ class TransactionController extends BaseController * piggy bank and store an event. */ if (!is_null(Input::get('piggybank_id')) && intval(Input::get('piggybank_id')) > 0) { - Event::fire('piggybank.createTransfer', [$journal, intval(Input::get('piggybank_id'))]); + Event::fire('piggybank.storeTransfer', [$journal, intval(Input::get('piggybank_id'))]); } if ($data['post_submit_action'] == 'create_another') { diff --git a/app/lib/FireflyIII/Database/Piggybank.php b/app/lib/FireflyIII/Database/Piggybank.php index 491ff71ba2..804dcc307c 100644 --- a/app/lib/FireflyIII/Database/Piggybank.php +++ b/app/lib/FireflyIII/Database/Piggybank.php @@ -59,8 +59,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface exit; } $piggybank->save(); - \Event::fire('piggybanks.store', [$piggybank]); - $piggybank->save(); + return $piggybank; } /** diff --git a/app/lib/FireflyIII/Event/Piggybank.php b/app/lib/FireflyIII/Event/Piggybank.php index d946cc3bd8..d881b0ece2 100644 --- a/app/lib/FireflyIII/Event/Piggybank.php +++ b/app/lib/FireflyIII/Event/Piggybank.php @@ -28,11 +28,75 @@ class Piggybank } } + public function destroyTransfer(\TransactionJournal $journal) + { + if ($journal->piggybankevents()->count() > 0) { + + /** @var \FireflyIII\Database\Piggybank $repository */ + $repository = \App::make('FireflyIII\Database\Piggybank'); + + /** @var \Piggybank $piggyBank */ + $piggyBank = $journal->piggybankevents()->first()->piggybank()->first(); + + /** @var \PiggybankRepetition $repetition */ + $repetition = $repository->findRepetitionByDate($piggyBank, $journal->date); + + $relevantTransaction = null; + /** @var \Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + if ($transaction->account_id == $piggyBank->account_id) { + $relevantTransaction = $transaction; + } + } + if (is_null($relevantTransaction)) { + return; + } + + $repetition->currentamount += floatval($relevantTransaction->amount * -1); + $repetition->save(); + + + $event = new \PiggybankEvent; + $event->piggybank()->associate($piggyBank); + $event->amount = floatval($relevantTransaction->amount * -1); + $event->date = new Carbon; + if (!$event->validate()) { + var_dump($event->errors()); + exit(); + } + $event->save(); + } + } + + /** + * @param \Piggybank $piggybank + * @param float $amount + */ + public function removeMoney(\Piggybank $piggybank, $amount = 0.0) + { + $amount = $amount * -1; + if ($amount < 0) { + $event = new \PiggybankEvent; + $event->piggybank()->associate($piggybank); + $event->amount = floatval($amount); + $event->date = new Carbon; + if (!$event->validate()) { + var_dump($event->errors()); + exit(); + } + $event->save(); + } + } + + /* + * + */ + /** * @param \TransactionJournal $journal * @param int $piggybankId */ - public function createTransfer(\TransactionJournal $journal, $piggybankId = 0) + public function storeTransfer(\TransactionJournal $journal, $piggybankId = 0) { /** @var \FireflyIII\Database\Piggybank $repository */ $repository = \App::make('FireflyIII\Database\Piggybank'); @@ -95,67 +159,14 @@ class Piggybank } } - public function destroyTransfer(\TransactionJournal $journal) - { - if ($journal->piggybankevents()->count() > 0) { - - /** @var \FireflyIII\Database\Piggybank $repository */ - $repository = \App::make('FireflyIII\Database\Piggybank'); - - /** @var \Piggybank $piggyBank */ - $piggyBank = $journal->piggybankevents()->first()->piggybank()->first(); - - /** @var \PiggybankRepetition $repetition */ - $repetition = $repository->findRepetitionByDate($piggyBank, $journal->date); - - $relevantTransaction = null; - /** @var \Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - if ($transaction->account_id == $piggyBank->account_id) { - $relevantTransaction = $transaction; - } - } - if (is_null($relevantTransaction)) { - return; - } - - $repetition->currentamount += floatval($relevantTransaction->amount * -1); + public function storePiggybank(\Piggybank $piggybank) { + if(intval($piggybank->repeats) == 0) { + $repetition = new \PiggybankRepetition; + $repetition->piggybank()->associate($piggybank); + $repetition->startdate = $piggybank->startdate; + $repetition->targetdate = $piggybank->targetdate; + $repetition->currentamount = 0; $repetition->save(); - - - $event = new \PiggybankEvent; - $event->piggybank()->associate($piggyBank); - $event->amount = floatval($relevantTransaction->amount * -1); - $event->date = new Carbon; - if (!$event->validate()) { - var_dump($event->errors()); - exit(); - } - $event->save(); - } - } - - /* - * - */ - - /** - * @param \Piggybank $piggybank - * @param float $amount - */ - public function removeMoney(\Piggybank $piggybank, $amount = 0.0) - { - $amount = $amount * -1; - if ($amount < 0) { - $event = new \PiggybankEvent; - $event->piggybank()->associate($piggybank); - $event->amount = floatval($amount); - $event->date = new Carbon; - if (!$event->validate()) { - var_dump($event->errors()); - exit(); - } - $event->save(); } } @@ -166,7 +177,8 @@ class Piggybank { $events->listen('piggybank.addMoney', 'FireflyIII\Event\Piggybank@addMoney'); $events->listen('piggybank.removeMoney', 'FireflyIII\Event\Piggybank@removeMoney'); - $events->listen('piggybank.createTransfer', 'FireflyIII\Event\Piggybank@createTransfer'); + $events->listen('piggybank.storeTransfer', 'FireflyIII\Event\Piggybank@storeTransfer'); + $events->listen('piggybank.storePiggybank', 'FireflyIII\Event\Piggybank@storePiggybank'); $events->listen('piggybank.destroyTransfer', 'FireflyIII\Event\Piggybank@destroyTransfer'); $events->listen('piggybank.updateTransfer', 'FireflyIII\Event\Piggybank@updateTransfer'); } @@ -176,7 +188,7 @@ class Piggybank if ($journal->piggybankevents()->count() > 0) { - $event = $journal->piggybankevents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->first(); + $event = $journal->piggybankevents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->first(); $eventSum = floatval($journal->piggybankevents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->sum('amount')); /** @var \FireflyIII\Database\Piggybank $repository */ diff --git a/app/views/list/piggybank-events.blade.php b/app/views/list/piggybank-events.blade.php index dfba52ee4f..e80ddc4c61 100644 --- a/app/views/list/piggybank-events.blade.php +++ b/app/views/list/piggybank-events.blade.php @@ -1,10 +1,18 @@
Piggy bank | + @endifDate | Amount | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
+ {{{$event->piggybank->name}}} + | + @endif
@if(!is_null($event->transaction_journal_id))
{{$event->date->format('j F Y')}}
diff --git a/app/views/transactions/show.blade.php b/app/views/transactions/show.blade.php
index f13a0b65c5..b4c3eb5414 100644
--- a/app/views/transactions/show.blade.php
+++ b/app/views/transactions/show.blade.php
@@ -2,71 +2,93 @@
@section('content')
-
+ Metadata-
+
+
+ @endif
-
+ Piggy banks
+
+
+ @include('list.piggybank-events',['events' => $journal->piggybankevents,'showPiggybank' => true])
+
+
-
+
Transactions- @foreach($journal->transactions as $t) -{{{$t->account->name}}}
- |
Amount | -{{mf($t->amount)}} | -
New balance | -{{mf($t->account->balanceBeforeJournal($journal))}} → {{mf($t->account->balanceBeforeJournal($journal) + $t->amount)}} | -
Description | -{{{$t->description}}} | -
Amount | +{{mf($t->amount)}} | +
New balance | +{{mf($t->account->balanceBeforeJournal($journal))}} → {{mf($t->account->balanceBeforeJournal($journal) + $t->amount)}} | +
Description | +{{{$t->description}}} | +