mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
More code for import routine.
This commit is contained in:
@@ -20,6 +20,7 @@ use FireflyIII\Import\ImportProcedureInterface;
|
||||
use FireflyIII\Import\Routine\ImportRoutine;
|
||||
use FireflyIII\Import\Storage\ImportStorage;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
@@ -188,11 +189,12 @@ class ImportController extends Controller
|
||||
if ($job->extended_status['total_steps'] !== 0) {
|
||||
$percentage = round(($job->extended_status['steps_done'] / $job->extended_status['total_steps']) * 100, 0);
|
||||
}
|
||||
if ($job->status === 'complete') {
|
||||
$tagId = $job->extended_status['importTag'];
|
||||
/** @var TagRepositoryInterface $repository */
|
||||
$repository = app(TagRepositoryInterface::class);
|
||||
$tag = $repository->find($tagId);
|
||||
if ($job->status === 'finished') {
|
||||
// $tagId = $job->extended_status['importTag'];
|
||||
// /** @var TagRepositoryInterface $repository */
|
||||
// $repository = app(TagRepositoryInterface::class);
|
||||
// $tag = $repository->find($tagId);
|
||||
$tag = new Tag;
|
||||
$result['finished'] = true;
|
||||
$result['finishedText'] = trans('firefly.import_finished_link', ['link' => route('tags.show', [$tag->id]), 'tag' => $tag->tag]);
|
||||
}
|
||||
@@ -201,6 +203,7 @@ class ImportController extends Controller
|
||||
$result['started'] = true;
|
||||
$result['running'] = true;
|
||||
$result['percentage'] = $percentage;
|
||||
$result['showPercentage'] = true;
|
||||
}
|
||||
|
||||
return Response::json($result);
|
||||
@@ -241,7 +244,7 @@ class ImportController extends Controller
|
||||
$routine = new ImportRoutine($job);
|
||||
$routine->run();
|
||||
|
||||
return 'ok dan';
|
||||
return 'done!';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -64,8 +64,6 @@ class CsvProcessor implements FileProcessorInterface
|
||||
*/
|
||||
public function run(): bool
|
||||
{
|
||||
$this->job->status = 'running';
|
||||
$this->job->save();
|
||||
Log::debug('Now in CsvProcessor run(). Job is now running...');
|
||||
|
||||
$entries = $this->getImportArray();
|
||||
@@ -84,9 +82,9 @@ class CsvProcessor implements FileProcessorInterface
|
||||
* 3. Store journal.
|
||||
* 4. Run rules.
|
||||
*/
|
||||
$this->job->addTotalSteps(4);
|
||||
$this->job->addStepsDone(1);
|
||||
$count++;
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@@ -21,10 +21,10 @@ use Log;
|
||||
class ImportRoutine
|
||||
{
|
||||
|
||||
/** @var Collection */
|
||||
public $journals;
|
||||
/** @var Collection */
|
||||
public $errors;
|
||||
/** @var Collection */
|
||||
public $journals;
|
||||
/** @var int */
|
||||
public $lines = 0;
|
||||
/** @var ImportJob */
|
||||
@@ -39,7 +39,7 @@ class ImportRoutine
|
||||
{
|
||||
$this->job = $job;
|
||||
$this->journals = new Collection;
|
||||
$this->errors = new Collection;
|
||||
$this->errors = new Collection;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,8 +60,14 @@ class ImportRoutine
|
||||
/** @var FileProcessorInterface $processor */
|
||||
$processor = app($class);
|
||||
$processor->setJob($this->job);
|
||||
|
||||
set_time_limit(0);
|
||||
if ($this->job->status == 'configured') {
|
||||
|
||||
// set job as "running"...
|
||||
$this->job->status = 'running';
|
||||
$this->job->save();
|
||||
|
||||
Log::debug('Job is configured, start with run()');
|
||||
$processor->run();
|
||||
$objects = $processor->getObjects();
|
||||
@@ -81,7 +87,10 @@ class ImportRoutine
|
||||
$this->job->save();
|
||||
|
||||
$this->journals = $storage->journals;
|
||||
$this->errors = $storage->errors;
|
||||
$this->errors = $storage->errors;
|
||||
|
||||
// run rules:
|
||||
|
||||
|
||||
Log::debug(sprintf('Done with import job %s', $this->job->key));
|
||||
|
||||
|
@@ -17,10 +17,12 @@ use FireflyIII\Import\Object\ImportJournal;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Rules\Processor;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
@@ -45,6 +47,8 @@ class ImportStorage
|
||||
private $job;
|
||||
/** @var Collection */
|
||||
private $objects;
|
||||
/** @var Collection */
|
||||
private $rules;
|
||||
|
||||
/**
|
||||
* ImportStorage constructor.
|
||||
@@ -54,6 +58,8 @@ class ImportStorage
|
||||
$this->objects = new Collection;
|
||||
$this->journals = new Collection;
|
||||
$this->errors = new Collection;
|
||||
$this->rules = $this->getUserRules();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +86,6 @@ class ImportStorage
|
||||
$this->objects = $objects;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Do storage of import objects
|
||||
*/
|
||||
@@ -88,6 +93,8 @@ class ImportStorage
|
||||
{
|
||||
$this->defaultCurrency = Amount::getDefaultCurrencyByUser($this->job->user);
|
||||
|
||||
|
||||
// routine below consists of 3 steps.
|
||||
/**
|
||||
* @var int $index
|
||||
* @var ImportJournal $object
|
||||
@@ -130,6 +137,8 @@ class ImportStorage
|
||||
$transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
}
|
||||
|
||||
$this->job->addStepsDone(1);
|
||||
|
||||
$journal = new TransactionJournal;
|
||||
$journal->user_id = $this->job->user_id;
|
||||
$journal->transaction_type_id = $transactionType->id;
|
||||
@@ -174,6 +183,9 @@ class ImportStorage
|
||||
}
|
||||
Log::debug(sprintf('Created transaction with ID #%d and account #%d', $two->id, $opposing->id));
|
||||
|
||||
$this->job->addStepsDone(1);
|
||||
sleep(1);
|
||||
|
||||
// category
|
||||
$category = $object->category->getCategory();
|
||||
if (!is_null($category->id)) {
|
||||
@@ -210,11 +222,67 @@ class ImportStorage
|
||||
if (strlen($object->notes) > 0) {
|
||||
$journal->setMeta('notes', $object->notes);
|
||||
}
|
||||
$this->job->addStepsDone(1);
|
||||
|
||||
// run rules:
|
||||
$this->applyRules($journal);
|
||||
$this->job->addStepsDone(1);
|
||||
|
||||
$this->journals->push($journal);
|
||||
$this->errors->push($errors);
|
||||
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function applyRules(TransactionJournal $journal): bool
|
||||
{
|
||||
if ($this->rules->count() > 0) {
|
||||
|
||||
/** @var Rule $rule */
|
||||
foreach ($this->rules as $rule) {
|
||||
Log::debug(sprintf('Going to apply rule #%d to journal %d.', $rule->id, $journal->id));
|
||||
$processor = Processor::make($rule);
|
||||
$processor->handleTransactionJournal($journal);
|
||||
|
||||
if ($rule->stop_processing) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
private function getUserRules(): Collection
|
||||
{
|
||||
$set = Rule::distinct()
|
||||
->where('rules.user_id', $this->job->user->id)
|
||||
->leftJoin('rule_groups', 'rule_groups.id', '=', 'rules.rule_group_id')
|
||||
->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
|
||||
->where('rule_groups.active', 1)
|
||||
->where('rule_triggers.trigger_type', 'user_action')
|
||||
->where('rule_triggers.trigger_value', 'store-journal')
|
||||
->where('rules.active', 1)
|
||||
->orderBy('rule_groups.order', 'ASC')
|
||||
->orderBy('rules.order', 'ASC')
|
||||
->get(['rules.*', 'rule_groups.order']);
|
||||
Log::debug(sprintf('Found %d user rules.', $set->count()));
|
||||
|
||||
return $set;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -133,6 +133,9 @@ class Tag extends Model
|
||||
*/
|
||||
public function getTagAttribute($value)
|
||||
{
|
||||
if(is_null($value)) {
|
||||
return null;
|
||||
}
|
||||
return Crypt::decrypt($value);
|
||||
}
|
||||
|
||||
|
@@ -92,6 +92,12 @@ class Map implements ConfigurationInterface
|
||||
foreach ($this->data as $index => $entry) {
|
||||
$this->data[$index]['values'] = array_unique($this->data[$index]['values']);
|
||||
}
|
||||
// save number of rows, thus number of steps, in job:
|
||||
$steps = $rowIndex * 5;
|
||||
$extended = $this->job->extended_status;
|
||||
$extended['total_steps'] = $steps;
|
||||
$this->job->extended_status = $extended;
|
||||
$this->job->save();
|
||||
|
||||
return $this->data;
|
||||
|
||||
|
Reference in New Issue
Block a user