diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php index 3a20a85e69..2cb8f4b263 100644 --- a/app/Helpers/Collector/GroupCollector.php +++ b/app/Helpers/Collector/GroupCollector.php @@ -60,6 +60,8 @@ class GroupCollector implements GroupCollectorInterface private $hasCatInformation; /** @var bool Will be true of the query has the tag info tables joined. */ private $hasJoinedTagTables; + /** @var bool Will be true for attachments */ + private $hasJoinedAttTables; /** @var int The maximum number of results. */ private $limit; /** @var int The page to return. */ @@ -86,7 +88,8 @@ class GroupCollector implements GroupCollectorInterface $this->hasBudgetInformation = false; $this->hasBillInformation = false; $this->hasJoinedTagTables = false; - $this->integerFields = [ + $this->hasJoinedAttTables = false; + $this->integerFields = [ 'transaction_group_id', 'user_id', 'transaction_journal_id', @@ -101,10 +104,10 @@ class GroupCollector implements GroupCollectorInterface 'destination_transaction_id', 'destination_account_id', 'category_id', - 'budget_id' + 'budget_id', ]; - $this->total = 0; - $this->fields = [ + $this->total = 0; + $this->fields = [ # group 'transaction_groups.id as transaction_group_id', 'transaction_groups.user_id as user_id', @@ -281,11 +284,10 @@ class GroupCollector implements GroupCollectorInterface */ public function getGroups(): Collection { - $start = microtime(true); + //$start = microtime(true); /** @var Collection $result */ $result = $this->query->get($this->fields); - $end = round(microtime(true) - $start, 5); - + //$end = round(microtime(true) - $start, 5); // log info about query time. //Log::info(sprintf('Query took Firefly III %s seconds', $end)); //Log::info($this->query->toSql(), $this->query->getBindings()); @@ -950,6 +952,19 @@ class GroupCollector implements GroupCollectorInterface } } + /** + * Join table to get attachment information. + */ + private function joinAttachmentTables(): void + { + if (false === $this->hasJoinedAttTables) { + // join some extra tables: + $this->hasJoinedAttTables = true; + $this->query->leftJoin('attachments', 'attachments.attachable_id', '=', 'transaction_journals.id') + ->where('attachments.attachable_type', TransactionJournal::class); + } + } + /** * @param array $existingJournal * @param TransactionJournal $newJournal @@ -980,6 +995,26 @@ class GroupCollector implements GroupCollectorInterface return $existingJournal; } + /** + * @param array $existingJournal + * @param TransactionJournal $newJournal + * + * @return array + */ + private function mergeAttachments(array $existingJournal, TransactionJournal $newJournal): array + { + $newArray = $newJournal->toArray(); + if (isset($newArray['attachment_id'])) { + $attachmentId = (int)$newJournal['tag_id']; + $existingJournal['attachments'][$attachmentId] = [ + 'id' => $attachmentId, + ]; + } + + return $existingJournal; + } + + /** * @param Collection $collection * @@ -994,7 +1029,7 @@ class GroupCollector implements GroupCollectorInterface if (!isset($groups[$groupId])) { // make new array - $parsedGroup = $this->parseAugmentedGroup($augumentedJournal); + $parsedGroup = $this->parseAugmentedJournal($augumentedJournal); $groupArray = [ 'id' => (int)$augumentedJournal->transaction_group_id, 'user_id' => (int)$augumentedJournal->user_id, @@ -1014,11 +1049,14 @@ class GroupCollector implements GroupCollectorInterface $groups[$groupId]['count']++; if (isset($groups[$groupId]['transactions'][$journalId])) { + // append data to existing group + journal (for multiple tags or multiple attachments) $groups[$groupId]['transactions'][$journalId] = $this->mergeTags($groups[$groupId]['transactions'][$journalId], $augumentedJournal); + $groups[$groupId]['transactions'][$journalId] = $this->mergeAttachments($groups[$groupId]['transactions'][$journalId], $augumentedJournal); } if (!isset($groups[$groupId]['transactions'][$journalId])) { - $groups[$groupId]['transactions'][$journalId] = $this->parseAugmentedGroup($augumentedJournal); + // create second, third, fourth split: + $groups[$groupId]['transactions'][$journalId] = $this->parseAugmentedJournal($augumentedJournal); } @@ -1034,10 +1072,11 @@ class GroupCollector implements GroupCollectorInterface * * @return array */ - private function parseAugmentedGroup(TransactionJournal $augumentedJournal): array + private function parseAugmentedJournal(TransactionJournal $augumentedJournal): array { - $result = $augumentedJournal->toArray(); - $result['tags'] = []; + $result = $augumentedJournal->toArray(); + $result['tags'] = []; + $result['attachments'] = []; try { $result['date'] = new Carbon($result['date']); $result['created_at'] = new Carbon($result['created_at']); @@ -1067,6 +1106,14 @@ class GroupCollector implements GroupCollectorInterface ]; } + // also merge attachments: + if (isset($augumentedJournal['attachment_id'])) { + $attachmentId = (int)$augumentedJournal['attachment_id']; + $result['attachments'][$attachmentId] = [ + 'id' => $attachmentId, + ]; + } + return $result; } @@ -1230,4 +1277,15 @@ class GroupCollector implements GroupCollectorInterface return $this; } + + /** + * @inheritDoc + */ + public function withAttachmentInformation(): GroupCollectorInterface + { + $this->fields[] = 'attachments.id as attachment_id'; + $this->joinAttachmentTables(); + + return $this; + } } diff --git a/app/Helpers/Collector/GroupCollectorInterface.php b/app/Helpers/Collector/GroupCollectorInterface.php index c0beab69df..df69fc4439 100644 --- a/app/Helpers/Collector/GroupCollectorInterface.php +++ b/app/Helpers/Collector/GroupCollectorInterface.php @@ -57,6 +57,13 @@ interface GroupCollectorInterface */ public function amountLess(string $amount): GroupCollectorInterface; + /** + * Add basic info on attachments of transactions. + * + * @return GroupCollectorInterface + */ + public function withAttachmentInformation(): GroupCollectorInterface; + /** * Get transactions where the amount is more than. * diff --git a/app/Http/Controllers/Transaction/IndexController.php b/app/Http/Controllers/Transaction/IndexController.php index 73156ea651..3f2ffa3704 100644 --- a/app/Http/Controllers/Transaction/IndexController.php +++ b/app/Http/Controllers/Transaction/IndexController.php @@ -107,7 +107,8 @@ class IndexController extends Controller ->setPage($page) ->withBudgetInformation() ->withCategoryInformation() - ->withAccountInformation(); + ->withAccountInformation() + ->withAttachmentInformation(); $groups = $collector->getPaginatedGroups(); $groups->setPath($path); @@ -147,7 +148,8 @@ class IndexController extends Controller ->setPage($page) ->withAccountInformation() ->withBudgetInformation() - ->withCategoryInformation(); + ->withCategoryInformation() + ->withAttachmentInformation(); $groups = $collector->getPaginatedGroups(); $groups->setPath($path); diff --git a/resources/views/v1/list/groups.twig b/resources/views/v1/list/groups.twig index d9dcfc4781..88f043ab2d 100644 --- a/resources/views/v1/list/groups.twig +++ b/resources/views/v1/list/groups.twig @@ -106,6 +106,9 @@ TODO: hide and show columns {% if transaction.reconciled %} {% endif %} + {% if transaction.attachments|length > 0 %} + + {% endif %} {% if group.count == 1 %} {% endif %}