Piggies can have no target amount.

This commit is contained in:
James Cole
2022-03-27 18:30:46 +02:00
parent 980120f1bd
commit 8ee4c2ea11
9 changed files with 94 additions and 59 deletions

View File

@@ -77,8 +77,11 @@ class AmountController extends Controller
{ {
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, today(config('app.timezone'))); $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, today(config('app.timezone')));
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$maxAmount = $leftOnAccount;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
}
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency')); return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency'));
@@ -97,8 +100,12 @@ class AmountController extends Controller
$date = session('end', today(config('app.timezone'))); $date = session('end', today(config('app.timezone')));
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date); $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date);
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$maxAmount = $leftOnAccount;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
}
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency')); return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency'));

View File

@@ -30,6 +30,7 @@ use FireflyIII\Http\Requests\PiggyBankUpdateRequest;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Amount;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector; use Illuminate\Routing\Redirector;
@@ -77,13 +78,14 @@ class EditController extends Controller
{ {
$subTitle = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); $subTitle = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]);
$subTitleIcon = 'fa-pencil'; $subTitleIcon = 'fa-pencil';
$targetDate = null;
$startDate = null;
$note = $piggyBank->notes()->first(); $note = $piggyBank->notes()->first();
// Flash some data to fill the form. // Flash some data to fill the form.
$targetDate = $piggyBank->targetdate?->format('Y-m-d'); $targetDate = $piggyBank->targetdate?->format('Y-m-d');
$startDate = $piggyBank->startdate?->format('Y-m-d'); $startDate = $piggyBank->startdate?->format('Y-m-d');
$currency = $this->accountRepository->getAccountCurrency($piggyBank->account); $currency = $this->accountRepository->getAccountCurrency($piggyBank->account);
if (null === $currency) {
$currency = Amount::getDefaultCurrency();
}
$preFilled = ['name' => $piggyBank->name, $preFilled = ['name' => $piggyBank->name,
'account_id' => $piggyBank->account_id, 'account_id' => $piggyBank->account_id,
@@ -93,6 +95,9 @@ class EditController extends Controller
'object_group' => $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '', 'object_group' => $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '',
'notes' => null === $note ? '' : $note->text, 'notes' => null === $note ? '' : $note->text,
]; ];
if(0.0 === (float) $piggyBank->targetamount) {
$preFilled['targetamount'] = '';
}
session()->flash('preFilled', $preFilled); session()->flash('preFilled', $preFilled);
// put previous url in session if not redirect from store (not "return_to_edit"). // put previous url in session if not redirect from store (not "return_to_edit").

View File

@@ -61,7 +61,7 @@ class PiggyBankStoreRequest extends FormRequest
return [ return [
'name' => 'required|between:1,255|uniquePiggyBankForUser', 'name' => 'required|between:1,255|uniquePiggyBankForUser',
'account_id' => 'required|belongsToUser:accounts', 'account_id' => 'required|belongsToUser:accounts',
'targetamount' => 'required|numeric|gte:0.01|max:1000000000', 'targetamount' => 'nullable|numeric|max:1000000000',
'startdate' => 'date', 'startdate' => 'date',
'targetdate' => 'date|nullable', 'targetdate' => 'date|nullable',
'order' => 'integer|min:1', 'order' => 'integer|min:1',

View File

@@ -65,7 +65,7 @@ class PiggyBankUpdateRequest extends FormRequest
return [ return [
'name' => sprintf('required|between:1,255|uniquePiggyBankForUser:%d', $piggy->id), 'name' => sprintf('required|between:1,255|uniquePiggyBankForUser:%d', $piggy->id),
'account_id' => 'required|belongsToUser:accounts', 'account_id' => 'required|belongsToUser:accounts',
'targetamount' => 'required|numeric|gte:0.01|max:1000000000', 'targetamount' => 'nullable|numeric|max:1000000000',
'startdate' => 'date', 'startdate' => 'date',
'targetdate' => 'date|nullable', 'targetdate' => 'date|nullable',
'order' => 'integer|min:1', 'order' => 'integer|min:1',

View File

@@ -77,6 +77,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property string $email * @property string $email
* @property int|null $user_group_id * @property int|null $user_group_id
* @method static \Illuminate\Database\Eloquent\Builder|Budget whereUserGroupId($value) * @method static \Illuminate\Database\Eloquent\Builder|Budget whereUserGroupId($value)
* @property-read Collection|\FireflyIII\Models\Note[] $notes
* @property-read int|null $notes_count
*/ */
class Budget extends Model class Budget extends Model
{ {

View File

@@ -91,8 +91,13 @@ trait ModifiesPiggyBanks
$today = today(config('app.timezone')); $today = today(config('app.timezone'));
$leftOnAccount = $this->leftOnAccount($piggyBank, $today); $leftOnAccount = $this->leftOnAccount($piggyBank, $today);
$savedSoFar = (string) $this->getRepetition($piggyBank)->currentamount; $savedSoFar = (string) $this->getRepetition($piggyBank)->currentamount;
$maxAmount = $leftOnAccount;
$leftToSave = null;
if (0.0 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount; $maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
}
$compare = bccomp($amount, $maxAmount); $compare = bccomp($amount, $maxAmount);
$result = $compare <= 0; $result = $compare <= 0;

View File

@@ -88,15 +88,23 @@ class PiggyBankTransformer extends AbstractTransformer
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank); $currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
$currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', ''); $currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', '');
// left to save: // Amounts, depending on 0.0 state of target amount
$percentage = null;
$targetAmountString = null;
$leftToSaveString = null;
$savePerMonth = null;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr); $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
$targetAmount = (string) $piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', $targetAmount) ? '0.01' : $targetAmount;
$percentage = (int) (0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
$targetAmountString = number_format((float) $targetAmount, $currency->decimal_places, '.', '');
$leftToSaveString = number_format((float) $leftToSave, $currency->decimal_places, '.', '');
$savePerMonth = number_format((float) $this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', '');
}
$startDate = $piggyBank->startdate?->toAtomString(); $startDate = $piggyBank->startdate?->toAtomString();
$targetDate = $piggyBank->targetdate?->toAtomString(); $targetDate = $piggyBank->targetdate?->toAtomString();
// target and percentage:
$targetAmount = $piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', (string)$targetAmount) ? '0.01' : $targetAmount;
$percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
return [ return [
'id' => (string) $piggyBank->id, 'id' => (string) $piggyBank->id,
@@ -109,11 +117,11 @@ class PiggyBankTransformer extends AbstractTransformer
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int) $currency->decimal_places, 'currency_decimal_places' => (int) $currency->decimal_places,
'target_amount' => number_format((float)$targetAmount, $currency->decimal_places, '.', ''), 'target_amount' => $targetAmountString,
'percentage' => $percentage, 'percentage' => $percentage,
'current_amount' => $currentAmount, 'current_amount' => $currentAmount,
'left_to_save' => number_format((float)$leftToSave, $currency->decimal_places, '.', ''), 'left_to_save' => $leftToSaveString,
'save_per_month' => number_format((float)$this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', ''), 'save_per_month' => $savePerMonth,
'start_date' => $startDate, 'start_date' => $startDate,
'target_date' => $targetDate, 'target_date' => $targetDate,
'order' => (int) $piggyBank->order, 'order' => (int) $piggyBank->order,

View File

@@ -57,6 +57,7 @@
</td> </td>
<td class="hidden-sm hidden-xs piggyBar"> <td class="hidden-sm hidden-xs piggyBar">
{% if null != piggy.percentage %}
<div class="progress progress" style="margin-bottom:0;"> <div class="progress progress" style="margin-bottom:0;">
<div <div
{% if piggy.percentage == 100 %} {% if piggy.percentage == 100 %}
@@ -71,17 +72,20 @@
{{ piggy.percentage }}% {{ piggy.percentage }}%
</div> </div>
</div> </div>
{% endif %}
</td> </td>
<td class="hidden-sm hidden-xs" style="width:40px;"> <td class="hidden-sm hidden-xs" style="width:40px;">
{% if piggy.left_to_save > 0 %} {% if piggy.left_to_save > 0 or null == piggy.left_to_save %}
<a href="{{ route('piggy-banks.add-money', piggy.id) }}" class="btn btn-default btn-xs addMoney" data-id="{{ piggy.id }}"> <a href="{{ route('piggy-banks.add-money', piggy.id) }}" class="btn btn-default btn-xs addMoney" data-id="{{ piggy.id }}">
<span data-id="{{ piggy.id }}" class="fa fa-plus"></span></a> <span data-id="{{ piggy.id }}" class="fa fa-plus"></span></a>
{% endif %} {% endif %}
</td> </td>
<td class="hidden-sm hidden-xs" style="text-align:right;"> <td class="hidden-sm hidden-xs" style="text-align:right;">
{% if null != piggy.target_amount %}
<span title="{{ 'target_amount'|_ }}">{{ formatAmountBySymbol(piggy.target_amount,piggy.currency_symbol,piggy.currency_decimal_places) }}</span> <span title="{{ 'target_amount'|_ }}">{{ formatAmountBySymbol(piggy.target_amount,piggy.currency_symbol,piggy.currency_decimal_places) }}</span>
{% endif %}
</td> </td>
<td class="hidden-sm hidden-xs" style="text-align:right;"> <td class="hidden-sm hidden-xs" style="text-align:right;">
{% if piggy.left_to_save > 0 %} {% if piggy.left_to_save > 0 %}

View File

@@ -42,24 +42,28 @@
<td>{{ piggy.object_group_title }}</a></td> <td>{{ piggy.object_group_title }}</a></td>
</tr> </tr>
{% endif %} {% endif %}
{% if null != piggy.target_amount %}
<tr> <tr>
<td>{{ 'target_amount'|_ }}</td> <td>{{ 'target_amount'|_ }}</td>
<td> <td>
{{ formatAmountBySymbol(piggy.target_amount, piggy.currency_symbol, piggy.currency_decimal_places) }} {{ formatAmountBySymbol(piggy.target_amount, piggy.currency_symbol, piggy.currency_decimal_places) }}
</td> </td>
</tr> </tr>
{% endif %}
<tr> <tr>
<td>{{ 'saved_so_far'|_ }}</td> <td>{{ 'saved_so_far'|_ }}</td>
<td> <td>
{{ formatAmountBySymbol(piggy.current_amount, piggy.currency_symbol, piggy.currency_decimal_places) }} {{ formatAmountBySymbol(piggy.current_amount, piggy.currency_symbol, piggy.currency_decimal_places) }}
</td> </td>
</tr> </tr>
{% if null != piggy.left_to_save %}
<tr> <tr>
<td>{{ 'left_to_save'|_ }}</td> <td>{{ 'left_to_save'|_ }}</td>
<td> <td>
{{ formatAmountBySymbol(piggy.left_to_save, piggy.currency_symbol, piggy.currency_decimal_places) }} {{ formatAmountBySymbol(piggy.left_to_save, piggy.currency_symbol, piggy.currency_decimal_places) }}
</td> </td>
</tr> </tr>
{% endif %}
<tr> <tr>
<td>{{ 'start_date'|_ }}</td> <td>{{ 'start_date'|_ }}</td>
<td> <td>