Remove references to ImportResult. Add the application of user rules.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole
2016-08-26 07:37:47 +02:00
parent 43a7544dd7
commit 6490a4240d

View File

@@ -13,12 +13,14 @@ namespace FireflyIII\Import;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Rule;
use FireflyIII\Models\Tag; use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Rules\Processor;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -84,9 +86,16 @@ class ImportStorage
Log::notice(sprintf('Started storing %d entry(ies).', $this->entries->count())); Log::notice(sprintf('Started storing %d entry(ies).', $this->entries->count()));
foreach ($this->entries as $index => $entry) { foreach ($this->entries as $index => $entry) {
Log::debug(sprintf('--- import store start for row %d ---', $index)); Log::debug(sprintf('--- import store start for row %d ---', $index));
$result = $this->storeSingle($index, $entry);
// store entry:
$journal = $this->storeSingle($index, $entry);
$this->job->addStepsDone(1); $this->job->addStepsDone(1);
$collection->put($index, $result);
// apply rules:
$this->applyRules($journal);
$this->job->addStepsDone(1);
$collection->put($index, $journal);
} }
Log::notice(sprintf('Finished storing %d entry(ies).', $collection->count())); Log::notice(sprintf('Finished storing %d entry(ies).', $collection->count()));
@@ -109,13 +118,37 @@ class ImportStorage
return new TransactionJournal; return new TransactionJournal;
} }
/**
* @param TransactionJournal $journal
*
* @return bool
*/
private 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 Tag * @return Tag
*/ */
private function createImportTag(): Tag private function createImportTag(): Tag
{ {
/** @var TagRepositoryInterface $repository */ /** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class); $repository = app(TagRepositoryInterface::class, [$this->user]);
$data = [ $data = [
'tag' => trans('firefly.import_with_key', ['key' => $this->job->key]), 'tag' => trans('firefly.import_with_key', ['key' => $this->job->key]),
'date' => null, 'date' => null,
@@ -135,6 +168,21 @@ class ImportStorage
*/ */
private function getUserRules(): Collection private function getUserRules(): Collection
{ {
$set = Rule::distinct()
->where('rules.user_id', $this->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.*']);
Log::debug(sprintf('Found %d user rules.', $set->count()));
return $set;
} }
/** /**
@@ -275,41 +323,35 @@ class ImportStorage
* @param int $index * @param int $index
* @param ImportEntry $entry * @param ImportEntry $entry
* *
* @return ImportResult * @return TransactionJournal
* @throws FireflyException * @throws FireflyException
*/ */
private function storeSingle(int $index, ImportEntry $entry): ImportResult private function storeSingle(int $index, ImportEntry $entry): TransactionJournal
{ {
if ($entry->valid === false) { if ($entry->valid === false) {
Log::warning(sprintf('Cannot import row %d, because the entry is not valid.', $index)); Log::warning(sprintf('Cannot import row %d, because the entry is not valid.', $index));
$result = new ImportResult();
$result->failed();
$errors = join(', ', $entry->errors->all()); $errors = join(', ', $entry->errors->all());
$errorText = sprintf('Row #%d: ' . $errors, $index); $errorText = sprintf('Row #%d: ' . $errors, $index);
$result->appendError($errorText);
$extendedStatus = $this->job->extended_status; $extendedStatus = $this->job->extended_status;
$extendedStatus['errors'][] = $errorText; $extendedStatus['errors'][] = $errorText;
$this->job->extended_status = $extendedStatus; $this->job->extended_status = $extendedStatus;
$this->job->save(); $this->job->save();
return $result; return new TransactionJournal;
} }
$alreadyImported = $this->alreadyImported($entry->hash); $alreadyImported = $this->alreadyImported($entry->hash);
if (!is_null($alreadyImported->id)) { if (!is_null($alreadyImported->id)) {
Log::warning(sprintf('Cannot import row %d, because it has already been imported (journal #%d).', $index, $alreadyImported->id)); Log::warning(sprintf('Cannot import row %d, because it has already been imported (journal #%d).', $index, $alreadyImported->id));
$result = new ImportResult();
$result->failed();
$errorText = trans( $errorText = trans(
'firefly.import_double', 'firefly.import_double',
['row' => $index, 'link' => route('transactions.show', [$alreadyImported->id]), 'description' => $alreadyImported->description] ['row' => $index, 'link' => route('transactions.show', [$alreadyImported->id]), 'description' => $alreadyImported->description]
); );
$result->appendError($errorText);
$extendedStatus = $this->job->extended_status; $extendedStatus = $this->job->extended_status;
$extendedStatus['errors'][] = $errorText; $extendedStatus['errors'][] = $errorText;
$this->job->extended_status = $extendedStatus; $this->job->extended_status = $extendedStatus;
$this->job->save(); $this->job->save();
return $result; return new TransactionJournal;
} }
Log::debug(sprintf('Going to store row %d', $index)); Log::debug(sprintf('Going to store row %d', $index));
@@ -350,13 +392,6 @@ class ImportStorage
$this->storeCategory($journal, $entry); $this->storeCategory($journal, $entry);
$this->storeBill($journal, $entry); $this->storeBill($journal, $entry);
$result = new ImportResult(); return $journal;
$result->success();
$result->appendMessage(sprintf('Journal titled "%s" imported.', $journal->description));
$result->setJournal($journal);
return $result;
} }
} }