From a45a050e7d20fe96e6181960ef245b0510d328df Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 18 Jul 2023 06:30:45 +0200 Subject: [PATCH] Fix https://github.com/firefly-iii/firefly-iii/issues/7749 --- .../Extensions/CollectorProperties.php | 1 + app/Helpers/Collector/GroupCollector.php | 35 +++++++++++++++++-- .../Collector/GroupCollectorInterface.php | 12 +++++++ .../Controllers/Account/ShowController.php | 6 ++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/Helpers/Collector/Extensions/CollectorProperties.php b/app/Helpers/Collector/Extensions/CollectorProperties.php index 328b920256..4735c40275 100644 --- a/app/Helpers/Collector/Extensions/CollectorProperties.php +++ b/app/Helpers/Collector/Extensions/CollectorProperties.php @@ -33,6 +33,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; trait CollectorProperties { public const TEST = 'Test'; + private bool $expandGroupSearch; private array $fields; private bool $hasAccountInfo; private bool $hasBillInformation; diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php index cbbb97e89d..e6753daa01 100644 --- a/app/Helpers/Collector/GroupCollector.php +++ b/app/Helpers/Collector/GroupCollector.php @@ -76,6 +76,7 @@ class GroupCollector implements GroupCollectorInterface $this->hasNotesInformation = false; $this->hasJoinedTagTables = false; $this->hasJoinedAttTables = false; + $this->expandGroupSearch = false; $this->hasJoinedMetaTables = false; $this->integerFields = [ 'transaction_group_id', @@ -454,6 +455,14 @@ class GroupCollector implements GroupCollectorInterface return $this; } + /** + * @return bool + */ + public function getExpandGroupSearch(): bool + { + return $this->expandGroupSearch; + } + /** * Return the transaction journals without group information. Is useful in some instances. * @@ -480,10 +489,16 @@ class GroupCollector implements GroupCollectorInterface * Return the groups. * * @return Collection - * @throws FireflyException */ public function getGroups(): Collection { + if ($this->expandGroupSearch) { + // get group ID's for the query: + $groupIds = $this->getCollectedGroupIds(); + // add to query: + $this->query->orWhereIn('transaction_journals.transaction_group_id', $groupIds); + } + $result = $this->query->get($this->fields); // now to parse this into an array. @@ -505,6 +520,14 @@ class GroupCollector implements GroupCollectorInterface return $collection; } + /** + * @return array + */ + private function getCollectedGroupIds(): array + { + return $this->query->get(['transaction_journals.transaction_group_id'])->pluck('transaction_group_id')->toArray(); + } + /** * @param Collection $collection * @@ -516,7 +539,7 @@ class GroupCollector implements GroupCollectorInterface $groups = []; /** @var TransactionJournal $augumentedJournal */ foreach ($collection as $augumentedJournal) { - $groupId = $augumentedJournal->transaction_group_id; + $groupId = (int)$augumentedJournal->transaction_group_id; if (!array_key_exists($groupId, $groups)) { // make new array @@ -871,6 +894,14 @@ class GroupCollector implements GroupCollectorInterface return $this; } + /** + * @param bool $expandGroupSearch + */ + public function setExpandGroupSearch(bool $expandGroupSearch): void + { + $this->expandGroupSearch = $expandGroupSearch; + } + /** * @inheritDoc */ diff --git a/app/Helpers/Collector/GroupCollectorInterface.php b/app/Helpers/Collector/GroupCollectorInterface.php index 887b199593..c8dabf0e82 100644 --- a/app/Helpers/Collector/GroupCollectorInterface.php +++ b/app/Helpers/Collector/GroupCollectorInterface.php @@ -583,6 +583,11 @@ interface GroupCollectorInterface */ public function foreignAmountMore(string $amount): GroupCollectorInterface; + /** + * @return bool + */ + public function getExpandGroupSearch(): bool; + /** * Return the transaction journals without group information. Is useful in some instances. * @@ -1072,6 +1077,11 @@ interface GroupCollectorInterface */ public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface; + /** + * @param bool $expandGroupSearch + */ + public function setExpandGroupSearch(bool $expandGroupSearch); + /** * Look for specific external ID's. * @@ -1502,4 +1512,6 @@ interface GroupCollectorInterface * @return GroupCollectorInterface */ public function yearIsNot(string $year): GroupCollectorInterface; + + } diff --git a/app/Http/Controllers/Account/ShowController.php b/app/Http/Controllers/Account/ShowController.php index 7a1b28cb8f..062ef715de 100644 --- a/app/Http/Controllers/Account/ShowController.php +++ b/app/Http/Controllers/Account/ShowController.php @@ -131,6 +131,12 @@ class ShowController extends Controller ->setLimit($pageSize) ->setPage($page)->withAccountInformation()->withCategoryInformation() ->setRange($start, $end); + + // this search will not include transaction groups where this asset account (or liability) + // is just part of ONE of the journals. To force this: + $collector->setExpandGroupSearch(true); + + $groups = $collector->getPaginatedGroups(); $groups->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]));