More code for import routine.

This commit is contained in:
James Cole
2017-06-22 21:50:10 +02:00
parent cddaccb7f7
commit edb355941c
9 changed files with 275 additions and 113 deletions

View File

@@ -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;

View File

@@ -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));

View File

@@ -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;
}
}