Skip to content

Firefly III: a personal finances manager

Show suggested monthly savings for a piggybank

Joris de Vriesa0e66b913bd9
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 %}
</td>
</tr>
+ {% if piggyBank.targetdate %}
+ <tr>
+ <td>Suggested monthly amount to save</td>
+ <td>
+ {{ suggestedMonthlyAmount(piggyBank)|formatAmount }}
+ </td>
+ </tr>
+ {% endif %}
</table>
</div>
</div>