diff --git a/app/Import/Object/ImportJournal.php b/app/Import/Object/ImportJournal.php index 3a0b74ade5..6c5a5eb7e3 100644 --- a/app/Import/Object/ImportJournal.php +++ b/app/Import/Object/ImportJournal.php @@ -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; + } } diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php index ad0904b866..69a719233a 100644 --- a/app/Import/Storage/ImportStorage.php +++ b/app/Import/Storage/ImportStorage.php @@ -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).