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
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 53 additions and 5 deletions

View File

@ -16,6 +16,7 @@ use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Converter\Amount;
use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Illuminate\Support\Collection;
@ -50,6 +51,8 @@ class ImportJournal
public $notes = '';
/** @var ImportAccount */
public $opposing;
/** @var array */
public $tags = [];
/** @var string */
private $amount = '0';
/** @var ImportCurrency */
@ -60,8 +63,6 @@ class ImportJournal
private $externalId = '';
/** @var array */
private $modifiers = [];
/** @var array */
private $tags = [];
/** @var User */
private $user;
@ -269,7 +270,7 @@ class ImportJournal
break;
case 'tags-comma':
case 'tags-space':
$this->tags[] = $array;
$this->setTags($array);
break;
case 'date-interest':
$this->metaDates['interest_date'] = $array['value'];
@ -282,4 +283,18 @@ class ImportJournal
break;
}
}
/**
* @param array $array
*/
private function setTags(array $array): void
{
$preProcessorClass = config(sprintf('csv.import_roles.%s.pre-process-mapper', $array['role']));
/** @var PreProcessorInterface $preProcessor */
$preProcessor = app(sprintf('\FireflyIII\Import\MapperPreProcess\%s', $preProcessorClass));
$tags = $preProcessor->run($array['value']);
$this->tags = array_merge($this->tags, $tags);
return;
}
}

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).