Also store links when importing data.

This commit is contained in:
James Cole
2017-07-28 14:52:01 +02:00
parent 8e27291417
commit f4994ef151
2 changed files with 53 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Rules\Processor;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
@@ -58,6 +59,8 @@ class ImportStorage
private $objects;
/** @var Collection */
private $rules;
/** @var TagRepositoryInterface */
private $tagRepository;
/**
* ImportStorage constructor.
@@ -82,11 +85,17 @@ class ImportStorage
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
$this->job = $job;
$repository = app(CurrencyRepositoryInterface::class);
$repository->setUser($job->user);
$this->currencyRepository = $repository;
$this->rules = $this->getUserRules();
$repository = app(TagRepositoryInterface::class);
$repository->setUser($job->user);
$this->tagRepository = $repository;
$this->rules = $this->getUserRules();
}
/**
@@ -423,6 +432,9 @@ class ImportStorage
$journal->setMeta('notes', $importJournal->notes);
}
// store tags
$this->storeTags($importJournal->tags, $journal);
// set journal completed:
$journal->completed = true;
$journal->save();
@@ -462,6 +474,27 @@ class ImportStorage
}
}
/**
* @param array $tags
* @param TransactionJournal $journal
*/
private function storeTags(array $tags, TransactionJournal $journal): void
{
foreach ($tags as $tag) {
$dbTag = $this->tagRepository->findByTag($tag);
if (is_null($dbTag->id)) {
$dbTag = $this->tagRepository->store(
['tag' => $tag, 'date' => null, 'description' => null, 'latitude' => null, 'longitude' => null,
'zoomLevel' => null, 'tagMode' => 'nothing']
);
}
$journal->tags()->save($dbTag);
Log::debug(sprintf('Linked tag %d ("%s") to journal #%d', $dbTag->id, $dbTag->tag, $journal->id));
}
return;
}
/**
* This method checks if the given transaction is a transfer and if so, if it might be a duplicate of an already imported transfer.
* This is important for import files that cover multiple accounts (and include both A<>B and B<>A transactions).