diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepository.php b/app/Repositories/TransactionGroup/TransactionGroupRepository.php index 4404b1966d..57854ef33e 100644 --- a/app/Repositories/TransactionGroup/TransactionGroupRepository.php +++ b/app/Repositories/TransactionGroup/TransactionGroupRepository.php @@ -32,6 +32,7 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\TransactionGroupFactory; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\Attachment; +use FireflyIII\Models\Location; use FireflyIII\Models\Note; use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\Transaction; @@ -472,4 +473,15 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface return $journal->tags()->get(); } + + /** + * @inheritDoc + */ + public function getLocation(int $journalId): ?Location + { + /** @var TransactionJournal $journal */ + $journal = $this->user->transactionJournals()->find($journalId); + + return $journal->locations()->first(); + } } diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepositoryInterface.php b/app/Repositories/TransactionGroup/TransactionGroupRepositoryInterface.php index 2bf572db27..a797e1b0a7 100644 --- a/app/Repositories/TransactionGroup/TransactionGroupRepositoryInterface.php +++ b/app/Repositories/TransactionGroup/TransactionGroupRepositoryInterface.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\TransactionGroup; use FireflyIII\Exceptions\DuplicateTransactionException; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Models\Location; use FireflyIII\Models\TransactionGroup; use FireflyIII\Support\NullArrayObject; use FireflyIII\User; @@ -96,6 +97,16 @@ interface TransactionGroupRepositoryInterface */ public function getMetaFields(int $journalId, array $fields): NullArrayObject; + + /** + * Get the location of a journal or NULL. + * + * @param int $journalId + * + * @return Location|null + */ + public function getLocation(int $journalId): ?Location; + /** * Get the note text for a journal (by ID). * diff --git a/app/Transformers/TransactionGroupTransformer.php b/app/Transformers/TransactionGroupTransformer.php index 52eb1e5e66..3fac9a2d2b 100644 --- a/app/Transformers/TransactionGroupTransformer.php +++ b/app/Transformers/TransactionGroupTransformer.php @@ -27,6 +27,7 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\Category; +use FireflyIII\Models\Location; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionGroup; @@ -70,13 +71,14 @@ class TransactionGroupTransformer extends AbstractTransformer */ public function transform(array $group): array { - $data = new NullArrayObject($group); - $first = new NullArrayObject(reset($group['transactions'])); + $data = new NullArrayObject($group); + $first = new NullArrayObject(reset($group['transactions'])); + return [ - 'id' => (int) $first['transaction_group_id'], + 'id' => (int)$first['transaction_group_id'], 'created_at' => $first['created_at']->toAtomString(), 'updated_at' => $first['updated_at']->toAtomString(), - 'user' => (int) $data['user_id'], + 'user' => (int)$data['user_id'], 'group_title' => $data['title'], 'transactions' => $this->transformTransactions($data), 'links' => [ @@ -98,10 +100,10 @@ class TransactionGroupTransformer extends AbstractTransformer { try { $result = [ - 'id' => (int) $group->id, + 'id' => (int)$group->id, 'created_at' => $group->created_at->toAtomString(), 'updated_at' => $group->updated_at->toAtomString(), - 'user' => (int) $group->user_id, + 'user' => (int)$group->user_id, 'group_title' => $group->title, 'transactions' => $this->transformJournals($group->transactionJournals), 'links' => [ @@ -152,7 +154,7 @@ class TransactionGroupTransformer extends AbstractTransformer if (null === $bill) { return $array; } - $array['id'] = (int) $bill->id; + $array['id'] = (int)$bill->id; $array['name'] = $bill->name; return $array; @@ -172,7 +174,7 @@ class TransactionGroupTransformer extends AbstractTransformer if (null === $budget) { return $array; } - $array['id'] = (int) $budget->id; + $array['id'] = (int)$budget->id; $array['name'] = $budget->name; return $array; @@ -192,7 +194,7 @@ class TransactionGroupTransformer extends AbstractTransformer if (null === $category) { return $array; } - $array['id'] = (int) $category->id; + $array['id'] = (int)$category->id; $array['name'] = $category->name; return $array; @@ -234,7 +236,7 @@ class TransactionGroupTransformer extends AbstractTransformer { $result = $journal->transactions->first( static function (Transaction $transaction) { - return (float) $transaction->amount > 0; + return (float)$transaction->amount > 0; } ); if (null === $result) { @@ -276,10 +278,10 @@ class TransactionGroupTransformer extends AbstractTransformer if (null === $currency) { return $array; } - $array['id'] = (int) $currency->id; + $array['id'] = (int)$currency->id; $array['code'] = $currency->code; $array['symbol'] = $currency->symbol; - $array['decimal_places'] = (int) $currency->decimal_places; + $array['decimal_places'] = (int)$currency->decimal_places; return $array; } @@ -294,7 +296,7 @@ class TransactionGroupTransformer extends AbstractTransformer { $result = $journal->transactions->first( static function (Transaction $transaction) { - return (float) $transaction->amount < 0; + return (float)$transaction->amount < 0; } ); if (null === $result) { @@ -326,37 +328,47 @@ class TransactionGroupTransformer extends AbstractTransformer $bill = $this->getBill($journal->bill); if (null !== $foreignAmount && null !== $foreignCurrency) { - $foreignAmount = number_format((float) $foreignAmount, $foreignCurrency['decimal_places'], '.', ''); + $foreignAmount = number_format((float)$foreignAmount, $foreignCurrency['decimal_places'], '.', ''); + } + + $longitude = null; + $latitude = null; + $zoomLevel = null; + $location = $this->getLocation($journal); + if (null !== $location) { + $longitude = $location->longitude; + $latitude = $location->latitude; + $zoomLevel = $location->zoom_level; } return [ - 'user' => (int) $journal->user_id, - 'transaction_journal_id' => (int) $journal->id, + 'user' => (int)$journal->user_id, + 'transaction_journal_id' => (int)$journal->id, 'type' => strtolower($type), 'date' => $journal->date->toAtomString(), 'order' => $journal->order, - 'currency_id' => (int) $currency->id, + 'currency_id' => (int)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, - 'currency_decimal_places' => (int) $currency->decimal_places, + 'currency_decimal_places' => (int)$currency->decimal_places, 'foreign_currency_id' => $foreignCurrency['id'], 'foreign_currency_code' => $foreignCurrency['code'], 'foreign_currency_symbol' => $foreignCurrency['symbol'], 'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'], - 'amount' => number_format((float) $amount, $currency->decimal_places, '.', ''), + 'amount' => number_format((float)$amount, $currency->decimal_places, '.', ''), 'foreign_amount' => $foreignAmount, 'description' => $journal->description, - 'source_id' => (int) $source->account_id, + 'source_id' => (int)$source->account_id, 'source_name' => $source->account->name, 'source_iban' => $source->account->iban, 'source_type' => $source->account->accountType->type, - 'destination_id' => (int) $destination->account_id, + 'destination_id' => (int)$destination->account_id, 'destination_name' => $destination->account->name, 'destination_iban' => $destination->account->iban, 'destination_type' => $destination->account->accountType->type, @@ -396,6 +408,11 @@ class TransactionGroupTransformer extends AbstractTransformer 'due_date' => $metaDates['due_date'], 'payment_date' => $metaDates['payment_date'], 'invoice_date' => $metaDates['invoice_date'], + + // location data + 'longitude' => $longitude, + 'latitude' => $latitude, + 'zoom_level' => $zoomLevel, ]; } @@ -428,11 +445,13 @@ class TransactionGroupTransformer extends AbstractTransformer foreach ($transactions as $transaction) { $result[] = $this->transformTransaction($transaction); } + return $result; } /** * @param array $transaction + * * @return array */ private function transformTransaction(array $transaction): array @@ -447,21 +466,31 @@ class TransactionGroupTransformer extends AbstractTransformer $foreignAmount = app('steam')->positive($row['foreign_amount']); } - $metaFieldData = $this->groupRepos->getMetaFields((int) $row['transaction_journal_id'], $this->metaFields); - $metaDateData = $this->groupRepos->getMetaDateFields((int) $row['transaction_journal_id'], $this->metaDateFields); + $metaFieldData = $this->groupRepos->getMetaFields((int)$row['transaction_journal_id'], $this->metaFields); + $metaDateData = $this->groupRepos->getMetaDateFields((int)$row['transaction_journal_id'], $this->metaDateFields); + + $longitude = null; + $latitude = null; + $zoomLevel = null; + $location = $this->getLocationById((int)$row['transaction_journal_id']); + if (null !== $location) { + $longitude = $location->longitude; + $latitude = $location->latitude; + $zoomLevel = $location->zoom_level; + } return [ - 'user' => (int) $row['user_id'], - 'transaction_journal_id' => (int) $row['transaction_journal_id'], + 'user' => (int)$row['user_id'], + 'transaction_journal_id' => (int)$row['transaction_journal_id'], 'type' => strtolower($type), 'date' => $row['date']->toAtomString(), 'order' => $row['order'], - 'currency_id' => (int) $row['currency_id'], + 'currency_id' => (int)$row['currency_id'], 'currency_code' => $row['currency_code'], 'currency_name' => $row['currency_name'], 'currency_symbol' => $row['currency_symbol'], - 'currency_decimal_places' => (int) $row['currency_decimal_places'], + 'currency_decimal_places' => (int)$row['currency_decimal_places'], 'foreign_currency_id' => $this->integerFromArray($transaction, 'foreign_currency_id'), 'foreign_currency_code' => $row['foreign_currency_code'], @@ -473,12 +502,12 @@ class TransactionGroupTransformer extends AbstractTransformer 'description' => $row['description'], - 'source_id' => (int) $row['source_account_id'], + 'source_id' => (int)$row['source_account_id'], 'source_name' => $row['source_account_name'], 'source_iban' => $row['source_account_iban'], 'source_type' => $row['source_account_type'], - 'destination_id' => (int) $row['destination_account_id'], + 'destination_id' => (int)$row['destination_account_id'], 'destination_name' => $row['destination_account_name'], 'destination_iban' => $row['destination_account_iban'], 'destination_type' => $row['destination_account_type'], @@ -493,8 +522,8 @@ class TransactionGroupTransformer extends AbstractTransformer 'bill_name' => $row['bill_name'], 'reconciled' => $row['reconciled'], - 'notes' => $this->groupRepos->getNoteText((int) $row['transaction_journal_id']), - 'tags' => $this->groupRepos->getTags((int) $row['transaction_journal_id']), + 'notes' => $this->groupRepos->getNoteText((int)$row['transaction_journal_id']), + 'tags' => $this->groupRepos->getTags((int)$row['transaction_journal_id']), 'internal_reference' => $metaFieldData['internal_reference'], 'external_id' => $metaFieldData['external_id'], @@ -521,6 +550,11 @@ class TransactionGroupTransformer extends AbstractTransformer 'due_date' => $this->dateFromArray($metaDateData, 'due_date'), 'payment_date' => $this->dateFromArray($metaDateData, 'payment_date'), 'invoice_date' => $this->dateFromArray($metaDateData, 'invoice_date'), + + // location data + 'longitude' => $longitude, + 'latitude' => $latitude, + 'zoom_level' => $zoomLevel, ]; } @@ -528,6 +562,7 @@ class TransactionGroupTransformer extends AbstractTransformer * @param array $array * @param string $key * @param string|null $default + * * @return string|null */ private function stringFromArray(array $array, string $key, ?string $default): ?string @@ -538,25 +573,29 @@ class TransactionGroupTransformer extends AbstractTransformer if (null !== $default) { return $default; } + return null; } /** * @param array $array * @param string $key + * * @return int|null */ private function integerFromArray(array $array, string $key): ?int { if (array_key_exists($key, $array)) { - return (int) $array[$key]; + return (int)$array[$key]; } + return null; } /** * @param NullArrayObject $object * @param string $key + * * @return string|null */ private function dateFromArray(NullArrayObject $object, string $key): ?string @@ -567,4 +606,24 @@ class TransactionGroupTransformer extends AbstractTransformer return $object[$key]->toAtomString(); } + + /** + * @param TransactionJournal $journal + * + * @return Location|null + */ + private function getLocation(TransactionJournal $journal): ?Location + { + return $journal->locations()->first(); + } + + /** + * @param int $journalId + * + * @return Location|null + */ + private function getLocationById(int $journalId): ?Location + { + return $this->groupRepos->getLocation($journalId); + } }