Skip to content

Firefly III: a personal finances manager

Back to basics.

James Colea879528ed8e9
diff --git a/app/Import/FileProcessor/CsvProcessor.php b/app/Import/FileProcessor/CsvProcessor.php
index 209c891dd7..e81ec8a87a 100644
--- a/app/Import/FileProcessor/CsvProcessor.php
+++ b/app/Import/FileProcessor/CsvProcessor.php
@@ -99,6 +99,7 @@ class CsvProcessor implements FileProcessorInterface
$this->job->addStepsDone(1);
}
);
+
return true;
}
@@ -215,6 +216,7 @@ class CsvProcessor implements FileProcessorInterface
throw new FireflyException(sprintf('Error while encoding JSON for CSV row: %s', $this->getJsonError($jsonError)));
}
$hash = hash('sha256', $json);
+
return $hash;
}
@@ -238,7 +240,7 @@ class CsvProcessor implements FileProcessorInterface
$journal->setHash($hash);
/**
- * @var int $rowIndex
+ * @var int $rowIndex
* @var string $value
*/
foreach ($row as $rowIndex => $value) {
@@ -249,7 +251,8 @@ class CsvProcessor implements FileProcessorInterface
$journal->setValue($annotated);
}
}
- Log::debug('ImportJournal complete, returning.');
+ // set some extra info:
+ $journal->asset->setDefaultAccountId($this->job->configuration['import-account']);
return $journal;
}
@@ -263,12 +266,12 @@ class CsvProcessor implements FileProcessorInterface
*/
private function rowAlreadyImported(array $array): bool
{
- $hash = $this->getRowHash($array);
- $json = json_encode($hash);
- $entry = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
- ->where('data', $json)
- ->where('name', 'importHash')
- ->first();
+ $hash = $this->getRowHash($array);
+ $json = json_encode($hash);
+ $entry = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
+ ->where('data', $json)
+ ->where('name', 'importHash')
+ ->first();
if (!is_null($entry)) {
return true;
}
diff --git a/app/Import/Object/ImportAccount.php b/app/Import/Object/ImportAccount.php
index a4c3d966e1..0c36d403dc 100644
--- a/app/Import/Object/ImportAccount.php
+++ b/app/Import/Object/ImportAccount.php
@@ -335,7 +335,7 @@ class ImportAccount
}
$this->expectedType = $oldExpectedType;
- // if search for an asset account, fall back to given "default account" (mandatory)
+ // 4: if search for an asset account, fall back to given "default account" (mandatory)
if ($this->expectedType === AccountType::ASSET) {
$this->account = $this->repository->find($this->defaultAccountId);
Log::debug(sprintf('Fall back to default account #%d "%s"', $this->account->id, $this->account->name));
@@ -343,6 +343,7 @@ class ImportAccount
return true;
}
+ // 5: then maybe, create one:
Log::debug(sprintf('Found no account of type %s so must create one ourselves.', $this->expectedType));
$data = [
diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php
index 5600967a07..133fdd4137 100644
--- a/app/Import/Storage/ImportStorage.php
+++ b/app/Import/Storage/ImportStorage.php
@@ -11,7 +11,6 @@ declare(strict_types=1);
namespace FireflyIII\Import\Storage;
-use Amount;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Object\ImportAccount;
@@ -86,16 +85,15 @@ class ImportStorage
public function setJob(ImportJob $job)
{
$this->job = $job;
-
- $repository = app(CurrencyRepositoryInterface::class);
- $repository->setUser($job->user);
- $this->currencyRepository = $repository;
-
- $repository = app(TagRepositoryInterface::class);
- $repository->setUser($job->user);
- $this->tagRepository = $repository;
-
- $this->rules = $this->getUserRules();
+ // $repository = app(CurrencyRepositoryInterface::class);
+ // $repository->setUser($job->user);
+ // $this->currencyRepository = $repository;
+
+ // $repository = app(TagRepositoryInterface::class);
+ // $repository->setUser($job->user);
+ // $this->tagRepository = $repository;
+ // $this->rules = $this->getUserRules();
+ // $this->defaultCurrency = Amount::getDefaultCurrencyByUser($this->job->user);
}
/**
@@ -107,180 +105,178 @@ class ImportStorage
}
/**
- * Do storage of import objects.
- */
- public function store()
- {
- $this->defaultCurrency = Amount::getDefaultCurrencyByUser($this->job->user);
-
- /**
- * @var int $index
- * @var ImportJournal $object
- */
- foreach ($this->objects as $index => $object) {
- try {
- $this->storeImportJournal($index, $object);
- } catch (FireflyException $e) {
- $this->errors->push($e->getMessage());
- Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage()));
- }
- }
- Log::info('ImportStorage has finished.');
-
- return true;
- }
-
- /**
- * @param TransactionJournal $journal
+ * Do storage of import objects. Is the main function.
*
* @return bool
*/
- protected function applyRules(TransactionJournal $journal): bool
+ public function store(): 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;
+ $this->objects->each(
+ function (ImportJournal $importJournal, int $index) {
+ try {
+ $this->storeImportJournal($index, $importJournal);
+ } catch (FireflyException $e) {
+ $this->errors->push($e->getMessage());
+ Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage()));
}
}
- }
-
- return true;
- }
-
- /**
- * @param array $parameters
- *
- * @return bool
- * @throws FireflyException
- */
- private function createTransaction(array $parameters): bool
- {
- $transaction = new Transaction;
- $transaction->account_id = $parameters['account'];
- $transaction->transaction_journal_id = $parameters['id'];
- $transaction->transaction_currency_id = $parameters['currency'];
- $transaction->amount = $parameters['amount'];
- $transaction->foreign_currency_id = $parameters['foreign_currency'];
- $transaction->foreign_amount = $parameters['foreign_amount'];
- $transaction->save();
- if (is_null($transaction->id)) {
- $errorText = join(', ', $transaction->getErrors()->all());
- throw new FireflyException($errorText);
- }
- Log::debug(sprintf('Created transaction with ID #%d, account #%d, amount %s', $transaction->id, $parameters['account'], $parameters['amount']));
-
- return true;
- }
-
- /**
- * @param Collection $set
- * @param ImportJournal $importJournal
- *
- * @return bool
- */
- private function filterTransferSet(Collection $set, ImportJournal $importJournal): bool
- {
- $amount = Steam::positive($importJournal->getAmount());
- $asset = $importJournal->asset->getAccount();
- $opposing = $this->getOpposingAccount($importJournal->opposing,$asset->id, $amount);
- $description = $importJournal->getDescription();
-
- $filtered = $set->filter(
- function (TransactionJournal $journal) use ($asset, $opposing, $description) {
- $match = true;
- $original = [app('steam')->tryDecrypt($journal->source_name), app('steam')->tryDecrypt($journal->destination_name)];
- $compare = [$asset->name, $opposing->name];
- sort($original);
- sort($compare);
-
- // description does not match? Then cannot be duplicate.
- if ($journal->description !== $description) {
- $match = false;
- }
- // not both accounts in journal? Then cannot be duplicate.
- if ($original !== $compare) {
- $match = false;
- }
-
- if ($match) {
- return $journal;
- }
-
- return null;
- }
);
- if (count($filtered) > 0) {
- return true;
- }
-
- return false;
- }
-
- /**
- * @param ImportJournal $importJournal
- *
- * @param Account $account
- *
- * @return TransactionCurrency
- */
- private function getCurrency(ImportJournal $importJournal, Account $account): TransactionCurrency
- {
- // start with currency pref of account, if any:
- $currency = $this->currencyRepository->find(intval($account->getMeta('currency_id')));
- if (!is_null($currency->id)) {
- return $currency;
- }
-
- // use given currency
- $currency = $importJournal->getCurrency()->getTransactionCurrency();
- if (!is_null($currency->id)) {
- return $currency;
- }
-
- // backup to default
- $currency = $this->defaultCurrency;
-
- return $currency;
-
- }
-
- /**
- * @param ImportJournal $importJournal
- * @param TransactionCurrency $localCurrency
- *
- * @return int|null
- */
- private function getForeignCurrencyId(ImportJournal $importJournal, TransactionCurrency $localCurrency): ?int
- {
- // get journal currency, if any:
- $currency = $importJournal->getCurrency()->getTransactionCurrency();
- if (is_null($currency->id)) {
- Log::debug('getForeignCurrencyId: Journal has no currency, so can\'t be foreign either way.');
-
- // journal has no currency, so can't be foreign either way:
- return null;
- }
-
- if ($currency->id !== $localCurrency->id) {
- Log::debug(
- sprintf('getForeignCurrencyId: journal is %s, but account is %s. Return id of journal currency.', $currency->code, $localCurrency->code)
- );
-
- // journal has different currency than account does, return its ID:
- return $currency->id;
- }
-
- Log::debug('getForeignCurrencyId: journal has no foreign currency.');
+ Log::info('ImportStorage has finished.');
- // return null in other cases.
- return null;
+ return true;
}
+//
+// /**
+// * @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;
+// }
+
+// /**
+// * @param array $parameters
+// *
+// * @return bool
+// * @throws FireflyException
+// */
+// private function createTransaction(array $parameters): bool
+// {
+// $transaction = new Transaction;
+// $transaction->account_id = $parameters['account'];
+// $transaction->transaction_journal_id = $parameters['id'];
+// $transaction->transaction_currency_id = $parameters['currency'];
+// $transaction->amount = $parameters['amount'];
+// $transaction->foreign_currency_id = $parameters['foreign_currency'];
+// $transaction->foreign_amount = $parameters['foreign_amount'];
+// $transaction->save();
+// if (is_null($transaction->id)) {
+// $errorText = join(', ', $transaction->getErrors()->all());
+// throw new FireflyException($errorText);
+// }
+// Log::debug(sprintf('Created transaction with ID #%d, account #%d, amount %s', $transaction->id, $parameters['account'], $parameters['amount']));
+//
+// return true;
+// }
+
+// /**
+// * @param Collection $set
+// * @param ImportJournal $importJournal
+// *
+// * @return bool
+// */
+// private function filterTransferSet(Collection $set, ImportJournal $importJournal): bool
+// {
+// $amount = Steam::positive($importJournal->getAmount());
+// $asset = $importJournal->asset->getAccount();
+// $opposing = $this->getOpposingAccount($importJournal->opposing, $asset->id, $amount);
+// $description = $importJournal->getDescription();
+//
+// $filtered = $set->filter(
+// function (TransactionJournal $journal) use ($asset, $opposing, $description) {
+// $match = true;
+// $original = [app('steam')->tryDecrypt($journal->source_name), app('steam')->tryDecrypt($journal->destination_name)];
+// $compare = [$asset->name, $opposing->name];
+// sort($original);
+// sort($compare);
+//
+// // description does not match? Then cannot be duplicate.
+// if ($journal->description !== $description) {
+// $match = false;
+// }
+// // not both accounts in journal? Then cannot be duplicate.
+// if ($original !== $compare) {
+// $match = false;
+// }
+//
+// if ($match) {
+// return $journal;
+// }
+//
+// return null;
+// }
+// );
+// if (count($filtered) > 0) {
+// return true;
+// }
+//
+// return false;
+// }
+
+// /**
+// * @param ImportJournal $importJournal
+// *
+// * @param Account $account
+// *
+// * @return TransactionCurrency
+// */
+// private function getCurrency(ImportJournal $importJournal, Account $account): TransactionCurrency
+// {
+// // start with currency pref of account, if any:
+// $currency = $this->currencyRepository->find(intval($account->getMeta('currency_id')));
+// if (!is_null($currency->id)) {
+// return $currency;
+// }
+//
+// // use given currency
+// $currency = $importJournal->getCurrency()->getTransactionCurrency();
+// if (!is_null($currency->id)) {
+// return $currency;
+// }
+//
+// // backup to default
+// $currency = $this->defaultCurrency;
+//
+// return $currency;
+//
+// }
+
+// /**
+// * @param ImportJournal $importJournal
+// * @param TransactionCurrency $localCurrency
+// *
+// * @return int|null
+// */
+// private function getForeignCurrencyId(ImportJournal $importJournal, TransactionCurrency $localCurrency): ?int
+// {
+// // get journal currency, if any:
+// $currency = $importJournal->getCurrency()->getTransactionCurrency();
+// if (is_null($currency->id)) {
+// Log::debug('getForeignCurrencyId: Journal has no currency, so can\'t be foreign either way.');
+//
+// // journal has no currency, so can't be foreign either way:
+// return null;
+// }
+//
+// if ($currency->id !== $localCurrency->id) {
+// Log::debug(
+// sprintf('getForeignCurrencyId: journal is %s, but account is %s. Return id of journal currency.', $currency->code, $localCurrency->code)
+// );
+//
+// // journal has different currency than account does, return its ID:
+// return $currency->id;
+// }
+//
+// Log::debug('getForeignCurrencyId: journal has no foreign currency.');
+//
+// // return null in other cases.
+// return null;
+// }
/**
* @param ImportAccount $account
@@ -306,288 +302,294 @@ class ImportStorage
return $databaseAccount;
}
+//
+// /**
+// * @param string $amount
+// *
+// * @return TransactionType
+// */
+// private function getTransactionType(string $amount): TransactionType
+// {
+// $transactionType = new TransactionType();
+// // amount is negative, it's a withdrawal, opposing is an expense:
+// if (bccomp($amount, '0') === -1) {
+// $transactionType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
+// }
+// if (bccomp($amount, '0') === 1) {
+// $transactionType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
+// }
+//
+// return $transactionType;
+// }
+
+// /**
+// * @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;
+//
+// }
+//
+// /**
+// * @param TransactionJournal $journal
+// * @param Bill $bill
+// */
+// private function storeBill(TransactionJournal $journal, Bill $bill)
+// {
+// if (!is_null($bill->id)) {
+// Log::debug(sprintf('Linked bill #%d to journal #%d', $bill->id, $journal->id));
+// $journal->bill()->associate($bill);
+// $journal->save();
+// }
+// }
+
+// /**
+// * @param TransactionJournal $journal
+// * @param Budget $budget
+// */
+// private function storeBudget(TransactionJournal $journal, Budget $budget)
+// {
+// if (!is_null($budget->id)) {
+// Log::debug(sprintf('Linked budget #%d to journal #%d', $budget->id, $journal->id));
+// $journal->budgets()->save($budget);
+// }
+// }
+//
+// /**
+// * @param TransactionJournal $journal
+// * @param Category $category
+// */
+// private function storeCategory(TransactionJournal $journal, Category $category)
+// {
+//
+// if (!is_null($category->id)) {
+// Log::debug(sprintf('Linked category #%d to journal #%d', $category->id, $journal->id));
+// $journal->categories()->save($category);
+// }
+//
+// }
/**
- * @param string $amount
+ * @param int $index
+ * @param ImportJournal $importJournal
*
- * @return TransactionType
- */
- private function getTransactionType(string $amount): TransactionType
- {
- $transactionType = new TransactionType();
- // amount is negative, it's a withdrawal, opposing is an expense:
- if (bccomp($amount, '0') === -1) {
- $transactionType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
- }
- if (bccomp($amount, '0') === 1) {
- $transactionType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
- }
-
- return $transactionType;
- }
-
- /**
- * @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;
-
- }
-
- /**
- * @param TransactionJournal $journal
- * @param Bill $bill
- */
- private function storeBill(TransactionJournal $journal, Bill $bill)
- {
- if (!is_null($bill->id)) {
- Log::debug(sprintf('Linked bill #%d to journal #%d', $bill->id, $journal->id));
- $journal->bill()->associate($bill);
- $journal->save();
- }
- }
-
- /**
- * @param TransactionJournal $journal
- * @param Budget $budget
- */
- private function storeBudget(TransactionJournal $journal, Budget $budget)
- {
- if (!is_null($budget->id)) {
- Log::debug(sprintf('Linked budget #%d to journal #%d', $budget->id, $journal->id));
- $journal->budgets()->save($budget);
- }
- }
-
- /**
- * @param TransactionJournal $journal
- * @param Category $category
+ * @return bool
+ * @throws FireflyException
*/
- private function storeCategory(TransactionJournal $journal, Category $category)
- {
-
- if (!is_null($category->id)) {
- Log::debug(sprintf('Linked category #%d to journal #%d', $category->id, $journal->id));
- $journal->categories()->save($category);
- }
-
- }
-
private function storeImportJournal(int $index, ImportJournal $importJournal): bool
{
Log::debug(sprintf('Going to store object #%d with description "%s"', $index, $importJournal->getDescription()));
- $importJournal->asset->setDefaultAccountId($this->job->configuration['import-account']);
$asset = $importJournal->asset->getAccount();
$amount = $importJournal->getAmount();
- $currency = $this->getCurrency($importJournal, $asset);
- $foreignCurrencyId = $this->getForeignCurrencyId($importJournal, $currency);
- $date = $importJournal->getDate($this->dateFormat);
- $transactionType = $this->getTransactionType($amount);
- $opposing = $this->getOpposingAccount($importJournal->opposing, $asset->id, $amount);
-
- // if opposing is an asset account, it's a transfer:
- if ($opposing->accountType->type === AccountType::ASSET) {
- Log::debug(sprintf('Opposing account #%d %s is an asset account, make transfer.', $opposing->id, $opposing->name));
- $transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first();
- }
-
- // verify that opposing account is of the correct type:
- if ($opposing->accountType->type === AccountType::EXPENSE && $transactionType->type !== TransactionType::WITHDRAWAL) {
- $message = sprintf('Row #%d is imported as a %s but opposing is an expense account. This cannot be!', $index, $transactionType->type);
- Log::error($message);
- throw new FireflyException($message);
-
- }
-
- /*** First step done! */
- $this->job->addStepsDone(1);
-
- // could be that transfer is double: verify this.
- if ($this->verifyDoubleTransfer($transactionType, $importJournal)) {
- // add three steps:
- $this->job->addStepsDone(3);
- // throw error
- throw new FireflyException('Detected a possible duplicate, skip this one.');
-
- }
-
- // create a journal:
- $journal = new TransactionJournal;
- $journal->user_id = $this->job->user_id;
- $journal->transaction_type_id = $transactionType->id;
- $journal->transaction_currency_id = $currency->id;// always currency of account
- $journal->description = $importJournal->getDescription();
- $journal->date = $date->format('Y-m-d');
- $journal->order = 0;
- $journal->tag_count = 0;
- $journal->completed = false;
-
- if (!$journal->save()) {
- $errorText = join(', ', $journal->getErrors()->all());
- // add three steps:
- $this->job->addStepsDone(3);
- // throw error
- throw new FireflyException($errorText);
- }
-
- // save meta data:
- $journal->setMeta('importHash', $importJournal->hash);
- Log::debug(sprintf('Created journal with ID #%d', $journal->id));
-
- // create transactions:
- $one = [
- 'id' => $journal->id,
- 'account' => $asset->id,
- 'currency' => $currency->id,
- 'amount' => $amount,
- 'foreign_currency' => $foreignCurrencyId,
- 'foreign_amount' => is_null($foreignCurrencyId) ? null : $amount,
- ];
- $two = [
- 'id' => $journal->id,
- 'account' => $opposing->id,
- 'currency' => $currency->id,
- 'amount' => Steam::opposite($amount),
- 'foreign_currency' => $foreignCurrencyId,
- 'foreign_amount' => is_null($foreignCurrencyId) ? null : Steam::opposite($amount),
- ];
- $this->createTransaction($one);
- $this->createTransaction($two);
-
- /*** Another step done! */
- $this->job->addStepsDone(1);
-
- // store meta object things:
- $this->storeCategory($journal, $importJournal->category->getCategory());
- $this->storeBudget($journal, $importJournal->budget->getBudget());
- $this->storeBill($journal, $importJournal->bill->getBill());
- $this->storeMeta($journal, $importJournal->metaDates);
-
- // sepa thing as note:
- if (strlen($importJournal->notes) > 0) {
- $journal->setMeta('notes', $importJournal->notes);
- }
-
- // store tags
- $this->storeTags($importJournal->tags, $journal);
-
- // set journal completed:
- $journal->completed = true;
- $journal->save();
-
- $this->job->addStepsDone(1);
-
- // run rules:
- $this->applyRules($journal);
- $this->job->addStepsDone(1);
- $this->journals->push($journal);
-
- Log::info(
- sprintf(
- 'Imported new journal #%d with description "%s" and amount %s %s.', $journal->id, $journal->description, $journal->transactionCurrency->code,
- $amount
- )
- );
+// $currency = $this->getCurrency($importJournal, $asset);
+// $foreignCurrencyId = $this->getForeignCurrencyId($importJournal, $currency);
+// $date = $importJournal->getDate($this->dateFormat);
+// $transactionType = $this->getTransactionType($amount);
+// $opposing = $this->getOpposingAccount($importJournal->opposing, $asset->id, $amount);
+//
+// // if opposing is an asset account, it's a transfer:
+// if ($opposing->accountType->type === AccountType::ASSET) {
+// Log::debug(sprintf('Opposing account #%d %s is an asset account, make transfer.', $opposing->id, $opposing->name));
+// $transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first();
+// }
+//
+// // verify that opposing account is of the correct type:
+// if ($opposing->accountType->type === AccountType::EXPENSE && $transactionType->type !== TransactionType::WITHDRAWAL) {
+// $message = sprintf('Row #%d is imported as a %s but opposing is an expense account. This cannot be!', $index, $transactionType->type);
+// Log::error($message);
+// throw new FireflyException($message);
+//
+// }
+//
+// /*** First step done! */
+// $this->job->addStepsDone(1);
+//
+// // could be that transfer is double: verify this.
+// if ($this->verifyDoubleTransfer($transactionType, $importJournal)) {
+// // add three steps:
+// $this->job->addStepsDone(3);
+// // throw error
+// throw new FireflyException('Detected a possible duplicate, skip this one.');
+//
+// }
+//
+// // create a journal:
+// $journal = new TransactionJournal;
+// $journal->user_id = $this->job->user_id;
+// $journal->transaction_type_id = $transactionType->id;
+// $journal->transaction_currency_id = $currency->id;// always currency of account
+// $journal->description = $importJournal->getDescription();
+// $journal->date = $date->format('Y-m-d');
+// $journal->order = 0;
+// $journal->tag_count = 0;
+// $journal->completed = false;
+//
+// if (!$journal->save()) {
+// $errorText = join(', ', $journal->getErrors()->all());
+// // add three steps:
+// $this->job->addStepsDone(3);
+// // throw error
+// throw new FireflyException($errorText);
+// }
+//
+// // save meta data:
+// $journal->setMeta('importHash', $importJournal->hash);
+// Log::debug(sprintf('Created journal with ID #%d', $journal->id));
+//
+// // create transactions:
+// $one = [
+// 'id' => $journal->id,
+// 'account' => $asset->id,
+// 'currency' => $currency->id,
+// 'amount' => $amount,
+// 'foreign_currency' => $foreignCurrencyId,
+// 'foreign_amount' => is_null($foreignCurrencyId) ? null : $amount,
+// ];
+// $two = [
+// 'id' => $journal->id,
+// 'account' => $opposing->id,
+// 'currency' => $currency->id,
+// 'amount' => Steam::opposite($amount),
+// 'foreign_currency' => $foreignCurrencyId,
+// 'foreign_amount' => is_null($foreignCurrencyId) ? null : Steam::opposite($amount),
+// ];
+// $this->createTransaction($one);
+// $this->createTransaction($two);
+//
+// /*** Another step done! */
+// $this->job->addStepsDone(1);
+//
+// // store meta object things:
+// $this->storeCategory($journal, $importJournal->category->getCategory());
+// $this->storeBudget($journal, $importJournal->budget->getBudget());
+// $this->storeBill($journal, $importJournal->bill->getBill());
+// $this->storeMeta($journal, $importJournal->metaDates);
+//
+// // sepa thing as note:
+// if (strlen($importJournal->notes) > 0) {
+// $journal->setMeta('notes', $importJournal->notes);
+// }
+//
+// // store tags
+// $this->storeTags($importJournal->tags, $journal);
+//
+// // set journal completed:
+// $journal->completed = true;
+// $journal->save();
+//
+// $this->job->addStepsDone(1);
+//
+// // run rules:
+// $this->applyRules($journal);
+// $this->job->addStepsDone(1);
+// $this->journals->push($journal);
+//
+// Log::info(
+// sprintf(
+// 'Imported new journal #%d with description "%s" and amount %s %s.', $journal->id, $journal->description, $journal->transactionCurrency->code,
+// $amount
+// )
+// );
return true;
}
- /**
- * @param TransactionJournal $journal
- * @param array $dates
- */
- private function storeMeta(TransactionJournal $journal, array $dates)
- {
- // all other date fields as meta thing:
- foreach ($dates as $name => $value) {
- try {
- $date = new Carbon($value);
- $journal->setMeta($name, $date);
- } catch (\Exception $e) {
- // don't care, ignore:
- Log::warning(sprintf('Could not parse "%s" into a valid Date object for field %s', $value, $name));
- }
- }
- }
-
- /**
- * @param array $tags
- * @param TransactionJournal $journal
- */
- private function storeTags(array $tags, TransactionJournal $journal): void
- {
- foreach ($tags as $tag) {
- $dbTag = $this->tagRepository->findByTag($tag);
- if (is_null($dbTag->id)) {
- $dbTag = $this->tagRepository->store(
- ['tag' => $tag, 'date' => null, 'description' => null, 'latitude' => null, 'longitude' => null,
- 'zoomLevel' => null, 'tagMode' => 'nothing']
- );
- }
- $journal->tags()->save($dbTag);
- Log::debug(sprintf('Linked tag %d ("%s") to journal #%d', $dbTag->id, $dbTag->tag, $journal->id));
- }
-
- return;
- }
-
- /**
- * This method checks if the given transaction is a transfer and if so, if it might be a duplicate of an already imported transfer.
- * This is important for import files that cover multiple accounts (and include both A<>B and B<>A transactions).
- *
- * @param TransactionType $transactionType
- * @param ImportJournal $importJournal
- *
- * @return bool
- */
- private function verifyDoubleTransfer(TransactionType $transactionType, ImportJournal $importJournal): bool
- {
- if ($transactionType->type === TransactionType::TRANSFER) {
- $amount = Steam::positive($importJournal->getAmount());
- $date = $importJournal->getDate($this->dateFormat);
- $set = TransactionJournal::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
- ->leftJoin(
- 'transactions AS source', function (JoinClause $join) {
- $join->on('transaction_journals.id', '=', 'source.transaction_journal_id')->where('source.amount', '<', 0);
- }
- )
- ->leftJoin(
- 'transactions AS destination', function (JoinClause $join) {
- $join->on('transaction_journals.id', '=', 'destination.transaction_journal_id')->where(
- 'destination.amount', '>', 0
- );
- }
- )
- ->leftJoin('accounts as source_accounts', 'source.account_id', '=', 'source_accounts.id')
- ->leftJoin('accounts as destination_accounts', 'destination.account_id', '=', 'destination_accounts.id')
- ->where('transaction_journals.user_id', $this->job->user_id)
- ->where('transaction_types.type', TransactionType::TRANSFER)
- ->where('transaction_journals.date', $date->format('Y-m-d'))
- ->where('destination.amount', $amount)
- ->get(
- ['transaction_journals.id', 'transaction_journals.encrypted', 'transaction_journals.description',
- 'source_accounts.name as source_name', 'destination_accounts.name as destination_name']
- );
-
- return $this->filterTransferSet($set, $importJournal);
- }
-
-
- return false;
- }
+// /**
+// * @param TransactionJournal $journal
+// * @param array $dates
+// */
+// private function storeMeta(TransactionJournal $journal, array $dates)
+// {
+// // all other date fields as meta thing:
+// foreach ($dates as $name => $value) {
+// try {
+// $date = new Carbon($value);
+// $journal->setMeta($name, $date);
+// } catch (\Exception $e) {
+// // don't care, ignore:
+// Log::warning(sprintf('Could not parse "%s" into a valid Date object for field %s', $value, $name));
+// }
+// }
+// }
+
+// /**
+// * @param array $tags
+// * @param TransactionJournal $journal
+// */
+// private function storeTags(array $tags, TransactionJournal $journal): void
+// {
+// foreach ($tags as $tag) {
+// $dbTag = $this->tagRepository->findByTag($tag);
+// if (is_null($dbTag->id)) {
+// $dbTag = $this->tagRepository->store(
+// ['tag' => $tag, 'date' => null, 'description' => null, 'latitude' => null, 'longitude' => null,
+// 'zoomLevel' => null, 'tagMode' => 'nothing']
+// );
+// }
+// $journal->tags()->save($dbTag);
+// Log::debug(sprintf('Linked tag %d ("%s") to journal #%d', $dbTag->id, $dbTag->tag, $journal->id));
+// }
+//
+// return;
+// }
+
+// /**
+// * This method checks if the given transaction is a transfer and if so, if it might be a duplicate of an already imported transfer.
+// * This is important for import files that cover multiple accounts (and include both A<>B and B<>A transactions).
+// *
+// * @param TransactionType $transactionType
+// * @param ImportJournal $importJournal
+// *
+// * @return bool
+// */
+// private function verifyDoubleTransfer(TransactionType $transactionType, ImportJournal $importJournal): bool
+// {
+// if ($transactionType->type === TransactionType::TRANSFER) {
+// $amount = Steam::positive($importJournal->getAmount());
+// $date = $importJournal->getDate($this->dateFormat);
+// $set = TransactionJournal::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
+// ->leftJoin(
+// 'transactions AS source', function (JoinClause $join) {
+// $join->on('transaction_journals.id', '=', 'source.transaction_journal_id')->where('source.amount', '<', 0);
+// }
+// )
+// ->leftJoin(
+// 'transactions AS destination', function (JoinClause $join) {
+// $join->on('transaction_journals.id', '=', 'destination.transaction_journal_id')->where(
+// 'destination.amount', '>', 0
+// );
+// }
+// )
+// ->leftJoin('accounts as source_accounts', 'source.account_id', '=', 'source_accounts.id')
+// ->leftJoin('accounts as destination_accounts', 'destination.account_id', '=', 'destination_accounts.id')
+// ->where('transaction_journals.user_id', $this->job->user_id)
+// ->where('transaction_types.type', TransactionType::TRANSFER)
+// ->where('transaction_journals.date', $date->format('Y-m-d'))
+// ->where('destination.amount', $amount)
+// ->get(
+// ['transaction_journals.id', 'transaction_journals.encrypted', 'transaction_journals.description',
+// 'source_accounts.name as source_name', 'destination_accounts.name as destination_name']
+// );
+//
+// return $this->filterTransferSet($set, $importJournal);
+// }
+//
+//
+// return false;
+// }
}