diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index c376c755e6..345f28114c 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -215,6 +215,9 @@ class TagRepository implements TagRepositoryInterface } /** + * The incoming journal ($journal)'s accounts (source accounts for a withdrawal, destination accounts for a deposit) + * must match the already existing transaction's accounts exactly. + * * @param TransactionJournal $journal * @param Tag $tag * @@ -223,16 +226,21 @@ class TagRepository implements TagRepositoryInterface */ protected function matchAll(TransactionJournal $journal, Tag $tag): bool { + $checkSources = join(',', TransactionJournal::sourceAccountList($journal)->pluck('id')->toArray()); + $checkDestinations = join(',', TransactionJournal::destinationAccountList($journal)->pluck('id')->toArray()); + $match = true; /** @var TransactionJournal $check */ foreach ($tag->transactionjournals as $check) { // $checkAccount is the source_account for a withdrawal // $checkAccount is the destination_account for a deposit + $thisSources = join(',', TransactionJournal::sourceAccountList($check)->pluck('id')->toArray()); + $thisDestinations = join(',', TransactionJournal::destinationAccountList($check)->pluck('id')->toArray()); - if ($check->isWithdrawal() && TransactionJournal::sourceAccount($check)->id != TransactionJournal::destinationAccount($journal)->id) { + if ($check->isWithdrawal() && $thisSources !== $checkSources) { $match = false; } - if ($check->isDeposit() && TransactionJournal::destinationAccount($check)->id != TransactionJournal::destinationAccount($journal)->id) { + if ($check->isDeposit() && $thisDestinations !== $checkDestinations) { $match = false; } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index 58799b60d4..49781144bf 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,9 +53,15 @@ final class FromAccountContains extends AbstractTrigger implements TriggerInterf */ public function triggered(TransactionJournal $journal): bool { - $fromAccountName = strtolower($journal->source_account_name ?? TransactionJournal::sourceAccount($journal)->name); - $search = strtolower($this->triggerValue); - $strpos = strpos($fromAccountName, $search); + $fromAccountName = ''; + + /** @var Account $account */ + foreach (TransactionJournal::sourceAccountList($journal) as $account) { + $fromAccountName .= strtolower($account->name); + } + + $search = strtolower($this->triggerValue); + $strpos = strpos($fromAccountName, $search); if (!($strpos === false)) { return true; diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index 96e83fadcf..2311bbffd3 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,7 +53,13 @@ final class FromAccountEnds extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $name = strtolower($journal->source_account_name ?? TransactionJournal::sourceAccount($journal)->name); + $name = ''; + + /** @var Account $account */ + foreach (TransactionJournal::sourceAccountList($journal) as $account) { + $name .= strtolower($account->name); + } + $nameLength = strlen($name); $search = strtolower($this->triggerValue); $searchLength = strlen($search); diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 061044cf7d..473c123eaf 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,7 +53,13 @@ final class FromAccountIs extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $name = strtolower($journal->source_account_name ?? TransactionJournal::sourceAccount($journal)->name); + $name = ''; + + /** @var Account $account */ + foreach (TransactionJournal::sourceAccountList($journal) as $account) { + $name .= strtolower($account->name); + } + $search = strtolower($this->triggerValue); if ($name == $search) { diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index 62eaafac67..3bebd02b2b 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,7 +53,13 @@ final class FromAccountStarts extends AbstractTrigger implements TriggerInterfac */ public function triggered(TransactionJournal $journal): bool { - $name = strtolower($journal->source_account_name ?? TransactionJournal::sourceAccount($journal)->name); + $name = ''; + + /** @var Account $account */ + foreach (TransactionJournal::sourceAccountList($journal) as $account) { + $name .= strtolower($account->name); + } + $search = strtolower($this->triggerValue); $part = substr($name, 0, strlen($search)); diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index f6d7c31c68..6ba591c13a 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,9 +53,15 @@ final class ToAccountContains extends AbstractTrigger implements TriggerInterfac */ public function triggered(TransactionJournal $journal): bool { - $toAccountName = strtolower($journal->destination_account_name ?? TransactionJournal::destinationAccount($journal)->name); - $search = strtolower($this->triggerValue); - $strpos = strpos($toAccountName, $search); + $toAccountName = ''; + + /** @var Account $account */ + foreach (TransactionJournal::destinationAccountList($journal) as $account) { + $toAccountName .= strtolower($account->name); + } + + $search = strtolower($this->triggerValue); + $strpos = strpos($toAccountName, $search); if (!($strpos === false)) { return true; diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index f01d8d8ba5..f4c08ebd3f 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,7 +53,13 @@ final class ToAccountEnds extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $toAccountName = strtolower($journal->destination_account_name ?? TransactionJournal::destinationAccount($journal)->name); + $toAccountName = ''; + + /** @var Account $account */ + foreach (TransactionJournal::destinationAccountList($journal) as $account) { + $toAccountName .= strtolower($account->name); + } + $toAccountNameLength = strlen($toAccountName); $search = strtolower($this->triggerValue); $searchLength = strlen($search); diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 6d1ea0181e..4c3649a675 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,7 +53,13 @@ final class ToAccountIs extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $toAccountName = strtolower($journal->destination_account_name ?? TransactionJournal::destinationAccount($journal)->name); + $toAccountName = ''; + + /** @var Account $account */ + foreach (TransactionJournal::destinationAccountList($journal) as $account) { + $toAccountName .= strtolower($account->name); + } + $search = strtolower($this->triggerValue); if ($toAccountName == $search) { diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index 8f00f80d0c..ffb3e5471a 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -10,6 +10,7 @@ declare(strict_types = 1); namespace FireflyIII\Rules\Triggers; +use FireflyIII\Models\Account; use FireflyIII\Models\TransactionJournal; /** @@ -52,10 +53,15 @@ final class ToAccountStarts extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $toAccountName = strtolower($journal->destination_account_name ?? TransactionJournal::destinationAccount($journal)->name); - $search = strtolower($this->triggerValue); + $toAccountName = ''; - $part = substr($toAccountName, 0, strlen($search)); + /** @var Account $account */ + foreach (TransactionJournal::destinationAccountList($journal) as $account) { + $toAccountName .= strtolower($account->name); + } + + $search = strtolower($this->triggerValue); + $part = substr($toAccountName, 0, strlen($search)); if ($part == $search) { return true; diff --git a/app/Support/Models/TransactionJournalSupport.php b/app/Support/Models/TransactionJournalSupport.php index 86ac894a81..ec7aac7dc1 100644 --- a/app/Support/Models/TransactionJournalSupport.php +++ b/app/Support/Models/TransactionJournalSupport.php @@ -172,7 +172,7 @@ class TransactionJournalSupport extends Model if ($cache->has()) { return $cache->get(); } - $transactions = $journal->transactions()->where('amount', '>', 0)->with('account')->get(); + $transactions = $journal->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get(); $list = new Collection; /** @var Transaction $t */ foreach ($transactions as $t) { @@ -316,7 +316,7 @@ class TransactionJournalSupport extends Model if ($cache->has()) { return $cache->get(); } - $transactions = $journal->transactions()->where('amount', '<', 0)->with('account')->get(); + $transactions = $journal->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get(); $list = new Collection; /** @var Transaction $t */ foreach ($transactions as $t) {