mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
First version of a web-based import status thing.
This commit is contained in:
77
app/Import/ImportProcedure.php
Normal file
77
app/Import/ImportProcedure.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportProcedure.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import;
|
||||
|
||||
use FireflyIII\Crud\Account\AccountCrud;
|
||||
use FireflyIII\Import\Importer\ImporterInterface;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class ImportProcedure
|
||||
*
|
||||
* @package FireflyIII\Import
|
||||
*/
|
||||
class ImportProcedure
|
||||
{
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public static function run(ImportJob $job): Collection
|
||||
{
|
||||
// update job to say we started.
|
||||
$job->status = 'import_running';
|
||||
$job->save();
|
||||
|
||||
// create Importer
|
||||
$valid = array_keys(config('firefly.import_formats'));
|
||||
$class = 'INVALID';
|
||||
if (in_array($job->file_type, $valid)) {
|
||||
$class = config('firefly.import_formats.' . $job->file_type);
|
||||
}
|
||||
|
||||
/** @var ImporterInterface $importer */
|
||||
$importer = app($class);
|
||||
$importer->setJob($job);
|
||||
|
||||
// create import entries
|
||||
$collection = $importer->createImportEntries();
|
||||
|
||||
// validate / clean collection:
|
||||
$validator = new ImportValidator($collection);
|
||||
$validator->setUser($job->user);
|
||||
$validator->setJob($job);
|
||||
if ($job->configuration['import-account'] != 0) {
|
||||
$repository = app(AccountCrud::class, [$job->user]);
|
||||
$validator->setDefaultImportAccount($repository->find($job->configuration['import-account']));
|
||||
}
|
||||
|
||||
$cleaned = $validator->clean();
|
||||
|
||||
// then import collection:
|
||||
$storage = new ImportStorage($cleaned);
|
||||
$storage->setJob($job);
|
||||
$storage->setUser($job->user);
|
||||
|
||||
// and run store routine:
|
||||
$result = $storage->store();
|
||||
|
||||
$job->status = 'import_complete';
|
||||
$job->save();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
@@ -12,6 +12,7 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Import;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
@@ -29,7 +30,8 @@ class ImportStorage
|
||||
|
||||
/** @var Collection */
|
||||
public $entries;
|
||||
|
||||
/** @var ImportJob */
|
||||
public $job;
|
||||
/** @var User */
|
||||
public $user;
|
||||
|
||||
@@ -44,6 +46,14 @@ class ImportStorage
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
@@ -62,6 +72,8 @@ class ImportStorage
|
||||
foreach ($this->entries as $index => $entry) {
|
||||
Log::debug(sprintf('--- import store start for row %d ---', $index));
|
||||
$result = $this->storeSingle($index, $entry);
|
||||
$this->job->addStepsDone(1);
|
||||
sleep(1);
|
||||
$collection->put($index, $result);
|
||||
}
|
||||
Log::notice(sprintf('Finished storing %d entry(ies).', $collection->count()));
|
||||
|
@@ -15,6 +15,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Crud\Account\AccountCrudInterface;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
@@ -35,6 +36,19 @@ class ImportValidator
|
||||
/** @var User */
|
||||
protected $user;
|
||||
|
||||
/** @var ImportJob */
|
||||
public $job;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ImportValidator constructor.
|
||||
*
|
||||
@@ -71,6 +85,8 @@ class ImportValidator
|
||||
$entry = $this->setTransactionCurrency($entry);
|
||||
|
||||
$newCollection->put($index, $entry);
|
||||
$this->job->addStepsDone(1);
|
||||
sleep(1);
|
||||
}
|
||||
Log::notice(sprintf('Finished validating %d entry(ies).', $newCollection->count()));
|
||||
|
||||
|
@@ -64,6 +64,9 @@ class CsvImporter implements ImporterInterface
|
||||
Log::debug(sprintf('Now going to import row %d.', $index));
|
||||
$importEntry = $this->importSingleRow($index, $row);
|
||||
$this->collection->put($line, $importEntry);
|
||||
$this->job->addTotalSteps(3);
|
||||
$this->job->addStepsDone(1);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('Import collection contains %d entries', $this->collection->count()));
|
||||
|
Reference in New Issue
Block a user