From 49138eb03a877526caffc8bdfa92c223cb786ba0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 29 Apr 2018 10:13:33 +0200 Subject: [PATCH] Can now import and handle external ID field. --- app/Factory/TransactionJournalFactory.php | 2 +- app/Import/Object/ImportJournal.php | 9 +- app/Import/Storage/ImportStorage.php | 146 ++++++++++++---------- resources/lang/en_US/list.php | 1 + resources/views/transactions/show.twig | 2 +- 5 files changed, 90 insertions(+), 70 deletions(-) diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index a4422b7b71..4afdb35465 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -91,7 +91,7 @@ class TransactionJournalFactory // store date meta fields (if present): $fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date', - 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id','importHash']; + 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id', 'importHash', 'external_id']; foreach ($fields as $field) { $this->storeMeta($journal, $data, $field); diff --git a/app/Import/Object/ImportJournal.php b/app/Import/Object/ImportJournal.php index 9fe1d82349..8c2311c09f 100644 --- a/app/Import/Object/ImportJournal.php +++ b/app/Import/Object/ImportJournal.php @@ -158,6 +158,14 @@ class ImportJournal return $this->description; } + /** + * @return string + */ + public function getExternalId(): string + { + return $this->externalId; + } + /** * @return string|null */ @@ -231,7 +239,6 @@ class ImportJournal return null; } - /** * @param string $hash */ diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php index e53c707ea2..116431f3bc 100644 --- a/app/Import/Storage/ImportStorage.php +++ b/app/Import/Storage/ImportStorage.php @@ -199,78 +199,87 @@ class ImportStorage throw new FireflyException($message); } + + /** + * Search for journals with the same external ID. + * + */ + + unset($parameters); $this->addStep(); + $budget = $importJournal->budget->getBudget(); + $category = $importJournal->category->getCategory(); + $bill = $importJournal->bill->getBill(); + $source = $assetAccount; + $destination = $opposingAccount; - try { - $budget = $importJournal->budget->getBudget(); - $category = $importJournal->category->getCategory(); - $bill = $importJournal->bill->getBill(); - $source = $assetAccount; - $destination = $opposingAccount; + // switch account arounds when the transaction type is a deposit. + if ($transactionType === TransactionType::DEPOSIT) { + [$destination, $source] = [$source, $destination]; + } + // switch accounts around when the amount is negative and it's a transfer. + // credits to @NyKoF + if ($transactionType === TransactionType::TRANSFER && -1 === bccomp($amount, '0')) { + [$destination, $source] = [$source, $destination]; + } + Log::debug( + sprintf('Will make #%s (%s) the source and #%s (%s) the destination.', $source->id, $source->name, $destination->id, $destination->name) + ); - // switch account arounds when the transaction type is a deposit. - if ($transactionType === TransactionType::DEPOSIT) { - [$destination, $source] = [$source, $destination]; - } - // switch accounts around when the amount is negative and it's a transfer. - // credits to @NyKoF - if($transactionType === TransactionType::TRANSFER && -1 === bccomp($amount, '0')) { - [$destination, $source] = [$source, $destination]; - } - Log::debug( - sprintf('Will make #%s (%s) the source and #%s (%s) the destination.', $source->id, $source->name, $destination->id, $destination->name) - ); - $data = [ - 'user' => $this->job->user_id, - 'type' => $transactionType, - 'date' => $importJournal->getDate($this->dateFormat), - 'description' => $importJournal->getDescription(), - 'piggy_bank_id' => null, - 'piggy_bank_name' => null, - 'bill_id' => null === $bill ? null : $bill->id, - 'bill_name' => null, - 'tags' => $importJournal->tags, - 'interest_date' => $importJournal->getMetaDate('interest_date'), - 'book_date' => $importJournal->getMetaDate('book_date'), - 'process_date' => $importJournal->getMetaDate('process_date'), - 'due_date' => $importJournal->getMetaDate('due_date'), - 'payment_date' => $importJournal->getMetaDate('payment_date'), - 'invoice_date' => $importJournal->getMetaDate('invoice_date'), - 'internal_reference' => $importJournal->metaFields['internal_reference'] ?? null, - 'notes' => $importJournal->notes, - 'sepa-cc' => $importJournal->getMetaString('sepa-cc'), - 'sepa-ct-op' => $importJournal->getMetaString('sepa-ct-op'), - 'sepa-ct-id' => $importJournal->getMetaString('sepa-ct-id'), - 'sepa-db' => $importJournal->getMetaString('sepa-db'), - 'sepa-country' => $importJournal->getMetaString('sepa-country'), - 'sepa-ep' => $importJournal->getMetaString('sepa-ep'), - 'sepa-ci' => $importJournal->getMetaString('sepa-ci'), - 'importHash' => $importJournal->hash, - 'transactions' => [ - // single transaction: - [ - 'description' => null, - 'amount' => $amount, - 'currency_id' => $currencyId, - 'currency_code' => null, - 'foreign_amount' => $foreignAmount, - 'foreign_currency_id' => $foreignCurrencyId, - 'foreign_currency_code' => null, - 'budget_id' => null === $budget ? null : $budget->id, - 'budget_name' => null, - 'category_id' => null === $category ? null : $category->id, - 'category_name' => null, - 'source_id' => $source->id, - 'source_name' => null, - 'destination_id' => $destination->id, - 'destination_name' => null, - 'reconciled' => false, - 'identifier' => 0, - ], + $data = [ + 'user' => $this->job->user_id, + 'type' => $transactionType, + 'date' => $importJournal->getDate($this->dateFormat), + 'description' => $importJournal->getDescription(), + 'piggy_bank_id' => null, + 'piggy_bank_name' => null, + 'bill_id' => null === $bill ? null : $bill->id, + 'bill_name' => null, + 'tags' => $importJournal->tags, + 'interest_date' => $importJournal->getMetaDate('interest_date'), + 'book_date' => $importJournal->getMetaDate('book_date'), + 'process_date' => $importJournal->getMetaDate('process_date'), + 'due_date' => $importJournal->getMetaDate('due_date'), + 'payment_date' => $importJournal->getMetaDate('payment_date'), + 'invoice_date' => $importJournal->getMetaDate('invoice_date'), + 'internal_reference' => $importJournal->metaFields['internal_reference'] ?? null, + 'notes' => $importJournal->notes, + 'external_id' => $importJournal->getExternalId(), + 'sepa-cc' => $importJournal->getMetaString('sepa-cc'), + 'sepa-ct-op' => $importJournal->getMetaString('sepa-ct-op'), + 'sepa-ct-id' => $importJournal->getMetaString('sepa-ct-id'), + 'sepa-db' => $importJournal->getMetaString('sepa-db'), + 'sepa-country' => $importJournal->getMetaString('sepa-country'), + 'sepa-ep' => $importJournal->getMetaString('sepa-ep'), + 'sepa-ci' => $importJournal->getMetaString('sepa-ci'), + 'importHash' => $importJournal->hash, + 'transactions' => [ + // single transaction: + [ + 'description' => null, + 'amount' => $amount, + 'currency_id' => $currencyId, + 'currency_code' => null, + 'foreign_amount' => $foreignAmount, + 'foreign_currency_id' => $foreignCurrencyId, + 'foreign_currency_code' => null, + 'budget_id' => null === $budget ? null : $budget->id, + 'budget_name' => null, + 'category_id' => null === $category ? null : $category->id, + 'category_name' => null, + 'source_id' => $source->id, + 'source_name' => null, + 'destination_id' => $destination->id, + 'destination_name' => null, + 'reconciled' => false, + 'identifier' => 0, ], - ]; + ], + ]; + $factoryJournal = null; + try { $factoryJournal = $this->factory->create($data); $this->journals->push($factoryJournal); } catch (FireflyException $e) { @@ -278,11 +287,12 @@ class ImportStorage Log::error($e->getTraceAsString()); } + // double add step because "match bills" no longer happens. $this->addStep(); $this->addStep(); // run rules if config calls for it: - if (true === $this->applyRules) { + if (true === $this->applyRules && null !== $factoryJournal) { Log::info('Will apply rules to this journal.'); $this->applyRules($factoryJournal); } @@ -291,6 +301,8 @@ class ImportStorage if (!(true === $this->applyRules)) { Log::info('Will NOT apply rules to this journal.'); } + + // double add step because some other extra thing was removed here. $this->addStep(); $this->addStep(); diff --git a/resources/lang/en_US/list.php b/resources/lang/en_US/list.php index 05ffa9c72c..27036cd560 100644 --- a/resources/lang/en_US/list.php +++ b/resources/lang/en_US/list.php @@ -111,6 +111,7 @@ return [ 'sepa-cc' => 'SEPA Clearing Code', 'sepa-ep' => 'SEPA External Purpose', 'sepa-ci' => 'SEPA Creditor Identifier', + 'external_id' => 'External ID', 'account_at_bunq' => 'Account with bunq', 'file_name' => 'File name', 'file_size' => 'File size', diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig index e1627928e6..e66b3ff28d 100644 --- a/resources/views/transactions/show.twig +++ b/resources/views/transactions/show.twig @@ -210,7 +210,7 @@ {% endif %} {% endfor %} {# all other meta values #} - {% for metaField in ['internal_reference','sepa-ct-id','sepa-ct-op','sepa-db','sepa-country','sepa-cc','sepa-ep','sepa-ci'] %} + {% for metaField in ['external_id','internal_reference','sepa-ct-id','sepa-ct-op','sepa-db','sepa-country','sepa-cc','sepa-ep','sepa-ci'] %} {% if journalHasMeta(journal, metaField) %} {{ trans('list.'~metaField) }}