From 7d8876f03ccd3d59d8aa4759cd0fa0951381a8f0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 18 Aug 2017 15:14:44 +0200 Subject: [PATCH] new export routine --- app/Console/Commands/UpgradeDatabase.php | 23 +++++++++---- app/Export/Entry/Entry.php | 5 +-- app/Export/ExpandedProcessor.php | 33 ++++++++++++++++++- app/Models/Transaction.php | 2 ++ .../Currency/CurrencyRepository.php | 10 ++++++ .../Currency/CurrencyRepositoryInterface.php | 7 ++++ 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index 59065e4d6c..b70011a8d9 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -27,6 +27,7 @@ use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use Illuminate\Console\Command; use Illuminate\Database\QueryException; +use Illuminate\Support\Collection; use Log; use Preferences; use Schema; @@ -262,15 +263,23 @@ class UpgradeDatabase extends Command $set->each( function (TransactionJournal $transfer) use ($repository) { - /** @var Transaction $transaction */ - $transaction = $transfer->transactions()->where('amount', '<', 0)->first(); - $this->updateTransactionCurrency($transaction); - $this->updateJournalCurrency($transaction); + /** @var Collection $transactions */ + $transactions = $transfer->transactions()->where('amount', '<', 0)->get(); + $transactions->each( + function (Transaction $transaction) { + $this->updateTransactionCurrency($transaction); + $this->updateJournalCurrency($transaction); + } + ); - /** @var Transaction $transaction */ - $transaction = $transfer->transactions()->where('amount', '>', 0)->first(); - $this->updateTransactionCurrency($transaction); + /** @var Collection $transactions */ + $transactions = $transfer->transactions()->where('amount', '>', 0)->get(); + $transactions->each( + function (Transaction $transaction) { + $this->updateTransactionCurrency($transaction); + } + ); } ); } diff --git a/app/Export/Entry/Entry.php b/app/Export/Entry/Entry.php index 817b71ce43..b652d9ad66 100644 --- a/app/Export/Entry/Entry.php +++ b/app/Export/Entry/Entry.php @@ -148,13 +148,14 @@ final class Entry $entry->asset_account_iban = $transaction->account_iban; $entry->asset_account_number = $transaction->account_number; $entry->asset_account_bic = $transaction->account_bic; - // asset_currency_code + $entry->asset_currency_code = $transaction->account_currency_code; + $entry->opposing_account_id = $transaction->opposing_account_id; $entry->opposing_account_name = app('steam')->tryDecrypt($transaction->opposing_account_name); $entry->opposing_account_iban = $transaction->opposing_account_iban; $entry->opposing_account_number = $transaction->opposing_account_number; $entry->opposing_account_bic = $transaction->opposing_account_bic; - // opposing currency code + $entry->opposing_currency_code = $transaction->opposing_currency_code; /** budget */ $entry->budget_id = $transaction->transaction_budget_id; diff --git a/app/Export/ExpandedProcessor.php b/app/Export/ExpandedProcessor.php index 703963f27d..569ff55d9d 100644 --- a/app/Export/ExpandedProcessor.php +++ b/app/Export/ExpandedProcessor.php @@ -25,6 +25,7 @@ use FireflyIII\Models\AccountMeta; use FireflyIII\Models\ExportJob; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournalMeta; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use Illuminate\Support\Collection; use Log; use Storage; @@ -101,17 +102,22 @@ class ExpandedProcessor implements ProcessorInterface $notes = $this->getNotes($ids); $tags = $this->getTags($ids); $ibans = $this->getIbans($assetIds) + $this->getIbans($opposingIds); + $currencies = $this->getAccountCurrencies($ibans); $transactions->each( - function (Transaction $transaction) use ($notes, $tags, $ibans) { + function (Transaction $transaction) use ($notes, $tags, $ibans, $currencies) { $journalId = intval($transaction->journal_id); $accountId = intval($transaction->account_id); $opposingId = intval($transaction->opposing_account_id); + $currencyId = $ibans[$accountId]['currency_id'] ?? 0; + $opposingCurrencyId = $ibans[$opposingId]['currency_id'] ?? 0; $transaction->notes = $notes[$journalId] ?? ''; $transaction->tags = join(',', $tags[$journalId] ?? []); $transaction->account_number = $ibans[$accountId]['accountNumber'] ?? ''; $transaction->account_bic = $ibans[$accountId]['BIC'] ?? ''; + $transaction->account_currency_code = $currencies[$currencyId] ?? ''; $transaction->opposing_account_number = $ibans[$opposingId]['accountNumber'] ?? ''; $transaction->opposing_account_bic = $ibans[$opposingId]['BIC'] ?? ''; + $transaction->opposing_currency_code = $currencies[$opposingCurrencyId] ?? ''; } ); @@ -230,6 +236,31 @@ class ExpandedProcessor implements ProcessorInterface } } + /** + * @param array $array + * + * @return array + */ + private function getAccountCurrencies(array $array): array + { + /** @var CurrencyRepositoryInterface $repository */ + $repository = app(CurrencyRepositoryInterface::class); + $return = []; + $ids = []; + $repository->setUser($this->job->user); + foreach ($array as $value) { + $ids[] = $value['currency_id'] ?? 0; + } + $ids = array_unique($ids); + $result = $repository->getByIds($ids); + + foreach ($result as $currency) { + $return[$currency->id] = $currency->code; + } + + return $return; + } + /** * Get all IBAN / SWIFT / account numbers * diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 2b9deae872..1437532083 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -34,12 +34,14 @@ use Watson\Validating\ValidatingTrait; * @property string $account_iban * @property string $account_number * @property string $account_bic + * @property string $account_currency_code * * @property-read int $opposing_account_id * @property string $opposing_account_name * @property string $opposing_account_iban * @property string $opposing_account_number * @property string $opposing_account_bic + * @property string $opposing_currency_code * * * @property-read int $transaction_budget_id diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index 6e0ae64ae7..d86adea16c 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -166,6 +166,16 @@ class CurrencyRepository implements CurrencyRepositoryInterface return TransactionCurrency::get(); } + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids): Collection + { + return TransactionCurrency::whereIn('id', $ids)->get(); + } + /** * @param Preference $preference * diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php index 6c2ccdd61b..07b953aeca 100644 --- a/app/Repositories/Currency/CurrencyRepositoryInterface.php +++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php @@ -90,6 +90,13 @@ interface CurrencyRepositoryInterface */ public function get(): Collection; + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids): Collection; + /** * @param Preference $preference *