Skip to content

Firefly III: a personal finances manager

Validate if amount can be added.

James Colea3f4ab9b1b92
diff --git a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php
index 2b1eb52ff9..4271b52ba7 100644
--- a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php
+++ b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php
@@ -28,8 +28,10 @@ use FireflyIII\Models\PiggyBank;
use FireflyIII\Rules\IsValidPositiveAmount;
use FireflyIII\Rules\IsValidZeroOrMoreAmount;
use FireflyIII\Rules\LessThanPiggyTarget;
+use FireflyIII\Rules\PiggyBank\IsEnoughInAccounts;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
+use FireflyIII\Validation\FireflyValidator;
use Illuminate\Foundation\Http\FormRequest;
/**
@@ -47,7 +49,7 @@ class UpdateRequest extends FormRequest
*/
public function getAll(): array
{
- $fields = [
+ $fields = [
'name' => ['name', 'convertString'],
'target_amount' => ['target_amount', 'convertString'],
'start_date' => ['start_date', 'convertDateTime'],
@@ -75,7 +77,7 @@ class UpdateRequest extends FormRequest
$piggyBank = $this->route()->parameter('piggyBank');
return [
- 'name' => 'min:1|max:255|uniquePiggyBankForUser:'.$piggyBank->id,
+ 'name' => 'min:1|max:255|uniquePiggyBankForUser:' . $piggyBank->id,
'current_amount' => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()],
'target_amount' => ['nullable', new IsValidZeroOrMoreAmount()],
'start_date' => 'date|nullable',
@@ -84,11 +86,12 @@ class UpdateRequest extends FormRequest
'accounts' => 'array',
'accounts.*' => 'array',
'accounts.*.account_id' => ['required', 'numeric', 'belongsToUser:accounts,id'],
- 'accounts.*.current_amount' => ['numeric', 'nullable', new IsValidZeroOrMoreAmount(true)],
+ 'accounts.*.current_amount' => ['numeric', 'nullable', new IsValidZeroOrMoreAmount(true), new IsEnoughInAccounts($piggyBank, $this->getAll())],
'object_group_id' => 'numeric|belongsToUser:object_groups,id',
'object_group_title' => ['min:1', 'max:255'],
'transaction_currency_id' => 'exists:transaction_currencies,id|nullable',
'transaction_currency_code' => 'exists:transaction_currencies,code|nullable',
];
}
+
}
diff --git a/app/Factory/PiggyBankFactory.php b/app/Factory/PiggyBankFactory.php
index 1f28513ec7..d21abb917c 100644
--- a/app/Factory/PiggyBankFactory.php
+++ b/app/Factory/PiggyBankFactory.php
@@ -134,6 +134,12 @@ class PiggyBankFactory
$previous = $toBeLinked[$account->id]['current_amount'] ?? '0';
$diff = bcsub($info['current_amount'], $previous);
+ // if money is added, check if we can!
+ if(1 === bccomp($diff, '0') && !$this->piggyBankRepository->canAddAmount($piggyBank, $account, $diff)) {
+ Log::debug(sprintf('Cannot add amount %s to piggy bank #%d ("%s")', $diff, $piggyBank->id, $piggyBank->name));
+ continue;
+ }
+
// create event for difference.
if (0 !== bccomp($diff, '0')) {
// 2025-10-01 for issue #10990 disable this event.
diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
index bdcd9d138e..8ceb899052 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
@@ -52,7 +52,6 @@ interface PiggyBankRepositoryInterface
public function addAmountToPiggyBank(PiggyBank $piggyBank, string $amount, TransactionJournal $journal): void;
public function canAddAmount(PiggyBank $piggyBank, Account $account, string $amount): bool;
-
public function canRemoveAmount(PiggyBank $piggyBank, Account $account, string $amount): bool;
/**
diff --git a/app/Rules/PiggyBank/IsEnoughInAccounts.php b/app/Rules/PiggyBank/IsEnoughInAccounts.php
new file mode 100644
index 0000000000..ecbab27737
--- /dev/null
+++ b/app/Rules/PiggyBank/IsEnoughInAccounts.php
@@ -0,0 +1,68 @@
+<?php
+/*
+ * IsEnoughInAccounts.php
+ * Copyright (c) 2026 [email protected]
+ *
+ * This file is part of Firefly III (https://github.com/firefly-iii).
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace FireflyIII\Rules\PiggyBank;
+
+use Closure;
+use FireflyIII\Models\PiggyBank;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
+use Illuminate\Contracts\Validation\ValidationRule;
+
+class IsEnoughInAccounts implements ValidationRule
+{
+ public function __construct(
+ private readonly PiggyBank $piggyBank,
+ private readonly array $data
+ ) {}
+
+
+ #[\Override]
+ public function validate(string $attribute, mixed $value, Closure $fail): void
+ {
+ // TODO: Implement validate() method.
+ if (!array_key_exists('accounts', $this->data)) {
+ return;
+ }
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class);
+ /** @var PiggyBankRepositoryInterface $piggyRepos */
+ $piggyRepos = app(PiggyBankRepositoryInterface::class);
+
+ $accounts = $this->data['accounts'];
+ foreach ($accounts as $info) {
+ $account = $repository->find((int)$info['account_id']);
+ $amount = $info['current_amount'] ?? '0';
+ if (null === $account) {
+ $fail('validation.no_asset_account')->translate();
+ return;
+ }
+ if ('' === $amount || 0 === bccomp($amount, '0')) {
+ $fail('validation.more_than_zero_correct')->translate();
+ return;
+ }
+ $diff = bcsub($amount, $piggyRepos->getCurrentAmount($this->piggyBank, $account));
+ if (1 === bccomp($diff, '0') && !$piggyRepos->canAddAmount($this->piggyBank, $account, $amount)) {
+ $fail('validation.cannot_add_piggy_amount')->translate();
+ }
+ }
+ }
+}
diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php
index 94ccca0233..8337c6eb98 100644
--- a/app/Support/Request/ConvertsDataTypes.php
+++ b/app/Support/Request/ConvertsDataTypes.php
@@ -474,7 +474,10 @@ trait ConvertsDataTypes
if (!array_key_exists('current_amount', $entry)) {
$amount = null;
}
- $return[] = ['account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')), 'current_amount' => $amount];
+ $return[] = [
+ 'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')),
+ 'current_amount' => $amount
+ ];
}
return $return;
diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php
index e48fa63962..2fb4e477df 100644
--- a/resources/lang/en_US/validation.php
+++ b/resources/lang/en_US/validation.php
@@ -70,6 +70,7 @@ return [
'rule_action_value' => 'This value is invalid for the selected action.',
'file_already_attached' => 'Uploaded file ":name" is already attached to this object.',
'file_attached' => 'Successfully uploaded file ":name".',
+ 'cannot_add_piggy_amount' => 'This amount cannot be added to the piggy bank.',
'file_zero' => 'The file is zero bytes in size.',
'must_exist' => 'The ID in field :attribute does not exist in the database.',
'all_accounts_equal' => 'All accounts in this field must be equal.',