2016-07-15 15:26:08 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Import.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-07-15 15:26:08 -05:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-07-15 15:26:08 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Console\Commands;
|
|
|
|
|
|
|
|
use FireflyIII\Import\Logging\CommandHandler;
|
2017-06-20 14:04:25 -05:00
|
|
|
use FireflyIII\Import\Routine\ImportRoutine;
|
2016-07-15 15:26:08 -05:00
|
|
|
use FireflyIII\Models\ImportJob;
|
2016-08-26 00:36:13 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2016-07-15 15:26:08 -05:00
|
|
|
use Illuminate\Console\Command;
|
2016-12-24 07:43:42 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2017-06-21 13:04:35 -05:00
|
|
|
use Illuminate\Support\MessageBag;
|
2016-07-15 15:26:08 -05:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Import
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Console\Commands
|
|
|
|
*/
|
|
|
|
class Import extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-10-20 12:10:43 -05:00
|
|
|
protected $description = 'This will start a new import.';
|
2016-07-15 15:26:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-10-20 12:10:43 -05:00
|
|
|
protected $signature = 'firefly:start-import {key}';
|
2016-07-15 15:26:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2016-10-20 12:10:43 -05:00
|
|
|
Log::debug('Start start-import command');
|
2016-07-15 15:26:08 -05:00
|
|
|
$jobKey = $this->argument('key');
|
2017-03-01 14:02:47 -06:00
|
|
|
$job = ImportJob::where('key', $jobKey)->first();
|
2017-02-26 04:48:38 -06:00
|
|
|
if (is_null($job)) {
|
|
|
|
$this->error(sprintf('No job found with key "%s"', $jobKey));
|
2017-03-01 14:02:47 -06:00
|
|
|
|
|
|
|
return;
|
2017-02-26 04:48:38 -06:00
|
|
|
}
|
2016-08-24 12:33:58 -05:00
|
|
|
if (!$this->isValid($job)) {
|
2016-10-20 12:10:43 -05:00
|
|
|
Log::error('Job is not valid for some reason. Exit.');
|
2016-10-22 02:33:03 -05:00
|
|
|
|
2016-07-15 15:26:08 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:04:14 -05:00
|
|
|
$this->line(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type));
|
2016-08-05 23:21:25 -05:00
|
|
|
|
2016-07-15 15:26:08 -05:00
|
|
|
$monolog = Log::getMonolog();
|
|
|
|
$handler = new CommandHandler($this);
|
|
|
|
$monolog->pushHandler($handler);
|
2017-06-20 14:04:25 -05:00
|
|
|
|
|
|
|
$routine = new ImportRoutine($job);
|
|
|
|
$routine->run();
|
2016-12-23 00:02:45 -06:00
|
|
|
|
2017-06-21 13:04:35 -05:00
|
|
|
/** @var MessageBag $error */
|
|
|
|
foreach($routine->errors as $index => $error) {
|
|
|
|
if($error->count() > 0) {
|
|
|
|
$message = join(', ',$error->all());
|
|
|
|
$this->error(sprintf('Error importing line #%d: %s', $index, $message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 07:43:42 -06:00
|
|
|
// display result to user:
|
2017-06-20 14:04:25 -05:00
|
|
|
//$this->presentResults($result);
|
2017-06-21 13:04:35 -05:00
|
|
|
$this->line(sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->journals->count(), $routine->lines));
|
2016-08-13 14:51:01 -05:00
|
|
|
|
2016-08-26 00:36:13 -05:00
|
|
|
// get any errors from the importer:
|
2017-06-20 14:04:25 -05:00
|
|
|
//$this->presentErrors($job);
|
2016-08-26 01:21:31 -05:00
|
|
|
|
2016-08-26 00:36:13 -05:00
|
|
|
return;
|
2016-07-15 15:26:08 -05:00
|
|
|
}
|
2016-08-24 12:33:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isValid(ImportJob $job): bool
|
|
|
|
{
|
|
|
|
if (is_null($job)) {
|
2017-06-20 14:04:25 -05:00
|
|
|
Log::error('This job does not seem to exist.');
|
2016-08-24 12:33:58 -05:00
|
|
|
$this->error('This job does not seem to exist.');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:04:25 -05:00
|
|
|
if ($job->status !== 'configured') {
|
|
|
|
Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status));
|
2016-08-24 12:33:58 -05:00
|
|
|
$this->error('This job is not ready to be imported.');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-24 07:43:42 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*/
|
|
|
|
private function presentErrors(ImportJob $job)
|
|
|
|
{
|
|
|
|
$extendedStatus = $job->extended_status;
|
|
|
|
if (isset($extendedStatus['errors']) && count($extendedStatus['errors']) > 0) {
|
|
|
|
$this->line(sprintf('The following %d error(s) occured during the import:', count($extendedStatus['errors'])));
|
|
|
|
foreach ($extendedStatus['errors'] as $error) {
|
|
|
|
$this->error($error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $result
|
|
|
|
*/
|
|
|
|
private function presentResults(Collection $result)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int $index
|
|
|
|
* @var TransactionJournal $journal
|
|
|
|
*/
|
|
|
|
foreach ($result as $index => $journal) {
|
|
|
|
if (!is_null($journal->id)) {
|
|
|
|
$this->line(sprintf('Line #%d has been imported as transaction #%d.', $index, $journal->id));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->error(sprintf('Could not store line #%d', $index));
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 15:26:08 -05:00
|
|
|
}
|