This commit is contained in:
James Cole 2018-05-26 07:48:49 +02:00
parent 4031057bc0
commit 664451d0c6
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
12 changed files with 88 additions and 50 deletions

View File

@ -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'));
}
/**

View File

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

View File

@ -29,6 +29,7 @@ use FireflyIII\Models\PiggyBank;
/**
* Class PiggyBankRepetition.
* @property string $currentamount
*/
class PiggyBankRepetition extends Model
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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