| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * TransactionGroupTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2022 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | namespace FireflyIII\Transformers\V2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\Category; | 
					
						
							| 
									
										
										
										
											2024-01-03 17:43:05 +01:00
										 |  |  | use FireflyIII\Models\Location; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Support\Http\Api\ExchangeRateConverter; | 
					
						
							| 
									
										
										
										
											2022-07-03 08:33:01 +02:00
										 |  |  | use FireflyIII\Support\NullArrayObject; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionGroupTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class TransactionGroupTransformer extends AbstractTransformer | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     private array                 $accountTypes = []; // account types collection.
 | 
					
						
							| 
									
										
										
										
											2024-03-06 07:16:01 +01:00
										 |  |  |     private ExchangeRateConverter $converter;         // collection of all journals and some important meta-data.
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private array                 $currencies   = []; | 
					
						
							|  |  |  |     private TransactionCurrency   $default; // collection of all currencies for this transformer.
 | 
					
						
							|  |  |  |     private array                 $journals     = []; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     private array                 $objects      = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //    private array                 $currencies        = [];
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |     //    private array                 $transactionTypes  = [];
 | 
					
						
							|  |  |  |     //    private array                 $meta              = [];
 | 
					
						
							|  |  |  |     //    private array                 $notes             = [];
 | 
					
						
							|  |  |  |     //    private array                 $locations         = [];
 | 
					
						
							|  |  |  |     //    private array                 $tags              = [];
 | 
					
						
							|  |  |  |     //    private array                 $amounts           = [];
 | 
					
						
							|  |  |  |     //    private array                 $foreignAmounts    = [];
 | 
					
						
							|  |  |  |     //    private array                 $journalCurrencies = [];
 | 
					
						
							|  |  |  |     //    private array                 $foreignCurrencies = [];
 | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-09 19:46:16 +01:00
										 |  |  |     public function collectMetaData(Collection $objects): Collection | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         $collectForObjects = false; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:49:50 +01:00
										 |  |  |         /** @var array|TransactionGroup $object */ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         foreach ($objects as $object) { | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             if (is_array($object)) { | 
					
						
							|  |  |  |                 $this->collectForArray($object); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             if ($object instanceof TransactionGroup) { | 
					
						
							|  |  |  |                 $this->collectForObject($object); | 
					
						
							|  |  |  |                 $collectForObjects = true; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |         $this->default     = app('amount')->getDefaultCurrency(); | 
					
						
							|  |  |  |         $this->converter   = new ExchangeRateConverter(); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->collectAllMetaData(); | 
					
						
							|  |  |  |         $this->collectAllNotes(); | 
					
						
							|  |  |  |         $this->collectAllLocations(); | 
					
						
							|  |  |  |         $this->collectAllTags(); | 
					
						
							|  |  |  |         if ($collectForObjects) { | 
					
						
							|  |  |  |             $this->collectAllCurrencies(); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |             //            $this->collectAllAmounts();
 | 
					
						
							|  |  |  |             //            $this->collectTransactionTypes();
 | 
					
						
							|  |  |  |             //            $this->collectAccounts();
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             // source accounts
 | 
					
						
							|  |  |  |             // destination accounts
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-03-09 19:46:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $objects; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function collectForArray(array $object): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($object['sums'] as $sum) { | 
					
						
							|  |  |  |             $this->currencies[(int)$sum['currency_id']] ??= TransactionCurrency::find($sum['currency_id']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var array $transaction */ | 
					
						
							|  |  |  |         foreach ($object['transactions'] as $transaction) { | 
					
						
							|  |  |  |             $this->journals[(int)$transaction['transaction_journal_id']] = []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectForObject(TransactionGroup $object): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($object->transactionJournals as $journal) { | 
					
						
							|  |  |  |             $this->journals[$journal->id] = []; | 
					
						
							|  |  |  |             $this->objects[]              = $journal; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAllMetaData(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $meta = TransactionJournalMeta::whereIn('transaction_journal_id', array_keys($this->journals))->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionJournalMeta $entry */ | 
					
						
							|  |  |  |         foreach ($meta as $entry) { | 
					
						
							|  |  |  |             $id                                        = $entry->transaction_journal_id; | 
					
						
							|  |  |  |             $this->journals[$id]['meta'] ??= []; | 
					
						
							|  |  |  |             $this->journals[$id]['meta'][$entry->name] = $entry->data; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAllNotes(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // grab all notes for all journals:
 | 
					
						
							|  |  |  |         $notes = Note::whereNoteableType(TransactionJournal::class)->whereIn('noteable_id', array_keys($this->journals))->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Note $note */ | 
					
						
							|  |  |  |         foreach ($notes as $note) { | 
					
						
							|  |  |  |             $id                           = $note->noteable_id; | 
					
						
							|  |  |  |             $this->journals[$id]['notes'] = $note->text; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAllLocations(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // grab all locations for all journals:
 | 
					
						
							|  |  |  |         $locations = Location::whereLocatableType(TransactionJournal::class)->whereIn('locatable_id', array_keys($this->journals))->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Location $location */ | 
					
						
							|  |  |  |         foreach ($locations as $location) { | 
					
						
							|  |  |  |             $id                              = $location->locatable_id; | 
					
						
							|  |  |  |             $this->journals[$id]['location'] = [ | 
					
						
							|  |  |  |                 'latitude'   => $location->latitude, | 
					
						
							|  |  |  |                 'longitude'  => $location->longitude, | 
					
						
							|  |  |  |                 'zoom_level' => $location->zoom_level, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAllTags(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // grab all tags for all journals:
 | 
					
						
							|  |  |  |         $tags = DB::table('tag_transaction_journal') | 
					
						
							|  |  |  |             ->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id') | 
					
						
							|  |  |  |             ->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($this->journals)) | 
					
						
							|  |  |  |             ->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag']) | 
					
						
							|  |  |  |         ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var \stdClass $tag */ | 
					
						
							|  |  |  |         foreach ($tags as $tag) { | 
					
						
							|  |  |  |             $id                            = (int)$tag->transaction_journal_id; | 
					
						
							|  |  |  |             $this->journals[$id]['tags'][] = $tag->tag; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function collectAllCurrencies(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($this->objects as $journal) { | 
					
						
							|  |  |  |             $id                                         = $journal->id; | 
					
						
							|  |  |  |             $this->journals[$id]['reconciled']          = false; | 
					
						
							|  |  |  |             $this->journals[$id]['foreign_amount']      = null; | 
					
						
							|  |  |  |             $this->journals[$id]['foreign_currency_id'] = null; | 
					
						
							|  |  |  |             $this->journals[$id]['amount']              = null; | 
					
						
							|  |  |  |             $this->journals[$id]['currency_id']         = null; | 
					
						
							|  |  |  |             $this->journals[$id]['type']                = $journal->transactionType->type; | 
					
						
							|  |  |  |             $this->journals[$id]['budget_id']           = null; | 
					
						
							|  |  |  |             $this->journals[$id]['budget_name']         = null; | 
					
						
							|  |  |  |             $this->journals[$id]['category_id']         = null; | 
					
						
							|  |  |  |             $this->journals[$id]['category_name']       = null; | 
					
						
							|  |  |  |             $this->journals[$id]['bill_id']             = null; | 
					
						
							|  |  |  |             $this->journals[$id]['bill_name']           = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // collect budget:
 | 
					
						
							|  |  |  |             /** @var null|Budget $budget */ | 
					
						
							|  |  |  |             $budget                                     = $journal->budgets()->first(); | 
					
						
							|  |  |  |             if (null !== $budget) { | 
					
						
							|  |  |  |                 $this->journals[$id]['budget_id']   = (string)$budget->id; | 
					
						
							|  |  |  |                 $this->journals[$id]['budget_name'] = $budget->name; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // collect category:
 | 
					
						
							|  |  |  |             /** @var null|Category $category */ | 
					
						
							|  |  |  |             $category                                   = $journal->categories()->first(); | 
					
						
							|  |  |  |             if (null !== $category) { | 
					
						
							|  |  |  |                 $this->journals[$id]['category_id']   = (string)$category->id; | 
					
						
							|  |  |  |                 $this->journals[$id]['category_name'] = $category->name; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // collect bill:
 | 
					
						
							|  |  |  |             if (null !== $journal->bill_id) { | 
					
						
							|  |  |  |                 $bill                             = $journal->bill; | 
					
						
							|  |  |  |                 $this->journals[$id]['bill_id']   = (string)$bill->id; | 
					
						
							|  |  |  |                 $this->journals[$id]['bill_name'] = $bill->name; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /** @var Transaction $transaction */ | 
					
						
							|  |  |  |             foreach ($journal->transactions as $transaction) { | 
					
						
							|  |  |  |                 if (-1 === bccomp($transaction->amount, '0')) { | 
					
						
							|  |  |  |                     // only collect source account info
 | 
					
						
							|  |  |  |                     $account                                    = $transaction->account; | 
					
						
							|  |  |  |                     $this->accountTypes[$account->account_type_id] ??= $account->accountType->type; | 
					
						
							|  |  |  |                     $this->journals[$id]['source_account_name'] = $account->name; | 
					
						
							|  |  |  |                     $this->journals[$id]['source_account_iban'] = $account->iban; | 
					
						
							|  |  |  |                     $this->journals[$id]['source_account_type'] = $this->accountTypes[$account->account_type_id]; | 
					
						
							|  |  |  |                     $this->journals[$id]['source_account_id']   = $transaction->account_id; | 
					
						
							|  |  |  |                     $this->journals[$id]['reconciled']          = $transaction->reconciled; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // add account
 | 
					
						
							|  |  |  |                 $account                                         = $transaction->account; | 
					
						
							|  |  |  |                 $this->accountTypes[$account->account_type_id] ??= $account->accountType->type; | 
					
						
							|  |  |  |                 $this->journals[$id]['destination_account_name'] = $account->name; | 
					
						
							|  |  |  |                 $this->journals[$id]['destination_account_iban'] = $account->iban; | 
					
						
							|  |  |  |                 $this->journals[$id]['destination_account_type'] = $this->accountTypes[$account->account_type_id]; | 
					
						
							|  |  |  |                 $this->journals[$id]['destination_account_id']   = $transaction->account_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // find and set currency
 | 
					
						
							|  |  |  |                 $currencyId                                      = $transaction->transaction_currency_id; | 
					
						
							|  |  |  |                 $this->currencies[$currencyId]                 ??= $transaction->transactionCurrency; | 
					
						
							|  |  |  |                 $this->journals[$id]['currency_id']              = $currencyId; | 
					
						
							|  |  |  |                 $this->journals[$id]['amount']                   = $transaction->amount; | 
					
						
							|  |  |  |                 // find and set foreign currency
 | 
					
						
							|  |  |  |                 if (null !== $transaction->foreign_currency_id) { | 
					
						
							|  |  |  |                     $foreignCurrencyId                          = $transaction->foreign_currency_id; | 
					
						
							|  |  |  |                     $this->currencies[$foreignCurrencyId] ??= $transaction->foreignCurrency; | 
					
						
							|  |  |  |                     $this->journals[$id]['foreign_currency_id'] = $foreignCurrencyId; | 
					
						
							|  |  |  |                     $this->journals[$id]['foreign_amount']      = $transaction->foreign_amount; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // find and set destination account info.
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |     public function transform(array|TransactionGroup $group): array | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_array($group)) { | 
					
						
							|  |  |  |             $first = reset($group['transactions']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return [ | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 'id'           => (string)$group['id'], | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |                 'created_at'   => $group['created_at']->toAtomString(), | 
					
						
							|  |  |  |                 'updated_at'   => $group['updated_at']->toAtomString(), | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 'user'         => (string)$first['user_id'], | 
					
						
							|  |  |  |                 'user_group'   => (string)$first['user_group_id'], | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |                 'group_title'  => $group['title'] ?? null, | 
					
						
							|  |  |  |                 'transactions' => $this->transformTransactions($group['transactions'] ?? []), | 
					
						
							|  |  |  |                 'links'        => [ | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         'rel' => 'self', | 
					
						
							|  |  |  |                         'uri' => sprintf('/transactions/%d', $group['id']), | 
					
						
							|  |  |  |                     ], | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'id'           => (string)$group->id, | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             'created_at'   => $group->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'   => $group->created_at->toAtomString(), | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'user'         => (string)$group->user_id, | 
					
						
							|  |  |  |             'user_group'   => (string)$group->user_group_id, | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             'group_title'  => $group->title ?? null, | 
					
						
							|  |  |  |             'transactions' => $this->transformJournals($group), | 
					
						
							|  |  |  |             'links'        => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							|  |  |  |                     'uri' => sprintf('/transactions/%d', $group->id), | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function transformTransactions(array $transactions): array | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $return = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var array $transaction */ | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $return[] = $this->transformTransaction($transaction); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-12-22 17:28:42 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     private function transformTransaction(array $transaction): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |         $transaction         = new NullArrayObject($transaction); | 
					
						
							|  |  |  |         $type                = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL); | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $journalId           = (int)$transaction['transaction_journal_id']; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |         $meta                = new NullArrayObject($this->meta[$journalId] ?? []); | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Convert and use amount: | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $amount              = app('steam')->positive((string)($transaction['amount'] ?? '0')); | 
					
						
							|  |  |  |         $currencyId          = (int)$transaction['currency_id']; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |         $nativeAmount        = $this->converter->convert($this->default, $this->currencies[$currencyId], $transaction['date'], $amount); | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |         $foreignAmount       = null; | 
					
						
							| 
									
										
										
										
											2022-07-17 08:51:53 +02:00
										 |  |  |         $nativeForeignAmount = null; | 
					
						
							| 
									
										
										
										
											2022-07-03 08:33:01 +02:00
										 |  |  |         if (null !== $transaction['foreign_amount']) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $foreignCurrencyId   = (int)$transaction['foreign_currency_id']; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |             $foreignAmount       = app('steam')->positive($transaction['foreign_amount']); | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             $nativeForeignAmount = $this->converter->convert($this->default, $this->currencies[$foreignCurrencyId], $transaction['date'], $foreignAmount); | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-22 06:14:14 +01:00
										 |  |  |         $this->converter->summarize(); | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  |         $longitude           = null; | 
					
						
							|  |  |  |         $latitude            = null; | 
					
						
							|  |  |  |         $zoomLevel           = null; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         if (array_key_exists('location', $this->journals[$journalId])) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             $latitude  = (string)$this->journals[$journalId]['location']['latitude']; | 
					
						
							|  |  |  |             $longitude = (string)$this->journals[$journalId]['location']['longitude']; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |             $zoomLevel = $this->journals[$journalId]['location']['zoom_level']; | 
					
						
							| 
									
										
										
										
											2024-01-03 17:43:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'user'                            => (string)$transaction['user_id'], | 
					
						
							|  |  |  |             'user_group'                      => (string)$transaction['user_group_id'], | 
					
						
							|  |  |  |             'transaction_journal_id'          => (string)$transaction['transaction_journal_id'], | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'type'                            => strtolower($type), | 
					
						
							|  |  |  |             'date'                            => $transaction['date']->toAtomString(), | 
					
						
							|  |  |  |             'order'                           => $transaction['order'], | 
					
						
							|  |  |  |             'amount'                          => $amount, | 
					
						
							|  |  |  |             'native_amount'                   => $nativeAmount, | 
					
						
							|  |  |  |             'foreign_amount'                  => $foreignAmount, | 
					
						
							|  |  |  |             'native_foreign_amount'           => $nativeForeignAmount, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'currency_id'                     => (string)$transaction['currency_id'], | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'currency_code'                   => $transaction['currency_code'], | 
					
						
							|  |  |  |             'currency_name'                   => $transaction['currency_name'], | 
					
						
							|  |  |  |             'currency_symbol'                 => $transaction['currency_symbol'], | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'currency_decimal_places'         => (int)$transaction['currency_decimal_places'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // converted to native currency
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'native_currency_id'              => (string)$this->default->id, | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |             'native_currency_code'            => $this->default->code, | 
					
						
							|  |  |  |             'native_currency_name'            => $this->default->name, | 
					
						
							|  |  |  |             'native_currency_symbol'          => $this->default->symbol, | 
					
						
							| 
									
										
										
										
											2023-11-26 12:24:37 +01:00
										 |  |  |             'native_currency_decimal_places'  => $this->default->decimal_places, | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             // foreign currency amount:
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'foreign_currency_id'             => $this->stringFromArray($transaction, 'foreign_currency_id', null), | 
					
						
							|  |  |  |             'foreign_currency_code'           => $transaction['foreign_currency_code'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |             'foreign_currency_name'           => $transaction['foreign_currency_name'], | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'foreign_currency_symbol'         => $transaction['foreign_currency_symbol'], | 
					
						
							|  |  |  |             'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             // foreign converted to native:
 | 
					
						
							|  |  |  |             'description'                     => $transaction['description'], | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'source_id'                       => (string)$transaction['source_account_id'], | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'source_name'                     => $transaction['source_account_name'], | 
					
						
							|  |  |  |             'source_iban'                     => $transaction['source_account_iban'], | 
					
						
							|  |  |  |             'source_type'                     => $transaction['source_account_type'], | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'destination_id'                  => (string)$transaction['destination_account_id'], | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'destination_name'                => $transaction['destination_account_name'], | 
					
						
							|  |  |  |             'destination_iban'                => $transaction['destination_account_iban'], | 
					
						
							|  |  |  |             'destination_type'                => $transaction['destination_account_type'], | 
					
						
							|  |  |  |             'budget_id'                       => $this->stringFromArray($transaction, 'budget_id', null), | 
					
						
							|  |  |  |             'budget_name'                     => $transaction['budget_name'], | 
					
						
							|  |  |  |             'category_id'                     => $this->stringFromArray($transaction, 'category_id', null), | 
					
						
							|  |  |  |             'category_name'                   => $transaction['category_name'], | 
					
						
							|  |  |  |             'bill_id'                         => $this->stringFromArray($transaction, 'bill_id', null), | 
					
						
							|  |  |  |             'bill_name'                       => $transaction['bill_name'], | 
					
						
							|  |  |  |             'reconciled'                      => $transaction['reconciled'], | 
					
						
							|  |  |  |             'notes'                           => $this->notes[$journalId] ?? null, | 
					
						
							|  |  |  |             'tags'                            => $this->tags[$journalId] ?? [], | 
					
						
							|  |  |  |             'internal_reference'              => $meta['internal_reference'], | 
					
						
							|  |  |  |             'external_id'                     => $meta['external_id'], | 
					
						
							|  |  |  |             'original_source'                 => $meta['original_source'], | 
					
						
							|  |  |  |             'recurrence_id'                   => $meta['recurrence_id'], | 
					
						
							|  |  |  |             'recurrence_total'                => $meta['recurrence_total'], | 
					
						
							|  |  |  |             'recurrence_count'                => $meta['recurrence_count'], | 
					
						
							|  |  |  |             'external_url'                    => $meta['external_url'], | 
					
						
							|  |  |  |             'import_hash_v2'                  => $meta['import_hash_v2'], | 
					
						
							|  |  |  |             'sepa_cc'                         => $meta['sepa_cc'], | 
					
						
							|  |  |  |             'sepa_ct_op'                      => $meta['sepa_ct_op'], | 
					
						
							|  |  |  |             'sepa_ct_id'                      => $meta['sepa_ct_id'], | 
					
						
							|  |  |  |             'sepa_db'                         => $meta['sepa_db'], | 
					
						
							|  |  |  |             'sepa_country'                    => $meta['sepa_country'], | 
					
						
							|  |  |  |             'sepa_ep'                         => $meta['sepa_ep'], | 
					
						
							|  |  |  |             'sepa_ci'                         => $meta['sepa_ci'], | 
					
						
							|  |  |  |             'sepa_batch_id'                   => $meta['sepa_batch_id'], | 
					
						
							|  |  |  |             'interest_date'                   => $this->date($meta['interest_date']), | 
					
						
							|  |  |  |             'book_date'                       => $this->date($meta['book_date']), | 
					
						
							|  |  |  |             'process_date'                    => $this->date($meta['process_date']), | 
					
						
							|  |  |  |             'due_date'                        => $this->date($meta['due_date']), | 
					
						
							|  |  |  |             'payment_date'                    => $this->date($meta['payment_date']), | 
					
						
							|  |  |  |             'invoice_date'                    => $this->date($meta['invoice_date']), | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // location data
 | 
					
						
							| 
									
										
										
										
											2024-01-03 17:43:05 +01:00
										 |  |  |             'longitude'                       => $longitude, | 
					
						
							|  |  |  |             'latitude'                        => $latitude, | 
					
						
							|  |  |  |             'zoom_level'                      => $zoomLevel, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             //
 | 
					
						
							|  |  |  |             //            'has_attachments' => $this->hasAttachments((int) $row['transaction_journal_id']),
 | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * TODO also in the old transformer. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |      * Used to extract a value from the given array, and fall back on a sensible default or NULL | 
					
						
							|  |  |  |      * if it can't be helped. | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function stringFromArray(NullArrayObject $array, string $key, ?string $default): ?string | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // app('log')->debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $array[$key] && null === $default) { | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2023-01-07 05:53:48 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (0 === $array[$key]) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('0' === $array[$key]) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $array[$key]) { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             return (string)$array[$key]; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $default) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function date(?string $string): ?Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (null === $string) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-06 18:23:32 +02:00
										 |  |  |         //        app('log')->debug(sprintf('Now in date("%s")', $string));
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (10 === strlen($string)) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             $res = Carbon::createFromFormat('Y-m-d', $string, config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |             if (false === $res) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             return $res; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-12 18:21:29 +02:00
										 |  |  |         if (25 === strlen($string)) { | 
					
						
							|  |  |  |             return Carbon::parse($string, config('app.timezone')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (19 === strlen($string) && str_contains($string, 'T')) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             $res = Carbon::createFromFormat('Y-m-d\TH:i:s', substr($string, 0, 19), config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |             if (false === $res) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             return $res; | 
					
						
							| 
									
										
										
										
											2023-08-12 18:21:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // 2022-01-01 01:01:01
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |         $res = Carbon::createFromFormat('Y-m-d H:i:s', substr($string, 0, 19), config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |         if (false === $res) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |         return $res; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function transformJournals(TransactionGroup $group): array | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $return = []; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($group->transactionJournals as $journal) { | 
					
						
							|  |  |  |             $return[] = $this->transformJournal($journal); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function transformJournal(TransactionJournal $journal): array | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $id                  = $journal->id; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         /** @var null|TransactionCurrency $foreignCurrency */ | 
					
						
							|  |  |  |         $foreignCurrency     = null; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |         $currency            = $this->currencies[$this->journals[$id]['currency_id']]; | 
					
						
							|  |  |  |         $nativeForeignAmount = null; | 
					
						
							|  |  |  |         $amount              = $this->journals[$journal->id]['amount']; | 
					
						
							|  |  |  |         $foreignAmount       = $this->journals[$journal->id]['foreign_amount']; | 
					
						
							|  |  |  |         $meta                = new NullArrayObject($this->meta[$id] ?? []); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         // has foreign amount?
 | 
					
						
							|  |  |  |         if (null !== $foreignAmount) { | 
					
						
							|  |  |  |             $foreignCurrency     = $this->currencies[$this->journals[$id]['foreign_currency_id']]; | 
					
						
							|  |  |  |             $nativeForeignAmount = $this->converter->convert($this->default, $foreignCurrency, $journal->date, $foreignAmount); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $nativeAmount        = $this->converter->convert($this->default, $currency, $journal->date, $amount); | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $longitude           = null; | 
					
						
							|  |  |  |         $latitude            = null; | 
					
						
							|  |  |  |         $zoomLevel           = null; | 
					
						
							|  |  |  |         if (array_key_exists('location', $this->journals[$id])) { | 
					
						
							|  |  |  |             $latitude  = (string)$this->journals[$id]['location']['latitude']; | 
					
						
							|  |  |  |             $longitude = (string)$this->journals[$id]['location']['longitude']; | 
					
						
							|  |  |  |             $zoomLevel = $this->journals[$id]['location']['zoom_level']; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             'user'                            => (string)$journal->user_id, | 
					
						
							|  |  |  |             'user_group'                      => (string)$journal->user_group_id, | 
					
						
							|  |  |  |             'transaction_journal_id'          => (string)$journal->id, | 
					
						
							|  |  |  |             'type'                            => $this->journals[$journal->id]['type'], | 
					
						
							|  |  |  |             'date'                            => $journal->date->toAtomString(), | 
					
						
							|  |  |  |             'order'                           => $journal->order, | 
					
						
							|  |  |  |             'amount'                          => $amount, | 
					
						
							|  |  |  |             'native_amount'                   => $nativeAmount, | 
					
						
							|  |  |  |             'foreign_amount'                  => $foreignAmount, | 
					
						
							|  |  |  |             'native_foreign_amount'           => $nativeForeignAmount, | 
					
						
							|  |  |  |             'currency_id'                     => (string)$currency->id, | 
					
						
							|  |  |  |             'currency_code'                   => $currency->code, | 
					
						
							|  |  |  |             'currency_name'                   => $currency->name, | 
					
						
							|  |  |  |             'currency_symbol'                 => $currency->symbol, | 
					
						
							|  |  |  |             'currency_decimal_places'         => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             // converted to native currency
 | 
					
						
							|  |  |  |             'native_currency_id'              => (string)$this->default->id, | 
					
						
							|  |  |  |             'native_currency_code'            => $this->default->code, | 
					
						
							|  |  |  |             'native_currency_name'            => $this->default->name, | 
					
						
							|  |  |  |             'native_currency_symbol'          => $this->default->symbol, | 
					
						
							|  |  |  |             'native_currency_decimal_places'  => $this->default->decimal_places, | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             // foreign currency amount:
 | 
					
						
							|  |  |  |             'foreign_currency_id'             => $foreignCurrency?->id, | 
					
						
							|  |  |  |             'foreign_currency_code'           => $foreignCurrency?->code, | 
					
						
							|  |  |  |             'foreign_currency_name'           => $foreignCurrency?->name, | 
					
						
							|  |  |  |             'foreign_currency_symbol'         => $foreignCurrency?->symbol, | 
					
						
							|  |  |  |             'foreign_currency_decimal_places' => $foreignCurrency?->decimal_places, | 
					
						
							| 
									
										
										
										
											2024-01-06 07:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'description'                     => $journal->description, | 
					
						
							|  |  |  |             'source_id'                       => (string)$this->journals[$id]['source_account_id'], | 
					
						
							|  |  |  |             'source_name'                     => $this->journals[$id]['source_account_name'], | 
					
						
							|  |  |  |             'source_iban'                     => $this->journals[$id]['source_account_iban'], | 
					
						
							|  |  |  |             'source_type'                     => $this->journals[$id]['source_account_type'], | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'destination_id'                  => (string)$this->journals[$id]['destination_account_id'], | 
					
						
							|  |  |  |             'destination_name'                => $this->journals[$id]['destination_account_name'], | 
					
						
							|  |  |  |             'destination_iban'                => $this->journals[$id]['destination_account_iban'], | 
					
						
							|  |  |  |             'destination_type'                => $this->journals[$id]['destination_account_type'], | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             'budget_id'                       => $this->journals[$id]['budget_id'], | 
					
						
							|  |  |  |             'budget_name'                     => $this->journals[$id]['budget_name'], | 
					
						
							|  |  |  |             'category_id'                     => $this->journals[$id]['category_id'], | 
					
						
							|  |  |  |             'category_name'                   => $this->journals[$id]['category_name'], | 
					
						
							|  |  |  |             'bill_id'                         => $this->journals[$id]['bill_id'], | 
					
						
							|  |  |  |             'bill_name'                       => $this->journals[$id]['bill_name'], | 
					
						
							|  |  |  |             'reconciled'                      => $this->journals[$id]['reconciled'], | 
					
						
							|  |  |  |             'notes'                           => $this->journals[$id]['notes'] ?? null, | 
					
						
							|  |  |  |             'tags'                            => $this->journals[$id]['tags'] ?? [], | 
					
						
							|  |  |  |             'internal_reference'              => $meta['internal_reference'], | 
					
						
							|  |  |  |             'external_id'                     => $meta['external_id'], | 
					
						
							|  |  |  |             'original_source'                 => $meta['original_source'], | 
					
						
							|  |  |  |             'recurrence_id'                   => $meta['recurrence_id'], | 
					
						
							|  |  |  |             'recurrence_total'                => $meta['recurrence_total'], | 
					
						
							|  |  |  |             'recurrence_count'                => $meta['recurrence_count'], | 
					
						
							|  |  |  |             'external_url'                    => $meta['external_url'], | 
					
						
							|  |  |  |             'import_hash_v2'                  => $meta['import_hash_v2'], | 
					
						
							|  |  |  |             'sepa_cc'                         => $meta['sepa_cc'], | 
					
						
							|  |  |  |             'sepa_ct_op'                      => $meta['sepa_ct_op'], | 
					
						
							|  |  |  |             'sepa_ct_id'                      => $meta['sepa_ct_id'], | 
					
						
							|  |  |  |             'sepa_db'                         => $meta['sepa_db'], | 
					
						
							|  |  |  |             'sepa_country'                    => $meta['sepa_country'], | 
					
						
							|  |  |  |             'sepa_ep'                         => $meta['sepa_ep'], | 
					
						
							|  |  |  |             'sepa_ci'                         => $meta['sepa_ci'], | 
					
						
							|  |  |  |             'sepa_batch_id'                   => $meta['sepa_batch_id'], | 
					
						
							|  |  |  |             'interest_date'                   => $this->date($meta['interest_date']), | 
					
						
							|  |  |  |             'book_date'                       => $this->date($meta['book_date']), | 
					
						
							|  |  |  |             'process_date'                    => $this->date($meta['process_date']), | 
					
						
							|  |  |  |             'due_date'                        => $this->date($meta['due_date']), | 
					
						
							|  |  |  |             'payment_date'                    => $this->date($meta['payment_date']), | 
					
						
							|  |  |  |             'invoice_date'                    => $this->date($meta['invoice_date']), | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |             // location data
 | 
					
						
							|  |  |  |             'longitude'                       => $longitude, | 
					
						
							|  |  |  |             'latitude'                        => $latitude, | 
					
						
							|  |  |  |             'zoom_level'                      => $zoomLevel, | 
					
						
							|  |  |  |             //
 | 
					
						
							|  |  |  |             //            'has_attachments' => $this->hasAttachments((int) $row['transaction_journal_id']),
 | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2024-01-06 07:26:03 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | } |