diff --git a/app/Support/Search/OperatorQuerySearch.php b/app/Support/Search/OperatorQuerySearch.php index f2ebfa8ed7..d28e75924a 100644 --- a/app/Support/Search/OperatorQuerySearch.php +++ b/app/Support/Search/OperatorQuerySearch.php @@ -337,7 +337,7 @@ class OperatorQuerySearch implements SearchInterface $parts = explode(',', $value); $collection = new Collection; foreach ($parts as $accountId) { - $account = $this->accountRepository->findNull((int) $value); + $account = $this->accountRepository->findNull((int) $accountId); if (null !== $account) { $collection->push($account); } diff --git a/app/TransactionRules/Actions/ActionInterface.php b/app/TransactionRules/Actions/ActionInterface.php index 4c845f08ba..90c4e5dd3d 100644 --- a/app/TransactionRules/Actions/ActionInterface.php +++ b/app/TransactionRules/Actions/ActionInterface.php @@ -37,16 +37,6 @@ interface ActionInterface */ public function __construct(RuleAction $action); - /** - * Execute the action. - * @deprecated - * - * @param TransactionJournal $journal - * - * @return bool - */ - public function act(TransactionJournal $journal): bool; - /** * Execute the action on an array. * diff --git a/app/TransactionRules/Actions/AddTag.php b/app/TransactionRules/Actions/AddTag.php index 92ad271402..743580701c 100644 --- a/app/TransactionRules/Actions/AddTag.php +++ b/app/TransactionRules/Actions/AddTag.php @@ -46,42 +46,6 @@ class AddTag implements ActionInterface $this->action = $action; } - /** - * @inheritDoc - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - // journal has this tag maybe? - /** @var TagFactory $factory */ - $factory = app(TagFactory::class); - $factory->setUser($journal->user); - - // TODO explode value on comma? - - - $tag = $factory->findOrCreate($this->action->action_value); - - if (null === $tag) { - // could not find, could not create tag. - Log::error(sprintf('RuleAction AddTag. Could not find or create tag "%s"', $this->action->action_value)); - - return false; - } - $count = $journal->tags()->where('tag_id', $tag->id)->count(); - if (0 === $count) { - $journal->tags()->save($tag); - $journal->touch(); - Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal->id)); - - return true; - } - Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id)); - - return false; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/AppendDescription.php b/app/TransactionRules/Actions/AppendDescription.php index 49ee90c680..370d747792 100644 --- a/app/TransactionRules/Actions/AppendDescription.php +++ b/app/TransactionRules/Actions/AppendDescription.php @@ -44,23 +44,6 @@ class AppendDescription implements ActionInterface $this->action = $action; } - /** - * Append description with X - * - * @param TransactionJournal $journal - * @codeCoverageIgnore - * @deprecated - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - Log::debug(sprintf('RuleAction AppendDescription appended "%s" to "%s".', $this->action->action_value, $journal->description)); - $journal->description .= $this->action->action_value; - $journal->save(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/AppendNotes.php b/app/TransactionRules/Actions/AppendNotes.php index cb18049e81..949ae9721f 100644 --- a/app/TransactionRules/Actions/AppendNotes.php +++ b/app/TransactionRules/Actions/AppendNotes.php @@ -45,30 +45,6 @@ class AppendNotes implements ActionInterface $this->action = $action; } - /** - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $dbNote = $journal->notes()->first(); - if (null === $dbNote) { - $dbNote = new Note; - $dbNote->noteable()->associate($journal); - } - $notes = $dbNote->text; - Log::debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $this->action->action_value, $notes)); - $notes .= $this->action->action_value; - $dbNote->text = $notes; - $dbNote->save(); - $journal->save(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/ClearBudget.php b/app/TransactionRules/Actions/ClearBudget.php index dfeb6bc54b..172b95d3a0 100644 --- a/app/TransactionRules/Actions/ClearBudget.php +++ b/app/TransactionRules/Actions/ClearBudget.php @@ -41,30 +41,6 @@ class ClearBudget implements ActionInterface { } - /** - * Clear all budgets - * - * @param TransactionJournal $journal - * @codeCoverageIgnore - * @return bool - * @deprecated - */ - public function act(TransactionJournal $journal): bool - { - $journal->budgets()->detach(); - $journal->touch(); - - // also remove budgets from transactions (although no longer necessary) - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $transaction->budgets()->detach(); - } - - Log::debug(sprintf('RuleAction ClearBudget removed all budgets from journal %d.', $journal->id)); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/ClearCategory.php b/app/TransactionRules/Actions/ClearCategory.php index 37012f7513..e0276c728b 100644 --- a/app/TransactionRules/Actions/ClearCategory.php +++ b/app/TransactionRules/Actions/ClearCategory.php @@ -41,31 +41,6 @@ class ClearCategory implements ActionInterface { } - /** - * Clear all categories - * - * @param TransactionJournal $journal - * @codeCoverageIgnore - * @deprecated - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $journal->categories()->detach(); - $journal->touch(); - - // also remove categories from transactions: - /** @var Transaction $transaction */ - foreach ($journal->transactions as $transaction) { - $transaction->categories()->detach(); - } - - Log::debug(sprintf('RuleAction ClearCategory removed all categories from journal %d.', $journal->id)); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/ClearNotes.php b/app/TransactionRules/Actions/ClearNotes.php index 27362be32a..c2b8d6b055 100644 --- a/app/TransactionRules/Actions/ClearNotes.php +++ b/app/TransactionRules/Actions/ClearNotes.php @@ -42,28 +42,6 @@ class ClearNotes implements ActionInterface { } - /** - * Remove notes - * - * @param TransactionJournal $journal - * @codeCoverageIgnore - * @deprecated - * @return bool - * @throws Exception - */ - public function act(TransactionJournal $journal): bool - { - Log::debug(sprintf('RuleAction ClearNotes removed all notes.')); - $notes = $journal->notes()->get(); - /** @var Note $note */ - foreach ($notes as $note) { - $note->delete(); - } - $journal->touch(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/ConvertToDeposit.php b/app/TransactionRules/Actions/ConvertToDeposit.php index bb39f67418..e6064cb3f4 100644 --- a/app/TransactionRules/Actions/ConvertToDeposit.php +++ b/app/TransactionRules/Actions/ConvertToDeposit.php @@ -53,112 +53,6 @@ class ConvertToDeposit implements ActionInterface $this->action = $action; } - /** - * Execute the action. - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - * @throws FireflyException - */ - public function act(TransactionJournal $journal): bool - { - $type = $journal->transactionType->type; - if (TransactionType::DEPOSIT === $type) { - // @codeCoverageIgnoreStart - Log::error(sprintf('Journal #%d is already a deposit (rule "%s").', $journal->id, $this->action->rule->title)); - - return false; - // @codeCoverageIgnoreEnd - } - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // break if count is zero: - if (1 !== $sourceTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d source transactions. ConvertToDeposit failed. (rule "%s").', - [$journal->id, $sourceTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - if (0 === $destTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d dest transactions. ConvertToDeposit failed. (rule "%s").', - [$journal->id, $destTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - - - if (TransactionType::WITHDRAWAL === $type) { - Log::debug('Going to transform a withdrawal to a deposit.'); - - return $this->convertWithdrawal($journal); - } - if (TransactionType::TRANSFER === $type) { - Log::debug('Going to transform a transfer to a deposit.'); - - return $this->convertTransfer($journal); - } - - return false; // @codeCoverageIgnore - } - - /** - * Input is a transfer from A to B. - * Output is a deposit from C to B. - * - * @param TransactionJournal $journal - * @return bool - * @throws FireflyException - * @deprecated - */ - private function convertTransfer(TransactionJournal $journal): bool - { - // find or create revenue account. - /** @var AccountFactory $factory */ - $factory = app(AccountFactory::class); - $factory->setUser($journal->user); - - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // get the action value, or use the original source name in case the action value is empty: - // this becomes a new or existing revenue account. - /** @var Account $source */ - $source = $sourceTransactions->first()->account; - $revenueName = '' === $this->action->action_value ? $source->name : $this->action->action_value; - $revenue = $factory->findOrCreate($revenueName, AccountType::REVENUE); - - Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $source->name)); - unset($source); - - // update source transaction(s) to be revenue account - $journal->transactions() - ->where('amount', '<', 0) - ->update(['account_id' => $revenue->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - Log::debug('Converted transfer to deposit.'); - - return true; - } - /** * Input is a transfer from A to B. * Output is a deposit from C to B. @@ -202,59 +96,6 @@ class ConvertToDeposit implements ActionInterface return true; } - /** - * Input is a withdrawal from A to B - * Is converted to a deposit from C to A. - * - * @param TransactionJournal $journal - * @deprecated - * @return bool - * @throws FireflyException - */ - private function convertWithdrawal(TransactionJournal $journal): bool - { - // find or create revenue account. - /** @var AccountFactory $factory */ - $factory = app(AccountFactory::class); - $factory->setUser($journal->user); - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // get the action value, or use the original destination name in case the action value is empty: - // this becomes a new or existing revenue account. - /** @var Account $destination */ - $destination = $destTransactions->first()->account; - $revenueName = '' === $this->action->action_value ? $destination->name : $this->action->action_value; - $revenue = $factory->findOrCreate($revenueName, AccountType::REVENUE); - - Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name)); - - - // get source account from transaction(s). - /** @var Account $source */ - $source = $sourceTransactions->first()->account; - - // update source transaction(s) to be revenue account - $journal->transactions() - ->where('amount', '<', 0) - ->update(['account_id' => $revenue->id]); - - // update destination transaction(s) to be original source account(s). - $journal->transactions() - ->where('amount', '>', 0) - ->update(['account_id' => $source->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - - Log::debug('Converted withdrawal to deposit.'); - - return true; - } - /** * Input is a withdrawal from A to B * Is converted to a deposit from C to A. diff --git a/app/TransactionRules/Actions/ConvertToTransfer.php b/app/TransactionRules/Actions/ConvertToTransfer.php index 7ef4733341..bfae1dd9b8 100644 --- a/app/TransactionRules/Actions/ConvertToTransfer.php +++ b/app/TransactionRules/Actions/ConvertToTransfer.php @@ -52,166 +52,6 @@ class ConvertToTransfer implements ActionInterface $this->action = $action; } - /** - * Execute the action. - * @deprecated - * @codeCoverageIgnore - * @param TransactionJournal $journal - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $type = $journal->transactionType->type; - if (TransactionType::TRANSFER === $type) { - // @codeCoverageIgnoreStart - Log::error(sprintf('Journal #%d is already a transfer so cannot be converted (rule "%s").', $journal->id, $this->action->rule->title)); - - return false; - // @codeCoverageIgnoreEnd - } - // find the asset account in the action value. - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - $repository->setUser($journal->user); - $asset = $repository->findByName( - $this->action->action_value, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE] - ); - if (null === $asset) { - // @codeCoverageIgnoreStart - Log::error( - sprintf( - 'Journal #%d cannot be converted because no asset with name "%s" exists (rule "%s").', $journal->id, $this->action->action_value, - $this->action->rule->title - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // break if count is zero: - if (1 !== $sourceTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d source transactions. ConvertToTransfer failed. (rule "%s").', - [$journal->id, $sourceTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - if (0 === $destTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d dest transactions. ConvertToTransfer failed. (rule "%s").', - [$journal->id, $destTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - - - if (TransactionType::WITHDRAWAL === $type) { - Log::debug('Going to transform a withdrawal to a transfer.'); - - return $this->convertWithdrawal($journal, $asset); - } - if (TransactionType::DEPOSIT === $type) { - Log::debug('Going to transform a deposit to a transfer.'); - - return $this->convertDeposit($journal, $asset); - } - - return false; // @codeCoverageIgnore - } - - /** - * A deposit is from Revenue to Asset. - * We replace the Revenue with another asset. - * @deprecated - * @codeCoverageIgnore - * @param TransactionJournal $journal - * @param Account $assetAccount - * - * @return bool - */ - private function convertDeposit(TransactionJournal $journal, Account $assetAccount): bool - { - /** @var Account $destinationAsset */ - $destinationAsset = $journal->transactions()->where('amount', '>', 0)->first()->account; - if ($destinationAsset->id === $assetAccount->id) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule "%s").', - [$journal->id, $assetAccount->name, $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - // update source transactions - $journal->transactions()->where('amount', '<', 0) - ->update(['account_id' => $assetAccount->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - Log::debug('Converted deposit to transfer.'); - - return true; - } - - /** - * A withdrawal is from Asset to Expense. - * We replace the Expense with another asset. - * @deprecated - * @codeCoverageIgnore - * @param TransactionJournal $journal - * @param Account $assetAccount - * - * @return bool - */ - private function convertWithdrawal(TransactionJournal $journal, Account $assetAccount): bool - { - /** @var Account $sourceAsset */ - $sourceAsset = $journal->transactions()->where('amount', '<', 0)->first()->account; - if ($sourceAsset->id === $assetAccount->id) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has already has "%s" as a source asset. ConvertToTransfer failed. (rule "%s").', - [$journal->id, $assetAccount->name, $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - // update destination transactions - $journal->transactions()->where('amount', '>', 0) - ->update(['account_id' => $assetAccount->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - Log::debug('Converted withdrawal to transfer.'); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/ConvertToWithdrawal.php b/app/TransactionRules/Actions/ConvertToWithdrawal.php index f9b2fca188..d51e3090f5 100644 --- a/app/TransactionRules/Actions/ConvertToWithdrawal.php +++ b/app/TransactionRules/Actions/ConvertToWithdrawal.php @@ -53,168 +53,6 @@ class ConvertToWithdrawal implements ActionInterface $this->action = $action; } - /** - * Execute the action. - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - * @throws FireflyException - */ - public function act(TransactionJournal $journal): bool - { - $type = $journal->transactionType->type; - if (TransactionType::WITHDRAWAL === $type) { - // @codeCoverageIgnoreStart - Log::error(sprintf('Journal #%d is already a withdrawal (rule "%s").', $journal->id, $this->action->rule->title)); - - return false; - // @codeCoverageIgnoreEnd - } - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // break if count is zero: - if (1 !== $sourceTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d source transactions. ConvertToWithdrawal failed. (rule "%s").', - [$journal->id, $sourceTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - if (0 === $destTransactions->count()) { - // @codeCoverageIgnoreStart - Log::error( - vsprintf( - 'Journal #%d has %d dest transactions. ConvertToWithdrawal failed. (rule "%s").', - [$journal->id, $destTransactions->count(), $this->action->rule->title] - ) - ); - - return false; - // @codeCoverageIgnoreEnd - } - - - if (TransactionType::DEPOSIT === $type) { - Log::debug('Going to transform a deposit to a withdrawal.'); - - return $this->convertDeposit($journal); - } - if (TransactionType::TRANSFER === $type) { - Log::debug('Going to transform a transfer to a withdrawal.'); - - return $this->convertTransfer($journal); - } - - return false; // @codeCoverageIgnore - } - - /** - * Input is a deposit from A to B - * Is converted to a withdrawal from B to C. - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - * @throws FireflyException - */ - private function convertDeposit(TransactionJournal $journal): bool - { - // find or create expense account. - /** @var AccountFactory $factory */ - $factory = app(AccountFactory::class); - $factory->setUser($journal->user); - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get(); - - // get the action value, or use the original source revenue name in case the action value is empty: - // this becomes a new or existing expense account. - /** @var Account $source */ - $source = $sourceTransactions->first()->account; - $expenseName = '' === $this->action->action_value ? $source->name : $this->action->action_value; - $expense = $factory->findOrCreate($expenseName, AccountType::EXPENSE); - - Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $source->name)); - unset($source); - - // get destination asset account from transaction(s). - /** @var Account $destination */ - $destination = $destTransactions->first()->account; - - // update source transaction(s) to be the original destination account - $journal->transactions() - ->where('amount', '<', 0) - ->update(['account_id' => $destination->id]); - - // update destination transaction(s) to be new expense account. - $journal->transactions() - ->where('amount', '>', 0) - ->update(['account_id' => $expense->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - - Log::debug('Converted deposit to withdrawal.'); - - return true; - } - - /** - * Input is a transfer from A to B. - * Output is a withdrawal from A to C. - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - * @throws FireflyException - */ - private function convertTransfer(TransactionJournal $journal): bool - { - // find or create expense account. - /** @var AccountFactory $factory */ - $factory = app(AccountFactory::class); - $factory->setUser($journal->user); - - $destTransactions = $journal->transactions()->where('amount', '>', 0)->get(); - - // get the action value, or use the original destination name in case the action value is empty: - // this becomes a new or existing expense account. - /** @var Account $destination */ - $destination = $destTransactions->first()->account; - $expenseName = '' === $this->action->action_value ? $destination->name : $this->action->action_value; - $expense = $factory->findOrCreate($expenseName, AccountType::EXPENSE); - - Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name)); - unset($source); - - // update destination transaction(s) to be the expense account - $journal->transactions() - ->where('amount', '>', 0) - ->update(['account_id' => $expense->id]); - - // change transaction type of journal: - $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); - $journal->transaction_type_id = $newType->id; - $journal->save(); - Log::debug('Converted transfer to withdrawal.'); - - return true; - } - - /** * Input is a transfer from A to B. * Output is a withdrawal from A to C. diff --git a/app/TransactionRules/Actions/DeleteTransaction.php b/app/TransactionRules/Actions/DeleteTransaction.php index 9a53db92f0..9a16f3acb0 100644 --- a/app/TransactionRules/Actions/DeleteTransaction.php +++ b/app/TransactionRules/Actions/DeleteTransaction.php @@ -44,43 +44,6 @@ class DeleteTransaction implements ActionInterface { } - /** - * Will delete transaction journal. Also the group if no other journals are in the group. - * @param TransactionJournal $journal - * - * @return bool - * @throws Exception - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - - $count = $journal->transactionGroup->transactionJournals()->count(); - - // destroy entire group. - if (1 === $count) { - Log::debug( - sprintf( - 'RuleAction DeleteTransaction DELETED the entire transaction group of journal #%d ("%s").', - $journal->id, $journal->description - ) - ); - $service = app(TransactionGroupDestroyService::class); - $service->destroy($journal->transactionGroup); - - return true; - } - Log::debug(sprintf('RuleAction DeleteTransaction DELETED transaction journal #%d ("%s").', $journal->id, $journal->description)); - - // trigger delete factory: - /** @var JournalDestroyService $service */ - $service = app(JournalDestroyService::class); - $service->destroy($journal); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/LinkToBill.php b/app/TransactionRules/Actions/LinkToBill.php index f78169e729..5331f14577 100644 --- a/app/TransactionRules/Actions/LinkToBill.php +++ b/app/TransactionRules/Actions/LinkToBill.php @@ -50,36 +50,6 @@ class LinkToBill implements ActionInterface $this->action = $action; } - /** - * Set bill to be X. - * @param TransactionJournal $journal - * - * @return bool - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - /** @var BillRepositoryInterface $repository */ - $repository = app(BillRepositoryInterface::class); - $repository->setUser($this->action->rule->user); - $billName = (string) $this->action->action_value; - $bill = $repository->findByName($billName); - - if (null !== $bill && $journal->transactionType->type === TransactionType::WITHDRAWAL) { - $journal->bill()->associate($bill); - $journal->save(); - Log::debug(sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal->id, $bill->id, $bill->name)); - - return true; - } - - Log::error(sprintf('RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found!', $journal->id, $billName)); - - - return false; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/PrependDescription.php b/app/TransactionRules/Actions/PrependDescription.php index 6ba9431b30..92c0f663ef 100644 --- a/app/TransactionRules/Actions/PrependDescription.php +++ b/app/TransactionRules/Actions/PrependDescription.php @@ -45,24 +45,6 @@ class PrependDescription implements ActionInterface $this->action = $action; } - /** - * Prepend description with X - * @codeCoverageIgnore - * @deprecated - * - * @param TransactionJournal $journal - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - Log::debug(sprintf('RuleAction PrependDescription prepended "%s" to "%s".', $this->action->action_value, $journal->description)); - $journal->description = $this->action->action_value . $journal->description; - $journal->save(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/PrependNotes.php b/app/TransactionRules/Actions/PrependNotes.php index cecf8b37b3..c7d817c84d 100644 --- a/app/TransactionRules/Actions/PrependNotes.php +++ b/app/TransactionRules/Actions/PrependNotes.php @@ -45,30 +45,6 @@ class PrependNotes implements ActionInterface $this->action = $action; } - /** - * Prepend notes with X - * - * @param TransactionJournal $journal - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $dbNote = $journal->notes()->first(); - if (null === $dbNote) { - $dbNote = new Note; - $dbNote->noteable()->associate($journal); - } - $notes = $dbNote->text; - Log::debug(sprintf('RuleAction PrependNotes prepended "%s" with "%s".', $notes, $this->action->action_value)); - $notes = $this->action->action_value . $notes; - $dbNote->text = $notes; - $dbNote->save(); - $journal->save(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/RemoveAllTags.php b/app/TransactionRules/Actions/RemoveAllTags.php index 4af62b1fe3..5f10978d29 100644 --- a/app/TransactionRules/Actions/RemoveAllTags.php +++ b/app/TransactionRules/Actions/RemoveAllTags.php @@ -42,23 +42,6 @@ class RemoveAllTags implements ActionInterface { } - /** - * Remove all tags - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - Log::debug(sprintf('RuleAction ClearCategory removed all tags from journal %d.', $journal->id)); - $journal->tags()->detach(); - $journal->touch(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/RemoveTag.php b/app/TransactionRules/Actions/RemoveTag.php index 64b2f61f60..16f3580846 100644 --- a/app/TransactionRules/Actions/RemoveTag.php +++ b/app/TransactionRules/Actions/RemoveTag.php @@ -44,32 +44,6 @@ class RemoveTag implements ActionInterface $this->action = $action; } - /** - * Remove tag X - * @deprecated - * @codeCoverageIgnore - * @param TransactionJournal $journal - * - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - // if tag does not exist, no need to continue: - $name = $this->action->action_value; - $tag = $journal->user->tags()->where('tag', $name)->first(); - - if (null !== $tag) { - Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id)); - $journal->tags()->detach([$tag->id]); - $journal->touch(); - - return true; - } - Log::debug(sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal->id)); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/SetBudget.php b/app/TransactionRules/Actions/SetBudget.php index a0e499a1a4..fab7f39671 100644 --- a/app/TransactionRules/Actions/SetBudget.php +++ b/app/TransactionRules/Actions/SetBudget.php @@ -46,46 +46,6 @@ class SetBudget implements ActionInterface $this->action = $action; } - /** - * Set budget. - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $search = $this->action->action_value; - - $budget = $journal->user->budgets()->where('name', $search)->first(); - if (null === $budget) { - Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search)); - - return false; - } - - if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) { - Log::debug( - sprintf( - 'RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a %s.', - $journal->id, - $search, - $journal->transactionType->type - ) - ); - - return true; - } - - Log::debug(sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal->id, $budget->id, $budget->name)); - - $journal->budgets()->sync([$budget->id]); - $journal->touch(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/SetCategory.php b/app/TransactionRules/Actions/SetCategory.php index ee8451d8b8..903dbfe975 100644 --- a/app/TransactionRules/Actions/SetCategory.php +++ b/app/TransactionRules/Actions/SetCategory.php @@ -47,35 +47,6 @@ class SetCategory implements ActionInterface $this->action = $action; } - /** - * Set category X - * - * @param TransactionJournal $journal - * @deprecated - * @codeCoverageIgnore - * @return bool - */ - public function act(TransactionJournal $journal): bool - { - $name = $this->action->action_value; - - /** @var CategoryFactory $factory */ - $factory = app(CategoryFactory::class); - $factory->setUser($journal->user); - $category = $factory->findOrCreate(null, $name); - if (null === $category) { - Log::error(sprintf('Action SetCategory did not fire because "%s" did not result in a valid category.', $name)); - - return false; - } - - $journal->categories()->sync([$category->id]); - - Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to category #%d ("%s").', $journal->id, $category->id, $category->name)); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/SetDescription.php b/app/TransactionRules/Actions/SetDescription.php index 8bd2a67458..6eae077bc1 100644 --- a/app/TransactionRules/Actions/SetDescription.php +++ b/app/TransactionRules/Actions/SetDescription.php @@ -44,33 +44,6 @@ class SetDescription implements ActionInterface $this->action = $action; } - /** - * Set description to X - * @param TransactionJournal $journal - * - * @return bool - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - $oldDescription = $journal->description; - $journal->description = $this->action->action_value; - $journal->save(); - - Log::debug( - sprintf( - 'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".', - $journal->id, - $oldDescription, - $this->action->action_value - ) - ); - $journal->touch(); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index d6ecd3eac5..3f4444579b 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -49,19 +49,6 @@ class SetDestinationAccount implements ActionInterface $this->action = $action; } - /** - * Set destination account to X - * @param TransactionJournal $journal - * - * @return bool - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - return false; - } - /** * @return Account|null */ diff --git a/app/TransactionRules/Actions/SetNotes.php b/app/TransactionRules/Actions/SetNotes.php index 168962cd7c..e86f53082d 100644 --- a/app/TransactionRules/Actions/SetNotes.php +++ b/app/TransactionRules/Actions/SetNotes.php @@ -44,31 +44,6 @@ class SetNotes implements ActionInterface $this->action = $action; } - /** - * Set notes to X - * - * @param TransactionJournal $journal - * @return bool - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - $dbNote = $journal->notes()->first(); - if (null === $dbNote) { - $dbNote = new Note; - $dbNote->noteable()->associate($journal); - } - $oldNotes = $dbNote->text; - $dbNote->text = $this->action->action_value; - $dbNote->save(); - $journal->save(); - - Log::debug(sprintf('RuleAction SetNotes changed the notes of journal #%d from "%s" to "%s".', $journal->id, $oldNotes, $this->action->action_value)); - - return true; - } - /** * @inheritDoc */ diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index 23f3aace23..ef22377f39 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -49,19 +49,6 @@ class SetSourceAccount implements ActionInterface $this->action = $action; } - /** - * Set source account to X - * - * @param TransactionJournal $journal - * @return bool - * @deprecated - * @codeCoverageIgnore - */ - public function act(TransactionJournal $journal): bool - { - return false; - } - /** * @param string $type * diff --git a/app/TransactionRules/Actions/UpdatePiggybank.php b/app/TransactionRules/Actions/UpdatePiggybank.php index 7460d9c983..5f3a530175 100644 --- a/app/TransactionRules/Actions/UpdatePiggybank.php +++ b/app/TransactionRules/Actions/UpdatePiggybank.php @@ -55,60 +55,14 @@ class UpdatePiggybank implements ActionInterface } /** - * @inheritDoc + * @param array $journalArray + * @param PiggyBank $piggyBank + * @param string $amount */ - 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 + private function addAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void { + $user = User::find($journalArray['user_id']); + $journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']); $repository = app(PiggyBankRepositoryInterface::class); $repository->setUser($journal->user); @@ -150,12 +104,14 @@ class UpdatePiggybank implements ActionInterface } /** - * @param TransactionJournal $journal - * @param PiggyBank $piggyBank - * @param string $amount + * @param array $journalArray + * @param PiggyBank $piggyBank + * @param string $amount */ - private function removeAmount(TransactionJournal $journal, PiggyBank $piggyBank, string $amount): void + private function removeAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void { + $user = User::find($journalArray['user_id']); + $journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']); $repository = app(PiggyBankRepositoryInterface::class); $repository->setUser($journal->user); @@ -190,6 +146,42 @@ class UpdatePiggybank implements ActionInterface */ public function actOnArray(array $journal): bool { - // TODO: Implement actOnArray() method. + Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id'])); + if (TransactionType::TRANSFER !== $journal['transaction_type_type']) { + Log::info(sprintf('Journal #%d is a "%s" so skip this action.', $journal['transaction_journal_id'], $journal['transaction_type_type'])); + + return false; + } + $user = User::find($journal['user_id']); + + $piggyBank = $this->findPiggybank($user); + if (null === $piggyBank) { + Log::info(sprintf('No piggy bank names "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id)); + + return false; + } + + Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name)); + + /** @var Transaction $source */ + $source = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->where('amount', '<', 0)->first(); + /** @var Transaction $destination */ + $destination = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->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; } } diff --git a/tests/Traits/CollectsValues.php b/tests/Traits/CollectsValues.php index 71507e3f71..558578037e 100644 --- a/tests/Traits/CollectsValues.php +++ b/tests/Traits/CollectsValues.php @@ -31,6 +31,7 @@ use FireflyIII\Models\AccountType; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\Category; +use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; @@ -82,6 +83,14 @@ trait CollectsValues return $this->user()->bills()->inRandomOrder()->first(); } + /** + * @return PiggyBank + */ + public function getRandomPiggyBank(): PiggyBank + { + return $this->user()->piggyBanks()->inRandomOrder()->first(); + } + /** * @return Tag @@ -99,6 +108,14 @@ trait CollectsValues return $this->getRandomJournal(TransactionType::WITHDRAWAL); } + /** + * @return TransactionJournal + */ + public function getRandomTransfer(): TransactionJournal + { + return $this->getRandomJournal(TransactionType::TRANSFER); + } + /** * @return TransactionJournal */ diff --git a/tests/Unit/TransactionRules/Actions/UpdatePiggybankTest.php b/tests/Unit/TransactionRules/Actions/UpdatePiggybankTest.php new file mode 100644 index 0000000000..a2c948d4a8 --- /dev/null +++ b/tests/Unit/TransactionRules/Actions/UpdatePiggybankTest.php @@ -0,0 +1,90 @@ +. + */ + +namespace Tests\Unit\TransactionRules\Actions; + + +use FireflyIII\Models\RuleAction; +use FireflyIII\TransactionRules\Actions\UpdatePiggybank; +use Log; +use Tests\TestCase; + +class UpdatePiggybankTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::info(sprintf('Now in %s.', get_class($this))); + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\UpdatePiggybank + */ + public function testActOnArraySource(): void + { + // from saving to checking + $transfer = $this->user()->transactionJournals()->where('description', 'Transfer for piggy bank 1')->first(); + // update (the only) piggy to belong to source: + $piggy = $this->user()->piggyBanks()->where('piggy_banks.name', 'Action test 1')->first(); + + + $array = [ + 'transaction_journal_id' => $transfer->id, + 'user_id' => $this->user()->id, + 'transaction_type_type' => 'Transfer', + ]; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $piggy->name; + $action = new UpdatePiggybank($ruleAction); + $result = $action->actOnArray($array); + $this->assertTrue($result); + } + + /** + * @covers \FireflyIII\TransactionRules\Actions\UpdatePiggybank + */ + public function testActOnArrayDestination(): void + { + // from saving to checking + $transfer = $this->user()->transactionJournals()->where('description', 'Transfer for piggy bank 2')->first(); + // update (the only) piggy to belong to source: + $piggy = $this->user()->piggyBanks()->where('piggy_banks.name', 'Action test 1')->first(); + + + $array = [ + 'transaction_journal_id' => $transfer->id, + 'user_id' => $this->user()->id, + 'transaction_type_type' => 'Transfer', + ]; + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = $piggy->name; + $action = new UpdatePiggybank($ruleAction); + $result = $action->actOnArray($array); + $this->assertTrue($result); + } +} \ No newline at end of file