From a0e66b913bd9fed8139a9952675b45beadfa81f1 Mon Sep 17 00:00:00 2001 From: Joris de Vries Date: Fri, 24 Feb 2017 22:00:49 +0100 Subject: [PATCH] Show suggested monthly savings for a piggybank If a piggybank has both a target date and a target amount, show how much money needs to be added to the piggybank each month to achieve both targets. Strings are currently hard-coded because I want to gauge the reaction to this :) --- app/Models/PiggyBank.php | 11 +++++++++++ app/Support/Twig/PiggyBank.php | 6 ++++++ resources/views/piggy-banks/show.twig | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index d5b2b00187..997d93fada 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -129,6 +129,17 @@ class PiggyBank extends Model } + public function getSuggestedMonthlyAmount() + { + if ($this->targetdate && $this->currentRelevantRep()->currentamount < $this->targetamount) { + $thisMonth = Carbon::now()->month; + $targetMonth = $this->targetdate->month; + $remainingAmount = $this->targetamount - $this->currentRelevantRep()->currentamount; + return $thisMonth < $targetMonth ? $remainingAmount / ($targetMonth - $thisMonth) : $remainingAmount ; + } + return 0; + } + /** * Get all of the piggy bank's notes. */ diff --git a/app/Support/Twig/PiggyBank.php b/app/Support/Twig/PiggyBank.php index 8a11fb546e..badb07889e 100644 --- a/app/Support/Twig/PiggyBank.php +++ b/app/Support/Twig/PiggyBank.php @@ -39,6 +39,12 @@ class PiggyBank extends Twig_Extension } ); + $functions[] = new Twig_SimpleFunction( + 'suggestedMonthlyAmount', function (PB $piggyBank) { + return $piggyBank->getSuggestedMonthlyAmount(); + } + ); + return $functions; } diff --git a/resources/views/piggy-banks/show.twig b/resources/views/piggy-banks/show.twig index 031aefb4ba..12b8140a71 100644 --- a/resources/views/piggy-banks/show.twig +++ b/resources/views/piggy-banks/show.twig @@ -70,6 +70,14 @@ {% endif %} + {% if piggyBank.targetdate %} + + Suggested monthly amount to save + + {{ suggestedMonthlyAmount(piggyBank)|formatAmount }} + + + {% endif %}