From be8aaa68af2480048dfa8fa53a841d76538a2564 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 29 Dec 2023 20:50:03 +0100 Subject: [PATCH] Fix some sums. --- app/Http/Controllers/Report/TagController.php | 7 ------- app/Repositories/Tag/OperationsRepository.php | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Report/TagController.php b/app/Http/Controllers/Report/TagController.php index 82b0d5461c..7da4514f8b 100644 --- a/app/Http/Controllers/Report/TagController.php +++ b/app/Http/Controllers/Report/TagController.php @@ -155,7 +155,6 @@ class TagController extends Controller */ public function accounts(Collection $accounts, Collection $tags, Carbon $start, Carbon $end) { - $tagIds = $tags->pluck('id')->toArray(); $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $tags); $earned = $this->opsRepository->listIncome($start, $end, $accounts, $tags); $report = []; @@ -185,9 +184,6 @@ class TagController extends Controller 'total_sum' => '0', ]; foreach ($currency['tags'] as $tag) { - if(!array_key_exists($tag['id'], $tagIds)) { - continue; - } foreach ($tag['transaction_journals'] as $journal) { $sourceAccountId = $journal['source_account_id']; $report[$sourceAccountId]['currencies'][$currencyId] ??= [ @@ -226,9 +222,6 @@ class TagController extends Controller 'total_sum' => '0', ]; foreach ($currency['tags'] as $tag) { - if(!array_key_exists($tag['id'], $tagIds)) { - continue; - } foreach ($tag['transaction_journals'] as $journal) { $destinationAccountId = $journal['destination_account_id']; $report[$destinationAccountId]['currencies'][$currencyId] ??= [ diff --git a/app/Repositories/Tag/OperationsRepository.php b/app/Repositories/Tag/OperationsRepository.php index 0458de3901..c12f583a41 100644 --- a/app/Repositories/Tag/OperationsRepository.php +++ b/app/Repositories/Tag/OperationsRepository.php @@ -48,14 +48,17 @@ class OperationsRepository implements OperationsRepositoryInterface /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]); + $tagIds = []; if (null !== $accounts && $accounts->count() > 0) { $collector->setAccounts($accounts); } if (null !== $tags && $tags->count() > 0) { $collector->setTags($tags); + $tagIds = $tags->pluck('id')->toArray(); } if (null === $tags || 0 === $tags->count()) { $collector->setTags($this->getTags()); + $tagIds = $this->getTags()->pluck('id')->toArray(); } $collector->withCategoryInformation()->withAccountInformation()->withBudgetInformation(); $journals = $collector->getExtractedJournals(); @@ -77,7 +80,11 @@ class OperationsRepository implements OperationsRepositoryInterface $tagId = (int)$tag['id']; $tagName = (string)$tag['name']; $journalId = (int)$journal['transaction_journal_id']; + if(!in_array($tagId, $tagIds, true)){ + continue; + } + // TODO not sure what this check does. if (in_array($journalId, $listedJournals, true)) { continue; } @@ -123,14 +130,17 @@ class OperationsRepository implements OperationsRepositoryInterface /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT]); + $tagIds = []; if (null !== $accounts && $accounts->count() > 0) { $collector->setAccounts($accounts); } if (null !== $tags && $tags->count() > 0) { $collector->setTags($tags); + $tagIds = $tags->pluck('id')->toArray(); } if (null === $tags || 0 === $tags->count()) { $collector->setTags($this->getTags()); + $tagIds = $this->getTags()->pluck('id')->toArray(); } $collector->withCategoryInformation()->withAccountInformation()->withBudgetInformation()->withTagInformation(); $journals = $collector->getExtractedJournals(); @@ -150,6 +160,9 @@ class OperationsRepository implements OperationsRepositoryInterface // may have multiple tags: foreach ($journal['tags'] as $tag) { + if(!in_array($tagId, $tagIds, true)){ + continue; + } $tagId = (int)$tag['id']; $tagName = (string)$tag['name']; $journalId = (int)$journal['transaction_journal_id']; @@ -204,6 +217,7 @@ class OperationsRepository implements OperationsRepositoryInterface private function getTags(): Collection { + /** @var TagRepositoryInterface $repository */ $repository = app(TagRepositoryInterface::class); return $repository->get();