Skip to content

Firefly III: a personal finances manager

Fixed #1405

James Cole664451d0c615
diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php
index e5fdce6c0a..fbfbf94505 100644
--- a/app/Http/Controllers/PiggyBankController.php
+++ b/app/Http/Controllers/PiggyBankController.php
@@ -404,11 +404,17 @@ class PiggyBankController extends Controller
*/
public function show(PiggyBank $piggyBank)
{
- $note = $piggyBank->notes()->first();
- $events = $this->piggyRepos->getEvents($piggyBank);
- $subTitle = $piggyBank->name;
+ /** @var Carbon $end */
+ $end = session('end', Carbon::now()->endOfMonth());
+ // transform piggies using the transformer:
+ $parameters = new ParameterBag;
+ $parameters->set('end', $end);
+ $transformer = new PiggyBankTransformer(new ParameterBag);
+ $piggy = $transformer->transform($piggyBank);
+ $events = $this->piggyRepos->getEvents($piggyBank);
+ $subTitle = $piggyBank->name;
- return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'note'));
+ return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'piggy'));
}
/**
diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php
index eefd0d0706..4e13b540c5 100644
--- a/app/Models/PiggyBank.php
+++ b/app/Models/PiggyBank.php
@@ -36,6 +36,11 @@ use FireflyIII\Models\Account;
/**
* Class PiggyBank.
+ *
+ * @property Carbon $targetdate
+ * @property Carbon $startdate
+ * @property string $targetamount
+ *
*/
class PiggyBank extends Model
{
@@ -132,32 +137,6 @@ class PiggyBank extends Model
return $value;
}
- /**
- * @deprecated
- * @return string
- */
- public function getSuggestedMonthlyAmount(): string
- {
- $savePerMonth = '0';
- if ($this->targetdate && $this->currentRelevantRep()->currentamount < $this->targetamount) {
- $now = Carbon::now();
- $diffInMonths = $now->diffInMonths($this->targetdate, false);
- $remainingAmount = bcsub($this->targetamount, $this->currentRelevantRep()->currentamount);
-
- // more than 1 month to go and still need money to save:
- if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
- $savePerMonth = bcdiv($remainingAmount, (string)$diffInMonths);
- }
-
- // less than 1 month to go but still need money to save:
- if (0 === $diffInMonths && 1 === bccomp($remainingAmount, '0')) {
- $savePerMonth = $remainingAmount;
- }
- }
-
- return $savePerMonth;
- }
-
/**
* @param Carbon $date
*
diff --git a/app/Models/PiggyBankRepetition.php b/app/Models/PiggyBankRepetition.php
index c1644456f4..3efac08652 100644
--- a/app/Models/PiggyBankRepetition.php
+++ b/app/Models/PiggyBankRepetition.php
@@ -29,6 +29,7 @@ use FireflyIII\Models\PiggyBank;
/**
* Class PiggyBankRepetition.
+ * @property string $currentamount
*/
class PiggyBankRepetition extends Model
{
diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php
index 55b22083ce..677b48c23e 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepository.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php
@@ -324,6 +324,39 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $piggyBank->piggyBankRepetitions()->first();
}
+ /**
+ * Returns the suggested amount the user should save per month, or "".
+ *
+ * @param PiggyBank $piggyBank
+ *
+ * @return string
+ */
+ public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string
+ {
+ $savePerMonth = '0';
+ $repetition = $this->getRepetition($piggyBank);
+ if(null === $repetition) {
+ return $savePerMonth;
+ }
+ if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) {
+ $now = Carbon::now();
+ $diffInMonths = $now->diffInMonths($piggyBank->targetdate, false);
+ $remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount);
+
+ // more than 1 month to go and still need money to save:
+ if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
+ $savePerMonth = bcdiv($remainingAmount, (string)$diffInMonths);
+ }
+
+ // less than 1 month to go but still need money to save:
+ if (0 === $diffInMonths && 1 === bccomp($remainingAmount, '0')) {
+ $savePerMonth = $remainingAmount;
+ }
+ }
+
+ return $savePerMonth;
+ }
+
/**
* Get for piggy account what is left to put in piggies.
*
diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
index ca9ab5ea38..9b22a8d3b8 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
@@ -67,6 +67,11 @@ interface PiggyBankRepositoryInterface
*/
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool;
+ /**
+ * Correct order of piggies in case of issues.
+ */
+ public function correctOrder(): void;
+
/**
* Create a new event.
*
@@ -86,11 +91,6 @@ interface PiggyBankRepositoryInterface
*/
public function createEventWithJournal(PiggyBank $piggyBank, string $amount, TransactionJournal $journal): PiggyBankEvent;
- /**
- * Correct order of piggies in case of issues.
- */
- public function correctOrder(): void;
-
/**
* Destroy piggy bank.
*
@@ -173,6 +173,15 @@ interface PiggyBankRepositoryInterface
*/
public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition;
+ /**
+ * Returns the suggested amount the user should save per month, or "".
+ *
+ * @param PiggyBank $piggyBank
+ *
+ * @return string
+ */
+ public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string;
+
/**
* Get for piggy account what is left to put in piggies.
*
@@ -202,7 +211,7 @@ interface PiggyBankRepositoryInterface
* Set specific piggy bank to specific order.
*
* @param PiggyBank $piggyBank
- * @param int $order
+ * @param int $order
*
* @return bool
*/
diff --git a/app/Support/Twig/PiggyBank.php b/app/Support/Twig/PiggyBank.php
index 4b5f7dd711..6096acf3de 100644
--- a/app/Support/Twig/PiggyBank.php
+++ b/app/Support/Twig/PiggyBank.php
@@ -45,13 +45,6 @@ class PiggyBank extends Twig_Extension
}
);
- $functions[] = new Twig_SimpleFunction(
- 'suggestedMonthlyAmount',
- function (PB $piggyBank) {
- return $piggyBank->getSuggestedMonthlyAmount();
- }
- );
-
return $functions;
}
diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php
index 4c7172476a..5afe13a6ca 100644
--- a/app/Transformers/PiggyBankTransformer.php
+++ b/app/Transformers/PiggyBankTransformer.php
@@ -166,6 +166,7 @@ class PiggyBankTransformer extends TransformerAbstract
'percentage' => $percentage,
'current_amount' => $currentAmount,
'left_to_save' => round($leftToSave, $decimalPlaces),
+ 'save_per_month' => $piggyRepos->getSuggestedMonthlyAmount($piggyBank),
'startdate' => $startDate,
'targetdate' => $targetDate,
'order' => (int)$piggyBank->order,
diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php
index c27cb6d6a4..1f9571e99d 100644
--- a/app/Validation/FireflyValidator.php
+++ b/app/Validation/FireflyValidator.php
@@ -472,12 +472,13 @@ class FireflyValidator extends Validator
$query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
if (null !== $exclude) {
- $query->where('piggy_banks.id', '!=', $exclude);
+ $query->where('piggy_banks.id', '!=', (int)$exclude);
}
$set = $query->get(['piggy_banks.*']);
/** @var PiggyBank $entry */
foreach ($set as $entry) {
+
$fieldValue = $this->tryDecrypt($entry->name);
if ($fieldValue === $value) {
return false;
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 2f3f7025b7..35cf2184d8 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -1017,6 +1017,7 @@ return [
'remove_money_from_piggy_title' => 'Remove money from piggy bank ":name"',
'add' => 'Add',
'no_money_for_piggy' => 'You have no money to put in this piggy bank.',
+ 'suggested_savings_per_month' => 'Suggested per month',
'remove' => 'Remove',
'max_amount_add' => 'The maximum amount you can add is',
diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php
index 3bec6107c0..9ca9ec0f61 100644
--- a/resources/lang/en_US/validation.php
+++ b/resources/lang/en_US/validation.php
@@ -110,6 +110,7 @@ return [
'in_array' => 'The :attribute field does not exist in :other.',
'present' => 'The :attribute field must be present.',
'amount_zero' => 'The total amount cannot be zero',
+ 'unique_piggy_bank_for_user' => 'The name of the piggy bank must be unique.',
'secure_password' => 'This is not a secure password. Please try again. For more information, visit http://bit.ly/FF3-password-security',
'attributes' => [
'email' => 'email address',
diff --git a/resources/views/list/piggy-banks.twig b/resources/views/list/piggy-banks.twig
index c6474908d1..19a57d9ea7 100644
--- a/resources/views/list/piggy-banks.twig
+++ b/resources/views/list/piggy-banks.twig
@@ -10,6 +10,7 @@
<th colspan="3" class="hidden-sm hidden-xs">&nbsp;</th>
<th style="text-align: right;" class="hidden-sm hidden-xs">{{ 'target_amount'|_ }}</th>
<th style="text-align: right;" class="hidden-sm hidden-xs">{{ 'left_to_save'|_ }}</th>
+ <th style="text-align: right;" class="hidden-sm hidden-xs">{{ 'suggested_savings_per_month'|_ }}</th>
</tr>
</thead>
<tbody>
@@ -79,6 +80,11 @@
<span title="{{ 'left_to_save'|_ }}">{{ formatAmountBySymbol(piggy.left_to_save,piggy.currency_symbol,piggy.currency_dp) }}</span>
{% endif %}
</td>
+ <td class="hidden-sm hidden-xs" style="text-align:right;">
+ {% if piggy.targetdate %}
+ {{ formatAmountBySymbol(piggy.save_per_month, piggy.currency_symbol, piggy.currency_dp) }}
+ {% endif %}
+ </td>
</tr>
{% endfor %}
</tbody>
diff --git a/resources/views/piggy-banks/show.twig b/resources/views/piggy-banks/show.twig
index 5aa3e4e5a1..6dc2addc62 100644
--- a/resources/views/piggy-banks/show.twig
+++ b/resources/views/piggy-banks/show.twig
@@ -26,8 +26,8 @@
<div class="btn-group">
<button class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown"><i class="fa fa-ellipsis-v"></i></button>
<ul class="dropdown-menu" role="menu">
- <li><a href="{{ route('piggy-banks.edit', piggyBank.id) }}"><i class="fa fa-pencil fa-fw"></i> {{ 'edit'|_ }}</a></li>
- <li><a href="{{ route('piggy-banks.delete', piggyBank.id) }}"><i class="fa fa-trash fa-fw"></i> {{ 'delete'|_ }}</a></li>
+ <li><a href="{{ route('piggy-banks.edit', piggy.id) }}"><i class="fa fa-pencil fa-fw"></i> {{ 'edit'|_ }}</a></li>
+ <li><a href="{{ route('piggy-banks.delete', piggy.id) }}"><i class="fa fa-trash fa-fw"></i> {{ 'delete'|_ }}</a></li>
</ul>
</div>
</div>
@@ -40,15 +40,21 @@
</tr>
<tr>
<td>{{ 'target_amount'|_ }}</td>
- <td>{{ piggyBank.targetamount|formatAmount }}</td>
+ <td>
+ {{ formatAmountBySymbol(piggy.target_amount, piggy.currency_symbol, piggy.currency_dp) }}
+ </td>
</tr>
<tr>
<td>{{ 'saved_so_far'|_ }}</td>
- <td>{{ currentRelevantRepAmount(piggyBank)|formatAmount }}</td>
+ <td>
+ {{ formatAmountBySymbol(piggy.current_amount, piggy.currency_symbol, piggy.currency_dp) }}
+ </td>
</tr>
<tr>
<td>{{ 'left_to_save'|_ }}</td>
- <td>{{ (piggyBank.targetamount - currentRelevantRepAmount(piggyBank))|formatAmount }}</td>
+ <td>
+ {{ formatAmountBySymbol(piggy.left_to_save, piggy.currency_symbol, piggy.currency_dp) }}
+ </td>
</tr>
<tr>
<td>{{ 'start_date'|_ }}</td>
@@ -74,7 +80,8 @@
<tr>
<td>{{ 'suggested_amount'|_ }}</td>
<td>
- {{ suggestedMonthlyAmount(piggyBank)|formatAmount }}
+ {{ formatAmountBySymbol(piggy.save_per_month, piggy.currency_symbol, piggy.currency_dp) }}
+
</td>
</tr>
{% endif %}