Skip to content

Firefly III: a personal finances manager

New action

James Coleaf50ad3db401
diff --git a/app/TransactionRules/Actions/UpdatePiggybank.php b/app/TransactionRules/Actions/UpdatePiggybank.php
new file mode 100644
index 0000000000..2ee81e812e
--- /dev/null
+++ b/app/TransactionRules/Actions/UpdatePiggybank.php
@@ -0,0 +1,166 @@
+<?php
+declare(strict_types=1);
+
+
+namespace FireflyIII\TransactionRules\Actions;
+
+
+use FireflyIII\Models\PiggyBank;
+use FireflyIII\Models\RuleAction;
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\TransactionType;
+use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
+use FireflyIII\User;
+use Log;
+
+/**
+ * Class UpdatePiggybank
+ */
+class UpdatePiggybank implements ActionInterface
+{
+
+ /** @var RuleAction The rule action */
+ private $action;
+
+ /**
+ * TriggerInterface constructor.
+ *
+ * @param RuleAction $action
+ */
+ public function __construct(RuleAction $action)
+ {
+ $this->action = $action;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function act(TransactionJournal $journal): bool
+ {
+ Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal->id));
+ if (TransactionType::TRANSFER !== $journal->transactionType->type) {
+ Log::info(sprintf('Journal #%d is a "%s" so skip this action.', $journal->id, $journal->transactionType->type));
+
+ return false;
+ }
+ $piggyBank = $this->findPiggybank($journal->user);
+ if (null === $piggyBank) {
+ Log::info(
+ sprintf(
+ 'No piggy bank names "%s", cant execute action #%d of rule #%d ("%s")',
+ $this->action->value, $this->action->id, $this->action->rule_id, $this->action->rule->title,
+ )
+ );
+
+ return false;
+ }
+
+ Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
+
+ /** @var Transaction $source */
+ $source = $journal->transactions()->where('amount', '<', 0)->first();
+ /** @var Transaction $destination */
+ $destination = $journal->transactions()->where('amount', '>', 0)->first();
+
+ if ((int) $source->account_id === (int) $piggyBank->account_id) {
+ Log::debug('Piggy bank account is linked to source, so remove amount.');
+ $this->removeAmount($journal, $piggyBank, $destination->amount);
+
+
+ return true;
+ }
+ if ((int) $destination->account_id === (int) $piggyBank->account_id) {
+ Log::debug('Piggy bank account is linked to source, so add amount.');
+ $this->addAmount($journal, $piggyBank, $destination->amount);
+
+ return true;
+ }
+ Log::info('Piggy bank is not linked to source or destination, so no action will be taken.');
+
+ return true;
+ }
+
+ /**
+ * @param TransactionJournal $journal
+ * @param PiggyBank $piggyBank
+ * @param string $amount
+ */
+ private function addAmount(TransactionJournal $journal, PiggyBank $piggyBank, string $amount): void
+ {
+ $repository = app(PiggyBankRepositoryInterface::class);
+ $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));
+
+ // 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)) {
+ Log::warning('Amount left is zero, stop.');
+
+ return;
+ }
+
+ // make sure we can add amount:
+ if (false === $repository->canAddAmount($piggyBank, $amount)) {
+ Log::warning(sprintf('Cannot add %s to piggy bank.', $amount));
+
+ return;
+ }
+ Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
+
+ $repository->addAmount($piggyBank, $amount);
+ $repository->createEventWithJournal($piggyBank, app('steam')->positive($amount), $journal);
+ }
+
+ /**
+ * @param User $user
+ *
+ * @return PiggyBank|null
+ */
+ private function findPiggybank(User $user): ?PiggyBank
+ {
+ return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->first();
+ }
+
+ /**
+ * @param TransactionJournal $journal
+ * @param PiggyBank $piggyBank
+ * @param string $amount
+ */
+ private function removeAmount(TransactionJournal $journal, PiggyBank $piggyBank, string $amount): void
+ {
+ $repository = app(PiggyBankRepositoryInterface::class);
+ $repository->setUser($journal->user);
+
+ // how much can we remove from piggy bank?
+ $toRemove = $repository->getCurrentAmount($piggyBank);
+ Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
+ // if $amount is bigger than $toRemove, shrink it.
+ $amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
+ Log::debug(sprintf('Amount is now %s', $amount));
+
+ // if amount is zero, stop.
+ if (0 === bccomp('0', $amount)) {
+ Log::warning('Amount left is zero, stop.');
+
+ return;
+ }
+
+ // make sure we can remove amount:
+ if (false === $repository->canRemoveAmount($piggyBank, $amount)) {
+ Log::warning(sprintf('Cannot remove %s from piggy bank.', $amount));
+
+ return;
+ }
+ Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
+
+ $repository->removeAmount($piggyBank, $amount);
+ $repository->createEventWithJournal($piggyBank, app('steam')->negative($amount), $journal);
+ }
+}
diff --git a/config/firefly.php b/config/firefly.php
index a33661b0b6..cc2360cf56 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -72,6 +72,7 @@ use FireflyIII\TransactionRules\Actions\PrependNotes;
use FireflyIII\TransactionRules\Actions\RemoveAllTags;
use FireflyIII\TransactionRules\Actions\RemoveTag;
use FireflyIII\TransactionRules\Actions\SetBudget;
+use FireflyIII\TransactionRules\Actions\UpdatePiggybank;
use FireflyIII\TransactionRules\Actions\SetCategory;
use FireflyIII\TransactionRules\Actions\SetDescription;
use FireflyIII\TransactionRules\Actions\SetDestinationAccount;
@@ -506,6 +507,7 @@ return [
'convert_withdrawal' => ConvertToWithdrawal::class,
'convert_deposit' => ConvertToDeposit::class,
'convert_transfer' => ConvertToTransfer::class,
+ 'update_piggy' => UpdatePiggybank::class,
],
'context-rule-actions' => [
'set_category',
diff --git a/public/v1/js/ff/rules/create-edit.js b/public/v1/js/ff/rules/create-edit.js
index 4a39675c43..5aa87e573d 100644
--- a/public/v1/js/ff/rules/create-edit.js
+++ b/public/v1/js/ff/rules/create-edit.js
@@ -274,6 +274,11 @@ function updateActionInput(selectList) {
console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
createAutoComplete(inputResult, 'json/bills');
break;
+ case 'update_piggy':
+ console.log('Select list value is ' + selectList.val() + ', so input needs auto complete.');
+ createAutoComplete(inputResult, 'json/piggy-banks');
+ break;
+ break;
default:
console.log('Select list value is ' + selectList.val() + ', destroy auto complete, do nothing else.');
inputResult.typeahead('destroy');