Import routine can handle new SEPA fields and many new date fields. See #1248

This commit is contained in:
James Cole
2018-03-19 19:38:17 +01:00
parent 4e69bc0e32
commit aecffe10d9
6 changed files with 113 additions and 50 deletions

View File

@@ -80,7 +80,7 @@ class CreateImport extends Command
public function handle() public function handle()
{ {
if (!$this->verifyAccessToken()) { if (!$this->verifyAccessToken()) {
$this->error('Invalid access token.'); $this->errorLine('Invalid access token.');
return; return;
} }
@@ -98,39 +98,34 @@ class CreateImport extends Command
$configurationData = json_decode(file_get_contents($configuration), true); $configurationData = json_decode(file_get_contents($configuration), true);
if (null === $configurationData) { if (null === $configurationData) {
$this->error(sprintf('Firefly III cannot read the contents of configuration file "%s" (working directory: "%s").', $configuration, $cwd)); $this->errorLine(sprintf('Firefly III cannot read the contents of configuration file "%s" (working directory: "%s").', $configuration, $cwd));
return; return;
} }
$this->line(sprintf('Going to create a job to import file: %s', $file)); $this->infoLine(sprintf('Going to create a job to import file: %s', $file));
$this->line(sprintf('Using configuration file: %s', $configuration)); $this->infoLine(sprintf('Using configuration file: %s', $configuration));
$this->line(sprintf('Import into user: #%d (%s)', $user->id, $user->email)); $this->infoLine(sprintf('Import into user: #%d (%s)', $user->id, $user->email));
$this->line(sprintf('Type of import: %s', $type)); $this->infoLine(sprintf('Type of import: %s', $type));
/** @var ImportJobRepositoryInterface $jobRepository */ /** @var ImportJobRepositoryInterface $jobRepository */
$jobRepository = app(ImportJobRepositoryInterface::class); $jobRepository = app(ImportJobRepositoryInterface::class);
$jobRepository->setUser($user); $jobRepository->setUser($user);
$job = $jobRepository->create($type); $job = $jobRepository->create($type);
$this->line(sprintf('Created job "%s"', $job->key)); $this->infoLine(sprintf('Created job "%s"', $job->key));
Artisan::call('firefly:encrypt-file', ['file' => $file, 'key' => $job->key]); Artisan::call('firefly:encrypt-file', ['file' => $file, 'key' => $job->key]);
$this->line('Stored import data...'); $this->infoLine('Stored import data...');
$jobRepository->setConfiguration($job, $configurationData); $jobRepository->setConfiguration($job, $configurationData);
$jobRepository->updateStatus($job, 'configured'); $jobRepository->updateStatus($job, 'configured');
$this->line('Stored configuration...'); $this->infoLine('Stored configuration...');
if (true === $this->option('start')) { if (true === $this->option('start')) {
$this->line('The import will start in a moment. This process is not visible...'); $this->infoLine('The import will start in a moment. This process is not visible...');
Log::debug('Go for import!'); Log::debug('Go for import!');
// normally would refer to other firefly:start-import but that doesn't seem to work all to well... // normally would refer to other firefly:start-import but that doesn't seem to work all to well...
$monolog = Log::getMonolog();
$handler = new CommandHandler($this);
$formatter = new LineFormatter(null, null, false, true);
$handler->setFormatter($formatter);
$monolog->pushHandler($handler);
// start the actual routine: // start the actual routine:
$type = 'csv' === $job->file_type ? 'file' : $job->file_type; $type = 'csv' === $job->file_type ? 'file' : $job->file_type;
@@ -147,9 +142,9 @@ class CreateImport extends Command
// give feedback. // give feedback.
/** @var MessageBag $error */ /** @var MessageBag $error */
foreach ($routine->getErrors() as $index => $error) { foreach ($routine->getErrors() as $index => $error) {
$this->error(sprintf('Error importing line #%d: %s', $index, $error)); $this->errorLine(sprintf('Error importing line #%d: %s', $index, $error));
} }
$this->line( $this->infoLine(
sprintf( sprintf(
'The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines() 'The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines()
) )
@@ -179,29 +174,50 @@ class CreateImport extends Command
$validTypes = config('import.options.file.import_formats'); $validTypes = config('import.options.file.import_formats');
$type = strtolower($this->option('type')); $type = strtolower($this->option('type'));
if (null === $user) { if (null === $user) {
$this->error(sprintf('There is no user with ID %d.', $this->option('user'))); $this->errorLine(sprintf('There is no user with ID %d.', $this->option('user')));
return false; return false;
} }
if (!in_array($type, $validTypes)) { if (!in_array($type, $validTypes)) {
$this->error(sprintf('Cannot import file of type "%s"', $type)); $this->errorLine(sprintf('Cannot import file of type "%s"', $type));
return false; return false;
} }
if (!file_exists($file)) { if (!file_exists($file)) {
$this->error(sprintf('Firefly III cannot find file "%s" (working directory: "%s").', $file, $cwd)); $this->errorLine(sprintf('Firefly III cannot find file "%s" (working directory: "%s").', $file, $cwd));
return false; return false;
} }
if (!file_exists($configuration)) { if (!file_exists($configuration)) {
$this->error(sprintf('Firefly III cannot find configuration file "%s" (working directory: "%s").', $configuration, $cwd)); $this->errorLine(sprintf('Firefly III cannot find configuration file "%s" (working directory: "%s").', $configuration, $cwd));
return false; return false;
} }
return true; return true;
} }
/**
* @param string $message
* @param array|null $data
*/
private function errorLine(string $message, array $data = null): void
{
Log::error($message, $data?? []);
$this->error($message);
}
/**
* @param string $message
* @param array $data
*/
private function infoLine(string $message, array $data = null): void
{
Log::info($message, $data?? []);
$this->line($message);
}
} }

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands; namespace FireflyIII\Console\Commands;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Logging\CommandHandler;
use FireflyIII\Import\Routine\RoutineInterface; use FireflyIII\Import\Routine\RoutineInterface;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@@ -68,21 +67,17 @@ class Import extends Command
$jobKey = $this->argument('key'); $jobKey = $this->argument('key');
$job = ImportJob::where('key', $jobKey)->first(); $job = ImportJob::where('key', $jobKey)->first();
if (null === $job) { if (null === $job) {
$this->error(sprintf('No job found with key "%s"', $jobKey)); $this->errorLine(sprintf('No job found with key "%s"', $jobKey));
return; return;
} }
if (!$this->isValid($job)) { if (!$this->isValid($job)) {
Log::error('Job is not valid for some reason. Exit.'); $this->errorLine('Job is not valid for some reason. Exit.');
return; return;
} }
$this->line(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type)); $this->infoLine(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type));
$monolog = Log::getMonolog();
$handler = new CommandHandler($this);
$monolog->pushHandler($handler);
// actually start job: // actually start job:
$type = 'csv' === $job->file_type ? 'file' : $job->file_type; $type = 'csv' === $job->file_type ? 'file' : $job->file_type;
@@ -99,16 +94,37 @@ class Import extends Command
/** @var MessageBag $error */ /** @var MessageBag $error */
foreach ($routine->getErrors() as $index => $error) { foreach ($routine->getErrors() as $index => $error) {
$this->error(sprintf('Error importing line #%d: %s', $index, $error)); $this->errorLine(sprintf('Error importing line #%d: %s', $index, $error));
} }
$this->line( $this->infoLine(
sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines()) sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines())
); );
return; return;
} }
/**
* @param string $message
* @param array|null $data
*/
private function errorLine(string $message, array $data = null): void
{
Log::error($message, $data ?? []);
$this->error($message);
}
/**
* @param string $message
* @param array $data
*/
private function infoLine(string $message, array $data = null): void
{
Log::info($message, $data ?? []);
$this->line($message);
}
/** /**
* Check if job is valid to be imported. * Check if job is valid to be imported.
* *
@@ -119,15 +135,14 @@ class Import extends Command
private function isValid(ImportJob $job): bool private function isValid(ImportJob $job): bool
{ {
if (null === $job) { if (null === $job) {
Log::error('This job does not seem to exist.'); $this->errorLine('This job does not seem to exist.');
$this->error('This job does not seem to exist.');
return false; return false;
} }
if ('configured' !== $job->status) { if ('configured' !== $job->status) {
Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status)); Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status));
$this->error('This job is not ready to be imported.'); $this->errorLine('This job is not ready to be imported.');
return false; return false;
} }

View File

@@ -312,9 +312,9 @@ class ImportAccount
Log::debug('Finding a mapped account based on', $array); Log::debug('Finding a mapped account based on', $array);
$search = intval($array['mapped'] ?? 0); $search = intval($array['mapped'] ?? 0);
$account = $this->repository->find($search); $account = $this->repository->findNull($search);
if (null === $account->id) { if (null === $account) {
Log::error(sprintf('There is no account with id #%d. Invalid mapping will be ignored!', $search)); Log::error(sprintf('There is no account with id #%d. Invalid mapping will be ignored!', $search));
return null; return null;
@@ -382,7 +382,7 @@ class ImportAccount
// 4: if search for an asset account, fall back to given "default account" (mandatory) // 4: if search for an asset account, fall back to given "default account" (mandatory)
if (AccountType::ASSET === $this->expectedType) { if (AccountType::ASSET === $this->expectedType) {
$this->account = $this->repository->find($this->defaultAccountId); $this->account = $this->repository->findNull($this->defaultAccountId);
Log::debug(sprintf('Fall back to default account #%d "%s"', $this->account->id, $this->account->name)); Log::debug(sprintf('Fall back to default account #%d "%s"', $this->account->id, $this->account->name));
return true; return true;

View File

@@ -54,6 +54,8 @@ class ImportJournal
public $hash; public $hash;
/** @var array */ /** @var array */
public $metaDates = []; public $metaDates = [];
/** @var array */
public $metaFields = [];
/** @var string */ /** @var string */
public $notes = ''; public $notes = '';
/** @var ImportAccount */ /** @var ImportAccount */
@@ -192,6 +194,18 @@ class ImportJournal
case 'account-id': case 'account-id':
$this->asset->setAccountId($array); $this->asset->setAccountId($array);
break; break;
case 'sepa-cc':
case 'sepa-ct-op':
case 'sepa-ct-id':
case 'sepa-db':
case 'sepa-country':
case 'sepa-ep':
case 'sepa-ci':
$value = trim(strval($array['value']));
if (strlen($value) > 0) {
$this->metaFields[$array['role']] = $value;
}
break;
case 'amount': case 'amount':
$this->amount = $array; $this->amount = $array;
break; break;
@@ -252,12 +266,6 @@ class ImportJournal
case 'description': case 'description':
$this->description .= $array['value']; $this->description .= $array['value'];
break; break;
case 'sepa-ct-op':
case 'sepa-ct-id':
case 'sepa-db':
$this->notes .= ' ' . $array['value'];
$this->notes = trim($this->notes);
break;
case 'note': case 'note':
$this->notes .= ' ' . $array['value']; $this->notes .= ' ' . $array['value'];
$this->notes = trim($this->notes); $this->notes = trim($this->notes);
@@ -265,6 +273,9 @@ class ImportJournal
case 'external-id': case 'external-id':
$this->externalId = $array['value']; $this->externalId = $array['value'];
break; break;
case 'internal-reference':
$this->metaFields['internal_reference'] = $array['value'];
break;
case '_ignore': case '_ignore':
break; break;
case 'ing-debit-credit': case 'ing-debit-credit':
@@ -299,6 +310,15 @@ class ImportJournal
case 'date-process': case 'date-process':
$this->metaDates['process_date'] = $array['value']; $this->metaDates['process_date'] = $array['value'];
break; break;
case 'date-due':
$this->metaDates['due_date'] = $array['value'];
break;
case 'date-payment':
$this->metaDates['payment_date'] = $array['value'];
break;
case 'date-invoice':
$this->metaDates['invoice_date'] = $array['value'];
break;
} }
} }

View File

@@ -31,6 +31,7 @@ use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
use Preferences; use Preferences;
@@ -61,9 +62,11 @@ class ImportStorage
/** @var Collection */ /** @var Collection */
protected $bills; protected $bills;
/** @var int */ /** @var int */
protected $defaultCurrencyId = 1; // yes, hard coded protected $defaultCurrencyId = 1;
/** @var ImportJob */ /** @var ImportJob */
protected $job; protected $job; // yes, hard coded
/** @var JournalRepositoryInterface */
protected $journalRepository;
/** @var ImportJobRepositoryInterface */ /** @var ImportJobRepositoryInterface */
protected $repository; protected $repository;
/** @var Collection */ /** @var Collection */
@@ -104,8 +107,10 @@ class ImportStorage
*/ */
public function setJob(ImportJob $job) public function setJob(ImportJob $job)
{ {
$this->repository = app(ImportJobRepositoryInterface::class); $this->repository = app(ImportJobRepositoryInterface::class);
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->repository->setUser($job->user); $this->repository->setUser($job->user);
$this->journalRepository->setUser($job->user);
$config = $this->repository->getConfiguration($job); $config = $this->repository->getConfiguration($job);
$currency = app('amount')->getDefaultCurrencyByUser($job->user); $currency = app('amount')->getDefaultCurrencyByUser($job->user);
@@ -232,9 +237,13 @@ class ImportStorage
$importJournal->bill->setAmount($amount); $importJournal->bill->setAmount($amount);
$this->storeBill($journal, $importJournal->bill->getBill()); $this->storeBill($journal, $importJournal->bill->getBill());
$this->storeMeta($journal, $importJournal->metaDates); $this->storeMetaDates($journal, $importJournal->metaDates);
$this->storeTags($importJournal->tags, $journal); $this->storeTags($importJournal->tags, $journal);
foreach ($importJournal->metaFields as $field => $value) {
$this->journalRepository->setMetaString($journal, $field, $value);
}
// set notes for journal: // set notes for journal:
$dbNote = new Note(); $dbNote = new Note();
$dbNote->noteable()->associate($journal); $dbNote->noteable()->associate($journal);

View File

@@ -39,6 +39,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\TransactionRules\Processor; use FireflyIII\TransactionRules\Processor;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
@@ -58,6 +59,8 @@ trait ImportSupport
protected $defaultCurrencyId = 1; protected $defaultCurrencyId = 1;
/** @var ImportJob */ /** @var ImportJob */
protected $job; protected $job;
/** @var JournalRepositoryInterface */
protected $journalRepository;
/** @var Collection */ /** @var Collection */
protected $rules; protected $rules;
@@ -441,7 +444,7 @@ trait ImportSupport
throw new FireflyException($errorText); throw new FireflyException($errorText);
} }
// save meta data: // save meta data:
$journal->setMeta('importHash', $parameters['hash']); $this->journalRepository->setMetaString($journal, 'importHash', $parameters['hash']);
Log::debug(sprintf('Created journal with ID #%d', $journal->id)); Log::debug(sprintf('Created journal with ID #%d', $journal->id));
// create transactions: // create transactions:
@@ -472,13 +475,13 @@ trait ImportSupport
* @param TransactionJournal $journal * @param TransactionJournal $journal
* @param array $dates * @param array $dates
*/ */
private function storeMeta(TransactionJournal $journal, array $dates) private function storeMetaDates(TransactionJournal $journal, array $dates)
{ {
// all other date fields as meta thing: // all other date fields as meta thing:
foreach ($dates as $name => $value) { foreach ($dates as $name => $value) {
try { try {
$date = new Carbon($value); $date = new Carbon($value);
$journal->setMeta($name, $date); $this->journalRepository->setMetaDate($journal, $name, $date);
} catch (Exception $e) { } catch (Exception $e) {
// don't care, ignore: // don't care, ignore:
Log::warning(sprintf('Could not parse "%s" into a valid Date object for field %s', $value, $name)); Log::warning(sprintf('Could not parse "%s" into a valid Date object for field %s', $value, $name));