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 :)
This commit is contained in:
Joris de Vries 2017-02-24 22:00:49 +01:00
parent e737683efb
commit a0e66b913b
3 changed files with 25 additions and 0 deletions

View File

@ -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.
*/

View File

@ -39,6 +39,12 @@ class PiggyBank extends Twig_Extension
}
);
$functions[] = new Twig_SimpleFunction(
'suggestedMonthlyAmount', function (PB $piggyBank) {
return $piggyBank->getSuggestedMonthlyAmount();
}
);
return $functions;
}

View File

@ -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>