mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Also store links when importing data.
This commit is contained in:
@@ -16,6 +16,7 @@ use Carbon\Carbon;
|
|||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Import\Converter\Amount;
|
use FireflyIII\Import\Converter\Amount;
|
||||||
use FireflyIII\Import\Converter\ConverterInterface;
|
use FireflyIII\Import\Converter\ConverterInterface;
|
||||||
|
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -50,6 +51,8 @@ class ImportJournal
|
|||||||
public $notes = '';
|
public $notes = '';
|
||||||
/** @var ImportAccount */
|
/** @var ImportAccount */
|
||||||
public $opposing;
|
public $opposing;
|
||||||
|
/** @var array */
|
||||||
|
public $tags = [];
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $amount = '0';
|
private $amount = '0';
|
||||||
/** @var ImportCurrency */
|
/** @var ImportCurrency */
|
||||||
@@ -60,8 +63,6 @@ class ImportJournal
|
|||||||
private $externalId = '';
|
private $externalId = '';
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $modifiers = [];
|
private $modifiers = [];
|
||||||
/** @var array */
|
|
||||||
private $tags = [];
|
|
||||||
/** @var User */
|
/** @var User */
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
@@ -269,7 +270,7 @@ class ImportJournal
|
|||||||
break;
|
break;
|
||||||
case 'tags-comma':
|
case 'tags-comma':
|
||||||
case 'tags-space':
|
case 'tags-space':
|
||||||
$this->tags[] = $array;
|
$this->setTags($array);
|
||||||
break;
|
break;
|
||||||
case 'date-interest':
|
case 'date-interest':
|
||||||
$this->metaDates['interest_date'] = $array['value'];
|
$this->metaDates['interest_date'] = $array['value'];
|
||||||
@@ -282,4 +283,18 @@ class ImportJournal
|
|||||||
break;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,6 +28,7 @@ use FireflyIII\Models\TransactionCurrency;
|
|||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||||
use FireflyIII\Rules\Processor;
|
use FireflyIII\Rules\Processor;
|
||||||
use Illuminate\Database\Query\JoinClause;
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -58,6 +59,8 @@ class ImportStorage
|
|||||||
private $objects;
|
private $objects;
|
||||||
/** @var Collection */
|
/** @var Collection */
|
||||||
private $rules;
|
private $rules;
|
||||||
|
/** @var TagRepositoryInterface */
|
||||||
|
private $tagRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ImportStorage constructor.
|
* ImportStorage constructor.
|
||||||
@@ -82,11 +85,17 @@ class ImportStorage
|
|||||||
*/
|
*/
|
||||||
public function setJob(ImportJob $job)
|
public function setJob(ImportJob $job)
|
||||||
{
|
{
|
||||||
$this->job = $job;
|
$this->job = $job;
|
||||||
|
|
||||||
$repository = app(CurrencyRepositoryInterface::class);
|
$repository = app(CurrencyRepositoryInterface::class);
|
||||||
$repository->setUser($job->user);
|
$repository->setUser($job->user);
|
||||||
$this->currencyRepository = $repository;
|
$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);
|
$journal->setMeta('notes', $importJournal->notes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// store tags
|
||||||
|
$this->storeTags($importJournal->tags, $journal);
|
||||||
|
|
||||||
// set journal completed:
|
// set journal completed:
|
||||||
$journal->completed = true;
|
$journal->completed = true;
|
||||||
$journal->save();
|
$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 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).
|
* This is important for import files that cover multiple accounts (and include both A<>B and B<>A transactions).
|
||||||
|
Reference in New Issue
Block a user