Skip to content

Firefly III: a personal finances manager

Fix piggy action.

James Colecb4e5a80b27c
diff --git a/app/TransactionRules/Actions/UpdatePiggybank.php b/app/TransactionRules/Actions/UpdatePiggybank.php
index a2dc6ef1d4..173306ac3a 100644
--- a/app/TransactionRules/Actions/UpdatePiggybank.php
+++ b/app/TransactionRules/Actions/UpdatePiggybank.php
@@ -83,7 +83,7 @@ class UpdatePiggybank implements ActionInterface
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
/** @var Transaction $source */
- $source = $journalObj->transactions()->where('amount', '<', 0)->first();
+ $source = $journalObj->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $destination */
$destination = $journalObj->transactions()->where('amount', '>', 0)->first();
@@ -199,12 +199,18 @@ class UpdatePiggybank implements ActionInterface
$repository->setUser($journal->user);
// how much can we add to the piggy bank?
- $toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
- Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
+ if (0 !== bccomp($piggyBank->targetamount, '0')) {
+ $toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
+ Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
+
+ // update amount to fit:
+ $amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
+ Log::debug(sprintf('Amount is now %s', $amount));
+ }
+ if (0 === bccomp($piggyBank->targetamount, '0')) {
+ Log::debug('Target amount is zero, can add anything.');
+ }
- // update amount to fit:
- $amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
- Log::debug(sprintf('Amount is now %s', $amount));
// if amount is zero, stop.
if (0 === bccomp('0', $amount)) {