| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * TransactionGroupRepository.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\TransactionGroup; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  | use FireflyIII\Enums\TransactionTypeEnum; | 
					
						
							| 
									
										
										
										
											2019-10-26 07:49:12 +02:00
										 |  |  | use FireflyIII\Exceptions\DuplicateTransactionException; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							|  |  |  | use FireflyIII\Factory\TransactionGroupFactory; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							| 
									
										
										
										
											2021-02-16 10:19:20 +01:00
										 |  |  | use FireflyIII\Models\Location; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  | use FireflyIII\Models\PiggyBankEvent; | 
					
						
							|  |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournalLink; | 
					
						
							| 
									
										
										
										
											2022-03-28 07:54:52 +02:00
										 |  |  | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-08-01 06:22:07 +02:00
										 |  |  | use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Services\Internal\Update\GroupUpdateService; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | use FireflyIII\Support\NullArrayObject; | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2020-04-07 18:19:29 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionGroupRepository | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-08-27 07:26:32 +02:00
										 |  |  | class TransactionGroupRepository implements TransactionGroupRepositoryInterface | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  |     use UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:59:58 +02:00
										 |  |  |     public function countAttachments(int $journalId): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         $journal = $this->user->transactionJournals()->find($journalId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $journal->attachments()->count(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Find a transaction group by its ID. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function find(int $groupId): ?TransactionGroup | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         /** @var null|TransactionGroup */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $this->user->transactionGroups()->find($groupId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 07:26:32 +02:00
										 |  |  |     public function destroy(TransactionGroup $group): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         $service = new TransactionGroupDestroyService(); | 
					
						
							| 
									
										
										
										
											2019-08-27 07:26:32 +02:00
										 |  |  |         $service->destroy($group); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 06:42:07 +01:00
										 |  |  |     public function expandGroup(TransactionGroup $group): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result                         = $group->toArray(); | 
					
						
							|  |  |  |         $result['transaction_journals'] = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 06:42:07 +01:00
										 |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($group->transactionJournals as $journal) { | 
					
						
							|  |  |  |             $result['transaction_journals'][] = $this->expandJournal($journal); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function expandJournal(TransactionJournal $journal): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $array                      = $journal->toArray(); | 
					
						
							|  |  |  |         $array['transactions']      = []; | 
					
						
							|  |  |  |         $array['meta']              = $journal->transactionJournalMeta->toArray(); | 
					
						
							|  |  |  |         $array['tags']              = $journal->tags->toArray(); | 
					
						
							|  |  |  |         $array['categories']        = $journal->categories->toArray(); | 
					
						
							|  |  |  |         $array['budgets']           = $journal->budgets->toArray(); | 
					
						
							|  |  |  |         $array['notes']             = $journal->notes->toArray(); | 
					
						
							|  |  |  |         $array['locations']         = []; | 
					
						
							|  |  |  |         $array['attachments']       = $journal->attachments->toArray(); | 
					
						
							|  |  |  |         $array['links']             = []; | 
					
						
							|  |  |  |         $array['piggy_bank_events'] = $journal->piggyBankEvents->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         foreach ($journal->transactions as $transaction) { | 
					
						
							|  |  |  |             $array['transactions'][] = $this->expandTransaction($transaction); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $array; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function expandTransaction(Transaction $transaction): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $array               = $transaction->toArray(); | 
					
						
							|  |  |  |         $array['account']    = $transaction->account->toArray(); | 
					
						
							|  |  |  |         $array['budgets']    = []; | 
					
						
							|  |  |  |         $array['categories'] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($transaction->categories as $category) { | 
					
						
							|  |  |  |             $array['categories'][] = $category->toArray(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($transaction->budgets as $budget) { | 
					
						
							|  |  |  |             $array['budgets'][] = $budget->toArray(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $array; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return all attachments for all journals in the group. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAttachments(TransactionGroup $group): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-28 07:54:52 +02:00
										 |  |  |         $repository = app(AttachmentRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($this->user); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $journals   = $group->transactionJournals->pluck('id')->toArray(); | 
					
						
							|  |  |  |         $set        = Attachment::whereIn('attachable_id', $journals) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->where('attachable_type', TransactionJournal::class) | 
					
						
							|  |  |  |             ->where('uploaded', true) | 
					
						
							|  |  |  |             ->whereNull('deleted_at')->get() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $result     = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |         /** @var Attachment $attachment */ | 
					
						
							|  |  |  |         foreach ($set as $attachment) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $journalId                = $attachment->attachable_id; | 
					
						
							|  |  |  |             $result[$journalId] ??= []; | 
					
						
							|  |  |  |             $current                  = $attachment->toArray(); | 
					
						
							|  |  |  |             $current['file_exists']   = true; | 
					
						
							|  |  |  |             $current['notes']         = $repository->getNoteText($attachment); | 
					
						
							| 
									
										
										
										
											2022-12-31 13:32:42 +01:00
										 |  |  |             // already determined that this attachable is a TransactionJournal.
 | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |             $current['journal_title'] = $attachment->attachable->description; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |             $result[$journalId][]     = $current; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the note text for a journal (by ID). | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getNoteText(int $journalId): ?string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|Note $note */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $note = Note::where('noteable_id', $journalId) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->where('noteable_type', TransactionJournal::class) | 
					
						
							|  |  |  |             ->first() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $note) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $note->text; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return all journal links for all journals in the group. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getLinks(TransactionGroup $group): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return   = []; | 
					
						
							|  |  |  |         $journals = $group->transactionJournals->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         $set      = TransactionJournalLink::where( | 
					
						
							| 
									
										
										
										
											2023-12-21 05:07:26 +01:00
										 |  |  |             static function (Builder $q) use ($journals): void { | 
					
						
							| 
									
										
										
										
											2022-10-31 05:53:36 +01:00
										 |  |  |                 $q->whereIn('source_id', $journals); | 
					
						
							|  |  |  |                 $q->orWhereIn('destination_id', $journals); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->with(['source', 'destination', 'source.transactions']) | 
					
						
							|  |  |  |             ->leftJoin('link_types', 'link_types.id', '=', 'journal_links.link_type_id') | 
					
						
							|  |  |  |             ->get(['journal_links.*', 'link_types.inward', 'link_types.outward', 'link_types.editable']) | 
					
						
							|  |  |  |         ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |         /** @var TransactionJournalLink $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $journalId = in_array($entry->source_id, $journals, true) ? $entry->source_id : $entry->destination_id; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:45:59 +01:00
										 |  |  |             $return[$journalId] ??= []; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-31 13:32:42 +01:00
										 |  |  |             // phpstan: the editable field is provided by the query.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |             if ($journalId === $entry->source_id) { | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                 $amount               = $this->getFormattedAmount($entry->destination); | 
					
						
							|  |  |  |                 $foreignAmount        = $this->getFormattedForeignAmount($entry->destination); | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |                 $return[$journalId][] = [ | 
					
						
							| 
									
										
										
										
											2019-07-20 06:47:34 +02:00
										 |  |  |                     'id'             => $entry->id, | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                     'link'           => $entry->outward, | 
					
						
							|  |  |  |                     'group'          => $entry->destination->transaction_group_id, | 
					
						
							|  |  |  |                     'description'    => $entry->destination->description, | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |                     'editable'       => 1 === (int) $entry->editable, | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                     'amount'         => $amount, | 
					
						
							|  |  |  |                     'foreign_amount' => $foreignAmount, | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($journalId === $entry->destination_id) { | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                 $amount               = $this->getFormattedAmount($entry->source); | 
					
						
							|  |  |  |                 $foreignAmount        = $this->getFormattedForeignAmount($entry->source); | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |                 $return[$journalId][] = [ | 
					
						
							| 
									
										
										
										
											2019-07-20 06:47:34 +02:00
										 |  |  |                     'id'             => $entry->id, | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                     'link'           => $entry->inward, | 
					
						
							|  |  |  |                     'group'          => $entry->source->transaction_group_id, | 
					
						
							|  |  |  |                     'description'    => $entry->source->description, | 
					
						
							| 
									
										
										
										
											2025-01-04 19:43:58 +01:00
										 |  |  |                     'editable'       => 1 === (int) $entry->editable, | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                     'amount'         => $amount, | 
					
						
							|  |  |  |                     'foreign_amount' => $foreignAmount, | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function getFormattedAmount(TransactionJournal $journal): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         $transaction = $journal->transactions->first(); | 
					
						
							|  |  |  |         $currency    = $transaction->transactionCurrency; | 
					
						
							|  |  |  |         $type        = $journal->transactionType->type; | 
					
						
							|  |  |  |         $amount      = app('steam')->positive($transaction->amount); | 
					
						
							|  |  |  |         $return      = ''; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         if (TransactionTypeEnum::WITHDRAWAL->value === $type) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $return = app('amount')->formatAnything($currency, app('steam')->negative($amount)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         if (TransactionTypeEnum::WITHDRAWAL->value !== $type) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $return = app('amount')->formatAnything($currency, $amount); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function getFormattedForeignAmount(TransactionJournal $journal): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Transaction $transaction */ | 
					
						
							|  |  |  |         $transaction = $journal->transactions->first(); | 
					
						
							|  |  |  |         if (null === $transaction->foreign_amount || '' === $transaction->foreign_amount) { | 
					
						
							|  |  |  |             return ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (0 === bccomp('0', $transaction->foreign_amount)) { | 
					
						
							|  |  |  |             return ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $currency    = $transaction->foreignCurrency; | 
					
						
							|  |  |  |         $type        = $journal->transactionType->type; | 
					
						
							|  |  |  |         $amount      = app('steam')->positive($transaction->foreign_amount); | 
					
						
							|  |  |  |         $return      = ''; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         if (TransactionTypeEnum::WITHDRAWAL->value === $type) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $return = app('amount')->formatAnything($currency, app('steam')->negative($amount)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         if (TransactionTypeEnum::WITHDRAWAL->value !== $type) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $return = app('amount')->formatAnything($currency, $amount); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |     public function getLocation(int $journalId): ?Location | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         $journal = $this->user->transactionJournals()->find($journalId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 07:39:16 +01:00
										 |  |  |         /** @var null|Location */ | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |         return $journal->locations()->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return object with all found meta field things as Carbon objects. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @throws \Exception | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getMetaDateFields(int $journalId, array $fields): NullArrayObject | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $query  = \DB::table('journal_meta') | 
					
						
							|  |  |  |             ->where('transaction_journal_id', $journalId) | 
					
						
							|  |  |  |             ->whereIn('name', $fields) | 
					
						
							|  |  |  |             ->whereNull('deleted_at') | 
					
						
							|  |  |  |             ->get(['name', 'data']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         $return = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($query as $row) { | 
					
						
							| 
									
										
										
										
											2020-06-21 19:16:21 +02:00
										 |  |  |             $return[$row->name] = new Carbon(json_decode($row->data, true, 512, JSON_THROW_ON_ERROR)); | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new NullArrayObject($return); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return object with all found meta field things. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getMetaFields(int $journalId, array $fields): NullArrayObject | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $query  = \DB::table('journal_meta') | 
					
						
							|  |  |  |             ->where('transaction_journal_id', $journalId) | 
					
						
							|  |  |  |             ->whereIn('name', $fields) | 
					
						
							|  |  |  |             ->whereNull('deleted_at') | 
					
						
							|  |  |  |             ->get(['name', 'data']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |         $return = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($query as $row) { | 
					
						
							|  |  |  |             $return[$row->name] = json_decode($row->data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new NullArrayObject($return); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return all piggy bank events for all journals in the group. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |     public function getPiggyEvents(TransactionGroup $group): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |         $return   = []; | 
					
						
							|  |  |  |         $journals = $group->transactionJournals->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2025-01-19 11:54:40 +01:00
										 |  |  |         $currency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         $data     = PiggyBankEvent::whereIn('transaction_journal_id', $journals) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->with('piggyBank', 'piggyBank.account') | 
					
						
							|  |  |  |             ->get(['piggy_bank_events.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |         /** @var PiggyBankEvent $row */ | 
					
						
							|  |  |  |         foreach ($data as $row) { | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |             if (null === $row->piggyBank) { | 
					
						
							| 
									
										
										
										
											2020-06-27 09:56:38 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |             // get currency preference.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $currencyPreference   = AccountMeta::where('account_id', $row->piggyBank->account_id) | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 ->where('name', 'currency_id') | 
					
						
							|  |  |  |                 ->first() | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |             if (null !== $currencyPreference) { | 
					
						
							|  |  |  |                 $currency = TransactionCurrency::where('id', $currencyPreference->data)->first(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $journalId            = $row->transaction_journal_id; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:45:59 +01:00
										 |  |  |             $return[$journalId] ??= []; | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $return[$journalId][] = [ | 
					
						
							| 
									
										
										
										
											2019-07-20 06:47:34 +02:00
										 |  |  |                 'piggy'    => $row->piggyBank->name, | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |                 'piggy_id' => $row->piggy_bank_id, | 
					
						
							| 
									
										
										
										
											2019-07-20 06:47:34 +02:00
										 |  |  |                 'amount'   => app('amount')->formatAnything($currency, $row->amount), | 
					
						
							| 
									
										
										
										
											2019-04-18 20:05:40 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |     public function getTagObjects(int $journalId): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         $journal = $this->user->transactionJournals()->find($journalId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $journal->tags()->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the tags for a journal (by ID). | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getTags(int $journalId): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $result = \DB::table('tag_transaction_journal') | 
					
						
							|  |  |  |             ->leftJoin('tags', 'tag_transaction_journal.tag_id', '=', 'tags.id') | 
					
						
							|  |  |  |             ->where('tag_transaction_journal.transaction_journal_id', $journalId) | 
					
						
							|  |  |  |             ->orderBy('tags.tag', 'ASC') | 
					
						
							|  |  |  |             ->get(['tags.tag']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-03-25 15:14:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $result->pluck('tag')->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-10-26 07:49:12 +02:00
										 |  |  |      * @throws DuplicateTransactionException | 
					
						
							| 
									
										
										
										
											2020-03-12 05:06:42 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     public function store(array $data): TransactionGroup | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionGroupFactory $factory */ | 
					
						
							|  |  |  |         $factory = app(TransactionGroupFactory::class); | 
					
						
							| 
									
										
										
										
											2025-02-09 09:30:44 +01:00
										 |  |  |         $factory->setUser($data['user']); | 
					
						
							|  |  |  |         $factory->setUserGroup($data['user_group']); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-22 11:05:16 +01:00
										 |  |  |         try { | 
					
						
							|  |  |  |             return $factory->create($data); | 
					
						
							|  |  |  |         } catch (DuplicateTransactionException $e) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning('Group repository caught group factory with a duplicate exception!'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             throw new DuplicateTransactionException($e->getMessage(), 0, $e); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:20:01 +01:00
										 |  |  |         } catch (FireflyException $e) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning('Group repository caught group factory with an exception!'); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             throw new FireflyException($e->getMessage(), 0, $e); | 
					
						
							| 
									
										
										
										
											2020-02-22 11:05:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws DuplicateTransactionException | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(TransactionGroup $transactionGroup, array $data): TransactionGroup | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var GroupUpdateService $service */ | 
					
						
							| 
									
										
										
										
											2019-08-27 07:26:32 +02:00
										 |  |  |         $service = app(GroupUpdateService::class); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 16:54:48 +02:00
										 |  |  |         return $service->update($transactionGroup, $data); | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |