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

@@ -55,7 +55,7 @@ class AmountController extends Controller
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.piggyBanks'));
app('view')->share('title', (string) trans('firefly.piggyBanks'));
app('view')->share('mainTitleIcon', 'fa-bullseye');
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
@@ -77,9 +77,12 @@ class AmountController extends Controller
{
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, today(config('app.timezone')));
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
$maxAmount = $leftOnAccount;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
}
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
return view('piggy-banks.add', compact('piggyBank', 'maxAmount', 'currency'));
}
@@ -97,9 +100,13 @@ class AmountController extends Controller
$date = session('end', today(config('app.timezone')));
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, $date);
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
$maxAmount = $leftOnAccount;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
}
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount', 'currency'));
}
@@ -125,7 +132,7 @@ class AmountController extends Controller
$this->piggyRepos->createEvent($piggyBank, $amount);
session()->flash(
'success',
(string)trans(
(string) trans(
'firefly.added_amount_to_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
)
@@ -138,7 +145,7 @@ class AmountController extends Controller
Log::error('Cannot add ' . $amount . ' because canAddAmount returned false.');
session()->flash(
'error',
(string)trans(
(string) trans(
'firefly.cannot_add_amount_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)]
)
@@ -167,7 +174,7 @@ class AmountController extends Controller
$this->piggyRepos->removeAmount($piggyBank, $amount);
session()->flash(
'success',
(string)trans(
(string) trans(
'firefly.removed_amount_from_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => $piggyBank->name]
)
@@ -176,11 +183,11 @@ class AmountController extends Controller
return redirect(route('piggy-banks.index'));
}
$amount = number_format((float)$request->get('amount'), 12, '.', '');
$amount = number_format((float) $request->get('amount'), 12, '.', '');
session()->flash(
'error',
(string)trans(
(string) trans(
'firefly.cannot_remove_from_piggy',
['amount' => app('amount')->formatAnything($currency, $amount, false), 'name' => e($piggyBank->name)]
)

View File

@@ -30,6 +30,7 @@ use FireflyIII\Http\Requests\PiggyBankUpdateRequest;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Amount;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
@@ -55,11 +56,11 @@ class EditController extends Controller
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.piggyBanks'));
app('view')->share('title', (string) trans('firefly.piggyBanks'));
app('view')->share('mainTitleIcon', 'fa-bullseye');
$this->attachments = app(AttachmentHelperInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
@@ -75,24 +76,28 @@ class EditController extends Controller
*/
public function edit(PiggyBank $piggyBank)
{
$subTitle = (string)trans('firefly.update_piggy_title', ['name' => $piggyBank->name]);
$subTitle = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]);
$subTitleIcon = 'fa-pencil';
$targetDate = null;
$startDate = null;
$note = $piggyBank->notes()->first();
// Flash some data to fill the form.
$targetDate = $piggyBank->targetdate?->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,
'account_id' => $piggyBank->account_id,
'targetamount' => number_format((float)$piggyBank->targetamount, $currency->decimal_places,'.',''),
'targetamount' => number_format((float) $piggyBank->targetamount, $currency->decimal_places, '.', ''),
'targetdate' => $targetDate,
'startdate' => $startDate,
'object_group' => $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '',
'notes' => null === $note ? '' : $note->text,
];
if(0.0 === (float) $piggyBank->targetamount) {
$preFilled['targetamount'] = '';
}
session()->flash('preFilled', $preFilled);
// put previous url in session if not redirect from store (not "return_to_edit").
@@ -117,7 +122,7 @@ class EditController extends Controller
$data = $request->getPiggyBankData();
$piggyBank = $this->piggyRepos->update($piggyBank, $data);
session()->flash('success', (string)trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name]));
session()->flash('success', (string) trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name]));
app('preferences')->mark();
// store new attachment(s):
@@ -127,7 +132,7 @@ class EditController extends Controller
$this->attachments->saveAttachmentsForModel($piggyBank, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
session()->flash('info', (string)trans('firefly.no_att_demo_user'));
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
@@ -135,7 +140,7 @@ class EditController extends Controller
}
$redirect = redirect($this->getPreviousUri('piggy-banks.edit.uri'));
if (1 === (int)$request->get('return_to_edit')) {
if (1 === (int) $request->get('return_to_edit')) {
session()->put('piggy-banks.edit.fromUpdate', true);